From 58214be0dbc5aab5830d3fb2609a4290a571085a Mon Sep 17 00:00:00 2001 From: Inferencebot Date: Wed, 11 Nov 2026 05:16:13 +0000 Subject: [PATCH] Guest state: the overview payload carries the definitive display state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Single derivation point (tested once, here): 'invited' while the Cloudron invite is unaccepted, else the guests-ledger status (the record of record), membership state only as a no-ledger-row fallback. The panel and dashboard become pure consumers of `state` — their local invited/active derivation goes away in their own repos next. Live-behaviour note: today's four live guests render byte-identically; the change only shows for ended guests (ledger 'expired' instead of the bare inactive flag), which is what the panel already displayed via its own guest_status preference. --- app/main.py | 15 +++++++++++++- tests/test_guest_invited_state.py | 34 +++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index afb7d59..0e3138e 100644 --- a/app/main.py +++ b/app/main.py @@ -4787,7 +4787,9 @@ async def admin_overview(request: Request): # until acceptance flips it. Display/state semantics only: access # rules, the $2 cap and the expiry box are unchanged (an unaccepted # guest cannot reach anything anyway — the setup link is the only - # door in). + # door in). This override is half of the SINGLE guest-state + # derivation (see the guest_status block below for the other half); + # the panel and dashboard consume `state` and derive nothing. sent = (guest_info.get(email) or {}).get("sent") mstate = "invited" mstate_reason = (f"awaiting setup — invite sent {str(sent)[:10]}, " @@ -4821,6 +4823,17 @@ async def admin_overview(request: Request): gi = guest_info.get(email) or {} entry["guest_inviter"] = gi.get("inviter") entry["guest_status"] = gi.get("status") + # The other half of the SINGLE guest-state derivation (portal = + # source of truth; tested once in tests/test_guest_invited_state.py, + # consumed as-is by the panel and dashboard). Once the invite is + # accepted the guests-ledger status IS the display state — the + # record of record (active / converted / expired / depleted / + # cancelled by recipient) — so an ended guest reads its ledger + # status, not the bare active/inactive membership flag. A guest + # with no ledger row keeps the membership state as the fallback. + if entry["state"] != "invited" and gi.get("status"): + entry["state"] = gi["status"] + entry["state_reason"] = "" if email.lower() in OPERATOR_EMAILS: # Operator accounts: surfaced under a separate "admin" category in # the panel, excluded from member counts and aggregates (their diff --git a/tests/test_guest_invited_state.py b/tests/test_guest_invited_state.py index 0be8988..d46c533 100644 --- a/tests/test_guest_invited_state.py +++ b/tests/test_guest_invited_state.py @@ -17,8 +17,11 @@ WHY THIS EXISTS 1. guest_invite_pending — the derivation itself: accepted, username-set, unseen-record (fetch failed) and no-user-id cases. 2. GET /admin/overview: a fresh guest (inviteAccepted=false, no - username) reports state='invited' with an 'awaiting setup' reason; - the guests ledger status stays the record of record. + username) reports state='invited' with an 'awaiting setup' reason. + Once accepted, state FOLLOWS THE LEDGER STATUS (active / expired / + ...) — the single derivation point for the guest display state, + consumed as-is by the panel and dashboard; a guest with no ledger + row falls back to the membership state. 3. Simulated acceptance (inviteAccepted flips true, or a username is chosen) → the same endpoint reports state='active'. 4. A FAILED Cloudron users fetch never claims 'invited' — the row falls @@ -205,8 +208,31 @@ def main(): check("overview: activated flag agrees (pending)", g.get("activated") is False, str(g.get("activated"))) ended = [x for x in data["guests"] if x["email"] == "ended@x.org"][0] - check("overview: an inactive guest still reads inactive (not invited)", - ended["state"] == "inactive", str(ended.get("state"))) + check("overview: ended guest reads its LEDGER status ('expired'), not the bare flag", + ended["state"] == "expired" and ended["state_reason"] == "", + f"{ended.get('state')} / {ended.get('state_reason')!r}") + + # The ledger is the record of record even when it disagrees with the + # members.active flag: an active-flagged guest whose access has ENDED + # (expired ledger row, active flag not yet flipped) reads 'expired'. + conn = m.get_db() + CLOUDRON_USERS["uid-lapsed@x.org"] = {"inviteAccepted": True, "username": "lapsedg"} + seed_account(conn, "lapsed@x.org", account_type="guest", active=1, + uid="uid-lapsed@x.org") + seed_guest_ledger(conn, inviter, "lapsed@x.org", status="expired") + # ...and a guest with NO ledger row falls back to the membership state. + CLOUDRON_USERS["uid-noledger@x.org"] = {"inviteAccepted": True, "username": "noledger"} + seed_account(conn, "noledger@x.org", account_type="guest", active=1, + uid="uid-noledger@x.org") + conn.commit() + conn.close() + data = asyncio.run(overview()) + lapsed = [x for x in data["guests"] if x["email"] == "lapsed@x.org"][0] + check("overview: ledger status wins over the active flag (lapsed guest = 'expired')", + lapsed["state"] == "expired", str(lapsed.get("state"))) + noledger = [x for x in data["guests"] if x["email"] == "noledger@x.org"][0] + check("overview: no ledger row → membership-state fallback ('active')", + noledger["state"] == "active", str(noledger.get("state"))) # --- 3. simulated acceptance: the flip to active --------------------------- CLOUDRON_USERS["uid-guest@x.org"]["inviteAccepted"] = True