Incident 2026-11-04: a live run of sync_loomio_memberships() emailed a Loomio
invite notification to essentially the WHOLE membership (notification 744:
~83 in-app recipients, ~79 emails 'sent' 18:53-18:56 UTC). The sync handed
Loomio the full active-member list every run with no record of who had been
invited, relying on Loomio's add-if-absent semantics — which produced a
whole-group notification.
Guards now make a repeat impossible:
* LOOMIO_SYNC_ENABLED (off by default) — restoring LOOMIO_API_KEY alone no
longer re-arms the integration.
* members.loomio_invited_at — positive exclusion: an address is sent only if
it is neither in Loomio's current member list NOR ever handed over, and
each is recorded once. (The new column starts NULL for the ~89 members
already in Loomio, so the current-member check is what closes that gap.)
* FAIL CLOSED — no invite if Loomio's member list cannot be read.
* remove_absent is dropped: no evict-then-re-add (re-invite) path.
Tests: tests/test_loomio_guard.py (10/10).
168 lines
5.6 KiB
Python
168 lines
5.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Regression test: the Loomio sync can never invite an existing member twice.
|
|
|
|
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:
|
|
|
|
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).
|
|
|
|
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-key"
|
|
os.environ["LOOMIO_GROUP_ID"] = "1"
|
|
os.environ["LOOMIO_SYNC_ENABLED"] = "off"
|
|
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
|
|
|
|
sent = [] # every b2/memberships POST payload
|
|
GET_FAILS = False # toggled to exercise fail-closed
|
|
|
|
|
|
class FakePostResp:
|
|
status_code = 200
|
|
|
|
def json(self):
|
|
return {"added_emails": ["new@x.org"]}
|
|
|
|
|
|
class FakeGetResp:
|
|
def __init__(self):
|
|
self.status_code = 200
|
|
|
|
def raise_for_status(self):
|
|
pass
|
|
|
|
def json(self):
|
|
# member@x.org is ALREADY in the Loomio group.
|
|
return {"memberships": [{"user_email": "Member@x.org"}]}
|
|
|
|
|
|
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 read failure")
|
|
return FakeGetResp()
|
|
|
|
async def post(self, url, headers=None, json=None):
|
|
sent.append({"url": url, "json": json or {}})
|
|
return FakePostResp()
|
|
|
|
async def patch(self, *a, **k):
|
|
return FakePostResp()
|
|
|
|
|
|
async def fake_active():
|
|
# new@ never seen; old@ previously invited (record); member@ already in Loomio
|
|
return ["new@x.org", "old@x.org", "member@x.org"]
|
|
|
|
|
|
async def fake_activated(emails):
|
|
return emails
|
|
|
|
|
|
results = []
|
|
|
|
|
|
def check(name, ok):
|
|
results.append((name, ok))
|
|
|
|
|
|
async def main():
|
|
global GET_FAILS
|
|
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)
|
|
|
|
m.get_active_member_emails = fake_active
|
|
m.filter_loomio_activated = fake_activated
|
|
m.httpx.AsyncClient = FakeClient
|
|
|
|
# 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 == [])
|
|
|
|
# 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()
|
|
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)",
|
|
"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)
|
|
|
|
# 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()
|
|
await m.sync_loomio_memberships()
|
|
check("second run: NO Loomio call (nobody new)", sent == [])
|
|
|
|
# 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()
|
|
GET_FAILS = True
|
|
sent.clear()
|
|
await m.sync_loomio_memberships()
|
|
check("fail-closed: no invite sent when Loomio members unreadable", sent == [])
|
|
GET_FAILS = False
|
|
|
|
# 5. 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)
|
|
|
|
|
|
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)
|