Skip to content

Commit 5e1ea0f

Browse files
Tests: Test signals for password reset pre/post
1 parent 7c89b57 commit 5e1ea0f

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

tests/test.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
from rest_framework import status
55
from rest_framework.test import APITestCase
66
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
812

913
# try getting reverse from django.urls
1014
try:
@@ -189,17 +193,39 @@ def test_reset_password_multiple_users(self, mock_reset_password_token_created):
189193
@patch('django_rest_passwordreset.signals.pre_password_reset.send')
190194
@patch('django_rest_passwordreset.signals.post_password_reset.send')
191195
def test_signals(self,
192-
mock_reset_password_token_created,
196+
mock_post_password_reset,
193197
mock_pre_password_reset,
194-
mock_post_password_reset):
198+
mock_reset_password_token_created
199+
):
195200
# check that all mocks have not been called yet
196201
self.assertFalse(mock_reset_password_token_created.called)
197202
self.assertFalse(mock_post_password_reset.called)
198203
self.assertFalse(mock_pre_password_reset.called)
199204

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)
201220

202221
# 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+
203229

204230

205231

0 commit comments

Comments
 (0)