Files
admin-panel/tests/test_guest_guard.py
T
inference-bot f9c2ae6417 Remove the pending guest-requests section — nothing to approve anymore
The portal provisions member invites immediately (approval gate removed
2026-11-08), so the panel drops the Guest requests tab (queue, decisions,
badge and their JS) and the approve/reject API hops. Kept: the Guests list
(member-invited guests show inviter, credits, expiry, status, same as
admin-created ones) and the duplicate-send guard, now fed by the portal's
GET /admin/guest-ledger (ledger only — no pending key). Test updated: the
guard no longer has a pending branch; a legacy pending-shaped payload is
inert.
2026-11-08 20:20:33 +00:00

79 lines
3.3 KiB
Python

# 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).
#
# The guard reads the portal's guests ledger only: the member guest-request
# queue is GONE with the approval gate (removed 2026-11-08 — member
# invitations provision immediately, nothing is ever pending), so there is
# no "pending" branch to test and no payload carrying one.
#
# 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"}]
EMPTY = {"ledger": []}
# An ACTIVE guest for the email → refuse, message names the email.
refusal = guard({"ledger": LEDGER_ACTIVE}, "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},
" Friend@Example.COM ") is not None, True)
# The guard no longer knows a "pending" state: a payload shaped like the old
# guest-requests response carries nothing the guard would act on.
check("legacy pending-shaped payload is inert",
guard({"ledger": [], "pending": [{"id": 7, "inviter": "alice@example.com",
"recipient": "someone@example.com",
"status": "pending"}]},
"someone@example.com"), None)
# 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}, "friend@example.com"), None)
# Someone else's guest never blocks a new address.
check("other recipients ignored",
guard({"ledger": LEDGER_ACTIVE}, "new@example.com"), None)
# Empty/missing ledger (portal unreadable → guard falls through, never blocks).
check("empty ledger passes", 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.")