Write integration tests for RLS tenant scope configurator
epic-organization-selection-and-onboarding-foundation-task-005 — Write integration tests verifying that after SupabaseRLSTenantConfigurator is invoked, subsequent Supabase queries are scoped to the selected organization and do not return records from other tenants. Use a test Supabase project with seeded multi-tenant data. Validate GDPR non-leakage between orgs.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 3 - 413 tasks
Can start after Tier 2 completes
Implementation Notes
The test Supabase project should mirror production RLS policies exactly — use a migration script or snapshot to keep them in sync. Seed data via the Supabase service-role key (bypasses RLS) to set up cross-tenant records; then switch to user-scoped JWT tokens to execute test queries. Use Dart's `setUpAll`/`tearDownAll` for seed/cleanup and individual `setUp` to reset session state. To test scope switching, call `SupabaseRLSTenantConfigurator.configure(orgId)` then assert, then call again with a different orgId and re-assert — do not rely on a single configurator call per test file.
Consider extracting a `TestSupabaseFixture` helper class that encapsulates seeding, JWT generation, and teardown to keep individual tests concise. GDPR non-leakage assertions should use `expect(result.any((r) => r.orgId != expectedOrgId), isFalse)` pattern across all returned collections.
Testing Requirements
Integration tests only — no unit mocks for the Supabase client in this suite. Use a real test Supabase project (separate from staging/production) with deterministic seed data covering at least 3 organizations. Seed script must be idempotent (re-runnable without duplicates). Cover: (1) single-org scope after configurator invocation, (2) scope switch between orgs, (3) explicit cross-tenant ID lookup returning null/error, (4) all primary tables verified for RLS enforcement (activities, contacts, user_roles, user_stories, org_feature_flags).
Each test must independently set up its JWT/session state — no shared state between tests. Run the suite in CI on every PR touching authentication, RLS configuration, or database access layers.
iOS Keychain and Android Keystore have meaningfully different failure modes and permission models. The secure storage plugin may throw platform-specific exceptions (e.g., biometric enrollment required, Keystore wipe after device re-enrolment) that crash higher-level flows if not caught at the adapter boundary.
Mitigation & Contingency
Mitigation: Wrap all storage plugin calls in try/catch at the adapter layer and expose a typed StorageResult<T> instead of throwing. Write integration tests on real device simulators for both platforms in CI using Fastlane. Document the exception matrix during spike.
Contingency: If a platform-specific failure cannot be handled gracefully, fall back to in-memory-only storage for the current session and surface a non-blocking warning to the user; log the event for investigation.
Setting a session-level Postgres variable (app.current_org_id) via a Supabase RPC requires that RLS policies on every table reference this variable. If the Supabase project schema has not yet defined these policies, the configurator will set the variable but queries will return unfiltered data, giving a false sense of security.
Mitigation & Contingency
Mitigation: Include a smoke-test RPC in the SupabaseRLSTenantConfigurator that verifies the variable is readable from a policy-scoped query before marking setup as complete. Coordinate with the database migration task to ensure RLS policies reference app.current_org_id before the configurator is shipped.
Contingency: If RLS policies are not in place at integration time, gate all data-fetching components behind a runtime check in SupabaseRLSTenantConfigurator.isRlsScopeVerified(); block data access and surface a developer warning until policies are confirmed.
Fetching feature flags from Supabase on every cold start adds network latency before the first branded screen renders. On slow connections this may cause a perceptible blank-screen gap or cause the app to render with default (unflagged) state before flags arrive.
Mitigation & Contingency
Mitigation: Persist the last-known flag set to disk in the FeatureFlagProvider and serve stale-while-revalidate on startup. Gate flag refresh behind a configurable TTL (default 15 minutes) so network calls are not made on every launch.
Contingency: If stale flags cause a feature to appear that should be hidden, add a post-load re-evaluation pass that reconciles the live flag set with the rendered widget tree and triggers a targeted rebuild where needed.