Files
3ee91b5ad6 feat(points): staff auto-connect never scores; event feedback pays +5
- Migration 20261020120000: staff (type staff / Admin / @appe.at) are
  auto-connected to every participant of the event via connections.auto
  rows inserted directly as accepted - never through accept_connection,
  so zero points by construction; backfill + idempotent helper + trigger.
- Migration 20261020130000: submit_event_feedback awards +5 on the first
  submission per participant x event (edits earn 0), gated by
  _points_eligible; response gains earned column; ACL restored.
- Admin: points move from Contacts to Registrations (active-event rows
  only, in-place save); Dashboard Top points drops emails; Dashboard and
  Activity hide auto connections.
- Attendee: leaderboard masks surnames (Max M.); EventFeedback toast
  shows +5 pts on paid submissions.
- Tests: staff_auto_connections_tests.sql (14 assertions), rpc_tests
  section 11 (4 assertions). Docs + release record 2026-10-20.
- README: document the git-ignored .env provisioning step (missing key
  = white screen).

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-10-20 22:15:29 +02:00

527 lines
23 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]');
-- 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 = '[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 ('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('[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]');
-- 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('[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 (+ 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('[email protected]');
select id into v_conn_id from public.create_connection_request('CVV-ATT-1001');
-- felix accepts.
perform set_user('[email protected]');
select shared_talk into v_shared from public.accept_connection(v_conn_id);
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 + 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.
-- [email protected] (1003) -> [email protected] (1004).
perform set_user('[email protected]');
select id into v_conn_id from public.create_connection_request('CVV-ATT-1004');
-- emma accepts.
perform set_user('[email protected]');
select shared_talk into v_shared from public.accept_connection(v_conn_id);
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);
-- +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);
-- [email protected] (1002) and [email protected] (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('[email protected]');
perform public.checkin_attendee(v_session, 'CVV-ATT-1002');
perform public.checkin_attendee(v_session, 'CVV-ATT-1005');
perform set_user('[email protected]');
select id into v_conn_id from public.create_connection_request('CVV-ATT-1005');
perform set_user('[email protected]');
perform public.accept_connection(v_conn_id);
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);
-- +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('[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;
$$;
-- =============================================================================
-- 11. submit_event_feedback: first submission +5, re-submit idempotent,
-- ineligible contacts earn nothing.
-- Self-contained fixtures (seed-independent) — everything rolls back.
-- =============================================================================
do $$
declare
v_event uuid;
v_before integer;
v_after integer;
v_earned integer;
v_count integer;
v_ok boolean;
v_msg text;
begin
v_event := (select id from public.events where is_active limit 1);
if v_event is null then
insert into test_results values ('event feedback (11)', false, 'no active event');
return;
end if;
insert into public.contacts (email, full_name) values
('[email protected]', 'Effie Probe'),
('[email protected]', 'Stella Probe');
insert into public.registrations (event_id, contact_email, type) values
(v_event, '[email protected]', 'attendee'),
(v_event, '[email protected]', 'staff');
-- First submission (attendee) -> +5.
perform set_user('[email protected]');
select points into v_before from public.registrations
where contact_email = '[email protected]' and event_id = v_event;
select earned into v_earned from public.submit_event_feedback(4, 'Well organised');
select points into v_after from public.registrations
where contact_email = '[email protected]' and event_id = v_event;
v_ok := (v_earned = 5 and v_after = v_before + 5);
v_msg := format('expected earned=5/points=%s, got earned=%s points=%s', v_before + 5, v_earned, v_after);
insert into test_results values ('event feedback first submit earns +5', v_ok, case when v_ok then null else v_msg end);
-- Edit the answer: row updates, no second award.
select earned into v_earned from public.submit_event_feedback(5, 'Minor tweaks');
select points into v_after from public.registrations
where contact_email = '[email protected]' and event_id = v_event;
v_ok := (v_earned = 0 and v_after = v_before + 5);
v_msg := format('expected earned=0/points=%s, got earned=%s points=%s', v_before + 5, v_earned, v_after);
insert into test_results values ('event feedback edit earns nothing', v_ok, case when v_ok then null else v_msg end);
-- Exactly one +5 'feedback' points_log row.
select count(*) into v_count from public.points_log
where event_id = v_event and contact_email = '[email protected]'
and category = 'feedback' and delta = 5;
v_ok := (v_count = 1);
insert into test_results values ('event feedback logs one +5 row', v_ok,
case when v_ok then null else 'expected 1 points_log row, got ' || v_count end);
-- Staff registration -> eligible check fails, earns nothing, no log row.
perform set_user('[email protected]');
select earned into v_earned from public.submit_event_feedback(5, 'Staff view');
select count(*) into v_count from public.points_log
where event_id = v_event and contact_email = '[email protected]';
v_ok := (v_earned = 0 and v_count = 0);
v_msg := format('expected earned=0/log=0, got earned=%s log=%s', v_earned, v_count);
insert into test_results values ('event feedback staff earns nothing', v_ok, case when v_ok then null else v_msg end);
exception when others then
insert into test_results values ('event feedback (11)', 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;