1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- from django.contrib.auth import get_user_model
- from django.core import mail
- from ..testutils import AuthenticatedUserTestCase
- class UserChangePasswordTests(AuthenticatedUserTestCase):
- """
- tests for user change password RPC (/api/users/1/change-password/)
- """
- def setUp(self):
- super(UserChangePasswordTests, self).setUp()
- self.link = '/api/users/%s/change-password/' % self.user.pk
- def test_unsupported_methods(self):
- """api isn't supporting GET"""
- response = self.client.get(self.link)
- self.assertEqual(response.status_code, 405)
- def test_change_password(self):
- """api allows users to change their passwords"""
- response = self.client.post(self.link, data={
- 'new_password': 'N3wP@55w0rd',
- 'password': self.USER_PASSWORD
- })
- self.assertEqual(response.status_code, 200)
- self.assertIn('Confirm password change', mail.outbox[0].subject)
- for line in [l.strip() for l in mail.outbox[0].body.splitlines()]:
- if line.startswith('http://'):
- token = line.rstrip('/').split('/')[-1]
- break
- else:
- self.fail("E-mail sent didn't contain confirmation url")
- def test_invalid_password(self):
- """api errors correctly for invalid password"""
- response = self.client.post(self.link, data={
- 'new_password': 'N3wP@55w0rd',
- 'password': 'Lor3mIpsum'
- })
- self.assertContains(response, 'password is invalid', status_code=400)
- def test_invalid_input(self):
- """api errors correctly for invalid input"""
- response = self.client.post(self.link, data={
- 'new_password': '',
- 'password': self.USER_PASSWORD
- })
- self.assertContains(response, 'new_password":["This field is required', status_code=400)
- response = self.client.post(self.link, data={
- 'new_password': 'n',
- 'password': self.USER_PASSWORD
- })
- self.assertContains(response, 'password is too short', status_code=400)
|