-
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
17 changed files
with
256 additions
and
36 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,31 @@ | ||
import os | ||
|
||
from django.contrib.sites.shortcuts import get_current_site | ||
from django.utils.http import urlsafe_base64_encode | ||
from django.core.mail import EmailMessage | ||
from django.utils.encoding import force_bytes | ||
|
||
from .models import User | ||
from .backends import account_activation_token | ||
|
||
|
||
def send_confirmation_email(credentials, request): | ||
""" | ||
This is a function that sends the EmailMessage to the user | ||
embedded with tokens and user id | ||
""" | ||
registered_user = User.objects.get(email=credentials.get('email')) | ||
|
||
# send an email to the user with the token | ||
mail_subject = 'Activate your account.' | ||
current_site = get_current_site(request) | ||
uid = urlsafe_base64_encode(force_bytes(registered_user.pk)).decode() | ||
token = account_activation_token.make_token(registered_user) | ||
protocol = os.getenv('PROTOCOL') | ||
activation_link = protocol + "{0}/api/users/activate/{1}/{2}".format( | ||
current_site, uid, token) | ||
message = "Hello {0},\n {1}".format( | ||
registered_user.username, activation_link) | ||
to_email = credentials.get('email') | ||
email = EmailMessage(mail_subject, message, to=[to_email]) | ||
email.send() |
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
Empty file.
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
79 changes: 79 additions & 0 deletions
79
authors/apps/authentication/tests/test_account_activation.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,79 @@ | ||
from django.test import TestCase | ||
from django.urls import reverse | ||
from django.core import mail | ||
from django.utils.encoding import force_bytes | ||
from django.utils.http import urlsafe_base64_encode | ||
|
||
from rest_framework.test import ( | ||
APIClient, APIRequestFactory | ||
) | ||
from rest_framework import status | ||
|
||
from ..models import User | ||
from ..backends import account_activation_token | ||
|
||
|
||
class AccountActivationTestCase(TestCase): | ||
""" | ||
This class defines tests for user account activation view | ||
""" | ||
|
||
def setUp(self): | ||
self.client = APIClient() | ||
self.factory = APIRequestFactory() | ||
|
||
self.user = { | ||
'user': { | ||
'username': 'fabish', | ||
'email': 'fabish.olasi@andela.com', | ||
'password': 'secretsantaS#3' | ||
} | ||
} | ||
|
||
self.user_2 = { | ||
'user': { | ||
'username': 'jimmy', | ||
'email': 'jimmy@example.com', | ||
'password': 'secretsantaS#3' | ||
} | ||
} | ||
|
||
def sign_user(self, user_data): | ||
response = self.client.post( | ||
reverse('register'), | ||
user_data, format='json') | ||
return response | ||
|
||
def test_email_sent_to_new_user(self): | ||
|
||
self.client.post( | ||
'/api/users/register', | ||
self.user, | ||
format='json' | ||
) | ||
self.assertEqual(len(mail.outbox), 1) | ||
msg = mail.outbox[0] | ||
self.assertEqual(msg.subject, 'Activate your account.') | ||
|
||
def test_email_not_sent_if_invalid(self): | ||
self.client.post( | ||
'api/users/register', | ||
self.user_2, | ||
format='json' | ||
) | ||
self.assertEqual(len(mail.outbox), 0) | ||
|
||
def test_link_activation_successful(self): | ||
self.sign_user(self.user) | ||
details = User.objects.get(username=self.user['user']['username']) | ||
pk = urlsafe_base64_encode(force_bytes(details.id)).decode() | ||
token = account_activation_token.make_token(details) | ||
protocol = 'http://' | ||
path = 'api/users/activate/' | ||
url = '{0}localhost:8000/{1}{pk}/{token}'.format( | ||
protocol, | ||
path, | ||
pk=pk, | ||
token=token) | ||
response = self.client.get(url, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) |
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
Oops, something went wrong.