Skip to content

Commit

Permalink
backup of code
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-gray-tangent committed Jul 18, 2024
1 parent 44d76e1 commit 5e0ea95
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
4 changes: 1 addition & 3 deletions app/accounts/service/active_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@


class SendActiveEmailService:
def __init__(self, user):
self.user = user

@staticmethod
def send_activation_email(request, user):
if user and request:
current_site = get_current_site(request)
Expand Down
54 changes: 54 additions & 0 deletions app/accounts/tests/test_active_email.py
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,
)

0 comments on commit 5e0ea95

Please sign in to comment.