Guest-invite form: outcome shown in the card, double-submit guard server-side

The operator could never tell what a submit did: the only outcome message was
the top flash, off-screen from the mid-page form, and a portal refusal left
the fields filled with nothing visible where the operator was looking.

- invite card now carries its own persistent status line (same palette as
  .flash): success 'Invite sent to <email> — expires <date>.' with the fields
  cleared; any refusal/failure shown plainly with the fields KEPT.
- button held disabled ('Sending…') for the whole request, restored after.
- flash(): clear the previous timer so a message shown soon after another is
  not hidden by the first message's timeout.
- SERVER-SIDE duplicate guard (guest_duplicate_refusal, pure + unit-tested):
  /api/provision-guest refuses 409 before any portal call when an ACTIVE
  guest for the email exists ('A guest account for <email> already exists')
  or a PENDING request awaits the decision ('awaits activation'). Holds for
  any client; the portal's own one-guest-per-recipient guard stays behind it.
- admin-panel-invite-harness.js pins the whole behavior.
This commit is contained in:
inference-bot committed 2026-11-08 03:12:30 +00:00
1 parent c16d044113
commit aabee20534
3 files changed
+269 -6

No files matched your search

+72
View File
@@ -0,0 +1,72 @@
# 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.")