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:
@@ -1,30 +1,35 @@
|
|||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from .. import views as lyric_views
|
from unittest import mock
|
||||||
|
|
||||||
class SendLoginEmailViewTest(TestCase):
|
class SendLoginEmailViewTest(TestCase):
|
||||||
def test_redirects_to_home_page(self):
|
def test_redirects_to_home_page(self):
|
||||||
response = self.client.post(
|
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, "/")
|
self.assertRedirects(response, "/")
|
||||||
|
|
||||||
def test_sends_mail_to_address_from_post(self):
|
@mock.patch("apps.lyric.views.send_mail")
|
||||||
self.send_mail_called = False
|
def test_sends_mail_to_address_from_post(self, mock_send_mail):
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
self.client.post(
|
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(mock_send_mail.called, True)
|
||||||
self.assertEqual(self.subject, "A magic login link for your Dashboard")
|
(subject, body, from_email, to_list), kwargs = mock_send_mail.call_args
|
||||||
self.assertEqual(self.from_email, "adman@howdy.earthmanrpg.me")
|
self.assertEqual(subject, "A magic login link to your Dashboard")
|
||||||
self.assertEqual(self.to_list, ["disco@example.com"])
|
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")
|
||||||
|
|||||||
@@ -1,7 +1,17 @@
|
|||||||
|
from django.contrib import messages
|
||||||
from django.core.mail import send_mail
|
from django.core.mail import send_mail
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
|
|
||||||
def send_login_email(request):
|
def send_login_email(request):
|
||||||
email = request.POST["email"]
|
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("/")
|
return redirect("/")
|
||||||
|
|||||||
@@ -4,11 +4,11 @@ from selenium.webdriver.common.by import By
|
|||||||
from selenium.webdriver.common.keys import Keys
|
from selenium.webdriver.common.keys import Keys
|
||||||
from .base import FunctionalTest
|
from .base import FunctionalTest
|
||||||
|
|
||||||
TEST_EMAIL = "disco@example.com"
|
TEST_EMAIL = "discoman@example.com"
|
||||||
SUBJECT = "a magic link to login to your Dashboard"
|
SUBJECT = "A magic login link to your Dashboard"
|
||||||
|
|
||||||
class LoginTest(FunctionalTest):
|
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.get(self.live_server_url)
|
||||||
self.browser.find_element(By.CSS_SELECTOR, "input[name=email]").send_keys(
|
self.browser.find_element(By.CSS_SELECTOR, "input[name=email]").send_keys(
|
||||||
TEST_EMAIL, Keys.ENTER
|
TEST_EMAIL, Keys.ENTER
|
||||||
@@ -25,7 +25,7 @@ class LoginTest(FunctionalTest):
|
|||||||
self.assertIn(TEST_EMAIL, email.to)
|
self.assertIn(TEST_EMAIL, email.to)
|
||||||
self.assertEqual(email.subject, SUBJECT)
|
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)
|
url_search = re.search(r"http://.+/.+$", email.body)
|
||||||
if not url_search:
|
if not url_search:
|
||||||
self.fail(f"Could not find url in email body:\n{email.body}")
|
self.fail(f"Could not find url in email body:\n{email.body}")
|
||||||
|
|||||||
@@ -12,13 +12,13 @@
|
|||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="navbar">
|
<nav class="navbar">
|
||||||
<div class="container-fluid">
|
<div class="container-fluid">
|
||||||
<a href="/" class="navbar-brand">Welcome, Earthman</a>
|
<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">
|
<div class="input-group">
|
||||||
<label for="id_email_input" class="navbar-text me-2">
|
<label for="id_email_input" class="navbar-text me-2">
|
||||||
enter your email to log in
|
enter email for login:
|
||||||
</label>
|
</label>
|
||||||
<input
|
<input
|
||||||
id="id_email_input"
|
id="id_email_input"
|
||||||
@@ -30,7 +30,21 @@
|
|||||||
</div>
|
</div>
|
||||||
</form>
|
</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>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<div class="row justify-content-center p-5 bg-body-tertiary rounded-3">
|
<div class="row justify-content-center p-5 bg-body-tertiary rounded-3">
|
||||||
<div class="col-lg-6 text-center">
|
<div class="col-lg-6 text-center">
|
||||||
|
|||||||
Reference in New Issue
Block a user