test_user_changeemail_api.py 2.6 KB

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