Files
python-tdd/src/functional_tests/test_login.py

45 lines
1.4 KiB
Python

import re
from django.core import mail
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from .base import FunctionalTest
TEST_EMAIL = "discoman@example.com"
SUBJECT = "A magic login link to your Dashboard"
class LoginTest(FunctionalTest):
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
)
self.wait_for(
lambda: self.assertIn(
"Check your email",
self.browser.find_element(By.CSS_SELECTOR, "body").text,
)
)
if self.test_server:
return
email = mail.outbox.pop()
self.assertIn(TEST_EMAIL, email.to)
self.assertEqual(email.subject, SUBJECT)
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}")
url = url_search.group(0)
self.assertIn(self.live_server_url, url)
self.browser.get(url)
self.wait_to_be_logged_in(email=TEST_EMAIL)
self.browser.find_element(By.CSS_SELECTOR, "#id_logout").click()
self.wait_to_be_logged_out(email=TEST_EMAIL)