created new FT file at functional_tests.test_my_lists, where the first test model focuses on the integrity of cookie sessions; much of FT login/logout logic offloaded to helper functions in .base;logout django templating added to base.html

This commit is contained in:
Disco DeDisco
2026-02-01 20:06:01 -05:00
parent 36f4182a4f
commit cf33f0ccd4
5 changed files with 51 additions and 10 deletions

View File

@@ -1,8 +1,10 @@
from django.urls import include, path from django.contrib.auth import views as auth_views
from . import views from django.urls import path
from . import views as lyric_views
urlpatterns = [ urlpatterns = [
path('send_login_email', views.send_login_email, name='send_login_email'), path('send_login_email', lyric_views.send_login_email, name='send_login_email'),
path('login', views.login, name="login"), path('login', lyric_views.login, name='login'),
path('logout', auth_views.LogoutView.as_view(next_page='/'), name='logout')
] ]

View File

@@ -43,3 +43,17 @@ class FunctionalTest(StaticLiveServerTestCase):
def get_item_input_box(self): def get_item_input_box(self):
return self.browser.find_element(By.ID, "id_text") return self.browser.find_element(By.ID, "id_text")
def wait_to_be_logged_in(self, email):
self.wait_for(
lambda: self.browser.find_element(By.CSS_SELECTOR, "#id_logout"),
)
navbar = self.browser.find_element(By.CSS_SELECTOR, ".navbar")
self.assertIn(email, navbar.text)
def wait_to_be_logged_out(self, email):
self.wait_for(
lambda: self.browser.find_element(By.CSS_SELECTOR, "input[name=email]"),
)
navbar = self.browser.find_element(By.CSS_SELECTOR, ".navbar")
self.assertNotIn(email, navbar.text)

View File

@@ -34,8 +34,8 @@ class LoginTest(FunctionalTest):
self.browser.get(url) self.browser.get(url)
self.wait_for( self.wait_to_be_logged_in(email=TEST_EMAIL)
lambda: self.browser.find_element(By.CSS_SELECTOR, "#id_logout"),
) self.browser.find_element(By.CSS_SELECTOR, "#id_logout").click()
navbar = self.browser.find_element(By.CSS_SELECTOR, ".navbar")
self.assertIn(TEST_EMAIL, navbar.text) self.wait_to_be_logged_out(email=TEST_EMAIL)

View File

@@ -0,0 +1,25 @@
from django.conf import settings
from django.contrib.auth import BACKEND_SESSION_KEY, SESSION_KEY, get_user_model
from django.contrib.sessions.backends.db import SessionStore
from .base import FunctionalTest
User = get_user_model()
class MyListsTest(FunctionalTest):
def create_pre_authenticated_session(self, email):
user = User.objects.create(email=email)
session = SessionStore()
session[SESSION_KEY] = user.pk
session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0]
session.save()
## to set a cookie we need to first visit the domain
## 404 pages load the quickest!
self.browser.get(self.live_server_url + "/404_no_such_url/")
self.browser.add_cookie(
dict(
name=settings.SESSION_COOKIE_NAME,
value=session.session_key,
path="/",
)
)

View File

@@ -19,7 +19,7 @@
</a> </a>
{% if user.email %} {% if user.email %}
<span class="navbar-text">Logged in as {{ user.email }}</span> <span class="navbar-text">Logged in as {{ user.email }}</span>
<form action="TODO" method="POST"> <form method="POST" action="{% url "logout" %}">
{% csrf_token %} {% csrf_token %}
<button id="id_logout" class="btn btn-outline-secondary" type="submit"> <button id="id_logout" class="btn btn-outline-secondary" type="submit">
Log Out Log Out