-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
164047059-feature(email verification): Implements email verification
feature - ensures user receives an email once he/she signs up - ensures user email is validated using a token - adds .env.example field for the upodated envs [Finishes #164047059]
- Loading branch information
1 parent
b5bd0ce
commit 6438bd2
Showing
10 changed files
with
221 additions
and
12 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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
export HOST=localhost # The default is usually localhost | ||
export DB_NAME=authors_haven # Create a database and use this | ||
export DB_USER=postgres # Use your postgres user. The default is usually postgres | ||
export DB_PASSWORD='' # Use your database password | ||
export DB_NAME=olympians # Create a database and use this | ||
export DB_USER=name # Use your postgres user. The default is usually postgres | ||
export DB_PASSWORD='' # Use your database password | ||
export SENDGRID_API_KEY='API_KEY here' #This is the sendgrid api key | ||
export EMAIL_FROM="test@test.com" # This is the email that is sending the email | ||
export URL="http://127.0.0.1:8000/" #The base url where the frontend app is hosted |
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 |
---|---|---|
|
@@ -97,4 +97,3 @@ db.sqlite3 | |
|
||
#ds_store | ||
.ds_store | ||
|
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,35 @@ | ||
import json | ||
from rest_framework.test import APITestCase, APIClient | ||
from rest_framework.views import status | ||
import uuid | ||
import unittest | ||
|
||
from ..utils import send_email, verify_message | ||
from ..models import User | ||
|
||
|
||
class TestEmailIntergration(unittest.TestCase): | ||
""" | ||
This class tests sending email with sendgrid and | ||
""" | ||
def test_success_email(self): | ||
self.assertEqual(send_email("test@test.com", "test", "test"), 'email sent') | ||
|
||
def test_failed_email(self): | ||
""" | ||
Testing providing a string instead of email | ||
""" | ||
self.assertEqual(send_email("testtestcom", "test", "test"), 'There was an error sending') | ||
|
||
|
||
class TestVerificationMessage(unittest.TestCase): | ||
""" | ||
This class tests sending email with sendgrid and | ||
""" | ||
def test_name_message(self): | ||
self.assertIn("Chirchir", verify_message("Chirchir", "token")) | ||
|
||
def test_token_message(self): | ||
token = str(uuid.uuid4()) | ||
self.assertIn(token, verify_message("Chirchir", token)) | ||
|
73 changes: 73 additions & 0 deletions
73
authors/apps/authentication/tests/test_email_verification.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,73 @@ | ||
from django.shortcuts import reverse | ||
import json | ||
from rest_framework.test import APITestCase, APIClient | ||
from rest_framework.views import status | ||
|
||
from ..models import EmailVerification, User | ||
|
||
|
||
class TestEmailVerification(APITestCase): | ||
""" | ||
Class tests for email verification . | ||
""" | ||
client = APIClient() | ||
|
||
def setUp(self): | ||
""" Creates user and user dictionary for testing.""" | ||
self.user = { | ||
"user": { | ||
"email": "chirchir@olympians.com", | ||
"username": "chirchir", | ||
"password": "test1234" | ||
} | ||
} | ||
|
||
def test_success_verification(self): | ||
""" Tests the token is created and saved on the db.""" | ||
response = self.client.post('/api/users/', self.user, format='json') | ||
self.assertEqual(EmailVerification.objects.count(), 1) | ||
|
||
def test_token_verified(self): | ||
""" | ||
tests the token is verified and used | ||
""" | ||
self.client.post('/api/users/', self.user, format='json') | ||
user1 = User.objects.get(username ='chirchir') | ||
verification = EmailVerification.objects.filter(user=user1).first() | ||
token = verification.token | ||
token_verify = {"token": token} | ||
response = self.client.post('/api/users/verify/', token_verify, format='json') | ||
result = json.loads(response.content) | ||
|
||
self.assertEqual(result["user"]["success"], "valid token") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_token_already_used(self): | ||
""" | ||
tests for an already used token by sending multiple tests | ||
""" | ||
self.client.post('/api/users/', self.user, format='json') | ||
user1 = User.objects.get(username='chirchir') | ||
verification = EmailVerification.objects.filter(user=user1).first() | ||
token = verification.token | ||
token_verify = {"token": token} | ||
self.client.post('/api/users/verify/', token_verify, format='json') | ||
response = self.client.post('/api/users/verify/', token_verify, format='json') | ||
result = json.loads(response.content) | ||
|
||
self.assertEqual(result["user"]["error"], "Token already used") | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
def test_token_doesnt_exist(self): | ||
""" | ||
Tests for a non existing token | ||
""" | ||
self.client.post('/api/users/', self.user, format='json') | ||
user1 = User.objects.get(username='chirchir') | ||
verification = EmailVerification.objects.filter(user=user1).first() | ||
token_verify = {"token": "test"} | ||
response = self.client.post('/api/users/verify/', token_verify, format='json') | ||
result = json.loads(response.content) | ||
|
||
self.assertEqual(result["user"]["error"], "invalid token") | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
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,28 @@ | ||
import sendgrid | ||
import os | ||
from sendgrid.helpers.mail import * | ||
|
||
|
||
def send_email(to_email, subject, message): | ||
sg = sendgrid.SendGridAPIClient(apikey=os.getenv("SENDGRID_API_KEY")) | ||
from_email = Email(os.getenv("EMAIL_FROM")) | ||
to_email = Email(to_email) | ||
subject = subject | ||
content = Content("text/plain", message) | ||
try: | ||
mail = Mail(from_email, subject, to_email, content) | ||
response = sg.client.mail.send.post(request_body=mail.get()) | ||
# response code 202 ensures the message is sent | ||
if response.status_code is not 202: | ||
return "email not sent check your api key and email from" | ||
return "email sent" | ||
except Exception: | ||
return "There was an error sending" | ||
|
||
|
||
def verify_message(name, token): | ||
message = "Thank you " + name + " for registering with us please verify your email\n" \ | ||
" by clicking on the following link\n" \ | ||
+ os.getenv("URL") + "/verify/" + token + "\n Welcome" | ||
|
||
return message |
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