apps.lyric.views now generates a token associated w. the user-entered email & includes a link w. the token to login to the testserver; .tests.test_views asserts the association and the inclusion

This commit is contained in:
Disco DeDisco
2026-01-30 17:52:44 -05:00
parent bab0b045b0
commit 2d61506a6d
2 changed files with 28 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
from django.test import TestCase
from unittest import mock
from ..models import Token
class SendLoginEmailViewTest(TestCase):
def test_redirects_to_home_page(self):
@@ -34,6 +35,25 @@ class SendLoginEmailViewTest(TestCase):
)
self.assertEqual(message.tags, "success")
def test_creates_token_associated_with_email(self):
self.client.post(
"/apps/lyric/send_login_email", data={"email": "discoman@example.com"}
)
token = Token.objects.get()
self.assertEqual(token.email, "discoman@example.com")
@mock.patch("apps.lyric.views.send_mail")
def test_sends_link_to_login_using_token_uid(self, mock_send_mail):
self.client.post(
"/apps/lyric/send_login_email", data={"email": "discoman@example.com"}
)
token = Token.objects.get()
expected_url = f"http://testserver/apps/lyric/login?token={token.uid}"
(subject, body, from_email, to_list), kwargs = mock_send_mail.call_args
self.assertIn(expected_url, body)
class LoginViewTest(TestCase):
def test_redirects_to_home_page(self):
response = self.client.get("/apps/lyric/login?token=abc123")

View File

@@ -1,13 +1,20 @@
from django.contrib import messages
from django.core.mail import send_mail
from django.shortcuts import redirect
from django.urls import reverse
from .models import Token
from ..dashboard.forms import ItemForm
def send_login_email(request):
email = request.POST["email"]
token = Token.objects.create(email=email)
url = request.build_absolute_uri(
reverse("login") + "?token=" + str(token.uid),
)
message_body = f"Use this magic link to login to your Dashboard:\n\n{url}"
send_mail(
"A magic login link to your Dashboard",
"Use this magic link to login to your Dashboard",
message_body,
"adman@howdy.earthmanrpg.me",
[email],
)