mocking added thruout apps.lyric.tests.test_views, where UTs mock message sending; status message loop added to base.html below navbar; hardcoded assert and model/view values aligned across lyric app, UTs & FTs

This commit is contained in:
Disco DeDisco
2026-01-30 17:23:07 -05:00
parent 4b1906b1ee
commit 93eb497dec
4 changed files with 57 additions and 28 deletions

View File

@@ -1,30 +1,35 @@
from django.test import TestCase
from .. import views as lyric_views
from unittest import mock
class SendLoginEmailViewTest(TestCase):
def test_redirects_to_home_page(self):
response = self.client.post(
"/apps/lyric/send_login_email", data={"email": "disco@example.com"}
"/apps/lyric/send_login_email", data={"email": "discoman@example.com"}
)
self.assertRedirects(response, "/")
def test_sends_mail_to_address_from_post(self):
self.send_mail_called = False
def fake_send_mail(subject, body, from_email, to_list):
self.send_mail_called = True
self.subject = subject
self.body = body
self.from_email = from_email
self.to_list = to_list
lyric_views.send_mail = fake_send_mail
@mock.patch("apps.lyric.views.send_mail")
def test_sends_mail_to_address_from_post(self, mock_send_mail):
self.client.post(
"/apps/lyric/send_login_email", data={"email": "disco@example.com"}
"/apps/lyric/send_login_email", data={"email": "discoman@example.com"}
)
self.assertTrue(self.send_mail_called)
self.assertEqual(self.subject, "A magic login link for your Dashboard")
self.assertEqual(self.from_email, "adman@howdy.earthmanrpg.me")
self.assertEqual(self.to_list, ["disco@example.com"])
self.assertEqual(mock_send_mail.called, True)
(subject, body, from_email, to_list), kwargs = mock_send_mail.call_args
self.assertEqual(subject, "A magic login link to your Dashboard")
self.assertEqual(from_email, "adman@howdy.earthmanrpg.me")
self.assertEqual(to_list, ["discoman@example.com"])
def test_adds_success_message(self):
response = self.client.post(
"/apps/lyric/send_login_email",
data={"email": "discoman@example.com"},
follow=True
)
message = list(response.context["messages"])[0]
self.assertEqual(
message.message,
"Check your email!—there you'll find a magic login link. But hurry… it's only temporary!",
)
self.assertEqual(message.tags, "success")

View File

@@ -1,7 +1,17 @@
from django.contrib import messages
from django.core.mail import send_mail
from django.shortcuts import redirect
def send_login_email(request):
email = request.POST["email"]
send_mail("A magic login link for your Dashboard", "magic link sample body", "adman@howdy.earthmanrpg.me", [email])
send_mail(
"A magic login link to your Dashboard",
"Use this magic link to login to your Dashboard",
"adman@howdy.earthmanrpg.me",
[email],
)
messages.success(
request,
"Check your email!—there you'll find a magic login link. But hurry… it's only temporary!",
)
return redirect("/")

View File

@@ -4,11 +4,11 @@ from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from .base import FunctionalTest
TEST_EMAIL = "disco@example.com"
SUBJECT = "a magic link to login to your Dashboard"
TEST_EMAIL = "discoman@example.com"
SUBJECT = "A magic login link to your Dashboard"
class LoginTest(FunctionalTest):
def test_login_using_magic_linl(self):
def test_login_using_magic_link(self):
self.browser.get(self.live_server_url)
self.browser.find_element(By.CSS_SELECTOR, "input[name=email]").send_keys(
TEST_EMAIL, Keys.ENTER
@@ -25,7 +25,7 @@ class LoginTest(FunctionalTest):
self.assertIn(TEST_EMAIL, email.to)
self.assertEqual(email.subject, SUBJECT)
self.assertIn("Use this link to login to your Dashboard", email.body)
self.assertIn("Use this magic link to login to your Dashboard", email.body)
url_search = re.search(r"http://.+/.+$", email.body)
if not url_search:
self.fail(f"Could not find url in email body:\n{email.body}")

View File

@@ -12,13 +12,13 @@
<body>
<div class="container">
<div class="navbar">
<nav class="navbar">
<div class="container-fluid">
<a href="/" class="navbar-brand">Welcome, Earthman</a>
<form method="POST" action="/apps/lyric/send_login_email">
<form method="POST" action="{% url "send_login_email" %}">
<div class="input-group">
<label for="id_email_input" class="navbar-text me-2">
enter your email to log in
enter email for login:
</label>
<input
id="id_email_input"
@@ -30,7 +30,21 @@
</div>
</form>
</div>
</div>
</nav>
{% if messages %}
<div class="row">
<div class="col-md-12">
{% for message in messages %}
{% if message.level_tag == 'success' %}
<div class="alert alert-success">{{ message }}</div>
{% else %}
<div class="alert alert-warning">{{ message }}</div>
{% endif %}
{% endfor %}
</div>
</div>
{% endif %}
<div class="row justify-content-center p-5 bg-body-tertiary rounded-3">
<div class="col-lg-6 text-center">