Coverage for app/db/models.py: 100%
35 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-10-31 11:18 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-10-31 11:18 +0000
1"""ORM models for svc-auth.
3Only one table for now: `sessions`, a durable record of every login session so
4the account UI can show active and historic sessions with device/IP/geo detail.
6Portable column types (generic ``Uuid``/``DateTime``/``Enum``) are used so the
7model maps cleanly onto both Postgres (runtime) and SQLite (tests).
8"""
10import enum
11import uuid
13from sqlalchemy import DateTime, Enum, Float, String, Text, Uuid
14from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
16from app.util.time import aware_utcnow
19class Base(DeclarativeBase):
20 pass
23class SessionEndReason(str, enum.Enum):
24 """Why a session is no longer active."""
26 LOGOUT = "logout" # user signed out of this session
27 REVOKED = "revoked" # user revoked this specific session remotely
28 REVOKED_OTHERS = "revoked_others" # killed by "sign out all other sessions"
29 EXPIRED = "expired" # Redis TTL lapsed (idle) — detected by reconciliation
30 REFRESH_FAILED = "refresh_failed" # token refresh failed at /verify
33class SessionStatus(str, enum.Enum):
34 """Derived runtime status, not persisted — computed when listing."""
36 ACTIVE = "active"
37 EXPIRED = "expired"
38 ENDED = "ended"
41class SessionRecord(Base):
42 """A login session, mirrored from the Redis session at /callback.
44 ``sid`` is the opaque Redis session token and is server-only — it is never
45 serialised to the frontend. The UUID ``id`` is the safe public handle used
46 for revocation.
47 """
49 __tablename__ = "sessions"
51 id: Mapped[uuid.UUID] = mapped_column(Uuid(), primary_key=True, default=uuid.uuid4)
52 # Opaque Redis session token. Unique, indexed, NEVER exposed to clients.
53 sid: Mapped[str] = mapped_column(
54 String(255), unique=True, index=True, nullable=False
55 )
56 # Auth0 subject (e.g. "auth0|abc123").
57 user_id: Mapped[str] = mapped_column(String(255), index=True, nullable=False)
59 created_at: Mapped["DateTime"] = mapped_column(
60 DateTime(timezone=True), default=aware_utcnow, nullable=False
61 )
62 last_seen_at: Mapped["DateTime"] = mapped_column(
63 DateTime(timezone=True), default=aware_utcnow, nullable=False
64 )
65 ended_at: Mapped["DateTime | None"] = mapped_column(
66 DateTime(timezone=True), nullable=True
67 )
68 end_reason: Mapped["SessionEndReason | None"] = mapped_column(
69 Enum(
70 SessionEndReason,
71 name="session_end_reason_enum",
72 # Without this, SQLAlchemy binds the member *name* (e.g. "EXPIRED")
73 # instead of its lowercase `.value` — the migration's Postgres enum
74 # type only accepts the latter, so every end_reason write silently
75 # failed against real Postgres (errors here are swallowed by design;
76 # SQLite's CHECK-constraint emulation doesn't catch the mismatch,
77 # which is why it passed unit tests).
78 values_callable=lambda enum_cls: [e.value for e in enum_cls],
79 ),
80 nullable=True,
81 )
83 # Identifying detail captured at login.
84 ip: Mapped[str | None] = mapped_column(String(64), nullable=True)
85 user_agent: Mapped[str | None] = mapped_column(Text, nullable=True)
86 device_type: Mapped[str | None] = mapped_column(String(32), nullable=True)
87 browser: Mapped[str | None] = mapped_column(String(128), nullable=True)
88 os: Mapped[str | None] = mapped_column(String(128), nullable=True)
89 location_city: Mapped[str | None] = mapped_column(String(128), nullable=True)
90 location_country: Mapped[str | None] = mapped_column(String(2), nullable=True)
91 location_lat: Mapped[float | None] = mapped_column(Float, nullable=True)
92 location_lng: Mapped[float | None] = mapped_column(Float, nullable=True)