-
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
12 changed files
with
252 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,3 +109,5 @@ sims1 | |
db.sqlite3 | ||
.vscode/ | ||
*cache* | ||
tmp | ||
my_env |
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 |
---|---|---|
|
@@ -14,4 +14,4 @@ script: | |
- coveralls | ||
|
||
after_script: | ||
#- coveralls | ||
#- coveralls |
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
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,73 @@ | ||
"""This module tests the password reset functionality of a user.""" | ||
from django.test import TestCase | ||
|
||
from rest_framework.test import APIClient | ||
from rest_framework import status | ||
from authors.apps.authentication.tests.base_test import BaseTest | ||
from authors.apps.authentication.models import User | ||
|
||
|
||
class PasswordResetTestCase(TestCase, BaseTest): | ||
""" This class defines the test suite for password reset """ | ||
|
||
def setUp(self): | ||
BaseTest.__init__(self) | ||
self.client = APIClient() | ||
|
||
# create a new user | ||
self.user = User.objects.create_user( | ||
self.user_name, self.user_email, self.password | ||
) | ||
|
||
# invoke a password reset email | ||
self.response = self.client.post("/api/users/reset/password", | ||
self.invoke_email, | ||
format="json") | ||
|
||
# This email is not associated with a user | ||
self.email_404 = { | ||
"user": { | ||
"email": "email_404@none.com" | ||
} | ||
} | ||
|
||
# Here we don't pass in an email | ||
self.empty_email = { | ||
"user": { | ||
"email": "" | ||
} | ||
} | ||
|
||
def test_invoke_password_reset(self): | ||
""" Test that password reset email is sent | ||
The user with this email exists | ||
""" | ||
|
||
self.assertEqual(self.response.status_code, status.HTTP_200_OK) | ||
|
||
def test_email_does_not_exist(self): | ||
""" Test user can not invoke password reset for email that does not exist""" | ||
|
||
self.response = self.client.post( | ||
"/api/users/reset/password", | ||
self.email_404, | ||
format="json" | ||
|
||
) | ||
|
||
self.assertEqual(self.response.status_code, status.HTTP_404_NOT_FOUND) | ||
|
||
def test_with_no_email(self): | ||
""" Test a user can not invoke password reset with no email """ | ||
|
||
self.response = self.client.post( | ||
"/api/users/reset/password", | ||
self.empty_email, | ||
format="json" | ||
) | ||
|
||
self.assertEqual(self.response.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
self.assertEqual(self.response.json()['errors'], | ||
{'email': ['This field may not be blank.']}) | ||
|
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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
from django.urls import path | ||
|
||
from .views import ( | ||
LoginAPIView, RegistrationAPIView, UserRetrieveUpdateAPIView | ||
LoginAPIView, RegistrationAPIView, UserRetrieveUpdateAPIView, InvokePasswordResetAPIView | ||
) | ||
|
||
urlpatterns = [ | ||
path('user/', UserRetrieveUpdateAPIView.as_view()), | ||
path('users/', RegistrationAPIView.as_view()), | ||
path('users/login/', LoginAPIView.as_view()), | ||
path('users/reset/password', InvokePasswordResetAPIView.as_view()), | ||
path('user/reset-password/<token>', UserRetrieveUpdateAPIView.as_view()) | ||
] |
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,22 @@ | ||
import os | ||
from django.core.mail import send_mail | ||
|
||
def send_password_reset_email(to_email, token, current_site): | ||
|
||
# current_site = os.environ.get("CURRENT_SITE") | ||
# email setup | ||
subject = 'Password reset' | ||
message = """ | ||
You're receiving this email because you invoked a password reset on | ||
Authors haven. If you think it is a mistake to receive this email just ignore it. | ||
----- Click the link below to reset your password ---- | ||
{}/api/user/reset-password/{} | ||
""".format(current_site, token) | ||
from_email = os.environ.get('EMAIL_HOST_USER') | ||
to_email = to_email | ||
try: | ||
send_mail(subject, message, from_email, [to_email], fail_silently=False,) | ||
except Exception as e: | ||
return {'email': str(e)} |
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
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