# The admin panel's SERVER-SIDE duplicate-guest guard (app/main.py # guest_duplicate_refusal). The function is pure (no FastAPI/state), so the # test extracts it from the source and runs it the same way check-page-js.sh # treats the page JS — slicing, not importing (importing app.main pulls in # app.oidc → authlib, which the steward host does not have). # # Run: python3 tests/test_guest_guard.py import re import sys from pathlib import Path SRC = Path(__file__).resolve().parent.parent / "app" / "main.py" start = SRC.read_text().index("def guest_duplicate_refusal(") end = SRC.read_text().index("\n@app.post(", start) ns: dict = {} exec(SRC.read_text()[start:end], ns) guard = ns["guest_duplicate_refusal"] failures = [] def check(name, got, want): if got != want: failures.append(f"{name}: got {got!r}, want {want!r}") else: print(f"ok - {name}") LEDGER_ACTIVE = [{"id": 3, "inviter": "info@inference.coop", "recipient": "friend@example.com", "status": "active"}] LEDGER_LAPSED = [{"id": 3, "inviter": "info@inference.coop", "recipient": "friend@example.com", "status": "expired"}] PENDING_REQ = [{"id": 7, "inviter": "alice@example.com", "recipient": "someone@example.com", "status": "pending"}] EMPTY = {"ledger": [], "pending": []} # An ACTIVE guest for the email → refuse, message names the email. refusal = guard({"ledger": LEDGER_ACTIVE, "pending": []}, "friend@example.com") check("active guest refused", refusal is not None and refusal.startswith( "A guest account for friend@example.com already exists"), True) check("active refusal states no second account", "no second account" in (refusal or ""), True) # Case/whitespace of the submitted email must not dodge the match. check("case-insensitive match", guard({"ledger": LEDGER_ACTIVE, "pending": []}, " Friend@Example.COM ") is not None, True) # A PENDING member request for the email → refuse as "awaits activation". refusal = guard({"ledger": [], "pending": PENDING_REQ}, "someone@example.com") check("pending request refused", refusal is not None and "awaits activation" in refusal and "someone@example.com" in refusal, True) # Lapsed (expired/depleted) ledger row is NOT a live account: no panel-side # refusal — the portal's one-guest-per-recipient-ever guard is the authority. check("expired ledger row falls through", guard({"ledger": LEDGER_LAPSED, "pending": []}, "friend@example.com"), None) # Someone else's guest/request never blocks a new address. check("other recipients ignored", guard({"ledger": LEDGER_ACTIVE, "pending": PENDING_REQ}, "new@example.com"), None) # Empty/missing queues (portal unreadable → guard falls through, never blocks). check("empty queues pass", guard(EMPTY, "friend@example.com"), None) check("missing keys pass", guard({}, "friend@example.com"), None) check("blank email passes (route validates first)", guard(EMPTY, ""), None) if failures: print("\nFAILURES:") for f in failures: print(" " + f) sys.exit(1) print("\nAll guest_duplicate_refusal checks passed.")