-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added templates - updated templates - updated forms - updated View
- Loading branch information
1 parent
323d164
commit 66dbfa0
Showing
20 changed files
with
544 additions
and
43 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,3 @@ | ||
SECRET_KEY='' | ||
DEBUG=True | ||
DB_HOST=db | ||
DB_PORT=5432 | ||
|
@@ -19,3 +18,4 @@ EMAIL_HOST_PASSWORD='' | |
EMAIL_BACKEND_CONSOLE='True/False' | ||
EMAIL_USE_TLS=True | ||
SECRET_KEY='' | ||
DEFAULT_FROM_EMAIL='[email protected]' |
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,34 @@ | ||
import os | ||
|
||
from django.contrib.sites.shortcuts import get_current_site | ||
from django.core.mail import EmailMultiAlternatives | ||
from django.template.loader import render_to_string | ||
from django.utils.encoding import force_bytes | ||
from django.utils.http import urlsafe_base64_encode | ||
|
||
from accounts.tokens import account_activation_token | ||
|
||
|
||
class SendActiveEmailService: | ||
@staticmethod | ||
def send_activation_email(request, user): | ||
if user and request: | ||
current_site = get_current_site(request) | ||
mail_subject = "Activate your account." | ||
message = render_to_string( | ||
"accounts/email/activation_email.html", | ||
{ | ||
"user": user, | ||
"domain": current_site.domain, | ||
"uid": urlsafe_base64_encode(force_bytes(user.pk)), | ||
"token": account_activation_token.make_token(user), | ||
}, | ||
) | ||
text_content = ( | ||
"Please activate your account by clicking the link provided in the email." | ||
) | ||
email = EmailMultiAlternatives( | ||
mail_subject, text_content, os.environ.get("DEFAULT_FROM_EMAIL"), [user.email] | ||
) | ||
email.attach_alternative(message, "text/html") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from unittest.mock import MagicMock, patch | ||
|
||
from django.contrib.sites.shortcuts import get_current_site | ||
from django.core import mail | ||
from django.test import RequestFactory, TestCase | ||
from django.utils.encoding import force_bytes | ||
from django.utils.http import urlsafe_base64_encode | ||
|
||
from accounts.service.active_email import ( # Adjust the import path as necessary | ||
SendActiveEmailService, | ||
) | ||
from accounts.tokens import account_activation_token | ||
from users.models import CustomUser # Import your custom user model | ||
|
||
|
||
class SendActiveEmailServiceTest(TestCase): | ||
def setUp(self): | ||
self.factory = RequestFactory() | ||
self.user = CustomUser.objects.create_user( | ||
username="testuser", email="[email protected]", password="password123" | ||
) | ||
self.request = self.factory.get("/fake-path") | ||
self.service = SendActiveEmailService() | ||
|
||
def test_send_activation_email(self): | ||
with patch("accounts.service.active_email.render_to_string") as mock_render: | ||
# Set up the mocks | ||
mock_render.return_value = "<html>mocked template</html>" | ||
|
||
# # Call the method | ||
self.service.send_activation_email(self.request, self.user) | ||
|
||
# Check that render_to_string was called with the correct parameters | ||
mock_render.assert_called_once_with( | ||
"accounts/email/activation_email.html", | ||
{ | ||
"user": self.user, | ||
"domain": get_current_site(self.request).domain, | ||
"uid": urlsafe_base64_encode(force_bytes(self.user.pk)), | ||
"token": account_activation_token.make_token(self.user), | ||
}, | ||
) | ||
|
||
# Check that an email was sent | ||
self.assertEqual(len(mail.outbox), 1) | ||
sent_email = mail.outbox[0] | ||
self.assertEqual(sent_email.subject, "Activate your account.") | ||
self.assertEqual(sent_email.to, [self.user.email]) | ||
self.assertIn("mocked template", sent_email.alternatives[0][0]) | ||
self.assertEqual(sent_email.alternatives[0][1], "text/html") | ||
self.assertIn( | ||
"Please activate your account by clicking the link provided in the email.", | ||
sent_email.body, | ||
) |
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,40 @@ | ||
import unittest | ||
from datetime import datetime | ||
|
||
import six | ||
from django.contrib.auth.tokens import PasswordResetTokenGenerator | ||
|
||
from accounts.tokens import AccountActivationTokenGenerator | ||
|
||
|
||
# Speculate user model class for test abstraction | ||
class User: | ||
def __init__(self, id, is_active): | ||
self.pk = id | ||
self.is_active = is_active | ||
|
||
|
||
class TestAccountActivationTokenGenerator(unittest.TestCase): | ||
def setUp(self): | ||
self.generator = AccountActivationTokenGenerator() | ||
self.timestamp = datetime.now() | ||
|
||
def test_make_hash_value_active_user(self): | ||
user = User(1, True) | ||
hash_val = self.generator._make_hash_value(user, self.timestamp) | ||
expected_val = ( | ||
six.text_type(user.pk) + six.text_type(self.timestamp) + six.text_type(user.is_active) | ||
) | ||
self.assertEqual(hash_val, expected_val) | ||
|
||
def test_make_hash_value_inactive_user(self): | ||
user = User(1, False) | ||
hash_val = self.generator._make_hash_value(user, self.timestamp) | ||
expected_val = ( | ||
six.text_type(user.pk) + six.text_type(self.timestamp) + six.text_type(user.is_active) | ||
) | ||
self.assertEqual(hash_val, expected_val) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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,24 @@ | ||
from django.contrib.auth.tokens import PasswordResetTokenGenerator | ||
|
||
|
||
class AccountActivationTokenGenerator(PasswordResetTokenGenerator): | ||
""" | ||
AccountActivationTokenGenerator class is a subclass of PasswordResetTokenGenerator. | ||
Methods: | ||
_make_hash_value(self, user, timestamp): | ||
This method takes in a user object and a timestamp and returns a hash value | ||
that is used to generate an account activation token. The hash value is | ||
calculated by concatenating the user's primary key, timestamp, and active | ||
status. | ||
DO NOT MODIFY THIS METHOD. UNLESS YOU GET PERMISSION FROM THE PROJECT OWNER. | ||
""" | ||
|
||
def _make_hash_value(self, user, timestamp): | ||
return f"{user.pk}{timestamp}{user.is_active}" | ||
|
||
|
||
account_activation_token = AccountActivationTokenGenerator() |
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,13 @@ | ||
{% extends "base.html" %} | ||
{% load static %} | ||
{% load i18n %} | ||
|
||
{% block title %}{% trans "Activated" %}{% endblock %} | ||
|
||
{% block content %} | ||
<div class="container"> | ||
<h1>Account Activated</h1> | ||
<p>Your account has been successfully activated. You can now log in using your credentials.</p> | ||
<a href="{% url 'accounts:login' %}">Login</a> | ||
</div> | ||
{% endblock %} |
Oops, something went wrong.