-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
165305756-ft(Password Reset):User should be able to reset password vi…
…a email - Add password reset endpoint - Add password reset confirm endpoint - Update Readme [Finishes #165305756]
- Loading branch information
Ogutu-Brian
committed
Apr 30, 2019
1 parent
df2b233
commit 9467a40
Showing
6 changed files
with
472 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
authors/apps/authentication/tests/test_password_reset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
from .basetests import PasswordResetBaseTest | ||
from rest_framework import status | ||
|
||
|
||
class TestPasswordReset(PasswordResetBaseTest): | ||
""" | ||
Tests password reset by user | ||
""" | ||
|
||
def test_invalid_email_address(self): | ||
""" | ||
Tests posting of invalid email address | ||
""" | ||
self.reset_data["user"]["email"] = "johnsoon.com" | ||
response = self.password_reset() | ||
self.assertEqual( | ||
response.status_code, | ||
status.HTTP_400_BAD_REQUEST | ||
) | ||
self.assertEqual( | ||
self.contains_error( | ||
response.data.get("errors").get("email"), | ||
"Enter a valid email address." | ||
), True | ||
) | ||
|
||
def test_missing_email(self): | ||
""" | ||
Tests password reset without a mail | ||
""" | ||
self.reset_data["user"]["email"] = None | ||
response = self.password_reset() | ||
self.assertEqual( | ||
response.status_code, | ||
status.HTTP_406_NOT_ACCEPTABLE | ||
) | ||
self.assertEqual( | ||
self.contains_error( | ||
response.data.get("errors").get("email"), | ||
"this field is required" | ||
), True | ||
) | ||
|
||
def test_unexisting_ccount(self): | ||
""" | ||
Tests unexisting account | ||
""" | ||
self.reset_data["user"]["email"] = "jeff@gmail.com" | ||
response = self.password_reset() | ||
self.assertEqual( | ||
response.status_code, | ||
status.HTTP_406_NOT_ACCEPTABLE | ||
) | ||
self.assertEqual( | ||
self.contains_error( | ||
response.data.get("errors").get("email"), | ||
"no account with that email address" | ||
), True | ||
) | ||
|
||
def test_unmatching_password(self): | ||
""" | ||
Tests if passwords match | ||
""" | ||
self.password_data["password_confirm"] = "SomeTestPassword#!2" | ||
self.password_reset() | ||
response = self.password_reset_confirm() | ||
self.assertEqual( | ||
response.status_code, | ||
status.HTTP_406_NOT_ACCEPTABLE | ||
) | ||
self.assertEqual( | ||
self.contains_error( | ||
response.data.get("errors").get("password"), | ||
"passwords did not match" | ||
), True | ||
) | ||
|
||
def test_invalid_password(self): | ||
""" | ||
Tests invalid password | ||
""" | ||
self.password_data["password"] = "123" | ||
self.password_data["password_confirm"] = "123" | ||
self.password_reset() | ||
response = self.password_reset_confirm() | ||
self.assertEqual( | ||
response.status_code, | ||
status.HTTP_406_NOT_ACCEPTABLE | ||
) | ||
self.assertEqual( | ||
self.contains_error( | ||
response.data.get("errors").get("password"), | ||
"This password is too short. It must contain at least 8 characters." | ||
), True | ||
) | ||
|
||
def test_invalid_token(self): | ||
""" | ||
Tests changing of password with invalid token | ||
""" | ||
self.password_data["token"] = "abcd898adwhi3454asddwhfwh" | ||
response = self.password_reset_confirm() | ||
self.assertEqual( | ||
response.status_code, | ||
status.HTTP_401_UNAUTHORIZED | ||
) | ||
self.assertEqual( | ||
self.contains_error( | ||
response.data.get("errors").get("token"), | ||
"invalid token" | ||
), True | ||
) | ||
|
||
def test_missing_token(self): | ||
""" | ||
Tests password reset without a token | ||
""" | ||
response = self.password_reset_confirm() | ||
self.assertEqual( | ||
response.status_code, | ||
status.HTTP_406_NOT_ACCEPTABLE | ||
) | ||
self.assertEqual( | ||
self.contains_error( | ||
response.data.get("errors").get("token"), | ||
"this field is required" | ||
), True | ||
) | ||
|
||
def test_missing_password(self): | ||
""" | ||
Tests missing password | ||
""" | ||
self.password_data["password"] = None | ||
self.password_reset() | ||
response = self.password_reset_confirm() | ||
self.assertEqual( | ||
response.status_code, | ||
status.HTTP_406_NOT_ACCEPTABLE | ||
) | ||
self.assertEqual( | ||
self.contains_error( | ||
response.data.get("errors").get("password"), | ||
"this field is required" | ||
), True | ||
) | ||
|
||
def test_missing_password_confirm(self): | ||
""" | ||
Tests missing password confirm | ||
""" | ||
self.password_data["password_confirm"] = None | ||
self.password_reset() | ||
response = self.password_reset_confirm() | ||
self.assertEqual( | ||
response.status_code, | ||
status.HTTP_406_NOT_ACCEPTABLE | ||
) | ||
self.assertEqual( | ||
self.contains_error( | ||
response.data.get("errors").get("password_confirm"), | ||
"this field is required" | ||
), True | ||
) | ||
|
||
def test_successful_password_reset(self): | ||
""" | ||
Tests successful password rese | ||
""" | ||
self.password_reset() | ||
response = self.password_reset_confirm() | ||
message = None | ||
for item in response.data.get("data"): | ||
if item.get("message"): | ||
message = item.get("message") | ||
break | ||
self.assertEqual( | ||
response.status_code, | ||
status.HTTP_200_OK | ||
) | ||
self.assertEqual( | ||
message, | ||
"you have successfully reset your password" | ||
) | ||
|
||
def test_token_reuse(self): | ||
""" | ||
Tests if a user can use token generated more than once to | ||
reset password | ||
""" | ||
self.password_reset() | ||
self.password_reset_confirm() | ||
response = self.password_reset_confirm() | ||
self.assertEqual( | ||
response.status_code, | ||
status.HTTP_401_UNAUTHORIZED | ||
) | ||
self.assertEqual( | ||
self.contains_error( | ||
response.data.get("errors").get("token"), | ||
"invalid token" | ||
), True | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.