test_user_changeemail_api.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from django.contrib.auth import get_user_model
  2. from django.core import mail
  3. from misago.users.testutils import AuthenticatedUserTestCase
  4. UserModel = get_user_model()
  5. class UserChangeEmailTests(AuthenticatedUserTestCase):
  6. """
  7. tests for user change email RPC (/api/users/1/change-email/)
  8. """
  9. def setUp(self):
  10. super(UserChangeEmailTests, self).setUp()
  11. self.link = '/api/users/%s/change-email/' % self.user.pk
  12. def test_unsupported_methods(self):
  13. """api isn't supporting GET"""
  14. response = self.client.get(self.link)
  15. self.assertEqual(response.status_code, 405)
  16. def test_change_email(self):
  17. """api allows users to change their e-mail addresses"""
  18. response = self.client.post(self.link, data={
  19. 'new_email': 'new@email.com',
  20. 'password': self.USER_PASSWORD
  21. })
  22. self.assertEqual(response.status_code, 200)
  23. self.assertIn('Confirm e-mail change', mail.outbox[0].subject)
  24. for line in [l.strip() for l in mail.outbox[0].body.splitlines()]:
  25. if line.startswith('http://'):
  26. token = line.rstrip('/').split('/')[-1]
  27. break
  28. else:
  29. self.fail("E-mail sent didn't contain confirmation url")
  30. def test_invalid_password(self):
  31. """api errors correctly for invalid password"""
  32. response = self.client.post(self.link, data={
  33. 'new_email': 'new@email.com',
  34. 'password': 'Lor3mIpsum'
  35. })
  36. self.assertContains(response, 'password is invalid', status_code=400)
  37. def test_invalid_input(self):
  38. """api errors correctly for invalid input"""
  39. response = self.client.post(self.link, data={
  40. 'new_email': '',
  41. 'password': self.USER_PASSWORD
  42. })
  43. self.assertContains(response, 'new_email":["This field is required', status_code=400)
  44. response = self.client.post(self.link, data={
  45. 'new_email': 'newmail',
  46. 'password': self.USER_PASSWORD
  47. })
  48. self.assertContains(response, 'valid email address', status_code=400)
  49. def test_email_taken(self):
  50. """api validates email usage"""
  51. UserModel.objects.create_user('BobBoberson', 'new@email.com', 'Pass.123')
  52. response = self.client.post(self.link, data={
  53. 'new_email': 'new@email.com',
  54. 'password': self.USER_PASSWORD
  55. })
  56. self.assertContains(response, 'not available', status_code=400)