Skip to content

Commit 6815a81

Browse files
authored
Merge pull request #188 from emickiewicz/bugfix/reset_password_token_created_signal_fired_without_token
Fix the reset_password_token_created signal to be fired even when no token have been created.
2 parents 1c69aea + 861611b commit 6815a81

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ PyPi: [https://pypi.org/project/django-rest-passwordreset/](https://pypi.org/pro
88

99
## [Unreleased]
1010

11+
### Fixed
12+
- Fix the reset_password_token_created signal to be fired even when no token have been created. (#188)
13+
1114
## [1.4.0]
1215

1316
### Added

django_rest_passwordreset/views.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,14 @@ def generate_token_for_email(email, user_agent='', ip_address=''):
7575
break
7676

7777
# No active user found, raise a ValidationError
78-
# but not if DJANGO_REST_PASSWORDRESET_NO_INFORMATION_LEAKAGE == True
79-
if not active_user_found and not getattr(settings, 'DJANGO_REST_PASSWORDRESET_NO_INFORMATION_LEAKAGE', False):
80-
raise exceptions.ValidationError({
81-
'email': [_(
82-
"We couldn't find an account associated with that email. Please try a different e-mail address.")],
83-
})
78+
# but not if DJANGO_REST_PASSWORDRESET_NO_INFORMATION_LEAKAGE == True, in that case we return None
79+
if not active_user_found:
80+
if not getattr(settings, 'DJANGO_REST_PASSWORDRESET_NO_INFORMATION_LEAKAGE', False):
81+
raise exceptions.ValidationError({
82+
'email': [_(
83+
"We couldn't find an account associated with that email. Please try a different e-mail address.")],
84+
})
85+
return None
8486

8587
# last but not least: iterate over all users that are active and can change their password
8688
# and create a Reset Password Token and send a signal with the created token
@@ -199,9 +201,13 @@ def post(self, request, *args, **kwargs):
199201
ip_address=request.META.get(HTTP_IP_ADDRESS_HEADER, ''),
200202
)
201203

202-
# send a signal that the password token was created
203-
# let whoever receives this signal handle sending the email for the password reset
204-
reset_password_token_created.send(sender=self.__class__, instance=self, reset_password_token=token)
204+
if token:
205+
# send a signal that the password token was created
206+
# let whoever receives this signal handle sending the email for the password reset
207+
reset_password_token_created.send(
208+
sender=self.__class__,
209+
instance=self, reset_password_token=token
210+
)
205211

206212
return Response({'status': 'OK'})
207213

tests/test/test_auth_test_case.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,15 @@ def test_signals(self,
360360
self.assertEqual(mock_pre_password_reset.call_args[1]['reset_password_token'], token1)
361361

362362
@override_settings(DJANGO_REST_PASSWORDRESET_NO_INFORMATION_LEAKAGE=True)
363-
def test_try_reset_password_email_does_not_exist_no_leakage_enabled(self):
363+
@patch('django_rest_passwordreset.signals.reset_password_token_created.send')
364+
def test_try_reset_password_email_does_not_exist_no_leakage_enabled(self, mock_reset_signal):
364365
"""
365366
Tests requesting a token for an email that does not exist when
366367
DJANGO_REST_PASSWORDRESET_NO_INFORMATION_LEAKAGE == True
367368
"""
368369
response = self.rest_do_request_reset_token(email="foobar@doesnotexist.com")
369370
self.assertEqual(response.status_code, status.HTTP_200_OK)
371+
self.assertFalse(mock_reset_signal.called)
370372

371373
def test_user_without_password(self):
372374
""" Tests requesting a token for an email without a password doesn't work"""

0 commit comments

Comments
 (0)