Files
Convivo/docs/backend/tests.md
T

97 lines
4.8 KiB
Markdown

# Backend — RPC Test Suite
A self-contained, idempotent test suite for the transactional points RPCs, role
guards, and the speaker auto-promote trigger. It runs directly against the local
Supabase Postgres container.
## Run it
```bash
npm run db:start # the Supabase core must be running
npm run test:rpc # runs supabase/tests/rpc_tests.sql, exits non-zero on failure
```
Or manually:
```bash
docker exec -i supabase_db_Convivo psql -U postgres < supabase/tests/rpc_tests.sql
```
## How it works
- **No seed mutation.** Everything runs in a single transaction that **rolls
back** at the end, so the seed data is never changed. Safe to run repeatedly.
- **Current user = PostgREST-style JWT claim.** Each test sets the user the same
way PostgREST does:
`set_config('request.jwt.claims', '{"email":"..."}', false)`, which is what
[`public.current_email()`](./rpcs.md) and `public.is_admin()` read.
- **Every test always runs.** Each test is a `DO` block that catches its own
exceptions and records a pass/fail row into a temp table, so one failure never
aborts the rest. A summary is printed before the rollback.
- **The runner fails the process** (`npm run test:rpc`) if any test failed, so it
can be wired into CI.
## What it covers
| Area | Tests |
|---|---|
| `checkin_attendee` | awards +10, marks `checked_in`, idempotent re-check-in, service-session guard, non-admin role guard |
| `submit_feedback` | +5 rating-only, idempotent re-submit, requires check-in |
| `accept_connection` | +5 to both parties |
| `adjust_points` | admin-only role guard |
| Speaker auto-promote trigger | attendee → speaker, never overrides staff |
| Leaderboard | excludes staff |
## Speaker Portal
[speaker_portal_tests.sql](../../supabase/tests/speaker_portal_tests.sql) is an
independent fail-fast suite (`ON_ERROR_STOP`) with generated contacts/events/talks,
temporary snapshots and a single transaction ending in `ROLLBACK`. It needs the
[speaker portal migration](../../supabase/migrations/20261006175631_speaker_portal.sql)
and [staff overlap migration](../../supabase/migrations/20261007153957_speaker_portal_staff_overlap.sql)
on the **local** database. Apply without resetting existing data:
```bash
docker exec -i supabase_db_Convivo psql -U postgres -d postgres -v ON_ERROR_STOP=1 --single-transaction < supabase/migrations/20261006175631_speaker_portal.sql
docker exec -i supabase_db_Convivo psql -U postgres -d postgres -v ON_ERROR_STOP=1 --single-transaction < supabase/migrations/20261007153957_speaker_portal_staff_overlap.sql
docker exec -i supabase_db_Convivo psql -U postgres -d postgres -v ON_ERROR_STOP=1 < supabase/tests/speaker_portal_tests.sql
```
This direct migration application reloads the local API schema but does not
write CLI migration history. It is not a production deployment command. No
database reset is needed; fixture sequence state is not advanced.
The suite checks assigned speaker, non-admin staff and Admin staff access,
unassigned staff, mismatched same-event registrations, attendee assignments,
unregistered assignments, automatic service-session exclusion, active/inactive past/future
events, malformed and cross-session paths, required uploaded objects, Storage
INSERT/UPDATE/DELETE (including UPDATE WITH CHECK) and unchanged registration,
points-log, check-in and leaderboard state. Storage policy checks use the
`authenticated` database role, not a superuser that bypasses RLS. A failed
assertion exits non-zero and connection closure rolls back the transaction.
[speaker-portal.spec.ts](../../e2e/tests/speaker-portal.spec.ts) uses mocked Auth,
REST and Storage responses, without real credentials or mail delivery:
```bash
cd e2e
CONVIVO_BASE_URL=http://127.0.0.1:3000 npx playwright test tests/speaker-portal.spec.ts --project=chromium --reporter=list --output=/tmp/convivo-speaker-results
```
Start Convivo's dev server first. Coverage includes both routes, existing-users-only
link requests, URL scrubbing, callback errors/rejections, StrictMode single-use
implicit/PKCE callbacks, local logout, empty access, upload/replace/remove across
desktop/mobile, failed attachment cleanup and the normal app's check-in gate
after portal login. Mocked tests do not validate live mail delivery, deployment
redirect allowlists or real Storage behavior; the SQL suite validates database
authorization separately. See [screen contract](../frontend/screens.md#speaker-portal),
[API client](../frontend/api-client.md#speaker-portal), [RLS](./rls.md#speaker-portal-authorization)
and [RPCs](./rpcs.md#speaker-portal-rpcs).
## Files
- [`supabase/tests/rpc_tests.sql`](../../supabase/tests/rpc_tests.sql) — the test
script (fixtures + assertions + summary).
- [`supabase/tests/run-rpc-tests.js`](../../supabase/tests/run-rpc-tests.js) — the
runner (executes the script, parses the summary, sets the exit code).