Vaihe 1/2: asiakkaat, yhteyshenkilöt ja laskutusyksiköt

- customers-moduuli Clean Architecture -kerroksin: asiakas (yritys/kuluttaja),
  Y-tunnuksen tarkiste ja yksilöllisyys, yhteyshenkilöt, laskutusyksiköt
  (verkkolaskuosoite vaatii välittäjätunnuksen)
- henkilötunnus salattuna (Fernet) ja HMAC-tiiviste päällekkäisyyksiin;
  pääkäyttäjä ja johto näkevät ja poistavat, muut näkevät peitettynä
- sisäinen asiakas data-migraatiolla, ei poistettavissa
- yleinen poistonesto käänteisten relaatioiden perusteella (tulevat
  PROTECT-viiteavaimet estävät poiston automaattisesti)
- asiakkaan sivu: lisäys ja muokkaus dialogissa HTMX:llä, fokuksen hallinta
  ja ruudunlukijailmoitukset; HTMX näyttää nyt 422-validointivirheet
- testit: domain, application, integraatio ja selaintestit (axe);
  yhteiset fixturet apps/conftest.py:hin, FakeUnitOfWork shared-moduuliin
- import-linter: moduulien riippumattomuussäännöt
- dokumentit päivitetty

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-29 13:47:09 +02:00
co-authored by Claude Opus 5.5
parent e7b2745916
commit a81c073d6a
68 changed files with 3470 additions and 144 deletions
+28
View File
@@ -5,6 +5,7 @@ from collections.abc import Callable
from typing import Any
import pytest
from axe_playwright_python.sync_playwright import Axe
# Playwright's sync API runs an event loop; Django ORM calls in fixtures are still synchronous.
os.environ.setdefault("DJANGO_ALLOW_ASYNC_UNSAFE", "true")
@@ -38,3 +39,30 @@ def login(live_server: Any) -> Callable[[Any, str], None]:
page.wait_for_url(f"{live_server.url}/")
return do_login
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]
+1 -28
View File
@@ -2,35 +2,8 @@ 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]
from tests.browser.conftest import ALL, assert_accessible, no_horizontal_scroll
@pytest.mark.parametrize("themed_page", ALL, indirect=True, ids=lambda p: f"{p[0]}-{p[1]}")
+77
View File
@@ -0,0 +1,77 @@
from collections.abc import Callable
from typing import Any
import pytest
from tests.browser.conftest import ALL, assert_accessible, no_horizontal_scroll
@pytest.fixture
def customer(admin_user: Any) -> Any:
from apps.customers.infrastructure.orm import BillingUnitModel, ContactModel, CustomerModel
c = CustomerModel.objects.create(
name="Oy Esimerkki Ab", type="company", business_id="0112038-9"
)
ContactModel.objects.create(customer=c, name="Maija Meikäläinen", email="[email protected]")
BillingUnitModel.objects.create(
customer=c,
name="Pääkonttori",
e_invoice_address="003701120389",
e_invoice_operator="003721291126",
)
return c
@pytest.mark.parametrize("themed_page", ALL, indirect=True, ids=lambda p: f"{p[0]}-{p[1]}")
def test_customer_pages(
themed_page: Any,
live_server: Any,
admin_user: Any,
customer: Any,
login: Callable[[Any, str], None],
) -> None:
login(themed_page, admin_user.email)
for path in (
"/customers/",
"/customers/new/",
f"/customers/{customer.pk}/",
f"/customers/{customer.pk}/edit/",
):
themed_page.goto(f"{live_server.url}{path}")
assert_accessible(themed_page)
assert no_horizontal_scroll(themed_page), path
# The contact dialog (loaded with HTMX) is accessible too.
themed_page.goto(f"{live_server.url}/customers/{customer.pk}/")
themed_page.get_by_role("link", name="Lisää yhteyshenkilö").click()
themed_page.locator("#item-dialog[open] #id_name").wait_for()
themed_page.wait_for_timeout(300)
assert_accessible(themed_page)
def test_add_contact_with_keyboard(
page: Any, live_server: Any, admin_user: Any, customer: Any, login: Callable[[Any, str], None]
) -> None:
login(page, admin_user.email)
page.goto(f"{live_server.url}/customers/{customer.pk}/")
page.get_by_role("link", name="Lisää yhteyshenkilö").focus()
page.keyboard.press("Enter")
name = page.locator("#item-dialog[open] #id_name")
name.wait_for()
# Submitting empty shows the error and keeps focus in the dialog on the invalid field.
page.locator("#item-dialog button[type=submit]").focus()
page.keyboard.press("Enter")
page.wait_for_selector("#id_name[aria-invalid='true']")
# Focus moves in htmx:afterSettle, a moment after the swap.
page.wait_for_function("document.activeElement && document.activeElement.id === 'id_name'")
page.keyboard.type("Pekka Puhelin")
page.keyboard.press("Enter")
page.wait_for_function(
"document.getElementById('live-region').textContent.includes('tallennettu')"
)
assert page.evaluate("document.activeElement.id") == "contacts-heading"
assert not page.evaluate("document.getElementById('item-dialog').open")
assert "Pekka Puhelin" in page.locator("#contacts-section").inner_text()