|
4 | 4 | from rest_framework import status
|
5 | 5 | from rest_framework.test import APITestCase
|
6 | 6 | from django_rest_passwordreset.models import ResetPasswordToken
|
7 |
| -from unittest.mock import patch |
| 7 | +try: |
| 8 | + from unittest.mock import patch |
| 9 | +except: |
| 10 | + # Python 2.7 fallback |
| 11 | + from mock import patch |
8 | 12 |
|
9 | 13 | # try getting reverse from django.urls
|
10 | 14 | try:
|
@@ -189,17 +193,39 @@ def test_reset_password_multiple_users(self, mock_reset_password_token_created):
|
189 | 193 | @patch('django_rest_passwordreset.signals.pre_password_reset.send')
|
190 | 194 | @patch('django_rest_passwordreset.signals.post_password_reset.send')
|
191 | 195 | def test_signals(self,
|
192 |
| - mock_reset_password_token_created, |
| 196 | + mock_post_password_reset, |
193 | 197 | mock_pre_password_reset,
|
194 |
| - mock_post_password_reset): |
| 198 | + mock_reset_password_token_created |
| 199 | + ): |
195 | 200 | # check that all mocks have not been called yet
|
196 | 201 | self.assertFalse(mock_reset_password_token_created.called)
|
197 | 202 | self.assertFalse(mock_post_password_reset.called)
|
198 | 203 | self.assertFalse(mock_pre_password_reset.called)
|
199 | 204 |
|
200 |
| - # request token |
| 205 | + # request token for user1 |
| 206 | + response = self.rest_do_request_reset_token(email="user1@mail.com") |
| 207 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 208 | + self.assertEqual(ResetPasswordToken.objects.all().count(), 1) |
| 209 | + |
| 210 | + # verify that the reset_password_token_created signal was fired |
| 211 | + self.assertTrue(mock_reset_password_token_created.called) |
| 212 | + self.assertEquals(mock_reset_password_token_created.call_count, 1) |
| 213 | + |
| 214 | + token1 = mock_reset_password_token_created.call_args[1]['reset_password_token'] |
| 215 | + self.assertNotEqual(token1.key, "", msg="Verify that the reset_password_token of the reset_password_Token_created signal is not empty") |
| 216 | + |
| 217 | + # verify that the other two signals have not yet been called |
| 218 | + self.assertFalse(mock_post_password_reset.called) |
| 219 | + self.assertFalse(mock_pre_password_reset.called) |
201 | 220 |
|
202 | 221 | # reset password
|
| 222 | + response = self.rest_do_reset_password_with_token(token1.key, "new_secret") |
| 223 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 224 | + |
| 225 | + # now the other two signals should have been called |
| 226 | + self.assertTrue(mock_post_password_reset.called) |
| 227 | + self.assertTrue(mock_pre_password_reset.called) |
| 228 | + |
203 | 229 |
|
204 | 230 |
|
205 | 231 |
|
0 commit comments