Vaihe 1/1: users-moduuli, kirjautuminen ja käyttäjähallinta
- shared-moduuli: DomainError, EmailAddress, UnitOfWork, tapahtumat, TimestampedModel, daisyUI-lomakerenderöinti ja yhteiset pohjat - users-moduuli Clean Architecture -kerroksin: oma käyttäjämalli (AUTH_USER_MODEL), roolit ja oikeudet, harjoittelija-, vastuuhenkilö- ja viimeisen pääkäyttäjän säännöt - kirjautuminen sähköpostilla, yritysten rajoitus, "muista minut", istunnon katkaisu kun tunnus poistetaan käytöstä tai harjoittelu päättyy - kertakäyttöiset salasanalinkit (72 h / 60 min, uusi mitätöi vanhan), salasanasääntö ja create_admin-komento - käyttäjähallinta: lista (HTMX-haku), lomakkeet ja kopioitava linkki - Tailwind 4 + daisyUI 5 + HTMX + Alpine npm-buildilla, suomennokset - testit: domain, application (fakeilla), integraatio ja selaintestit (axe, vaalea/tumma, 1920/320 px); ruff, mypy ja import-linter - dokumentit päivitetty toteutuksen mukaisiksi Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
"""Playwright + axe browser tests. Run: pytest tests/browser -m browser (after npm run build)."""
|
||||
|
||||
import os
|
||||
from collections.abc import Callable
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
# Playwright's sync API runs an event loop; Django ORM calls in fixtures are still synchronous.
|
||||
os.environ.setdefault("DJANGO_ALLOW_ASYNC_UNSAFE", "true")
|
||||
|
||||
PASSWORD = "Vahva-Salasana-2026"
|
||||
|
||||
VIEWPORTS = {"desktop": {"width": 1920, "height": 1080}, "mobile": {"width": 320, "height": 640}}
|
||||
|
||||
|
||||
def pytest_collection_modifyitems(items: list[pytest.Item]) -> None:
|
||||
for item in items:
|
||||
item.add_marker(pytest.mark.browser)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def admin_user(transactional_db: Any) -> Any:
|
||||
from apps.users.infrastructure.orm import User
|
||||
|
||||
return User.objects.create_user(
|
||||
email="[email protected]", name="Aino Admin", password=PASSWORD, role="admin"
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def login(live_server: Any) -> Callable[[Any, str], None]:
|
||||
def do_login(page: Any, email: str) -> None:
|
||||
page.goto(f"{live_server.url}/login/")
|
||||
page.fill("#id_username", email)
|
||||
page.fill("#id_password", PASSWORD)
|
||||
page.click("button[type=submit]")
|
||||
page.wait_for_url(f"{live_server.url}/")
|
||||
|
||||
return do_login
|
||||
@@ -0,0 +1,113 @@
|
||||
from collections.abc import Callable
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from axe_playwright_python.sync_playwright import Axe
|
||||
|
||||
from tests.browser.conftest import VIEWPORTS
|
||||
|
||||
THEMES = ["light", "dark"]
|
||||
|
||||
|
||||
def assert_accessible(page: Any) -> None:
|
||||
results = Axe().run(
|
||||
page, options={"runOnly": ["wcag2a", "wcag2aa", "wcag21a", "wcag21aa", "wcag22aa"]}
|
||||
)
|
||||
assert results.violations_count == 0, results.generate_report()
|
||||
|
||||
|
||||
def no_horizontal_scroll(page: Any) -> bool:
|
||||
return bool(page.evaluate("document.documentElement.scrollWidth <= window.innerWidth"))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def themed_page(browser: Any, request: pytest.FixtureRequest) -> Any:
|
||||
theme, viewport = request.param
|
||||
context = browser.new_context(viewport=VIEWPORTS[viewport])
|
||||
context.add_init_script(f"localStorage.setItem('theme', '{theme}')")
|
||||
page = context.new_page()
|
||||
yield page
|
||||
context.close()
|
||||
|
||||
|
||||
ALL = [(t, v) for t in THEMES for v in VIEWPORTS]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("themed_page", ALL, indirect=True, ids=lambda p: f"{p[0]}-{p[1]}")
|
||||
def test_public_pages(themed_page: Any, live_server: Any, transactional_db: Any) -> None:
|
||||
for path in (
|
||||
"/login/",
|
||||
"/login/?access=ended",
|
||||
"/password/forgot/",
|
||||
"/password/set/setup/x/y/",
|
||||
):
|
||||
themed_page.goto(f"{live_server.url}{path}")
|
||||
assert_accessible(themed_page)
|
||||
assert no_horizontal_scroll(themed_page), path
|
||||
|
||||
|
||||
@pytest.mark.parametrize("themed_page", ALL, indirect=True, ids=lambda p: f"{p[0]}-{p[1]}")
|
||||
def test_user_management_pages(
|
||||
themed_page: Any, live_server: Any, admin_user: Any, login: Callable[[Any, str], None]
|
||||
) -> None:
|
||||
login(themed_page, admin_user.email)
|
||||
for path in ("/", "/users/", "/users/new/", f"/users/{admin_user.pk}/edit/"):
|
||||
themed_page.goto(f"{live_server.url}{path}")
|
||||
assert_accessible(themed_page)
|
||||
assert no_horizontal_scroll(themed_page), path
|
||||
|
||||
|
||||
def test_form_errors_are_accessible(
|
||||
page: Any, live_server: Any, admin_user: Any, login: Callable[[Any, str], None]
|
||||
) -> None:
|
||||
login(page, admin_user.email)
|
||||
page.goto(f"{live_server.url}/users/new/")
|
||||
page.select_option("#id_role", "intern")
|
||||
page.click("main button[type=submit]")
|
||||
end_date = page.locator("#id_name")
|
||||
assert end_date.get_attribute("aria-invalid") == "true"
|
||||
assert "id_name_error" in (end_date.get_attribute("aria-describedby") or "")
|
||||
assert_accessible(page)
|
||||
|
||||
|
||||
def test_keyboard_password_link_flow(
|
||||
page: Any, live_server: Any, admin_user: Any, login: Callable[[Any, str], None]
|
||||
) -> None:
|
||||
"""Create a password link with the keyboard only and copy it."""
|
||||
page.context.grant_permissions(["clipboard-read", "clipboard-write"])
|
||||
login(page, admin_user.email)
|
||||
page.goto(f"{live_server.url}/users/{admin_user.pk}/edit/")
|
||||
page.get_by_role("button", name="Luo salasanalinkki").focus()
|
||||
page.keyboard.press("Enter")
|
||||
dialog = page.locator("dialog#confirm-password-link")
|
||||
assert dialog.evaluate("d => d.open")
|
||||
page.get_by_role("button", name="Luo linkki").focus()
|
||||
page.keyboard.press("Enter")
|
||||
link_box = page.locator("#password-link-box")
|
||||
link_box.wait_for()
|
||||
assert not dialog.evaluate("d => d.open")
|
||||
page.wait_for_timeout(500) # let the modal close animation finish before axe
|
||||
page.get_by_role("button", name="Kopioi").focus()
|
||||
page.keyboard.press("Enter")
|
||||
page.wait_for_function(
|
||||
"document.getElementById('live-region').textContent.includes('kopioitu')"
|
||||
)
|
||||
copied = page.evaluate("navigator.clipboard.readText()")
|
||||
assert "/password/set/setup/" in copied
|
||||
assert_accessible(page)
|
||||
|
||||
|
||||
def test_htmx_search_updates_table(
|
||||
page: Any, live_server: Any, admin_user: Any, login: Callable[[Any, str], None]
|
||||
) -> None:
|
||||
from apps.users.infrastructure.orm import User
|
||||
|
||||
User.objects.create_user(email="[email protected]", name="Eeva Esimerkki")
|
||||
login(page, admin_user.email)
|
||||
page.goto(f"{live_server.url}/users/")
|
||||
page.fill("#id_q", "eeva")
|
||||
page.wait_for_url("**/users/?q=eeva**")
|
||||
rows = page.locator("#user-table tbody tr")
|
||||
rows.first.wait_for()
|
||||
assert rows.count() == 1
|
||||
assert "Eeva Esimerkki" in rows.first.inner_text()
|
||||
Reference in New Issue
Block a user