loomio: enable by default; idempotent add (never re-invite) + targeted remove

The guard direction from 2026-11-04 is corrected: the co-op DOES use Loomio and
wants every new member added. Removing members/notifications is not the goal —
not duplicating them is. The feature is switched back ON and the sync is made
idempotent instead.

Enablement
  * LOOMIO_SYNC_ENABLED now defaults to ENABLED (kill switch: set =0 to pause).
    Restoring LOOMIO_API_KEY and deploying is all that is needed again.

Non-duplicating ADD (the real fix), two layers:
  * (a) live pre-check against Loomio group membership (b3 index, ordered and
    paginated stably). An address already in the group is skipped SILENTLY —
    it is never placed in an invite payload, so no API call about it can fire
    a notification or email.
  * (b) members.loomio_invited_at: an address we have handed over before is
    never sent again, even if it is not (yet) visible in the live roster.
  * FAIL CLOSED if the live roster cannot be read.
  * remove_absent is never sent.

Removal reconciliation preserved (deactivation must still propagate):
  * A member in the group who is no longer active is removed via a TARGETED
    b3 DELETE by membership id — it names only the member being removed, so it
    cannot re-invite or notify anyone staying. Operator allowlist
    (LOOMIO_ALWAYS_KEEP) is never removed. 404 (already gone) counts as
    success, so a re-run neither errors nor churns.
  * The keep-set is computed from the UNFILTERED active list: an active member
    whose Cloudron account is not yet activated is not removed (only their
    invite is deferred).

Tests: tests/test_loomio_guard.py (26/26) — add/dedupe, silent skip, record
guard, fail-closed, remove-once, allowlist-never-removed, targeted removal
(no present member in any payload), double-run no-op for both phases, 404
tolerance, kill switch.
This commit is contained in:
inference-bot committed 2026-11-04 19:25:45 +00:00
1 parent 17329c5d6f
commit f7bbec8a0a
2 files changed
+270 -133

No files matched your search

+130 -66
View File
@@ -309,12 +309,14 @@ LOOMIO_API_KEY = os.environ.get("LOOMIO_API_KEY", "")
# mangled auto-handle (email local-part + random suffix). The B3 users API # mangled auto-handle (email local-part + random suffix). The B3 users API
# lets us rename them to the member's Cloudron username. # lets us rename them to the member's Cloudron username.
LOOMIO_B3_KEY = os.environ.get("LOOMIO_B3_KEY", "") LOOMIO_B3_KEY = os.environ.get("LOOMIO_B3_KEY", "")
# Master switch for the Loomio membership sync. OFF unless explicitly enabled. # Master switch for the Loomio membership sync. It defaults to ENABLED: the
# The sync's only effect is Loomio's invite notification, and a bad run emails # co-op wants every new member added to Loomio, so restoring LOOMIO_API_KEY
# the whole group (incident 2026-11-04: notification 744 delivered to ~83 # and deploying is all that is needed. It is expressed as a KILL SWITCH —
# members). Restoring LOOMIO_API_KEY alone must NOT re-arm it — this flag has # set LOOMIO_SYNC_ENABLED=0 (or false/off/no) to pause the sync; any other
# to be set deliberately by the operator, once Loomio's purpose is decided. # value, or leaving it unset, keeps it ON. (Incident 2026-11-04 was fixed the
LOOMIO_SYNC_ENABLED = os.environ.get("LOOMIO_SYNC_ENABLED", "").strip().lower() in ("1", "true", "on", "yes") # right way by making the ADD path idempotent, NOT by switching the feature
# off — see sync_loomio_memberships for the guards.)
LOOMIO_SYNC_ENABLED = os.environ.get("LOOMIO_SYNC_ENABLED", "1").strip().lower() not in ("0", "false", "off", "no")
# Matrix homeserver (self-hosted Synapse at matrix.inference.coop). The bot # Matrix homeserver (self-hosted Synapse at matrix.inference.coop). The bot
# account (admin) invites every active member to the co-op Space on sync. # account (admin) invites every active member to the co-op Space on sync.
@@ -1582,76 +1584,124 @@ async def matrix_member_mxids(client: httpx.AsyncClient):
yield mxid, mxid yield mxid, mxid
async def fetch_loomio_member_emails(client: httpx.AsyncClient) -> set[str]: async def fetch_loomio_members(client: httpx.AsyncClient) -> dict[str, int]:
"""Return the emails Loomio already has in the group (lowercased). """Return {email_lower: membership_id} for everyone in the Loomio group.
This is the 'has this member EVER been in that system' half of the positive Uses the Server API (b3) so we get membership ids in one pass (the id is
exclusion. It must succeed before any invite is sent — see the fail-closed what a targeted removal needs) AND a STABLE read: the b3 index is ordered by
handling in sync_loomio_memberships. id and paginates cleanly, whereas the User API b2 index has no ORDER BY, so
paging it can repeat or skip rows — a skipped row would make the pre-check
wrongly think an existing member is new. Reads the group's ACTIVE
memberships (pending invitees included, already-removed members excluded).
This is the live-membership half of the positive exclusion, and the ONLY
layer that reflects reality rather than our own records. It must succeed
before any invite or removal (fail-closed in sync_loomio_memberships).
""" """
members: dict[str, int] = {}
offset = 0
while True:
r = await client.get( r = await client.get(
f"{LOOMIO_BASE}/api/b2/memberships", f"{LOOMIO_BASE}/api/b3/memberships",
headers={"Authorization": f"Bearer {LOOMIO_API_KEY}"}, headers={"Authorization": f"Bearer {LOOMIO_B3_KEY}"},
params={"group_id": int(LOOMIO_GROUP_ID)}, params={"group_id": int(LOOMIO_GROUP_ID), "limit": 50, "offset": offset},
) )
r.raise_for_status() r.raise_for_status()
data = r.json() page = r.json().get("memberships", [])
return { for m in page:
(m.get("user_email") or "").lower() email = (m.get("user_email") or "").lower()
for m in data.get("memberships", []) if email:
if m.get("user_email") members[email] = m.get("id")
} if len(page) < 50:
break
offset += 50
return members
async def remove_loomio_membership(client: httpx.AsyncClient, membership_id: int) -> bool:
"""Revoke ONE Loomio membership by id, via the Server API (b3).
Targeted on purpose: the request names only the membership being removed, so
it carries no address of anyone who is staying and therefore cannot trigger
an invite or a group notification — unlike b2's `remove_absent`, which
requires re-sending the whole keep-list.
Idempotent: Loomio answers 404 for an id that is already gone (or never
existed); the end state we want is "not a member", so 404 counts as success
and a repeated run does not error or churn.
"""
r = await client.delete(
f"{LOOMIO_BASE}/api/b3/memberships/{membership_id}",
headers={"Authorization": f"Bearer {LOOMIO_B3_KEY}"},
)
if r.status_code in (200, 204, 404):
return True
logger.warning(
"Loomio remove of membership %s failed: %s %s",
membership_id, r.status_code, r.text[:200],
)
return False
async def sync_loomio_memberships() -> None: async def sync_loomio_memberships() -> None:
"""Add members to the Loomio group — NEVER re-invite an existing member. """Reconcile the Loomio group with the current ACTIVE-member list.
SAFETY (incident 2026-11-04): a live run of this function emailed a Loomio TWO jobs with DIFFERENT guards — a deactivation must still propagate:
invite notification to essentially the WHOLE membership. The guards below
make a repeat impossible:
1. LOOMIO_SYNC_ENABLED must be ON. The integration is OFF by default, so 1. ADD (invite) — a member is invited only if they are BOTH not already in
restoring LOOMIO_API_KEY alone does NOT re-arm it; enabling it is a the group (live pre-check) AND never handed to Loomio before
deliberate operator action. (members.loomio_invited_at). Exactly once, ever. This is the dedupe
2. POSITIVE EXCLUSION — an address is only ever sent if it is neither guard, and it applies to INVITATIONS ONLY.
currently in the Loomio group NOR recorded as previously handed to 2. REMOVE — a member no longer active is revoked so a lapse propagates to
Loomio (members.loomio_invited_at). A member who is already in Loomio, Loomio. The removal path is targeted (b3 DELETE by membership id) and
or was ever invited, can never be invited again. sends nothing for anyone who is staying, so it cannot re-invite or
3. FAIL CLOSED — if the current Loomio member list cannot be read, the sync notify a present member. Operator accounts (LOOMIO_ALWAYS_KEEP) are
skips entirely rather than sending a list it cannot verify. never removed.
Deliberately does NOT pass `remove_absent`: this sync only ADDS. Removing a Guards (incident 2026-11-04: a live run emailed Loomio invites to the whole
lapsed member is a separate, explicit decision. Sending only the membership):
never-invited subset (instead of the full list with remove_absent=1) also * The invite payload contains ONLY addresses Loomio has never seen and we
removes the reconcile path that could evict a member on one run and then have never handed over — an existing member is skipped SILENTLY, no API
re-add — and therefore re-invite — them on the next. call about them at all.
* FAIL CLOSED — if the live group roster cannot be read, NEITHER phase
runs. We never hand Loomio a list we could not check.
Loomio's b2/memberships endpoint has no "silent invite" flag, so the only Loomio's b2 invite has no "silent" flag, so the only way to guarantee no
way to guarantee no invite is to send an address exactly once. invite is to send an address exactly once, ever — hence the record.
""" """
if not LOOMIO_SYNC_ENABLED: if not LOOMIO_SYNC_ENABLED:
logger.info("Loomio sync disabled (LOOMIO_SYNC_ENABLED is off); skipping") logger.info("Loomio sync disabled (LOOMIO_SYNC_ENABLED=0); skipping")
return return
if not LOOMIO_API_KEY or not LOOMIO_GROUP_ID: if not LOOMIO_GROUP_ID:
logger.warning("Loomio not configured; skipping membership sync") logger.warning("Loomio not configured; skipping membership sync")
return return
emails = await get_active_member_emails() # The set that decides who STAYS in the group: every active member plus the
# Always keep operator accounts in the group. # operator allowlist. Computed from the UNFILTERED active list on purpose —
for keep in LOOMIO_ALWAYS_KEEP: # an active member whose Cloudron account is not yet activated is still a
if keep not in emails: # member and must NOT be removed from Loomio (only their invite is deferred).
emails.append(keep) keep_emails = {e.lower() for e in await get_active_member_emails()}
keep_emails |= {e.lower() for e in LOOMIO_ALWAYS_KEEP}
# Only members whose Cloudron account is already set up (inviteAccepted) — # Invite candidates are additionally filtered to activated accounts so a
# never email a Loomio invite to someone who hasn't created their account # Loomio invite email never precedes the member's own account setup.
# yet. Operator accounts bypass this filter (the bootstrap owner shows invite_emails = await get_active_member_emails()
# inviteAccepted: false forever despite being functional). for keep in LOOMIO_ALWAYS_KEEP:
emails = await filter_loomio_activated(emails) if keep not in invite_emails:
invite_emails.append(keep)
invite_emails = await filter_loomio_activated(invite_emails)
if not LOOMIO_B3_KEY:
# The live roster (and the removal path) needs the Server API key. Without
# it we cannot verify reality, so we do nothing rather than risk a
# blind invite.
logger.warning("Loomio sync: LOOMIO_B3_KEY unset; cannot read group roster (fail-closed)")
return
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
# FAIL CLOSED: never send a list we could not check against Loomio. # FAIL CLOSED: never act on a roster we could not read.
try: try:
current = await fetch_loomio_member_emails(client) current = await fetch_loomio_members(client)
except Exception as e: except Exception as e:
logger.warning( logger.warning(
"Loomio sync: could not read current group members; skipping " "Loomio sync: could not read current group members; skipping "
@@ -1659,19 +1709,33 @@ async def sync_loomio_memberships() -> None:
) )
return return
# POSITIVE EXCLUSION: only addresses Loomio has never seen, and that we # --- REMOVE: reconcile the group to the current active set. ---
# have never handed over. removed = 0
for email, mid in current.items():
if email in keep_emails or mid is None:
continue
if await remove_loomio_membership(client, mid):
removed += 1
logger.info("Loomio sync: removed lapsed membership %s", mid)
if removed:
logger.info("Loomio sync: %d lapsed membership(s) removed", removed)
# --- ADD: only addresses Loomio has never seen AND we never handed over. ---
fresh = [ fresh = [
e for e in emails e for e in invite_emails
if e.lower() not in current and not get_loomio_invited_at(e) if e.lower() not in current and not get_loomio_invited_at(e)
] ]
if not fresh: if not fresh:
logger.info( logger.info(
"Loomio sync: nobody new (all already in Loomio or already " "Loomio sync: nobody new (all already in Loomio or already "
"invited); nothing to send" "invited); nothing to invite"
) )
return elif not LOOMIO_API_KEY:
logger.warning(
"Loomio sync: %d member(s) to invite but LOOMIO_API_KEY unset; "
"skipping invites", len(fresh),
)
else:
r = await client.post( r = await client.post(
f"{LOOMIO_BASE}/api/b2/memberships", f"{LOOMIO_BASE}/api/b2/memberships",
headers={"Authorization": f"Bearer {LOOMIO_API_KEY}"}, headers={"Authorization": f"Bearer {LOOMIO_API_KEY}"},
@@ -1679,12 +1743,12 @@ async def sync_loomio_memberships() -> None:
) )
if r.status_code != 200: if r.status_code != 200:
logger.warning("Loomio membership sync failed: %s %s", r.status_code, r.text[:200]) logger.warning("Loomio membership sync failed: %s %s", r.status_code, r.text[:200])
return else:
result = r.json() added = r.json().get("added_emails", []) or []
added = result.get("added_emails", []) or [] # Record EVERY address we sent, so it is never sent again — even
# Record EVERY address we sent, so it is never sent again — even if # if Loomio didn't add it, or the membership is later revoked. A
# Loomio didn't add it, or the membership is later revoked. A revoked # revoked member is NOT a never-invited member, and must not be
# member is NOT a never-invited member, and must not be re-invited. # re-invited.
for e in fresh: for e in fresh:
mark_loomio_invited(e) mark_loomio_invited(e)
logger.info("Loomio sync: first-time invites sent (%d): %s", len(added), added) logger.info("Loomio sync: first-time invites sent (%d): %s", len(added), added)
+140 -67
View File
@@ -1,20 +1,30 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""Regression test: the Loomio sync can never invite an existing member twice. """Regression test: the Loomio sync is idempotent and non-duplicating.
WHY THIS EXISTS WHY THIS EXISTS
On 2026-11-04 a live run of sync_loomio_memberships() emailed a Loomio invite On 2026-11-04 a live run of sync_loomio_memberships() emailed a Loomio invite
notification to (effectively) the whole membership. The guards that must hold notification to (effectively) the whole membership. The guards that must hold
are proven here on a FIXTURE db with the network stubbed: are proven here on a FIXTURE db with the network STUBBED:
1. OFF by default — no Loomio call happens unless LOOMIO_SYNC_ENABLED is on, ADD / dedupe
so restoring LOOMIO_API_KEY alone cannot re-arm the integration. 1. A member already in the Loomio group is skipped SILENTLY — no invite
2. POSITIVE EXCLUSION — an address is only ever sent if it is neither in payload ever contains their address.
Loomio's current member list NOR recorded as previously invited 2. members.loomio_invited_at is a second guard: an address we have handed
(members.loomio_invited_at). This matters because the invite-record over before is never sent again, even if it is not (yet) visible in the
column starts NULL for the ~89 members ALREADY in Loomio. live roster.
3. FAIL CLOSED — if Loomio's current member list can't be read, no invite is 3. `remove_absent` is never sent (no evict-then-re-add re-invite path).
sent. 4. FAIL CLOSED — if the live roster cannot be read, nothing is sent.
4. `remove_absent` is never sent (no evict-then-re-add re-invite path). 5. Running the sync twice adds nobody a second time.
REMOVE / reconciliation (a deactivation MUST still propagate to Loomio)
6. A member in the group who is no longer active is removed exactly once.
7. The operator allowlist is never removed, even when not active.
8. Removal is targeted — it never re-sends a present member's address, so it
cannot re-invite or notify anyone who is staying.
9. A second run removes nobody again (the roster no longer lists them).
10. remove_loomio_membership treats 404 as success (idempotent, no error).
The sync is ON by default (kill switch LOOMIO_SYNC_ENABLED=0 pauses it).
Runnable: python3 tests/test_loomio_guard.py (from the repo root) Runnable: python3 tests/test_loomio_guard.py (from the repo root)
""" """
@@ -24,36 +34,37 @@ import tempfile
import asyncio import asyncio
os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "fixture.db") os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "fixture.db")
os.environ["LOOMIO_API_KEY"] = "test-key" os.environ["LOOMIO_API_KEY"] = "test-user-key"
os.environ["LOOMIO_B3_KEY"] = "test-server-key"
os.environ["LOOMIO_GROUP_ID"] = "1" os.environ["LOOMIO_GROUP_ID"] = "1"
os.environ["LOOMIO_SYNC_ENABLED"] = "off" # Leave LOOMIO_SYNC_ENABLED unset -> the sync must default to ENABLED.
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import app.main as m # noqa: E402 import app.main as m # noqa: E402
m.DB_PATH = os.environ["DB_PATH"] m.DB_PATH = os.environ["DB_PATH"]
m.LOOMIO_ALWAYS_KEEP = [] # keep the assertion about membership emails isolated # Keep the membership assertions isolated: no extra keep accounts, and no
# username-fix pass (it makes its own /api/b3/users calls).
m.LOOMIO_ALWAYS_KEEP = ["keep@x.org"]
sent = [] # every b2/memberships POST payload posts = [] # every b2/memberships POST payload
GET_FAILS = False # toggled to exercise fail-closed deletes = [] # every b3 membership DELETE url
ROSTER = {} # email -> membership id currently in the group
GET_FAILS = False # exercise fail-closed
DELETE_STATUS = 200 # toggle to prove 404 tolerance
class FakePostResp: class FakeResp:
status_code = 200 def __init__(self, status=200, payload=None):
self.status_code = status
def json(self): self._payload = payload or {}
return {"added_emails": ["new@x.org"]} self.text = str(payload or {})
class FakeGetResp:
def __init__(self):
self.status_code = 200
def raise_for_status(self): def raise_for_status(self):
pass if self.status_code >= 400:
raise RuntimeError(f"HTTP {self.status_code}")
def json(self): def json(self):
# member@x.org is ALREADY in the Loomio group. return self._payload
return {"memberships": [{"user_email": "Member@x.org"}]}
class FakeClient: class FakeClient:
@@ -68,19 +79,27 @@ class FakeClient:
async def get(self, url, headers=None, params=None): async def get(self, url, headers=None, params=None):
if GET_FAILS: if GET_FAILS:
raise RuntimeError("simulated Loomio read failure") raise RuntimeError("simulated Loomio roster read failure")
return FakeGetResp() offset = (params or {}).get("offset", 0)
limit = (params or {}).get("limit", 50)
rows = [{"id": i, "user_email": e} for e, i in ROSTER.items()]
page = rows[offset:offset + limit]
return FakeResp(200, {"memberships": page, "meta": {"total": len(rows)}})
async def post(self, url, headers=None, json=None): async def post(self, url, headers=None, json=None):
sent.append({"url": url, "json": json or {}}) posts.append({"url": url, "json": json or {}})
return FakePostResp() return FakeResp(200, {"added_emails": (json or {}).get("emails", [])})
async def delete(self, url, headers=None):
deletes.append(url)
return FakeResp(DELETE_STATUS)
async def patch(self, *a, **k): async def patch(self, *a, **k):
return FakePostResp() return FakeResp(200, {})
async def fake_active(): async def fake_active():
# new@ never seen; old@ previously invited (record); member@ already in Loomio # active members: new@ / old@ are NOT in the group; member@ IS.
return ["new@x.org", "old@x.org", "member@x.org"] return ["new@x.org", "old@x.org", "member@x.org"]
@@ -88,6 +107,10 @@ async def fake_activated(emails):
return emails return emails
async def noop_fix(_client):
return {}
results = [] results = []
@@ -96,7 +119,7 @@ def check(name, ok):
async def main(): async def main():
global GET_FAILS global GET_FAILS, ROSTER, DELETE_STATUS
m.get_db().close() # create + migrate the fixture schema m.get_db().close() # create + migrate the fixture schema
conn = m.get_db() conn = m.get_db()
for e in ("new@x.org", "old@x.org", "member@x.org"): for e in ("new@x.org", "old@x.org", "member@x.org"):
@@ -111,53 +134,103 @@ async def main():
m.get_active_member_emails = fake_active m.get_active_member_emails = fake_active
m.filter_loomio_activated = fake_activated m.filter_loomio_activated = fake_activated
m.httpx.AsyncClient = FakeClient m.httpx.AsyncClient = FakeClient
m.loomio_fix_usernames = noop_fix
# 1. Disabled -> no Loomio call at all. check("default: sync is ENABLED when the flag is unset", m.LOOMIO_SYNC_ENABLED is True)
m.LOOMIO_SYNC_ENABLED = False
sent.clear()
await m.sync_loomio_memberships()
check("disabled: NO Loomio call", sent == [])
# 2. Enabled: only the never-seen address is sent. old@ is excluded by its # Loomio group currently holds: an active member (member@), and the operator
# invite record; member@ is excluded because Loomio ALREADY has them # allowlist account (keep@) which is NOT an active portal member.
# (even though their loomio_invited_at is NULL — the backfill gap). ROSTER = {"member@x.org": 10, "keep@x.org": 2}
m.mark_loomio_invited("old@x.org")
m.LOOMIO_SYNC_ENABLED = True # --- 1. ADD path: only the never-seen address is invited. ---
sent.clear() m.mark_loomio_invited("old@x.org") # recorded -> excluded by layer (b)
posts.clear(); deletes.clear()
await m.sync_loomio_memberships() await m.sync_loomio_memberships()
body = sent[0]["json"] if sent else {} body = posts[0]["json"] if posts else {}
check("enabled: exactly one Loomio call", len(sent) == 1) check("add: exactly one invite call", len(posts) == 1)
check("enabled: only the never-seen address is sent", check("add: only the never-seen address is invited", body.get("emails") == ["new@x.org"])
body.get("emails") == ["new@x.org"]) check("add: a member ALREADY in Loomio is skipped silently",
check("enabled: a member already in Loomio is excluded (NULL record)",
"member@x.org" not in (body.get("emails") or [])) "member@x.org" not in (body.get("emails") or []))
check("enabled: remove_absent is NOT sent (no evict/re-add path)", check("add: the record guard excludes a previously-invited address",
"remove_absent" not in body) "old@x.org" not in (body.get("emails") or []))
check("add: remove_absent is NEVER sent (no re-add path)", "remove_absent" not in body)
check("add: the newly-invited address is recorded",
m.get_loomio_invited_at("new@x.org") is not None)
check("add: nothing removed when nobody has lapsed", deletes == [])
# 3. The newly-invited address is recorded; a second run sends nothing. # --- 2. Second run adds nobody again. ---
check("new@ recorded after invite", m.get_loomio_invited_at("new@x.org") is not None) ROSTER = {"member@x.org": 10, "keep@x.org": 2, "new@x.org": 99} # Loomio now has new@
sent.clear() posts.clear(); deletes.clear()
await m.sync_loomio_memberships() await m.sync_loomio_memberships()
check("second run: NO Loomio call (nobody new)", sent == []) check("double-run: no second invite", posts == [])
check("double-run: no removals", deletes == [])
# 4. Fail-closed: if Loomio's member list can't be read, nothing is sent. # --- 3. REMOVE path: a lapsed member is removed exactly once. ---
m.get_loomio_invited_at("new@x.org") # no-op; keep the record ROSTER = {"member@x.org": 10, "keep@x.org": 2, "new@x.org": 99, "lapsed@x.org": 77}
conn = m.get_db() posts.clear(); deletes.clear()
conn.execute("UPDATE members SET loomio_invited_at = NULL WHERE email = 'new@x.org'") await m.sync_loomio_memberships()
conn.commit() check("remove: the lapsed member is removed exactly once",
conn.close() deletes == [f"{m.LOOMIO_BASE}/api/b3/memberships/77"])
check("remove: the live active member is NOT removed",
f"{m.LOOMIO_BASE}/api/b3/memberships/10" not in deletes)
check("remove: the operator allowlist is NEVER removed (even though not active)",
f"{m.LOOMIO_BASE}/api/b3/memberships/2" not in deletes)
check("remove: no re-invite is sent while removing", posts == [])
check("remove: it does not send present members to any endpoint",
all("keep@x.org" not in (p["json"].get("emails") or []) for p in posts))
# --- 4. Second run after the removal changes nothing. ---
ROSTER = {"member@x.org": 10, "keep@x.org": 2, "new@x.org": 99} # lapsed gone
posts.clear(); deletes.clear()
await m.sync_loomio_memberships()
check("remove double-run: nothing removed again", deletes == [])
check("remove double-run: nothing invited", posts == [])
# --- 5. A re-run against a STALE roster (member already gone) must not error:
# Loomio answers 404 for an absent membership, which is success. ---
ROSTER = {"member@x.org": 10, "keep@x.org": 2, "lapsed@x.org": 77}
DELETE_STATUS = 404
posts.clear(); deletes.clear()
err = None
try:
await m.sync_loomio_memberships()
except Exception as e: # pragma: no cover
err = e
check("idempotent remove: a 404 (already absent) is treated as success", err is None)
check("idempotent remove: still no re-invite", posts == [])
DELETE_STATUS = 200
# --- 6. FAIL CLOSED: an unreadable roster stops BOTH phases. ---
ROSTER = {"lapsed@x.org": 77}
GET_FAILS = True GET_FAILS = True
sent.clear() posts.clear(); deletes.clear()
await m.sync_loomio_memberships() await m.sync_loomio_memberships()
check("fail-closed: no invite sent when Loomio members unreadable", sent == []) check("fail-closed: no invite when the roster is unreadable", posts == [])
check("fail-closed: no removal when the roster is unreadable", deletes == [])
GET_FAILS = False GET_FAILS = False
# 5. Recording is idempotent — an existing timestamp is never overwritten. # --- 7. The kill switch really pauses everything. ---
m.LOOMIO_SYNC_ENABLED = False
posts.clear(); deletes.clear()
await m.sync_loomio_memberships()
check("kill switch: no calls at all when LOOMIO_SYNC_ENABLED=0",
posts == [] and deletes == [])
m.LOOMIO_SYNC_ENABLED = True
# --- 8. Recording is idempotent — an existing timestamp is never overwritten. ---
first = m.get_loomio_invited_at("old@x.org") first = m.get_loomio_invited_at("old@x.org")
m.mark_loomio_invited("old@x.org") m.mark_loomio_invited("old@x.org")
check("mark_loomio_invited never overwrites an existing timestamp", check("mark_loomio_invited never overwrites an existing timestamp",
m.get_loomio_invited_at("old@x.org") == first) m.get_loomio_invited_at("old@x.org") == first)
# --- 9. remove_loomio_membership status handling (200/204/404 ok, 500 not). ---
async with FakeClient() as c:
DELETE_STATUS = 204
check("remove_loomio_membership: 204 is success", await m.remove_loomio_membership(c, 1))
DELETE_STATUS = 500
check("remove_loomio_membership: 500 is a failure", not await m.remove_loomio_membership(c, 1))
DELETE_STATUS = 200
asyncio.run(main()) asyncio.run(main())
fails = sum(not ok for _, ok in results) fails = sum(not ok for _, ok in results)