Files
PacketStation/tests/server/test_events.py
T
spencermandClaude Fable 5.1 97b0973130 Phase 1: identity, sessions, audit, frontend shell
AD sign-in over LDAPS (search with the service account, bind as the user,
nested groups through LDAP_MATCHING_RULE_IN_CHAIN, disabled accounts refused),
roles as the union of PS-* groups mapped by a table derived from the seeded
departments. Server-side sessions with idle, lock and absolute timeouts, Origin
and X-Requested-With check, step-up unlock. Optional Argon2id PIN set with the
domain password; PIN sign-in by employee ID on registered devices only; five
wrong attempts disable the PIN, three wrong at unlock or signature end the
session. Rate limits per account and per address before any bind. Devices,
signatures table and sign helper, audit rows for every identity event, SSE over
LISTEN/NOTIFY with presence. Account settings and Admin APIs and screens
(AD view, devices, PIN clearing, group map with missing-group flags, refresh
from AD now). CLI ad-groups --list and ad-refresh. Migration 0002.

Frontend: real sign-in (password tab, PIN keypad on registered devices), lock
screen, role switcher, Account settings, Admin shell, screen placeholders.

CI: Samba AD DC container (PSTEST.LOCAL) with a generated CA, 49 of the 50
groups plus one extra, 17 test users; wired into the CI compose overlay and
the Gitea workflow; Playwright sign-in scenario.

Verified on docker-1: 171 server tests green inside the CI stack against
PostgreSQL and the Samba DC, 48 frontend tests, 8 Playwright scenarios against
the live stack (password and PIN sign-in, lock and unlock, Admin group map).
Findings recorded in the docs: the DC needs CAP_SYS_ADMIN, a hidden tab locks
only after a minute as the old build did,
and the app uses the alias ps-app for Playwright because "app" is on
Chromium's HSTS preload list.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-10-16 07:40:29 -06:00

109 lines
4.0 KiB
Python

from __future__ import annotations
import asyncio
import json
import uuid
from pshelpers import needs_pg
from packetstation import db, events
from packetstation.settings import Settings
def _sub(hub: events.EventHub, name: str, screen: str | None = None) -> events.Subscriber:
return hub.subscribe(session_id=uuid.uuid4(), user_id=uuid.uuid4(), display_name=name, screen=screen)
async def _drain(q: asyncio.Queue[events.Item]) -> list[events.Item]:
await asyncio.sleep(0) # let call_soon_threadsafe callbacks run
out = []
while not q.empty():
out.append(q.get_nowait())
return out
def test_fan_out_presence_and_session_targeting() -> None:
async def run() -> None:
hub = events.EventHub()
a = _sub(hub, "Ann", "admin")
b = _sub(hub, "Bo", "stall")
got_a = await _drain(a.queue)
assert [n for n, _ in got_a] == ["presence", "presence"]
assert got_a[-1][1] == {
"users": [
{"user_id": a.user_id, "display_name": "Ann", "screen": "admin"},
{"user_id": b.user_id, "display_name": "Bo", "screen": "stall"},
]
}
await _drain(b.queue)
hub._deliver(json.dumps({"event": "changed", "collection": "packets", "id": 7}))
hub._deliver(json.dumps({"collection": "projects"})) # no event name means changed
hub._deliver("not json")
for q in (a.queue, b.queue):
assert await _drain(q) == [
("changed", {"collection": "packets", "id": 7}),
("changed", {"collection": "projects"}),
]
hub._deliver(json.dumps({"event": "session", "session_id": b.session_id, "action": "revoked"}))
assert await _drain(a.queue) == []
assert await _drain(b.queue) == [("session", {"session_id": b.session_id, "action": "revoked"})]
hub.unsubscribe(b)
got = await _drain(a.queue)
assert got == [("presence", {"users": [{"user_id": a.user_id, "display_name": "Ann", "screen": "admin"}]})]
asyncio.run(run())
def test_sse_stream_hello_ping_and_end_on_own_revoke() -> None:
async def run() -> list[str]:
hub = events.EventHub()
sub = _sub(hub, "Ann")
await _drain(sub.queue)
async def connected() -> bool:
return False
gen = events.stream(
hub, sub, hello={"session_id": sub.session_id, "build": "t"}, is_disconnected=connected, ping_seconds=0.05
)
chunks = [await gen.__anext__(), await gen.__anext__()]
hub._deliver(json.dumps({"event": "changed", "collection": "packets"}))
chunks.append(await gen.__anext__())
hub._deliver(json.dumps({"event": "session", "session_id": sub.session_id, "action": "revoked"}))
chunks.append(await gen.__anext__())
rest = [c async for c in gen]
assert rest == []
assert hub.presence() == {"users": []}
return chunks
chunks = asyncio.run(run())
assert chunks[0].startswith("event: hello\ndata: {") and '"build":"t"' in chunks[0]
assert chunks[1] == ": ping\n\n"
assert chunks[2] == 'event: changed\ndata: {"collection":"packets"}\n\n'
assert chunks[3].startswith("event: session\n")
@needs_pg
def test_pg_notify_round_trip(pg_settings: Settings) -> None:
async def run() -> events.Item:
hub = events.EventHub(connect=lambda: db.connect_as("app", pg_settings, autocommit=True))
sub = _sub(hub, "Ann")
await _drain(sub.queue)
hub.start()
try:
for _ in range(100):
if hub.connected:
break
await asyncio.sleep(0.05)
assert hub.connected
with db.connect_as("app", pg_settings, autocommit=True) as conn:
events.publish(conn, {"event": "changed", "collection": "packets", "id": "7A"})
return await asyncio.wait_for(sub.queue.get(), timeout=5)
finally:
await asyncio.to_thread(hub.stop)
assert asyncio.run(run()) == ("changed", {"collection": "packets", "id": "7A"})