test_user_changeemail_api.py 3.4 KB

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