- 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]>
90 lines
3.1 KiB
Python
90 lines
3.1 KiB
Python
"""In-memory test doubles for the users application layer (no database)."""
|
|
|
|
from dataclasses import replace
|
|
from datetime import UTC, date, datetime
|
|
|
|
from apps.shared.domain.value_objects import EmailAddress
|
|
from apps.shared.tests.fakes import FakeUnitOfWork # noqa: F401 (re-exported for users tests)
|
|
from apps.users.application.dto import PasswordLink, PasswordLinkPurpose
|
|
from apps.users.domain.entities import UserAccount
|
|
from apps.users.domain.enums import Role
|
|
|
|
|
|
class InMemoryUserRepository:
|
|
def __init__(self) -> None:
|
|
self.users: dict[int, UserAccount] = {}
|
|
self.locked = False
|
|
self._next_id = 1
|
|
|
|
def get_by_id(self, user_id: int) -> UserAccount | None:
|
|
return self.users.get(user_id)
|
|
|
|
def get_by_email(self, email: EmailAddress) -> UserAccount | None:
|
|
return next((u for u in self.users.values() if u.email == email), None)
|
|
|
|
def save(self, user: UserAccount) -> UserAccount:
|
|
if user.id is None:
|
|
user = user.with_id(self._next_id)
|
|
self._next_id += 1
|
|
self.users[user.id] = user # type: ignore[index]
|
|
return user
|
|
|
|
def is_email_taken(self, email: EmailAddress, *, exclude_id: int | None = None) -> bool:
|
|
return any(u.email == email and u.id != exclude_id for u in self.users.values())
|
|
|
|
def lock_active_admin_count(self) -> int:
|
|
self.locked = True
|
|
return sum(1 for u in self.users.values() if u.is_active_admin)
|
|
|
|
def has_active_interns(self, supervisor_id: int) -> bool:
|
|
return any(
|
|
u.is_active and u.role is Role.INTERN and u.supervisor_id == supervisor_id
|
|
for u in self.users.values()
|
|
)
|
|
|
|
|
|
class FakePasswordLinkService:
|
|
def __init__(self, *, send_ok: bool = True) -> None:
|
|
self.send_ok = send_ok
|
|
self.issued: list[tuple[int, PasswordLinkPurpose]] = []
|
|
self.sent: list[tuple[str, PasswordLinkPurpose]] = []
|
|
|
|
def issue(self, user_id: int, purpose: PasswordLinkPurpose) -> PasswordLink:
|
|
self.issued.append((user_id, purpose))
|
|
return PasswordLink(
|
|
url=f"http://test/password/{purpose.value}/{user_id}/",
|
|
expires_at=datetime(2026, 1, 1, tzinfo=UTC),
|
|
)
|
|
|
|
def send(self, user: UserAccount, link: PasswordLink, purpose: PasswordLinkPurpose) -> bool:
|
|
if self.send_ok:
|
|
self.sent.append((user.email.value, purpose))
|
|
return self.send_ok
|
|
|
|
|
|
class FixedClock:
|
|
def __init__(self, today: date) -> None:
|
|
self._today = today
|
|
|
|
def today(self) -> date:
|
|
return self._today
|
|
|
|
def set(self, today: date) -> None:
|
|
self._today = today
|
|
|
|
|
|
def account(**overrides: object) -> UserAccount:
|
|
"""A stored user without rule checks, for arranging test state."""
|
|
from apps.users.domain.value_objects import PersonName
|
|
|
|
base = UserAccount.reconstitute(
|
|
id=None, # type: ignore[arg-type]
|
|
name=PersonName("Test User"),
|
|
email=EmailAddress("[email protected]"),
|
|
role=Role.EMPLOYEE,
|
|
is_active=True,
|
|
employment_ends_on=None,
|
|
supervisor_id=None,
|
|
)
|
|
return replace(base, **overrides) # type: ignore[arg-type]
|