- Uusi catalog-moduuli: palveluyksikkö → (palvelu) → työtehtävä ilman hintoja. - Pohjatiedot-sivu /catalog/ pääkäyttäjälle ja johdolle (oikeus catalog.manage): lisäys ja muokkaus dialogissa, Siirrä ylös / Siirrä alas, käytöstä poisto ja käyttöön otto. Fokus ja aria-live-ilmoitus jokaisen muutoksen jälkeen. - Rivejä ei poisteta. Käytöstä poistetun yläkohdan alla olevat rivit säilyttävät oman tilansa, mutta eivät ole valittavissa. Nimi on yksilöllinen samalla tasolla kirjainkoosta riippumatta. - Työtehtävän voi siirtää saman yksikön toiseen palveluun tai suoraan yksikön alle. - Luvun 11.1 lista data-migraationa (0002_seed), jos taulut ovat tyhjiä. - CatalogQueries: work_type_options, active_service_units ja get_work_type tehtäviä ja tuntikirjausta varten. - Import-linter-sopimukset, suomennokset, yksikkö-, integraatio- ja selaintestit. - Päivitetty CLAUDE.md ja määrittelyn kohta 16.3. Co-Authored-By: Claude Opus 5.5 <[email protected]>
231 lines
9.9 KiB
Python
231 lines
9.9 KiB
Python
import json
|
|
from collections.abc import Callable
|
|
from datetime import date
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from django.db import IntegrityError, transaction
|
|
from django.test import Client
|
|
|
|
from apps.catalog.infrastructure.orm import ServiceModel, ServiceUnitModel, WorkTypeModel
|
|
from apps.catalog.infrastructure.queries import DjangoCatalogQueries
|
|
from apps.users.infrastructure.orm import User
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
HX = {"HX-Request": "true"}
|
|
|
|
|
|
def unit(name: str) -> ServiceUnitModel:
|
|
return ServiceUnitModel.objects.get(name=name)
|
|
|
|
|
|
def trigger(response: Any) -> dict[str, Any]:
|
|
return dict(json.loads(response["HX-Trigger-After-Settle"])["itemSaved"])
|
|
|
|
|
|
class TestSeed:
|
|
def test_seed_matches_specification(self) -> None:
|
|
assert list(ServiceUnitModel.objects.values_list("name", flat=True)) == [
|
|
"Mediatuotanto",
|
|
"Markkinointi",
|
|
"Painotuotanto",
|
|
"Ylläpitopalvelut",
|
|
"Verkkosivut",
|
|
"Ohjelmistot",
|
|
"Kuluttajapalvelut",
|
|
"Kaikille yhteiset",
|
|
]
|
|
assert WorkTypeModel.objects.count() == 29
|
|
media = unit("Mediatuotanto")
|
|
assert [s.name for s in media.services.all()] == [
|
|
"Videotuotanto",
|
|
"Valokuvaus",
|
|
"Äänituotanto",
|
|
]
|
|
assert not media.work_types.filter(service__isnull=True).exists()
|
|
video = media.services.get(name="Videotuotanto")
|
|
assert [w.name for w in video.work_types.all()][:2] == ["Videokuvaus", "Kuvausassistentti"]
|
|
shared = ServiceUnitModel.objects.get(is_shared=True)
|
|
assert shared.name == "Kaikille yhteiset"
|
|
assert shared.work_types.count() == 5
|
|
|
|
def test_service_unit_names_are_unique_in_database(self) -> None:
|
|
with pytest.raises(IntegrityError), transaction.atomic():
|
|
ServiceUnitModel.objects.create(name="Markkinointi")
|
|
|
|
|
|
class TestPermissions:
|
|
@pytest.mark.parametrize("role", ["admin", "management"])
|
|
def test_admin_and_management(
|
|
self, client: Client, make_user: Callable[..., User], role: str
|
|
) -> None:
|
|
client.force_login(make_user(role=role))
|
|
response = client.get("/catalog/")
|
|
assert response.status_code == 200
|
|
assert "Pohjatiedot" in response.content.decode()
|
|
|
|
@pytest.mark.parametrize("role", ["employee", "intern", "subcontractor"])
|
|
def test_others_get_403(
|
|
self, client: Client, make_user: Callable[..., User], role: str
|
|
) -> None:
|
|
extra: dict[str, Any] = {}
|
|
if role == "intern":
|
|
extra = {"supervisor": make_user(), "employment_ends_on": date(2099, 1, 1)}
|
|
client.force_login(make_user(role=role, **extra))
|
|
assert client.get("/catalog/").status_code == 403
|
|
marketing = unit("Markkinointi")
|
|
assert client.post(f"/catalog/service-units/{marketing.pk}/active/").status_code == 403
|
|
assert "/catalog/" not in client.get("/").content.decode()
|
|
|
|
|
|
class TestForms:
|
|
def test_add_service_unit_with_htmx(self, admin_client: Client) -> None:
|
|
response = admin_client.post(
|
|
"/catalog/service-units/new/", {"name": "Tapahtumat"}, headers=HX
|
|
)
|
|
assert response.status_code == 200
|
|
new = unit("Tapahtumat")
|
|
assert new.sort_order == 9
|
|
assert response["HX-Retarget"] == "#catalog-tree"
|
|
assert trigger(response) == {
|
|
"message": "Palveluyksikkö tallennettu.",
|
|
"focus": f"#name-unit-{new.pk}",
|
|
}
|
|
assert "Tapahtumat" in response.content.decode()
|
|
|
|
def test_duplicate_name_returns_422(self, admin_client: Client) -> None:
|
|
response = admin_client.post("/catalog/service-units/new/", {"name": "markkinointi"}, **HX)
|
|
assert response.status_code == 422
|
|
body = response.content.decode()
|
|
assert 'aria-invalid="true"' in body
|
|
assert "Samanniminen palveluyksikkö on jo olemassa." in body
|
|
|
|
def test_add_service_and_work_type(self, admin_client: Client) -> None:
|
|
marketing = unit("Markkinointi")
|
|
admin_client.post(
|
|
f"/catalog/service-units/{marketing.pk}/services/new/", {"name": "Some"}, **HX
|
|
)
|
|
some = ServiceModel.objects.get(name="Some")
|
|
form = admin_client.get(
|
|
f"/catalog/service-units/{marketing.pk}/work-types/new/", headers=HX
|
|
)
|
|
assert "Suoraan palveluyksikön alle" in form.content.decode()
|
|
response = admin_client.post(
|
|
f"/catalog/service-units/{marketing.pk}/work-types/new/",
|
|
{"name": "Somemainonta", "service": str(some.pk)},
|
|
headers=HX,
|
|
)
|
|
assert response.status_code == 200
|
|
assert WorkTypeModel.objects.get(name="Somemainonta").service_id == some.pk
|
|
|
|
def test_move_work_type_to_another_service(self, admin_client: Client) -> None:
|
|
media = unit("Mediatuotanto")
|
|
animation = WorkTypeModel.objects.get(name="Animointi")
|
|
photo = media.services.get(name="Valokuvaus")
|
|
url = f"/catalog/work-types/{animation.pk}/edit/"
|
|
edit_form = admin_client.get(url).content.decode()
|
|
assert 'value="Animointi"' in edit_form
|
|
admin_client.post(url, {"name": "Animointi", "service": str(photo.pk)}, headers=HX)
|
|
animation.refresh_from_db()
|
|
assert animation.service_id == photo.pk
|
|
assert animation.sort_order == 3 # after Kuvaus and Editointi
|
|
admin_client.post(url, {"name": "Animointi", "service": ""}, headers=HX)
|
|
animation.refresh_from_db()
|
|
assert animation.service_id is None
|
|
|
|
def test_service_of_another_unit_is_rejected(self, admin_client: Client) -> None:
|
|
animation = WorkTypeModel.objects.get(name="Animointi")
|
|
other = ServiceModel.objects.create(service_unit=unit("Markkinointi"), name="Some")
|
|
response = admin_client.post(
|
|
f"/catalog/work-types/{animation.pk}/edit/",
|
|
{"name": "Animointi", "service": str(other.pk)},
|
|
headers=HX,
|
|
)
|
|
assert response.status_code == 422 # not among the choices
|
|
|
|
def test_child_create_needs_a_unit(self, admin_client: Client) -> None:
|
|
assert admin_client.get("/catalog/service-units/999/services/new/").status_code == 404
|
|
assert admin_client.get("/catalog/nope/1/edit/").status_code == 404
|
|
|
|
def test_without_htmx_redirects(self, admin_client: Client) -> None:
|
|
response = admin_client.post("/catalog/service-units/new/", {"name": "Tapahtumat"})
|
|
assert response.status_code == 302
|
|
assert response["Location"] == f"/catalog/#row-unit-{unit('Tapahtumat').pk}"
|
|
|
|
|
|
class TestMoveAndToggle:
|
|
def test_move_down_keeps_focus_on_button(self, admin_client: Client) -> None:
|
|
media = unit("Mediatuotanto")
|
|
response = admin_client.post(f"/catalog/service-units/{media.pk}/move/down/", headers=HX)
|
|
assert response.status_code == 200
|
|
assert list(ServiceUnitModel.objects.values_list("name", flat=True))[:2] == [
|
|
"Markkinointi",
|
|
"Mediatuotanto",
|
|
]
|
|
assert trigger(response) == {
|
|
"message": "Mediatuotanto siirretty alas.",
|
|
"focus": f"#move-unit-{media.pk}-down",
|
|
}
|
|
|
|
def test_focus_moves_to_other_button_at_edge(self, admin_client: Client) -> None:
|
|
marketing = unit("Markkinointi")
|
|
response = admin_client.post(f"/catalog/service-units/{marketing.pk}/move/up/", headers=HX)
|
|
assert trigger(response)["focus"] == f"#move-unit-{marketing.pk}-down"
|
|
assert ServiceUnitModel.objects.first() == marketing
|
|
|
|
def test_cannot_move_past_edge(self, admin_client: Client) -> None:
|
|
media = unit("Mediatuotanto")
|
|
response = admin_client.post(f"/catalog/service-units/{media.pk}/move/up/", headers=HX)
|
|
assert trigger(response)["message"] == "Riviä ei voi siirtää pidemmälle."
|
|
|
|
def test_toggle(self, admin_client: Client) -> None:
|
|
media = unit("Mediatuotanto")
|
|
url = f"/catalog/service-units/{media.pk}/active/"
|
|
response = admin_client.post(url, {"active": "0"}, headers=HX)
|
|
media.refresh_from_db()
|
|
assert not media.is_active
|
|
assert trigger(response) == {
|
|
"message": "Mediatuotanto poistettu käytöstä.",
|
|
"focus": f"#toggle-unit-{media.pk}",
|
|
}
|
|
body = response.content.decode()
|
|
assert "Ei käytössä" in body
|
|
assert "Ota käyttöön" in body
|
|
admin_client.post(url, {"active": "1"}, headers=HX)
|
|
media.refresh_from_db()
|
|
assert media.is_active
|
|
|
|
|
|
class TestQueries:
|
|
def test_inactive_parents_hide_their_work_types(self) -> None:
|
|
q = DjangoCatalogQueries()
|
|
media, marketing = unit("Mediatuotanto"), unit("Markkinointi")
|
|
names = {o.name for o in q.work_type_options([media.pk])}
|
|
assert {"Videokuvaus", "Suunnittelu"} <= names # own and shared
|
|
assert "Graafinen työ" not in names
|
|
|
|
ServiceModel.objects.filter(name="Videotuotanto").update(is_active=False)
|
|
names = {o.name for o in q.work_type_options([media.pk])}
|
|
assert "Videokuvaus" not in names
|
|
assert "Kuvaus" in names
|
|
videokuvaus = WorkTypeModel.objects.get(name="Videokuvaus")
|
|
assert videokuvaus.is_active # own flag untouched
|
|
option = q.get_work_type(videokuvaus.pk)
|
|
assert option is not None and not option.is_selectable
|
|
assert option.label == "Mediatuotanto / Videotuotanto / Videokuvaus"
|
|
|
|
ServiceUnitModel.objects.filter(pk=marketing.pk).update(is_active=False)
|
|
assert not any(
|
|
o.service_unit_id == marketing.pk for o in q.work_type_options([marketing.pk])
|
|
)
|
|
assert marketing.pk not in [u.id for u in q.active_service_units()]
|
|
|
|
def test_tree(self) -> None:
|
|
tree = DjangoCatalogQueries().tree()
|
|
assert len(tree) == 8
|
|
media = tree[0]
|
|
assert [s.name for s in media.services] == ["Videotuotanto", "Valokuvaus", "Äänituotanto"]
|
|
assert media.work_types == ()
|
|
assert tree[-1].is_shared
|