test_user_changepassword_api.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from django.contrib.auth import get_user_model
  2. from django.core import mail
  3. from misago.users.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_email(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.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_password': '',
  41. 'password': self.USER_PASSWORD
  42. })
  43. self.assertEqual(response.status_code, 400)
  44. self.assertIn('new_password":["This field is required',
  45. response.content)
  46. response = self.client.post(self.link, data={
  47. 'new_password': 'n',
  48. 'password': self.USER_PASSWORD
  49. })
  50. self.assertEqual(response.status_code, 400)
  51. self.assertIn('password must be', response.content)