diff --git a/src/apps/lyric/tests/test_views.py b/src/apps/lyric/tests/test_views.py index 0369c7e..b70ef3b 100644 --- a/src/apps/lyric/tests/test_views.py +++ b/src/apps/lyric/tests/test_views.py @@ -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") diff --git a/src/apps/lyric/views.py b/src/apps/lyric/views.py index 96af34c..dfa0927 100644 --- a/src/apps/lyric/views.py +++ b/src/apps/lyric/views.py @@ -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("/") diff --git a/src/functional_tests/test_login.py b/src/functional_tests/test_login.py index d74d073..df73e8c 100644 --- a/src/functional_tests/test_login.py +++ b/src/functional_tests/test_login.py @@ -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}") diff --git a/src/templates/core/base.html b/src/templates/core/base.html index 487f41a..22ec788 100644 --- a/src/templates/core/base.html +++ b/src/templates/core/base.html @@ -12,13 +12,13 @@
-