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,100 @@
|
||||
"""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.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 FakeUnitOfWork:
|
||||
def __init__(self) -> None:
|
||||
self.entered = 0
|
||||
|
||||
def __enter__(self) -> FakeUnitOfWork:
|
||||
self.entered += 1
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc: object) -> None:
|
||||
return None
|
||||
|
||||
|
||||
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]
|
||||
Reference in New Issue
Block a user