-- ============================================================================= -- 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: amelia@appe.at (is_admin = true) -- attendee: lena.huber@gmail.com (QR CVV-ATT-1000, 0 points) -- attendee: felix.berger@outlook.com (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('amelia@appe.at'); -- Front-desk (welcome) check-in first: a session check-in now requires the -- attendee to be checked in to the event. perform public.welcome_checkin('CVV-ATT-1000'); 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 = 'lena.huber@gmail.com' 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 = 'lena.huber@gmail.com' and checked_in); v_msg := 'registration not marked checked_in'; insert into test_results values ('front-desk check-in 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('amelia@appe.at'); 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 = 'lena.huber@gmail.com' 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('amelia@appe.at'); -- Front-desk check-in first so the service-session guard (not the front-desk -- gate) is what rejects this scan. perform public.welcome_checkin('CVV-ATT-1001'); 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('lena.huber@gmail.com'); -- 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('amelia@appe.at'); 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('felix.berger@outlook.com'); 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 = 'felix.berger@outlook.com' 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 = 'felix.berger@outlook.com' 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('felix.berger@outlook.com'); -- 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 (+ talk networking bonus, since -- lena and felix were both checked into the slot-2 session above) -- ============================================================================= do $$ declare v_conn_id uuid; v_from integer; v_to integer; v_shared boolean; v_ok boolean; v_msg text; begin -- lena -> felix (both were checked into the slot-2 session). perform set_user('lena.huber@gmail.com'); select id into v_conn_id from public.create_connection_request('CVV-ATT-1001'); -- felix accepts. perform set_user('felix.berger@outlook.com'); select shared_talk into v_shared from public.accept_connection(v_conn_id); select points into v_from from public.registrations where contact_email = 'lena.huber@gmail.com' and event_id = (select id from public.events where is_active limit 1); select points into v_to from public.registrations where contact_email = 'felix.berger@outlook.com' and event_id = (select id from public.events where is_active limit 1); -- lena had 10 from the check-in test; +5 connection + 2 talk bonus = 17. -- felix had 15 (check-in + feedback); +5 connection + 2 talk bonus = 22. v_ok := (v_from = 17 and v_to = 22); v_msg := 'expected lena=17/felix=22, got lena=' || v_from || ' felix=' || v_to; insert into test_results values ('connection accepted +5 each (+ talk bonus)', v_ok, case when v_ok then null else v_msg end); v_ok := v_shared; v_msg := 'expected shared_talk=true (both checked into slot 2)'; insert into test_results values ('accept_connection shared_talk flag', 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; $$; -- ============================================================================= -- 7b. accept_connection: NO talk bonus when the parties share no check-in -- ============================================================================= do $$ declare v_conn_id uuid; v_from integer; v_to integer; v_shared boolean; v_ok boolean; v_msg text; begin -- Two attendees who were never checked in: no shared talk. -- jonas.unger@web.de (1003) -> emma.dorfer@gmx.at (1004). perform set_user('jonas.unger@web.de'); select id into v_conn_id from public.create_connection_request('CVV-ATT-1004'); -- emma accepts. perform set_user('emma.dorfer@gmx.at'); select shared_talk into v_shared from public.accept_connection(v_conn_id); select points into v_from from public.registrations where contact_email = 'jonas.unger@web.de' and event_id = (select id from public.events where is_active limit 1); select points into v_to from public.registrations where contact_email = 'emma.dorfer@gmx.at' and event_id = (select id from public.events where is_active limit 1); -- +5 connection each, NO talk bonus (no shared check-in). v_ok := (v_from = 5 and v_to = 5); v_msg := 'expected jonas=5/emma=5 (no bonus), got jonas=' || v_from || ' emma=' || v_to; insert into test_results values ('no talk bonus without shared check-in', v_ok, case when v_ok then null else v_msg end); v_ok := (v_shared = false); v_msg := 'expected shared_talk=false'; insert into test_results values ('shared_talk=false without shared check-in', v_ok, case when v_ok then null else v_msg end); exception when others then insert into test_results values ('connection (7b)', false, 'error: ' || sqlerrm); end; $$; -- ============================================================================= -- 7c. accept_connection: talk bonus disabled when talk_networking_bonus = 0 -- ============================================================================= do $$ declare v_session uuid; v_conn_id uuid; v_from integer; v_to integer; v_ok boolean; v_msg text; begin -- Disable the bonus for the active event. update public.events set talk_networking_bonus = 0 where id = (select id from public.events where is_active limit 1); -- mia.moser@proton.me (1002) and paul.klein@icloud.com (1005): check both -- into the slot-2 session so they share a check-in, then connect. select id into v_session from public.sessions where slot_id = 2 limit 1; perform set_user('amelia@appe.at'); perform public.checkin_attendee(v_session, 'CVV-ATT-1002'); perform public.checkin_attendee(v_session, 'CVV-ATT-1005'); perform set_user('mia.moser@proton.me'); select id into v_conn_id from public.create_connection_request('CVV-ATT-1005'); perform set_user('paul.klein@icloud.com'); perform public.accept_connection(v_conn_id); select points into v_from from public.registrations where contact_email = 'mia.moser@proton.me' and event_id = (select id from public.events where is_active limit 1); select points into v_to from public.registrations where contact_email = 'paul.klein@icloud.com' and event_id = (select id from public.events where is_active limit 1); -- +10 check-in + 5 connection each, NO bonus (disabled). v_ok := (v_from = 15 and v_to = 15); v_msg := 'expected mia=15/paul=15 (bonus disabled), got mia=' || v_from || ' paul=' || v_to; insert into test_results values ('talk bonus disabled when bonus=0', v_ok, case when v_ok then null else v_msg end); exception when others then insert into test_results values ('connection (7c)', false, 'error: ' || sqlerrm); end; $$; -- ============================================================================= -- 8. adjust_points: admin only -- ============================================================================= do $$ declare v_err text; begin perform set_user('lena.huber@gmail.com'); -- not an admin begin perform public.adjust_points('lena.huber@gmail.com', 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, 'lena.huber@gmail.com'); select "type" into v_type from public.registrations where contact_email = 'lena.huber@gmail.com' 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, 'amelia@appe.at'); select "type" into v_type from public.registrations where contact_email = 'amelia@appe.at' 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('amelia@appe.at'); perform public.adjust_points('amelia@appe.at', 50, 'test'); select count(*) into v_count from public.leaderboard where contact_email = 'amelia@appe.at' 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;