Files
Convivo/supabase/tests/rpc_tests.sql
T
b08a439949 Check-in gate + fixes, RPC test suite, docs
- Convivo: not-checked-in gate for non-staff (see-a-staff screen with QR
  code + logout); lifts automatically once staff checks them in
- Convivo: fix check-in 'pick a session first' race (synchronous default
  session fallback)
- Add RPC test suite (npm run test:rpc): points idempotency, role guards,
  service-session guard, speaker auto-promote, leaderboard staff exclusion
- Fix db:types script (create packages/shared/src); gitignore generated types
- Docs: README/roadmap drift fixes, tests.md, backend README link

Co-authored-by: Stephan Charles <[email protected]>
Co-authored-by: Amelia Hernandez <[email protected]>
Co-authored-by: Markus Franz <[email protected]>
Co-authored-by: Sabrina Hota <[email protected]>
Co-authored-by: Nilüfer Doğan <[email protected]>
2026-09-08 21:05:34 +02:00

364 lines
15 KiB
PL/PgSQL

-- =============================================================================
-- Convivo — RPC test suite
--
-- A self-contained, idempotent test script for the transactional points RPCs,
-- role guards, and the speaker auto-promote trigger.
--
-- HOW IT WORKS
-- * Everything runs inside a single transaction that ROLLS BACK at the end,
-- so the seed data is never mutated. Safe to run repeatedly.
-- * The "current user" is set the same way PostgREST does it:
-- set_config('request.jwt.claims', '{"email":"..."}', false)
-- which is what public.current_email() / public.is_admin() read.
-- * Each test is a DO block that catches its own exceptions and records a
-- pass/fail row into a temp table, so ALL tests always run and a summary
-- is always printed (the summary is printed BEFORE the rollback, since the
-- temp table is dropped by it). The runner (npm run test:rpc) fails the
-- process if any test failed.
--
-- RUN IT
-- npm run test:rpc (from the repo root)
-- or manually:
-- docker exec -i supabase_db_Convivo psql -U postgres \
-- < supabase/tests/rpc_tests.sql
-- =============================================================================
begin;
-- Scratch table for the pass/fail summary (transaction-scoped).
create temporary table test_results (
name text,
passed boolean,
message text
);
-- Set the "current user" for the following RPC calls (PostgREST-style JWT claim).
create or replace function set_user(p_email text)
returns void
language plpgsql as $$
begin
perform set_config('request.jwt.claims', json_build_object('email', p_email)::text, false);
end;
$$;
-- Fixtures (from the seed):
-- active event id
-- staff/admin: [email protected] (is_admin = true)
-- attendee: [email protected] (QR CVV-ATT-1000, 0 points)
-- attendee: [email protected] (QR CVV-ATT-1001, 0 points)
-- a normal session (slot 2) and a service session (slot 4).
-- =============================================================================
-- 1. checkin_attendee: awards +10, marks checked_in
-- =============================================================================
do $$
declare
v_session uuid;
v_res_points integer;
v_points integer;
v_ok boolean;
v_msg text;
begin
select id into v_session from public.sessions where slot_id = 2 limit 1;
perform set_user('[email protected]');
select points into v_res_points from public.checkin_attendee(v_session, 'CVV-ATT-1000');
select points into v_points from public.registrations
where contact_email = '[email protected]'
and event_id = (select id from public.events where is_active limit 1);
v_ok := (v_res_points = 10 and v_points = 10);
v_msg := 'expected 10 points, got ' || coalesce(v_res_points::text, 'null');
insert into test_results values ('checkin awards +10', v_ok, case when v_ok then null else v_msg end);
v_ok := exists (select 1 from public.registrations
where contact_email = '[email protected]' and checked_in);
v_msg := 'registration not marked checked_in';
insert into test_results values ('checkin marks checked_in', v_ok, case when v_ok then null else v_msg end);
exception when others then
insert into test_results values ('checkin (1)', false, 'error: ' || sqlerrm);
end;
$$;
-- =============================================================================
-- 2. checkin_attendee: idempotent (re-check-in does not double-award)
-- =============================================================================
do $$
declare
v_session uuid;
v_res_already boolean;
v_points integer;
v_ok boolean;
v_msg text;
begin
select id into v_session from public.sessions where slot_id = 2 limit 1;
perform set_user('[email protected]');
select already into v_res_already from public.checkin_attendee(v_session, 'CVV-ATT-1000');
select points into v_points from public.registrations
where contact_email = '[email protected]'
and event_id = (select id from public.events where is_active limit 1);
v_ok := v_res_already;
v_msg := 'expected already=true on second check-in';
insert into test_results values ('re-check-in flagged already', v_ok, case when v_ok then null else v_msg end);
v_ok := (v_points = 10);
v_msg := 'expected still 10 points, got ' || v_points;
insert into test_results values ('re-check-in no double-award', v_ok, case when v_ok then null else v_msg end);
exception when others then
insert into test_results values ('checkin (2)', false, 'error: ' || sqlerrm);
end;
$$;
-- =============================================================================
-- 3. checkin_attendee: service sessions cannot be checked in
-- =============================================================================
do $$
declare
v_session uuid;
v_err text;
begin
select id into v_session from public.sessions
where slot_id in (select id from public.slots where type = 'service_session')
limit 1;
perform set_user('[email protected]');
begin
perform public.checkin_attendee(v_session, 'CVV-ATT-1001');
insert into test_results values ('service session check-in rejected', false,
'expected an exception for a service session');
exception
when others then
get stacked diagnostics v_err = message_text;
insert into test_results values ('service session check-in rejected',
v_err like '%service sessions cannot be checked in%',
case when v_err like '%service sessions cannot be checked in%'
then null else 'unexpected error: ' || v_err end);
end;
exception when others then
insert into test_results values ('checkin (3)', false, 'error: ' || sqlerrm);
end;
$$;
-- =============================================================================
-- 4. checkin_attendee: role guard (non-admin rejected)
-- =============================================================================
do $$
declare
v_session uuid;
v_err text;
begin
select id into v_session from public.sessions where slot_id = 2 limit 1;
perform set_user('[email protected]'); -- an attendee, not an admin
begin
perform public.checkin_attendee(v_session, 'CVV-ATT-1001');
insert into test_results values ('check-in role guard (non-admin)', false,
'expected "staff or admin only"');
exception
when others then
get stacked diagnostics v_err = message_text;
insert into test_results values ('check-in role guard (non-admin)',
v_err = 'staff or admin only',
case when v_err = 'staff or admin only'
then null else 'unexpected error: ' || v_err end);
end;
exception when others then
insert into test_results values ('checkin (4)', false, 'error: ' || sqlerrm);
end;
$$;
-- =============================================================================
-- 5. submit_feedback: +5 rating only, idempotent on re-submit
-- =============================================================================
do $$
declare
v_session uuid;
v_res_earned integer;
v_points integer;
v_ok boolean;
v_msg text;
begin
select id into v_session from public.sessions where slot_id = 2 limit 1;
perform set_user('[email protected]');
perform public.checkin_attendee(v_session, 'CVV-ATT-1001');
-- Rating only -> +5. (felix already has +10 from the check-in above, so 15.)
perform set_user('[email protected]');
select earned into v_res_earned from public.submit_feedback(v_session, 4, null);
select points into v_points from public.registrations
where contact_email = '[email protected]'
and event_id = (select id from public.events where is_active limit 1);
v_ok := (v_res_earned = 5 and v_points = 15);
v_msg := 'expected earned=5/points=15, got earned=' || v_res_earned || ' points=' || v_points;
insert into test_results values ('feedback rating-only earns +5', v_ok, case when v_ok then null else v_msg end);
-- Re-submit (idempotent) -> no extra points (stays at 15).
select earned into v_res_earned from public.submit_feedback(v_session, 5, null);
select points into v_points from public.registrations
where contact_email = '[email protected]'
and event_id = (select id from public.events where is_active limit 1);
v_ok := (v_res_earned = 0 and v_points = 15);
v_msg := 'expected earned=0/points=15, got earned=' || v_res_earned || ' points=' || v_points;
insert into test_results values ('feedback re-submit idempotent', v_ok, case when v_ok then null else v_msg end);
exception when others then
insert into test_results values ('feedback (5)', false, 'error: ' || sqlerrm);
end;
$$;
-- =============================================================================
-- 6. submit_feedback: requires check-in
-- =============================================================================
do $$
declare
v_session uuid;
v_err text;
begin
select id into v_session from public.sessions where slot_id = 3 limit 1;
perform set_user('[email protected]'); -- not checked into slot 3
begin
perform public.submit_feedback(v_session, 4, null);
insert into test_results values ('feedback requires check-in', false,
'expected "you must be checked in"');
exception
when others then
get stacked diagnostics v_err = message_text;
insert into test_results values ('feedback requires check-in',
v_err like '%must be checked in%',
case when v_err like '%must be checked in%'
then null else 'unexpected error: ' || v_err end);
end;
exception when others then
insert into test_results values ('feedback (6)', false, 'error: ' || sqlerrm);
end;
$$;
-- =============================================================================
-- 7. accept_connection: +5 to both parties
-- =============================================================================
do $$
declare
v_conn uuid;
v_from integer;
v_to integer;
v_ok boolean;
v_msg text;
begin
-- lena -> felix (both fresh attendees).
perform set_user('[email protected]');
select id into v_conn from public.create_connection_request('CVV-ATT-1001');
-- felix accepts.
perform set_user('[email protected]');
perform public.accept_connection(v_conn);
select points into v_from from public.registrations
where contact_email = '[email protected]'
and event_id = (select id from public.events where is_active limit 1);
select points into v_to from public.registrations
where contact_email = '[email protected]'
and event_id = (select id from public.events where is_active limit 1);
-- lena had 10 from the check-in test; +5 connection = 15.
-- felix had 15 (check-in + feedback); +5 connection = 20.
v_ok := (v_from = 15 and v_to = 20);
v_msg := 'expected lena=15/felix=20, got lena=' || v_from || ' felix=' || v_to;
insert into test_results values ('connection accepted +5 each', v_ok, case when v_ok then null else v_msg end);
exception when others then
insert into test_results values ('connection (7)', false, 'error: ' || sqlerrm);
end;
$$;
-- =============================================================================
-- 8. adjust_points: admin only
-- =============================================================================
do $$
declare
v_err text;
begin
perform set_user('[email protected]'); -- not an admin
begin
perform public.adjust_points('[email protected]', 5, 'test');
insert into test_results values ('adjust_points role guard (non-admin)', false,
'expected "admin only"');
exception
when others then
get stacked diagnostics v_err = message_text;
insert into test_results values ('adjust_points role guard (non-admin)',
v_err = 'admin only',
case when v_err = 'admin only'
then null else 'unexpected error: ' || v_err end);
end;
exception when others then
insert into test_results values ('adjust_points (8)', false, 'error: ' || sqlerrm);
end;
$$;
-- =============================================================================
-- 9. Speaker auto-promote trigger (attendee -> speaker, never overrides staff)
-- =============================================================================
do $$
declare
v_session uuid;
v_type public.registration_type;
v_ok boolean;
v_msg text;
begin
select id into v_session from public.sessions where slot_id = 2 limit 1;
-- lena is an attendee; adding her as a speaker should promote her.
insert into public.session_speakers (session_id, contact_email)
values (v_session, '[email protected]');
select "type" into v_type from public.registrations
where contact_email = '[email protected]'
and event_id = (select id from public.events where is_active limit 1);
v_ok := (v_type = 'speaker');
v_msg := 'expected type=speaker, got ' || coalesce(v_type::text, 'null');
insert into test_results values ('speaker auto-promote (attendee->speaker)', v_ok, case when v_ok then null else v_msg end);
-- A staff registration must NOT be overridden by the promote trigger.
insert into public.session_speakers (session_id, contact_email)
values (v_session, '[email protected]');
select "type" into v_type from public.registrations
where contact_email = '[email protected]'
and event_id = (select id from public.events where is_active limit 1);
v_ok := (v_type = 'staff');
v_msg := 'expected type=staff, got ' || coalesce(v_type::text, 'null');
insert into test_results values ('speaker auto-promote never overrides staff', v_ok, case when v_ok then null else v_msg end);
exception when others then
insert into test_results values ('speaker promote (9)', false, 'error: ' || sqlerrm);
end;
$$;
-- =============================================================================
-- 10. Leaderboard excludes staff
-- =============================================================================
do $$
declare
v_count integer;
v_ok boolean;
v_msg text;
begin
-- Give amelia (staff) some points, then confirm she is not on the board.
perform set_user('[email protected]');
perform public.adjust_points('[email protected]', 50, 'test');
select count(*) into v_count from public.leaderboard
where contact_email = '[email protected]'
and event_id = (select id from public.events where is_active limit 1);
v_ok := (v_count = 0);
v_msg := 'expected 0 staff rows on leaderboard, got ' || v_count;
insert into test_results values ('leaderboard excludes staff', v_ok, case when v_ok then null else v_msg end);
exception when others then
insert into test_results values ('leaderboard (10)', false, 'error: ' || sqlerrm);
end;
$$;
-- =============================================================================
-- Summary (printed BEFORE the rollback, since the temp table is dropped by it)
-- =============================================================================
select name, passed, message
from test_results
order by name;
select
count(*) as total,
count(*) filter (where passed) as passed,
count(*) filter (where not passed) as failed
from test_results;
-- Roll back so the seed is never mutated.
rollback;