Files
member-portal/tests/test_loomio_guard.py
inference-bot ffed06d492
ci/woodpecker/push/woodpecker Pipeline failed
loomio sync: a stamp must not outlive the membership
The invite stamp meant 'handed to Loomio once, EVER', so a member whose
membership was later revoked (a lapse that was reversed) or who deleted
their Loomio account was recorded as invited and never invited again.

- Stamp only the addresses Loomio actually returns in added_emails; an
  address accepted in the payload but not added stays eligible.
- Clear the stamp for an ACTIVE member stamped-but-absent from the live
  roster; the invite itself waits one pass so a single stale page cannot
  become an invite.
- Cap stamp clearing per pass (LOOMIO_RECONCILE_MAX, default 10): more
  stamped-but-absent members means a short roster read, not a mass
  re-invite.
- loomio_opt_out exempts members who deliberately left.
- Username fix falls back to the portal slug when a handle is already
  taken (Loomio handles are instance-global; every OC alias resolves to
  opencollective).
2026-11-22 19:45:20 +00:00

317 lines
13 KiB
Python

#!/usr/bin/env python3
"""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:
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)
"""
import os
import sys
import tempfile
import asyncio
os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "fixture.db")
os.environ["LOOMIO_API_KEY"] = "test-user-key"
os.environ["LOOMIO_B3_KEY"] = "test-server-key"
os.environ["LOOMIO_GROUP_ID"] = "1"
# 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"]
# 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"]
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
ADDED_OMIT = set() # emails the fake Loomio accepts but does NOT add
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):
if self.status_code >= 400:
raise RuntimeError(f"HTTP {self.status_code}")
def json(self):
return self._payload
class FakeClient:
def __init__(self, *a, **k):
pass
async def __aenter__(self):
return self
async def __aexit__(self, *a):
return False
async def get(self, url, headers=None, params=None):
if GET_FAILS:
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):
posts.append({"url": url, "json": json or {}})
sent = (json or {}).get("emails", [])
return FakeResp(200, {"added_emails": [e for e in sent if e not in ADDED_OMIT]})
async def delete(self, url, headers=None):
deletes.append(url)
return FakeResp(DELETE_STATUS)
async def patch(self, *a, **k):
return FakeResp(200, {})
async def fake_active():
# active members: new@ / old@ are NOT in the group; member@ IS.
return ["new@x.org", "old@x.org", "member@x.org"]
async def fake_activated(emails):
return emails
async def noop_fix(_client):
return {}
results = []
def check(name, ok):
results.append((name, ok))
async def main():
global GET_FAILS, ROSTER, DELETE_STATUS, ADDED_OMIT
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"):
conn.execute("INSERT OR REPLACE INTO members (email, active) VALUES (?, 1)", (e,))
conn.commit()
conn.close()
cols = [r[1] for r in m.get_db().execute("PRAGMA table_info(members)").fetchall()]
m.get_db().close()
check("migration: members.loomio_invited_at exists", "loomio_invited_at" in cols)
check("migration: members.loomio_opt_out exists", "loomio_opt_out" in cols)
m.get_active_member_emails = fake_active
m.filter_loomio_activated = fake_activated
m.httpx.AsyncClient = FakeClient
m.loomio_fix_usernames = noop_fix
check("default: sync is ENABLED when the flag is unset", m.LOOMIO_SYNC_ENABLED is True)
# 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 = 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("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("reconcile: a stamped-but-absent member's stamp is cleared (not yet re-invited)",
m.get_loomio_invited_at("old@x.org") is None)
check("add: nothing removed when nobody has lapsed", deletes == [])
# --- 2. Second run: the cleared member is invited once, then recorded. ---
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()
body = posts[0]["json"] if posts else {}
check("reconcile: the cleared member is re-invited on the next pass",
body.get("emails") == ["old@x.org"])
check("reconcile: the re-invite is recorded", m.get_loomio_invited_at("old@x.org") is not None)
check("reconcile: a present member is never re-invited",
"member@x.org" not in (body.get("emails") or []))
# --- 2b. Third run: nothing left to invite. ---
ROSTER = {"member@x.org": 10, "keep@x.org": 2, "new@x.org": 99, "old@x.org": 88}
posts.clear(); deletes.clear()
await m.sync_loomio_memberships()
check("double-run: no second invite", posts == [])
check("double-run: no removals", deletes == [])
# --- 3. REMOVE path: a lapsed member is removed exactly once. ---
ROSTER = {"member@x.org": 10, "keep@x.org": 2, "new@x.org": 99, "old@x.org": 88, "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, "old@x.org": 88} # 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, "old@x.org": 88, "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, "old@x.org": 88}
GET_FAILS = True
posts.clear(); deletes.clear()
await m.sync_loomio_memberships()
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
# --- 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("member@x.org")
m.mark_loomio_invited("member@x.org")
check("mark_loomio_invited never overwrites an existing timestamp",
m.get_loomio_invited_at("member@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
# --- 10. OPT-OUT: a member who deliberately left is never re-invited. ---
conn = m.get_db()
conn.execute("INSERT OR REPLACE INTO members (email, active, loomio_opt_out) VALUES (?, 1, 1)",
("left@x.org",))
conn.commit()
conn.close()
m.mark_loomio_invited("left@x.org")
async def active_with_left():
return ["member@x.org", "left@x.org"]
m.get_active_member_emails = active_with_left
ROSTER = {"member@x.org": 10, "keep@x.org": 2}
posts.clear(); deletes.clear()
await m.sync_loomio_memberships()
check("opt-out: a member who left is not re-invited", posts == [])
check("opt-out: their stamp is left intact", m.get_loomio_invited_at("left@x.org") is not None)
# --- 11. RECONCILE CAP: many stamped-but-absent members mean a bad roster
# read, not a mass re-invite. Nothing is cleared. ---
many = [f"miss{n}@x.org" for n in range(1, m.LOOMIO_RECONCILE_MAX + 2)]
conn = m.get_db()
for e in many:
conn.execute("INSERT OR REPLACE INTO members (email, active) VALUES (?, 1)", (e,))
conn.commit()
conn.close()
for e in many:
m.mark_loomio_invited(e)
async def active_with_many():
return ["member@x.org"] + many
m.get_active_member_emails = active_with_many
ROSTER = {"member@x.org": 10, "keep@x.org": 2}
posts.clear(); deletes.clear()
await m.sync_loomio_memberships()
check("cap: an over-cap stamp reconciliation sends no invites", posts == [])
check("cap: stamps are untouched when the cap is exceeded",
all(m.get_loomio_invited_at(e) is not None for e in many))
# --- 12. NOT ADDED: if Loomio accepts the payload but adds nothing for an
# address, it must stay unstamped and eligible. ---
conn = m.get_db()
conn.execute("INSERT OR REPLACE INTO members (email, active) VALUES (?, 1)", ("lost@x.org",))
conn.commit()
conn.close()
async def active_with_lost():
return ["member@x.org", "lost@x.org"]
m.get_active_member_emails = active_with_lost
ADDED_OMIT = {"lost@x.org"}
ROSTER = {"member@x.org": 10, "keep@x.org": 2}
posts.clear(); deletes.clear()
await m.sync_loomio_memberships()
check("not-added: the address is sent", posts and posts[0]["json"]["emails"] == ["lost@x.org"])
check("not-added: an address Loomio did not add stays unstamped",
m.get_loomio_invited_at("lost@x.org") is None)
ADDED_OMIT = set()
asyncio.run(main())
fails = sum(not ok for _, ok in results)
for name, ok in results:
print(f" [{'PASS' if ok else 'FAIL'}] {name}")
print(f"\n{len(results) - fails}/{len(results)} passed")
sys.exit(1 if fails else 0)