test_user_changepassword_api.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from django.contrib.auth import get_user_model
  2. from django.core import mail
  3. from ..testutils import AuthenticatedUserTestCase
  4. class UserChangePasswordTests(AuthenticatedUserTestCase):
  5. """
  6. tests for user change password RPC (/api/users/1/change-password/)
  7. """
  8. def setUp(self):
  9. super(UserChangePasswordTests, self).setUp()
  10. self.link = '/api/users/%s/change-password/' % 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_password(self):
  16. """api allows users to change their passwords"""
  17. response = self.client.post(self.link, data={
  18. 'new_password': 'N3wP@55w0rd',
  19. 'password': self.USER_PASSWORD
  20. })
  21. self.assertEqual(response.status_code, 200)
  22. self.assertIn('Confirm password 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_password': 'N3wP@55w0rd',
  33. 'password': 'Lor3mIpsum'
  34. })
  35. self.assertContains(response, 'password is invalid', status_code=400)
  36. def test_invalid_input(self):
  37. """api errors correctly for invalid input"""
  38. response = self.client.post(self.link, data={
  39. 'new_password': '',
  40. 'password': self.USER_PASSWORD
  41. })
  42. self.assertContains(response, 'new_password":["This field is required', status_code=400)
  43. response = self.client.post(self.link, data={
  44. 'new_password': 'n',
  45. 'password': self.USER_PASSWORD
  46. })
  47. self.assertContains(response, 'password is too short', status_code=400)