new test_dashboard FT (part 1) for username applet on dashboard; apps/dashboard/home.html gained new applet section to support additions; new urlpatterns in apps.dash.urls; tweaks to .views, including the @login_required decorator and set_profile() FBV; new ITs in .tests.integrated.test_views
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Disco DeDisco
2026-03-04 00:07:10 -05:00
parent 649bd39df9
commit fd59b02c3a
6 changed files with 97 additions and 6 deletions

View File

@@ -241,7 +241,7 @@ class ViewAuthListTest(TestCase):
def test_anonymous_user_is_redirected(self):
response = self.client.get(reverse("view_list", args=[self.our_list.id]))
self.assertRedirects(response, "/")
self.assertRedirects(response, "/", fetch_redirect_response=False)
def test_non_owner_non_shared_user_gets_403(self):
stranger = User.objects.create(email="stranger@example.com")
@@ -308,3 +308,36 @@ class SetThemeTest(TestCase):
parsed = lxml.html.fromstring(response.content)
swatches = parsed.cssselect(".swatch")
self.assertEqual(len(swatches), len(response.context["themes"]))
class ProfileViewTest(TestCase):
def setUp(self):
self.user = User.objects.create(email="discoman@example.com")
self.client.force_login(self.user)
def test_post_username_saves_to_user(self):
self.client.post("/dashboard/set_profile", data={"username": "discoman"})
self.user.refresh_from_db()
self.assertEqual(self.user.username, "discoman")
def test_post_username_requires_login(self):
self.client.logout()
response = self.client.post("/dashboard/set_profile", data={"username": "somnambulist"})
self.assertRedirects(response, "/?next=/dashboard/set_profile")
def test_dash_renders_username_applet(self):
response = self.client.get("/")
parsed = lxml.html.fromstring(response.content)
[applet] = parsed.cssselect("#id_applet_username")
self.assertIn("di…an@e…e.com", applet.text_content())
[_] = parsed.cssselect("#id_new_username")
def test_dash_shows_display_name_in_applet(self):
self.user.username = "discoman"
self.user.save()
response = self.client.get("/")
parsed = lxml.html.fromstring(response.content)
[applet] = parsed.cssselect("#id_applet_username")
self.assertIn("discoman", applet.text_content())
[username_input] = parsed.cssselect("#id_new_username")
self.assertEqual("discoman", username_input.get("value"))

View File

@@ -4,7 +4,8 @@ from . import views
urlpatterns = [
path('new_list', views.new_list, name='new_list'),
path('list/<uuid:list_id>/', views.view_list, name='view_list'),
path('users/<uuid:user_id>/', views.my_lists, name='my_lists'),
path('list/<uuid:list_id>/share_list', views.share_list, name="share_list"),
path('set_theme', views.set_theme, name='set_theme'),
path('set_profile', views.set_profile, name='set_profile'),
path('users/<uuid:user_id>/', views.my_lists, name='my_lists'),
]

View File

@@ -1,4 +1,5 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseForbidden
from django.shortcuts import redirect, render
@@ -51,7 +52,7 @@ def view_list(request, list_id):
form.save()
return redirect(our_list)
return render(request, "apps/dashboard/list.html", {"list": our_list, "form": form})
def my_lists(request, user_id):
owner = User.objects.get(id=user_id)
if not request.user.is_authenticated:
@@ -72,12 +73,19 @@ def share_list(request, list_id):
messages.success(request, "An invite has been sent if that address is registered.")
return redirect(our_list)
@login_required(login_url="/")
def set_theme(request):
if not request.user.is_authenticated:
return redirect("home")
if request.method == "POST":
theme = request.POST.get("theme", "")
if theme in UNLOCKED_THEMES:
request.user.theme = theme
request.user.save(update_fields=["theme"])
return redirect("home")
@login_required(login_url="/")
def set_profile(request):
if request.method == "POST":
username = request.POST.get("username", "")
request.user.username = username
request.user.save(update_fields=["username"])
return redirect("/")

View File

@@ -65,7 +65,7 @@ class FunctionalTest(StaticLiveServerTestCase):
def dump_html(self):
path = SCREEN_DUMP_LOCATION / self._get_filename("html")
print("dumping page html to", path)
path.write_text(self.browser.page_source)
path.write_text(self.browser.page_source, encoding="utf-8")
def _get_filename(self, extension):
timestamp = datetime.now().isoformat().replace(":", ".")

View File

@@ -0,0 +1,39 @@
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from .base import FunctionalTest
class DashboardMaintenanceTest(FunctionalTest):
def test_user_without_username_can_claim_unclaimed_username(self):
# 1. Create a pre-authenticated session for discoman@example.com
self.create_pre_authenticated_session("discoman@example.com")
# 2. Navigate to self.live_server_url + "/"
self.browser.get(self.live_server_url)
# 3. Find the username applet on the page; look for a <section> or <div> with id="id_username_applet"
self.browser.find_element(By.ID, "id_applet_username")
# 4. Assert it shows the current display name (truncated email: di…an@e…e.com)
self.assertIn("di…an@e…e.com", self.browser.find_element(By.ID, "id_applet_username").text)
# 5. Find the username input field inside the applet & type a username
username_input = self.browser.find_element(By.CSS_SELECTOR, "#id_new_username")
# 6. Type a username, e.g., discoman
username_input.send_keys("discoman")
self.wait_for(
lambda: self.browser.find_element(By.CSS_SELECTOR, "#id_new_username:valid")
)
# 7. Submit the form (click a btn or press Enter)
username_input.send_keys(Keys.ENTER)
# 8. Without a page reload, wait for the navbar to update; user wait_for() to check that the navbar text now contains "discoman"
self.wait_for(
lambda: self.assertIn(
"discoman",
self.browser.find_element(By.CLASS_NAME, "navbar-text").text
)
)
# 9. Also assert the applet input now shows "discoman" as its value
self.wait_for(
lambda: self.assertEqual(
"discoman",
self.browser.find_element(By.CSS_SELECTOR, "#id_new_username").get_attribute("value")
)
)

View File

@@ -1,4 +1,5 @@
{% extends "core/base.html" %}
{% load lyric_extras %}
{% block title_text %}Start a new to-do list{% endblock title_text %}
{% block header_text %}Start a new to-do list{% endblock header_text %}
@@ -29,5 +30,14 @@
</div>
{% endfor %}
</section>
<section id="id_applet_username">
<h1>{{ user|display_name }}</h1>
<div class="form-container">
<form method="POST" action="{% url "set_profile" %}">
{% csrf_token %}
<input id="id_new_username" name="username" required value="{{ user.username|default:'' }}">
</form>
</div>
</section>
{% endif %}
{% endblock content %}