-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
146 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import datetime | ||
import jwt | ||
from django.conf import settings | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from authors.base_file import BaseTestCase | ||
|
||
|
||
class TestPasswordReset(BaseTestCase): | ||
# Test class for the password reset feature | ||
|
||
def test_request_password_reset_with_valid_email(self): | ||
self.client.post(self.register_url, self.register_data, format="json") | ||
data = {"email": self.register_data['user']['email']} | ||
response = self.client.post(reverse('authentication:forgot_password'), data, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_request_password_reset_with_invalid_email(self): | ||
data = {"email": "email@doesnotexist.com"} | ||
response = self.client.post(reverse('authentication:forgot_password'), data, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
|
||
def test_change_password_valid_password(self): | ||
self.client.post(self.register_url, self.register_data, format="json") | ||
token = jwt.encode({ | ||
"email": self.register_data['user']['email'], | ||
"iat": datetime.datetime.now(), | ||
"exp": datetime.datetime.utcnow() + datetime.timedelta(hours=24) | ||
}, settings.SECRET_KEY, algorithm='HS256').decode() | ||
data = {"password": "password!@1"} | ||
response = self.client.put(reverse("authentication:change_password", kwargs={'token': token}), | ||
data, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
|
||
def test_change_password_invalid_password(self): | ||
self.client.post(self.register_url, self.register_data, format="json") | ||
token = jwt.encode({ | ||
"email": self.register_data['user']['email'], | ||
"iat": datetime.datetime.now(), | ||
"exp": datetime.datetime.utcnow() + datetime.timedelta(hours=24) | ||
}, settings.SECRET_KEY, algorithm='HS256').decode() | ||
data = {"password": "password"} | ||
response = self.client.put(reverse("authentication:change_password", kwargs={'token': token}), | ||
data, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
|
||
|
||
|
||
|
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 |
---|---|---|
@@ -1,13 +1,17 @@ | ||
from django.urls import path | ||
|
||
from .views import ( | ||
LoginAPIView, RegistrationAPIView, UserRetrieveUpdateAPIView | ||
LoginAPIView, RegistrationAPIView, UserRetrieveUpdateAPIView, ForgotPasswordView, | ||
ChangePasswordView | ||
) | ||
|
||
urlpatterns = [ | ||
|
||
path('user/', UserRetrieveUpdateAPIView.as_view(), name="user-retrieve-profile"), | ||
path('users/', RegistrationAPIView.as_view(), name="user-signup"), | ||
path('users/login/', LoginAPIView.as_view(), name="user-login"), | ||
path('users/forgot_password/', ForgotPasswordView.as_view(), name="forgot_password"), | ||
path('users/change_password/<str:token>', ChangePasswordView.as_view(), name="change_password") | ||
|
||
|
||
] |
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