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:
1 parent
17329c5d6f
commit
f7bbec8a0a
2 files changed
+282
-145
No files matched your search
+142
-78
@@ -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
|
||||
# lets us rename them to the member's Cloudron username.
|
||||
LOOMIO_B3_KEY = os.environ.get("LOOMIO_B3_KEY", "")
|
||||
# Master switch for the Loomio membership sync. OFF unless explicitly enabled.
|
||||
# The sync's only effect is Loomio's invite notification, and a bad run emails
|
||||
# the whole group (incident 2026-11-04: notification 744 delivered to ~83
|
||||
# members). Restoring LOOMIO_API_KEY alone must NOT re-arm it — this flag has
|
||||
# to be set deliberately by the operator, once Loomio's purpose is decided.
|
||||
LOOMIO_SYNC_ENABLED = os.environ.get("LOOMIO_SYNC_ENABLED", "").strip().lower() in ("1", "true", "on", "yes")
|
||||
# Master switch for the Loomio membership sync. It defaults to ENABLED: the
|
||||
# co-op wants every new member added to Loomio, so restoring LOOMIO_API_KEY
|
||||
# and deploying is all that is needed. It is expressed as a KILL SWITCH —
|
||||
# set LOOMIO_SYNC_ENABLED=0 (or false/off/no) to pause the sync; any other
|
||||
# value, or leaving it unset, keeps it ON. (Incident 2026-11-04 was fixed the
|
||||
# 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
|
||||
# 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
|
||||
|
||||
|
||||
async def fetch_loomio_member_emails(client: httpx.AsyncClient) -> set[str]:
|
||||
"""Return the emails Loomio already has in the group (lowercased).
|
||||
async def fetch_loomio_members(client: httpx.AsyncClient) -> dict[str, int]:
|
||||
"""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
|
||||
exclusion. It must succeed before any invite is sent — see the fail-closed
|
||||
handling in sync_loomio_memberships.
|
||||
Uses the Server API (b3) so we get membership ids in one pass (the id is
|
||||
what a targeted removal needs) AND a STABLE read: the b3 index is ordered by
|
||||
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).
|
||||
"""
|
||||
r = await client.get(
|
||||
f"{LOOMIO_BASE}/api/b2/memberships",
|
||||
headers={"Authorization": f"Bearer {LOOMIO_API_KEY}"},
|
||||
params={"group_id": int(LOOMIO_GROUP_ID)},
|
||||
members: dict[str, int] = {}
|
||||
offset = 0
|
||||
while True:
|
||||
r = await client.get(
|
||||
f"{LOOMIO_BASE}/api/b3/memberships",
|
||||
headers={"Authorization": f"Bearer {LOOMIO_B3_KEY}"},
|
||||
params={"group_id": int(LOOMIO_GROUP_ID), "limit": 50, "offset": offset},
|
||||
)
|
||||
r.raise_for_status()
|
||||
page = r.json().get("memberships", [])
|
||||
for m in page:
|
||||
email = (m.get("user_email") or "").lower()
|
||||
if 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}"},
|
||||
)
|
||||
r.raise_for_status()
|
||||
data = r.json()
|
||||
return {
|
||||
(m.get("user_email") or "").lower()
|
||||
for m in data.get("memberships", [])
|
||||
if m.get("user_email")
|
||||
}
|
||||
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:
|
||||
"""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
|
||||
invite notification to essentially the WHOLE membership. The guards below
|
||||
make a repeat impossible:
|
||||
TWO jobs with DIFFERENT guards — a deactivation must still propagate:
|
||||
|
||||
1. LOOMIO_SYNC_ENABLED must be ON. The integration is OFF by default, so
|
||||
restoring LOOMIO_API_KEY alone does NOT re-arm it; enabling it is a
|
||||
deliberate operator action.
|
||||
2. POSITIVE EXCLUSION — an address is only ever sent if it is neither
|
||||
currently in the Loomio group NOR recorded as previously handed to
|
||||
Loomio (members.loomio_invited_at). A member who is already in Loomio,
|
||||
or was ever invited, can never be invited again.
|
||||
3. FAIL CLOSED — if the current Loomio member list cannot be read, the sync
|
||||
skips entirely rather than sending a list it cannot verify.
|
||||
1. ADD (invite) — a member is invited only if they are BOTH not already in
|
||||
the group (live pre-check) AND never handed to Loomio before
|
||||
(members.loomio_invited_at). Exactly once, ever. This is the dedupe
|
||||
guard, and it applies to INVITATIONS ONLY.
|
||||
2. REMOVE — a member no longer active is revoked so a lapse propagates to
|
||||
Loomio. The removal path is targeted (b3 DELETE by membership id) and
|
||||
sends nothing for anyone who is staying, so it cannot re-invite or
|
||||
notify a present member. Operator accounts (LOOMIO_ALWAYS_KEEP) are
|
||||
never removed.
|
||||
|
||||
Deliberately does NOT pass `remove_absent`: this sync only ADDS. Removing a
|
||||
lapsed member is a separate, explicit decision. Sending only the
|
||||
never-invited subset (instead of the full list with remove_absent=1) also
|
||||
removes the reconcile path that could evict a member on one run and then
|
||||
re-add — and therefore re-invite — them on the next.
|
||||
Guards (incident 2026-11-04: a live run emailed Loomio invites to the whole
|
||||
membership):
|
||||
* The invite payload contains ONLY addresses Loomio has never seen and we
|
||||
have never handed over — an existing member is skipped SILENTLY, no API
|
||||
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
|
||||
way to guarantee no invite is to send an address exactly once.
|
||||
Loomio's b2 invite has no "silent" flag, so the only way to guarantee no
|
||||
invite is to send an address exactly once, ever — hence the record.
|
||||
"""
|
||||
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
|
||||
if not LOOMIO_API_KEY or not LOOMIO_GROUP_ID:
|
||||
if not LOOMIO_GROUP_ID:
|
||||
logger.warning("Loomio not configured; skipping membership sync")
|
||||
return
|
||||
|
||||
emails = await get_active_member_emails()
|
||||
# Always keep operator accounts in the group.
|
||||
for keep in LOOMIO_ALWAYS_KEEP:
|
||||
if keep not in emails:
|
||||
emails.append(keep)
|
||||
# The set that decides who STAYS in the group: every active member plus the
|
||||
# operator allowlist. Computed from the UNFILTERED active list on purpose —
|
||||
# an active member whose Cloudron account is not yet activated is still a
|
||||
# member and must NOT be removed from Loomio (only their invite is deferred).
|
||||
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) —
|
||||
# never email a Loomio invite to someone who hasn't created their account
|
||||
# yet. Operator accounts bypass this filter (the bootstrap owner shows
|
||||
# inviteAccepted: false forever despite being functional).
|
||||
emails = await filter_loomio_activated(emails)
|
||||
# Invite candidates are additionally filtered to activated accounts so a
|
||||
# Loomio invite email never precedes the member's own account setup.
|
||||
invite_emails = await get_active_member_emails()
|
||||
for keep in LOOMIO_ALWAYS_KEEP:
|
||||
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:
|
||||
# FAIL CLOSED: never send a list we could not check against Loomio.
|
||||
# FAIL CLOSED: never act on a roster we could not read.
|
||||
try:
|
||||
current = await fetch_loomio_member_emails(client)
|
||||
current = await fetch_loomio_members(client)
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"Loomio sync: could not read current group members; skipping "
|
||||
@@ -1659,35 +1709,49 @@ async def sync_loomio_memberships() -> None:
|
||||
)
|
||||
return
|
||||
|
||||
# POSITIVE EXCLUSION: only addresses Loomio has never seen, and that we
|
||||
# have never handed over.
|
||||
# --- REMOVE: reconcile the group to the current active set. ---
|
||||
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 = [
|
||||
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 not fresh:
|
||||
logger.info(
|
||||
"Loomio sync: nobody new (all already in Loomio or already "
|
||||
"invited); nothing to send"
|
||||
"invited); nothing to invite"
|
||||
)
|
||||
return
|
||||
|
||||
r = await client.post(
|
||||
f"{LOOMIO_BASE}/api/b2/memberships",
|
||||
headers={"Authorization": f"Bearer {LOOMIO_API_KEY}"},
|
||||
json={"group_id": int(LOOMIO_GROUP_ID), "emails": fresh},
|
||||
)
|
||||
if r.status_code != 200:
|
||||
logger.warning("Loomio membership sync failed: %s %s", r.status_code, r.text[:200])
|
||||
return
|
||||
result = r.json()
|
||||
added = result.get("added_emails", []) or []
|
||||
# Record EVERY address we sent, so it is never sent again — even if
|
||||
# Loomio didn't add it, or the membership is later revoked. A revoked
|
||||
# member is NOT a never-invited member, and must not be re-invited.
|
||||
for e in fresh:
|
||||
mark_loomio_invited(e)
|
||||
logger.info("Loomio sync: first-time invites sent (%d): %s", len(added), added)
|
||||
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(
|
||||
f"{LOOMIO_BASE}/api/b2/memberships",
|
||||
headers={"Authorization": f"Bearer {LOOMIO_API_KEY}"},
|
||||
json={"group_id": int(LOOMIO_GROUP_ID), "emails": fresh},
|
||||
)
|
||||
if r.status_code != 200:
|
||||
logger.warning("Loomio membership sync failed: %s %s", r.status_code, r.text[:200])
|
||||
else:
|
||||
added = r.json().get("added_emails", []) or []
|
||||
# Record EVERY address we sent, so it is never sent again — even
|
||||
# if Loomio didn't add it, or the membership is later revoked. A
|
||||
# revoked member is NOT a never-invited member, and must not be
|
||||
# re-invited.
|
||||
for e in fresh:
|
||||
mark_loomio_invited(e)
|
||||
logger.info("Loomio sync: first-time invites sent (%d): %s", len(added), added)
|
||||
|
||||
# Fix auto-generated usernames (email+randomsuffix) → Cloudron username.
|
||||
# Best-effort: no B3 key or an API failure just logs and moves on.
|
||||
|
||||
+140
-67
@@ -1,20 +1,30 @@
|
||||
#!/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
|
||||
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
|
||||
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,
|
||||
so restoring LOOMIO_API_KEY alone cannot re-arm the integration.
|
||||
2. POSITIVE EXCLUSION — an address is only ever sent if it is neither in
|
||||
Loomio's current member list NOR recorded as previously invited
|
||||
(members.loomio_invited_at). This matters because the invite-record
|
||||
column starts NULL for the ~89 members ALREADY in Loomio.
|
||||
3. FAIL CLOSED — if Loomio's current member list can't be read, no invite is
|
||||
sent.
|
||||
4. `remove_absent` is never sent (no evict-then-re-add re-invite path).
|
||||
ADD / dedupe
|
||||
1. A member already in the Loomio group is skipped SILENTLY — no invite
|
||||
payload ever contains their address.
|
||||
2. members.loomio_invited_at is a second guard: an address we have handed
|
||||
over before is never sent again, even if it is not (yet) visible in the
|
||||
live roster.
|
||||
3. `remove_absent` is never sent (no evict-then-re-add re-invite path).
|
||||
4. FAIL CLOSED — if the live roster cannot be read, nothing is sent.
|
||||
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)
|
||||
"""
|
||||
@@ -24,36 +34,37 @@ import tempfile
|
||||
import asyncio
|
||||
|
||||
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_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__))))
|
||||
import app.main as m # noqa: E402
|
||||
|
||||
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
|
||||
GET_FAILS = False # toggled to exercise fail-closed
|
||||
posts = [] # every b2/memberships POST payload
|
||||
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:
|
||||
status_code = 200
|
||||
|
||||
def json(self):
|
||||
return {"added_emails": ["new@x.org"]}
|
||||
|
||||
|
||||
class FakeGetResp:
|
||||
def __init__(self):
|
||||
self.status_code = 200
|
||||
class FakeResp:
|
||||
def __init__(self, status=200, payload=None):
|
||||
self.status_code = status
|
||||
self._payload = payload or {}
|
||||
self.text = str(payload or {})
|
||||
|
||||
def raise_for_status(self):
|
||||
pass
|
||||
if self.status_code >= 400:
|
||||
raise RuntimeError(f"HTTP {self.status_code}")
|
||||
|
||||
def json(self):
|
||||
# member@x.org is ALREADY in the Loomio group.
|
||||
return {"memberships": [{"user_email": "Member@x.org"}]}
|
||||
return self._payload
|
||||
|
||||
|
||||
class FakeClient:
|
||||
@@ -68,19 +79,27 @@ class FakeClient:
|
||||
|
||||
async def get(self, url, headers=None, params=None):
|
||||
if GET_FAILS:
|
||||
raise RuntimeError("simulated Loomio read failure")
|
||||
return FakeGetResp()
|
||||
raise RuntimeError("simulated Loomio roster read failure")
|
||||
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):
|
||||
sent.append({"url": url, "json": json or {}})
|
||||
return FakePostResp()
|
||||
posts.append({"url": url, "json": json or {}})
|
||||
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):
|
||||
return FakePostResp()
|
||||
return FakeResp(200, {})
|
||||
|
||||
|
||||
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"]
|
||||
|
||||
|
||||
@@ -88,6 +107,10 @@ async def fake_activated(emails):
|
||||
return emails
|
||||
|
||||
|
||||
async def noop_fix(_client):
|
||||
return {}
|
||||
|
||||
|
||||
results = []
|
||||
|
||||
|
||||
@@ -96,7 +119,7 @@ def check(name, ok):
|
||||
|
||||
|
||||
async def main():
|
||||
global GET_FAILS
|
||||
global GET_FAILS, ROSTER, DELETE_STATUS
|
||||
m.get_db().close() # create + migrate the fixture schema
|
||||
conn = m.get_db()
|
||||
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.filter_loomio_activated = fake_activated
|
||||
m.httpx.AsyncClient = FakeClient
|
||||
m.loomio_fix_usernames = noop_fix
|
||||
|
||||
# 1. Disabled -> no Loomio call at all.
|
||||
m.LOOMIO_SYNC_ENABLED = False
|
||||
sent.clear()
|
||||
await m.sync_loomio_memberships()
|
||||
check("disabled: NO Loomio call", sent == [])
|
||||
check("default: sync is ENABLED when the flag is unset", m.LOOMIO_SYNC_ENABLED is True)
|
||||
|
||||
# 2. Enabled: only the never-seen address is sent. old@ is excluded by its
|
||||
# invite record; member@ is excluded because Loomio ALREADY has them
|
||||
# (even though their loomio_invited_at is NULL — the backfill gap).
|
||||
m.mark_loomio_invited("old@x.org")
|
||||
m.LOOMIO_SYNC_ENABLED = True
|
||||
sent.clear()
|
||||
# Loomio group currently holds: an active member (member@), and the operator
|
||||
# allowlist account (keep@) which is NOT an active portal member.
|
||||
ROSTER = {"member@x.org": 10, "keep@x.org": 2}
|
||||
|
||||
# --- 1. ADD path: only the never-seen address is invited. ---
|
||||
m.mark_loomio_invited("old@x.org") # recorded -> excluded by layer (b)
|
||||
posts.clear(); deletes.clear()
|
||||
await m.sync_loomio_memberships()
|
||||
body = sent[0]["json"] if sent else {}
|
||||
check("enabled: exactly one Loomio call", len(sent) == 1)
|
||||
check("enabled: only the never-seen address is sent",
|
||||
body.get("emails") == ["new@x.org"])
|
||||
check("enabled: a member already in Loomio is excluded (NULL record)",
|
||||
body = posts[0]["json"] if posts else {}
|
||||
check("add: exactly one invite call", len(posts) == 1)
|
||||
check("add: only the never-seen address is invited", body.get("emails") == ["new@x.org"])
|
||||
check("add: a member ALREADY in Loomio is skipped silently",
|
||||
"member@x.org" not in (body.get("emails") or []))
|
||||
check("enabled: remove_absent is NOT sent (no evict/re-add path)",
|
||||
"remove_absent" not in body)
|
||||
check("add: the record guard excludes a previously-invited address",
|
||||
"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.
|
||||
check("new@ recorded after invite", m.get_loomio_invited_at("new@x.org") is not None)
|
||||
sent.clear()
|
||||
# --- 2. Second run adds nobody again. ---
|
||||
ROSTER = {"member@x.org": 10, "keep@x.org": 2, "new@x.org": 99} # Loomio now has new@
|
||||
posts.clear(); deletes.clear()
|
||||
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.
|
||||
m.get_loomio_invited_at("new@x.org") # no-op; keep the record
|
||||
conn = m.get_db()
|
||||
conn.execute("UPDATE members SET loomio_invited_at = NULL WHERE email = 'new@x.org'")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
# --- 3. REMOVE path: a lapsed member is removed exactly once. ---
|
||||
ROSTER = {"member@x.org": 10, "keep@x.org": 2, "new@x.org": 99, "lapsed@x.org": 77}
|
||||
posts.clear(); deletes.clear()
|
||||
await m.sync_loomio_memberships()
|
||||
check("remove: the lapsed member is removed exactly once",
|
||||
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
|
||||
sent.clear()
|
||||
posts.clear(); deletes.clear()
|
||||
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
|
||||
|
||||
# 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")
|
||||
m.mark_loomio_invited("old@x.org")
|
||||
check("mark_loomio_invited never overwrites an existing timestamp",
|
||||
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())
|
||||
fails = sum(not ok for _, ok in results)
|
||||
|
||||
Reference in new issue
Block a user