@@ -17,6 +17,7 @@ def setUp(self):
1717 self .user1 = User .objects .create_user ("user1" , "user1@mail.com" , "secret1" )
1818 self .user2 = User .objects .create_user ("user2" , "user2@mail.com" , "secret2" )
1919 self .user3 = User .objects .create_user ("user3@mail.com" , "not-that-mail@mail.com" , "secret3" )
20+ self .user4 = User .objects .create_user ("user4" , "user4@mail.com" )
2021
2122 def test_try_reset_password_email_does_not_exist (self ):
2223 """ Tests requesting a token for an email that does not exist """
@@ -225,3 +226,56 @@ def test_signals(self,
225226 # now the other two signals should have been called
226227 self .assertTrue (mock_post_password_reset .called )
227228 self .assertTrue (mock_pre_password_reset .called )
229+
230+ def test_user_without_password (self ):
231+ """ Tests requesting a token for an email without a password doesn't work"""
232+ response = self .rest_do_request_reset_token (email = "user4@mail.com" )
233+ self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
234+ decoded_response = json .loads (response .content .decode ())
235+ # response should have "email" in it
236+ self .assertTrue ("email" in decoded_response )
237+
238+ @override_settings (DJANGO_REST_MULTITOKENAUTH_REQUIRE_USABLE_PASSWORD = False )
239+ @patch ('django_rest_passwordreset.signals.reset_password_token_created.send' )
240+ def test_user_without_password_where_not_required (self , mock_reset_password_token_created ):
241+ """ Tests requesting a token for an email without a password works when not required"""
242+ response = self .rest_do_request_reset_token (email = "user4@mail.com" )
243+ decoded_response = json .loads (response .content .decode ())
244+ self .assertEqual (response .status_code , status .HTTP_200_OK )
245+ # check that the signal was sent once
246+ self .assertTrue (mock_reset_password_token_created .called )
247+ self .assertEqual (mock_reset_password_token_created .call_count , 1 )
248+ last_reset_password_token = mock_reset_password_token_created .call_args [1 ]['reset_password_token' ]
249+ self .assertNotEqual (last_reset_password_token .key , "" )
250+
251+ # there should be one token
252+ self .assertEqual (ResetPasswordToken .objects .all ().count (), 1 )
253+
254+ # if the same user tries to reset again, the user will get the same token again
255+ response = self .rest_do_request_reset_token (email = "user4@mail.com" )
256+ self .assertEqual (response .status_code , status .HTTP_200_OK )
257+ self .assertEqual (mock_reset_password_token_created .call_count , 2 )
258+ last_reset_password_token = mock_reset_password_token_created .call_args [1 ]['reset_password_token' ]
259+ self .assertNotEqual (last_reset_password_token .key , "" )
260+
261+ # there should be one token
262+ self .assertEqual (ResetPasswordToken .objects .all ().count (), 1 )
263+ # and it should be assigned to user1
264+ self .assertEqual (
265+ ResetPasswordToken .objects .filter (key = last_reset_password_token .key ).first ().user .username ,
266+ "user4"
267+ )
268+
269+ # try to reset the password
270+ response = self .rest_do_reset_password_with_token (last_reset_password_token .key , "new_secret" )
271+ self .assertEqual (response .status_code , status .HTTP_200_OK )
272+
273+ # there should be zero tokens
274+ self .assertEqual (ResetPasswordToken .objects .all ().count (), 0 )
275+
276+ # try to login with the new username/Password (should work)
277+ self .assertTrue (
278+ self .django_check_login ("user4" , "new_secret" ),
279+ msg = "User 4 should be able to login with the modified credentials"
280+ )
281+
0 commit comments