-
Notifications
You must be signed in to change notification settings - Fork 2
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
9 changed files
with
134 additions
and
21 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 |
---|---|---|
|
@@ -16,4 +16,5 @@ omit = | |
*/migrations/* | ||
*/venv/* | ||
*email_reg_util* | ||
*reset_password_util* | ||
|
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,31 @@ | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
from django.template.loader import get_template | ||
import smtplib | ||
import os | ||
|
||
|
||
class ResetPasswordUtil(): | ||
def send_mail(self, request, sender_email, reciever_email, token): | ||
msg = MIMEMultipart() | ||
|
||
if sender_email == None or reciever_email == None: | ||
raise ValueError('Invalid parameters!') | ||
|
||
msg['From'] = sender_email | ||
msg['To'] = reciever_email | ||
msg['Subject'] = "Reseting password for your Author's Haven account." | ||
body = self.html_renderer(request, token) | ||
msg.attach(MIMEText(body, 'html')) | ||
|
||
server = smtplib.SMTP('smtp.gmail.com', os.environ.get('EMAIL_PORT')) | ||
server.starttls() | ||
server.login(os.environ.get('EMAIL_HOST_USER'), | ||
os.environ.get('EMAIL_HOST_USER_PASSWORD')) | ||
server.sendmail(msg['From'], msg['To'], msg.as_string()) | ||
server.quit() | ||
|
||
def html_renderer(self, request, token): | ||
template = get_template('authentication/reset_link.html') | ||
context = {'link': 'api/users/password/reset', 'token': token} | ||
return template.render(context, 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
7 changes: 7 additions & 0 deletions
7
authors/apps/authentication/templates/authentication/reset_link.html
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,7 @@ | ||
<html> | ||
|
||
<body> | ||
<a href='http://{{ request.get_host }}/{{link}}/{{token}}'>Click here to reset your password!</a> | ||
</body> | ||
|
||
</html> |
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,52 @@ | ||
from django.test import TestCase, RequestFactory | ||
import json | ||
import jwt | ||
from authors.apps.authentication.views import SendPasswordResetEmailAPIView, ResetPasswordAPIView, RegistrationAPIView | ||
from minimock import Mock | ||
import smtplib | ||
|
||
|
||
class ResetPasswordTestCase(TestCase): | ||
def setUp(self): | ||
self.factory = RequestFactory() | ||
self.user = { | ||
"user": { | ||
"email": "test@gmail.com", | ||
"username": "tester", | ||
"password": "testpass@word" | ||
} | ||
} | ||
self.email_dict = {'email': 'teste@gmail.com'} | ||
self.password_dict = {'password': 'password1234567#', | ||
'retyped_password': 'password1234567#'} | ||
|
||
smtplib.SMTP = Mock('smtplib.SMTP') | ||
smtplib.SMTP.mock_returns = Mock('smtp_connection') | ||
|
||
self.request = self.factory.post( | ||
'users/password/forgot/', data=json.dumps(self.email_dict), content_type='application/json') | ||
self.response = SendPasswordResetEmailAPIView.as_view()(self.request) | ||
|
||
"""create user in database""" | ||
self.reg_request = self.factory.post( | ||
'/api/users/', data=json.dumps(self.user), content_type='application/json') | ||
self.reg_response = RegistrationAPIView.as_view()(self.reg_request) | ||
self.kwargs = {'token': self.reg_response.data['token']} | ||
|
||
def test_send_reset_mail(self): | ||
self.assertEqual(self.response.status_code, 200) | ||
|
||
def test_reset_password(self): | ||
self.request = self.factory.put( | ||
'users/password/reset/', data=json.dumps(self.password_dict), content_type='application/json') | ||
self.response = ResetPasswordAPIView.as_view()(self.request, **self.kwargs) | ||
self.assertEqual(self.response.status_code, 201) | ||
|
||
def test_mismatched_password(self): | ||
password_dict = {'password': 'password12345687#', | ||
'retyped_password': '1234567#password'} | ||
with self.assertRaises(Exception) as context: | ||
self.request = self.factory.put( | ||
'users/password/reset/', data=json.dumps(password_dict), content_type='application/json') | ||
self.response = ResetPasswordAPIView.as_view()(self.request, **self.kwargs) | ||
self.assertIn('Passwords do not match!', str(context.exception)) |
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