172 lines
6.2 KiB
Python
172 lines
6.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Membership events — the durable record of who lapsed and who returned.
|
|
|
|
WHY THIS EXISTS
|
|
Before this table, a lapse was only members.active = 0: no date, no reason,
|
|
no source. A former member was indistinguishable from someone who had never
|
|
paid, and a returning payer silently erased the fact that they had lapsed.
|
|
Every deactivation/reactivation transition now writes one row, and the admin
|
|
overview carries the log plus the latest deactivation per member.
|
|
|
|
The tests pin:
|
|
1. deactivate_member logs ONE row on the active -> inactive transition;
|
|
a repeated call (OC re-delivery / second sweep pass) does not duplicate.
|
|
2. store_member logs `reactivated` when it flips an inactive member back on.
|
|
3. backfill gives a pre-existing inactive member one row with occurred_at
|
|
NULL and a reason derived from allowance_log; it runs once.
|
|
4. the overview helper returns the newest-first log and maps the latest
|
|
deactivation to the member's email (None date for a backfilled row).
|
|
|
|
Runnable: python3 tests/test_membership_events.py (from the repo root)
|
|
"""
|
|
import base64
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
|
|
os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "events-fixture.db")
|
|
os.environ["PORTAL_KEK"] = base64.b64encode(bytes(32)).decode()
|
|
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"]
|
|
|
|
FAILS = []
|
|
|
|
|
|
def check(cond, msg):
|
|
print((" ok " if cond else " FAIL ") + msg)
|
|
if not cond:
|
|
FAILS.append(msg)
|
|
|
|
|
|
def rows(q, args=()):
|
|
conn = m.get_db()
|
|
try:
|
|
return conn.execute(q, args).fetchall()
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
print("1. deactivation logs exactly one transition")
|
|
conn = m.get_db()
|
|
m._ensure_allowance_tables(conn)
|
|
conn.execute("DELETE FROM members")
|
|
conn.execute("DELETE FROM membership_events")
|
|
conn.execute("DELETE FROM portal_state")
|
|
conn.execute(
|
|
"INSERT INTO members (email, active, account_type) VALUES (?, 1, 'member')",
|
|
("lapse@example.org",),
|
|
)
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
m.deactivate_member(
|
|
"lapse@example.org",
|
|
reason="Membership lapsed: no longer on the live Open Collective member list",
|
|
source="sweep",
|
|
)
|
|
ev = rows(
|
|
"SELECT email, event, reason, source, occurred_at, account_type "
|
|
"FROM membership_events WHERE email = ?",
|
|
("lapse@example.org",),
|
|
)
|
|
check(len(ev) == 1, "one deactivation row written")
|
|
check(ev[0][1] == "deactivated", "event is 'deactivated'")
|
|
check(ev[0][3] == "sweep", "source records the calling path")
|
|
check("Open Collective" in ev[0][2], "reason records why")
|
|
check(bool(ev[0][4]), "live transition carries occurred_at")
|
|
check(ev[0][5] == "member", "account_type is carried onto the event")
|
|
|
|
# A re-delivered webhook / second sweep pass must not append another lapse.
|
|
m.deactivate_member(
|
|
"lapse@example.org",
|
|
reason="duplicate delivery",
|
|
source="webhook-opencollective",
|
|
)
|
|
ev = rows(
|
|
"SELECT event FROM membership_events WHERE email = ?",
|
|
("lapse@example.org",),
|
|
)
|
|
check(len(ev) == 1, "a repeated deactivation writes no duplicate row")
|
|
|
|
print("2. a returning payer is logged as reactivated")
|
|
m.store_member("lapse@example.org", "sk-fixture", "uid-fixture")
|
|
ev = rows(
|
|
"SELECT event, source FROM membership_events WHERE email = ? ORDER BY id",
|
|
("lapse@example.org",),
|
|
)
|
|
check([r[0] for r in ev] == ["deactivated", "reactivated"],
|
|
"deactivated then reactivated, in order")
|
|
check(ev[-1][1] == "provision", "reactivation source is provisioning")
|
|
active = rows("SELECT active FROM members WHERE email = ?", ("lapse@example.org",))
|
|
check(active[0][0] == 1, "member is active again")
|
|
|
|
print("3. backfill gives a pre-existing inactive member a dated-unknown row")
|
|
conn = m.get_db()
|
|
conn.execute("DELETE FROM membership_events")
|
|
conn.execute("DELETE FROM portal_state")
|
|
conn.execute("DELETE FROM members")
|
|
conn.execute(
|
|
"INSERT INTO members (email, active, account_type, welcome_sent_at) "
|
|
"VALUES (?, 0, 'member', ?)",
|
|
("old@example.org", "2026-11-11 19:00:47"),
|
|
)
|
|
conn.execute(
|
|
"INSERT INTO allowance_log (email, cycle, decision, evidence, decided_at) "
|
|
"VALUES (?, ?, ?, ?, ?)",
|
|
("old@example.org", "2026-11-16", "in_grace",
|
|
"no recurring (monthly) contribution on Open Collective; reminder sent 2026-11-16 (grace 7d)",
|
|
"2026-11-18 11:02:37"),
|
|
)
|
|
conn.commit()
|
|
m._backfill_membership_events(conn)
|
|
conn.commit()
|
|
ev = conn.execute(
|
|
"SELECT event, reason, source, occurred_at, detail FROM membership_events "
|
|
"WHERE email = ?", ("old@example.org",),
|
|
).fetchall()
|
|
conn.close()
|
|
check(len(ev) == 1, "one backfilled row")
|
|
check(ev[0][0] == "deactivated" and ev[0][2] == "backfill",
|
|
"backfill event/source are explicit")
|
|
check(ev[0][3] is None, "backfilled occurred_at stays NULL (unknown date)")
|
|
check("in_grace" in ev[0][1] and "2026-11-18" in ev[0][1],
|
|
"backfill reason quotes the allowance decision and date")
|
|
check("exact" in ev[0][4].lower(), "detail says the exact date is unknown")
|
|
|
|
# Re-running the backfill must not duplicate the row (the marker gates it).
|
|
conn = m.get_db()
|
|
m._backfill_membership_events(conn)
|
|
conn.commit()
|
|
n = conn.execute(
|
|
"SELECT COUNT(*) FROM membership_events WHERE email = ?", ("old@example.org",),
|
|
).fetchone()[0]
|
|
conn.close()
|
|
check(n == 1, "backfill is idempotent")
|
|
|
|
print("4. the overview helper returns the log and the latest deactivation")
|
|
conn = m.get_db()
|
|
conn.execute(
|
|
"INSERT INTO membership_events (email, account_type, event, reason, source, occurred_at) "
|
|
"VALUES (?, 'member', 'reactivated', ?, 'provision', ?)",
|
|
("old@example.org", "Returned to active membership", "2026-11-24 12:00:00"),
|
|
)
|
|
conn.commit()
|
|
events, latest = m._membership_events_for_overview(conn)
|
|
conn.close()
|
|
check(len(events) == 2, "both events are in the log")
|
|
check(events[0]["event"] == "reactivated", "newest event sorts first")
|
|
check("old@example.org" in latest, "latest deactivation is keyed by email")
|
|
check(latest["old@example.org"]["at"] is None,
|
|
"backfilled latest deactivation reports an unknown date")
|
|
check(latest["old@example.org"]["source"] == "backfill",
|
|
"latest deactivation keeps its source")
|
|
|
|
print()
|
|
if FAILS:
|
|
print(f"FAILED: {len(FAILS)} check(s) red")
|
|
sys.exit(1)
|
|
print("all membership-event checks green")
|