Write unit tests for dual-attribution data model
epic-coordinator-proxy-registration-foundation-task-009 — Write unit and integration tests for ProxyActivityRepository covering: correct persistence of coordinator_id and attributed_mentor_id as separate fields, rejection of records with missing attribution fields, RLS policy enforcement preventing cross-org data access, bulk insert correctness for all mentor entries in a batch. Use flutter_test and Supabase test helpers or mock clients. Verify audit log entries are created on each insert.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 4 - 323 tasks
Can start after Tier 3 completes
Implementation Notes
Define an abstract IProxyAuditLogger interface so ProxyActivityRepository depends on the interface, not the concrete class — this enables clean mocking via mocktail's Mock
which are fully mocked. Prioritize fully mocked tests for CI reliability. The audit-log-not-written-on-failure test is the most important edge case: wrap the Supabase insert in a try/catch in ProxyActivityRepository and only call the logger in the success path.
Testing Requirements
Use flutter_test. Mock the Supabase client using a mock class implementing the SupabaseClient interface (or use a package like mocktail). Mock ProxyAuditLogger via an interface so call counts can be asserted. Group tests into: (1) single insert - attribution fields, (2) single insert - validation rejections, (3) bulk insert - correctness, (4) RLS cross-org isolation, (5) audit log integration (verifying logger is called correctly).
Use setUp/tearDown to reset mock state between tests. Add a test for the case where Supabase throws a network error mid-insert to confirm no audit entry is written.
Supabase RLS policies for org-scoped proxy access may be difficult to express correctly, especially for coordinators with multi-chapter access. An overly permissive policy could allow cross-org proxy registrations, corrupting Bufdir reporting; an overly restrictive policy could block legitimate coordinators from registering.
Mitigation & Contingency
Mitigation: Write integration tests covering all access boundary cases (same org, cross-org, multi-chapter coordinator) before merging any RLS migration. Use parameterised RLS test helpers already established by the auth feature.
Contingency: If RLS proves insufficient, add a server-side Edge Function validation layer that re-checks org membership before persisting any proxy record, providing defence in depth.
Adding new tables and foreign key constraints to an existing production Supabase database risks migration failures or locking issues if the database already contains active sessions during deployment.
Mitigation & Contingency
Mitigation: Use additive-only migrations (no DROP or ALTER on existing tables). Test full migration sequence in a staging Supabase project before production deployment. Schedule during low-traffic window.
Contingency: Maintain a rollback migration script. If the migration fails, the feature remains unreachable behind a feature flag while the schema issue is resolved.
Audit log entries must be immutable for compliance, but Supabase RLS by default allows row owners to update their own rows. If audit records are accidentally mutable, dispute resolution and accountability guarantees are invalidated.
Mitigation & Contingency
Mitigation: Configure the proxy_audit_log table with an RLS policy that allows INSERT for coordinators but denies UPDATE and DELETE for all roles including service_role, enforced at the database level.
Contingency: If RLS cannot fully prevent updates, create a database trigger that reverts any UPDATE to the audit table and logs the attempt as a security event.