#!/usr/bin/env python3 """Regression tests for credit-pack webhook idempotence (audit H1, 2026-11-05). Runnable: * from the repo root: python3 tests/test_credit_pack_idempotence.py * inside the portal app: PORTAL_CODE_DIR=/app/code python3 /tmp/test_credit_pack_idempotence.py (the container has the app's own dependencies; the host usually does not) WHY THIS EXISTS The webhook credit-pack grant used to key its dedupe on `data.get('id')`, which is absent from real OC deliveries — every recorded reference came out `oc::` (empty suffix), so the key could not distinguish two purchases by the same member, and NO exists-check guarded the grant at all. One re-delivered (or replayed, or forged-but-OC-resolvable) event then credits the pack a second time. Covered: - the reference is built per-ORDER (per-purchase), non-empty - a replayed order credits exactly once, even though the handler runs twice (simulated by replaying the same grant sequence) - a NEW order from the same member is never blocked by an earlier one - a LEGACY empty-suffix ledger row neither matches nor rejects a new grant (no re-shaping of historical rows — the invariant the fix must respect) - held_credits.oc_reference carries a UNIQUE index (one hold per purchase; NULLs and distinct legacy refs stay legal) - the DB starts with the unique index present even though legacy rows exist (no duplicate live rows to violate it) """ import os import sqlite3 import sys import tempfile _TEST_DB = os.path.join(tempfile.mkdtemp(prefix="creditpack-test-"), "members.db") os.environ["DB_PATH"] = _TEST_DB # must exist BEFORE app.main is imported # Locate the app package: explicit env, the deployed path, or the repo root. for _cand in (os.environ.get("PORTAL_CODE_DIR"), "/app/code", os.path.dirname(os.path.dirname(os.path.abspath(__file__)))): if _cand and os.path.isfile(os.path.join(_cand, "app", "main.py")): sys.path.insert(0, _cand) break from app.main import ( # noqa: E402 add_credits, credit_pack_reference, credit_reference_exists, get_credit_balance, get_db, ) failures = 0 def check(name, got, want): global failures ok = got == want if not ok: failures += 1 print(f"[{'PASS' if ok else 'FAIL'}] {name}") if not ok: print(f" got {got!r}\n want {want!r}") def grant_once(email, amount, ref): """The exact grant sequence the webhook now runs (exists-check gates add).""" if credit_reference_exists(ref): return "already_credited" add_credits(email, amount, reason="credit pack purchase (Open Collective)", reference=ref) return "credits_added" # --- reference building: per-order, non-empty, distinct per purchase --- check("reference keyed on the order id", credit_pack_reference("caseyg", {"idV2": "or_abc123"}), "oc:caseyg:or_abc123") check("two purchases differ even from one member", credit_pack_reference("caseyg", {"idV2": "or_1"}) != credit_pack_reference("caseyg", {"idV2": "or_2"}), True) try: credit_pack_reference("caseyg", {}) check("missing order idV2 refused (no ambiguous/empty reference)", False, True) credit_pack_reference("caseyg", {"idV2": ""}) check("empty order idV2 refused", False, True) except ValueError: check("missing/empty order idV2 refused (no ambiguous/empty reference)", True, True) # --- replay on a real (temporary) DB: one replay = exactly one credit --- conn = get_db() # initializes schema (incl. the unique index) on the temp db conn.execute("INSERT INTO members (email, key_token, active) VALUES ('p@example.com', '', 1)") conn.commit() ref_a = credit_pack_reference("p", {"idV2": "or_test_a"}) r1 = grant_once("p@example.com", 10.0, ref_a) # first delivery r2 = grant_once("p@example.com", 10.0, ref_a) # re-delivery of the SAME order check("first delivery grants", r1, "credits_added") check("replay of the same order is refused", r2, "already_credited") n = conn.execute("SELECT COUNT(*) FROM credit_ledger WHERE reference = ?", (ref_a,)).fetchone()[0] check("exactly ONE ledger row for the replayed order", n, 1) check("balance credited once (10, not 20)", get_credit_balance("p@example.com"), 10.0) # --- a NEW purchase from the same member is never blocked --- ref_b = credit_pack_reference("p", {"idV2": "or_test_b"}) r3 = grant_once("p@example.com", 5.0, ref_b) check("a different order still credits normally", r3, "credits_added") check("balance includes the second purchase", get_credit_balance("p@example.com"), 15.0) # --- legacy empty-suffix rows: never matched, never reshaped --- grant_once("legacy@example.com", 23.0, "oc:caseyg:") # simulate a pre-fix ledger row legacy_rows = conn.execute( "SELECT COUNT(*) FROM credit_ledger WHERE reference = 'oc:caseyg:'").fetchone()[0] check("legacy row stored exactly as it was written", legacy_rows, 1) new_from_legacy_member = credit_pack_reference("caseyg", {"idV2": "or_new"}) check("a new purchase does NOT match the legacy empty-suffix row", credit_reference_exists(new_from_legacy_member), False) check("a new purchase by the legacy member credits normally", grant_once("legacy@example.com", 15.0, new_from_legacy_member), "credits_added") still = conn.execute( "SELECT COUNT(*) FROM credit_ledger WHERE reference = 'oc:caseyg:'").fetchone()[0] check("the legacy row was neither rejected nor re-shaped", still, 1) # --- held_credits.oc_reference: UNIQUE index semantics --- idx = conn.execute( "SELECT name FROM sqlite_master WHERE type='index' AND name='idx_held_credits_oc_reference'" ).fetchone() check("unique index exists on a fresh (and the live-shaped) db", bool(idx), True) conn.execute( "INSERT INTO held_credits (slug, email, name, amount, oc_reference, status) " "VALUES ('buyer-a', NULL, 'Buyer A', 20.0, 'oc:buyer-a:or_h1', 'held')") try: conn.execute( "INSERT INTO held_credits (slug, email, name, amount, oc_reference, status) " "VALUES ('buyer-a', NULL, 'Buyer A', 20.0, 'oc:buyer-a:or_h1', 'held')") conn.commit() check("second hold for the SAME order rejected by the unique index", False, True) except sqlite3.IntegrityError: conn.rollback() check("second hold for the SAME order rejected by the unique index", True, True) conn.execute( "INSERT INTO held_credits (slug, email, name, amount, oc_reference, status) " "VALUES ('buyer-b', NULL, 'Buyer B', 10.0, 'oc:buyer-b:or_h2', 'held')") conn.commit() check("a different purchase still gets its own hold", True, True) conn.execute( "INSERT INTO held_credits (slug, email, name, amount, oc_reference, status) " "VALUES ('unknown', NULL, NULL, 10.0, NULL, 'held')") conn.execute( "INSERT INTO held_credits (slug, email, name, amount, oc_reference, status) " "VALUES ('unknown', NULL, NULL, 10.0, NULL, 'held')") conn.commit() check("NULL oc_reference rows stay legal (index does not overblock)", True, True) conn.close() print(f"\n{'FAILED' if failures else 'OK'} — {failures} failure(s)") sys.exit(1 if failures else 0)