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]>
245 lines
19 KiB
PL/PgSQL
245 lines
19 KiB
PL/PgSQL
\set ON_ERROR_STOP on
|
|
begin;
|
|
|
|
create temporary table leaderboard_results (name text);
|
|
create function pg_temp.assert_board(p_condition boolean, p_name text) returns void
|
|
language plpgsql as $$
|
|
begin
|
|
if p_condition is distinct from true then raise exception 'FAIL: %', p_name; end if;
|
|
insert into leaderboard_results values (p_name);
|
|
end;
|
|
$$;
|
|
|
|
update public.events set is_active = false where is_active;
|
|
insert into public.events (id, name, start_date, end_date, is_active)
|
|
values ('a0150000-0000-0000-0000-000000000001', 'Leaderboard regression', current_date, current_date, true),
|
|
('a0150000-0000-0000-0000-000000000002', 'Other event', current_date, current_date, false);
|
|
insert into public.contacts (email, full_name, is_admin)
|
|
values ('[email protected]', 'Player', false), ('[email protected]', 'Peer', false),
|
|
('[email protected]', 'Other', false), ('[email protected]', 'Speaker', false),
|
|
('[email protected]', 'Staff', false), ('[email protected]', 'Admin', true),
|
|
('[email protected]', 'Domain Admin', false), ('[email protected]', 'Orphan', false);
|
|
insert into public.registrations (event_id, contact_email, type, qr_token, points, checked_in)
|
|
select 'a0150000-0000-0000-0000-000000000001'::uuid, email,
|
|
case when email = '[email protected]' then 'staff'
|
|
when email = '[email protected]' then 'speaker' else 'attendee' end::public.registration_type,
|
|
'LB-' || email, case when is_admin or email = '[email protected]' then 99 else 0 end,
|
|
true
|
|
from public.contacts where email in ('[email protected]', '[email protected]', '[email protected]',
|
|
'[email protected]', '[email protected]', '[email protected]', '[email protected]');
|
|
insert into public.registrations (event_id, contact_email, type, qr_token, points)
|
|
values ('a0150000-0000-0000-0000-000000000002', '[email protected]', 'attendee', 'LB-OTHER-EVENT', 50);
|
|
insert into public.slots (id, event_id, name, slot_date, start_time, end_time, type)
|
|
values (-151001, 'a0150000-0000-0000-0000-000000000001', 'Talk', current_date, '09:00', '10:00', 'session'),
|
|
(-151002, 'a0150000-0000-0000-0000-000000000001', 'Service', current_date, '10:00', '11:00', 'service_session');
|
|
insert into public.sessions (id, event_id, slot_id, title)
|
|
values ('a0150000-0000-0000-0000-000000000011', 'a0150000-0000-0000-0000-000000000001', -151001, 'Talk one'),
|
|
('a0150000-0000-0000-0000-000000000012', 'a0150000-0000-0000-0000-000000000001', -151001, 'Talk two'),
|
|
('a0150000-0000-0000-0000-000000000013', 'a0150000-0000-0000-0000-000000000001', -151002, 'Service');
|
|
|
|
-- A front-desk-UNCHECKED attendee: has a registration but checked_in = false,
|
|
-- so a session check-in must be blocked by the front-desk gate.
|
|
insert into public.contacts (email, full_name, is_admin)
|
|
values ('[email protected]', 'Fresh Face', false);
|
|
insert into public.registrations (event_id, contact_email, type, qr_token, points, checked_in)
|
|
values ('a0150000-0000-0000-0000-000000000001', '[email protected]', 'attendee', '[email protected]', 0, false);
|
|
|
|
do $$
|
|
declare
|
|
event_one uuid := 'a0150000-0000-0000-0000-000000000001';
|
|
talk_one uuid := 'a0150000-0000-0000-0000-000000000011';
|
|
talk_two uuid := 'a0150000-0000-0000-0000-000000000012';
|
|
excluded_email text;
|
|
feedback_result record;
|
|
welcome_result record;
|
|
begin
|
|
perform pg_temp.assert_board((select bool_and(points = 0) from public.registrations
|
|
where contact_email in ('[email protected]','[email protected]','[email protected]')), 'excluded inserts force zero');
|
|
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
|
perform public.checkin_attendee(talk_one, '[email protected]');
|
|
perform pg_temp.assert_board((select points = 10 and checked_in from public.registrations where contact_email = '[email protected]'), 'check-in awards ten');
|
|
perform pg_temp.assert_board((select already and points = 10 from public.checkin_attendee(talk_one, '[email protected]')), 'check-in idempotent');
|
|
perform public.checkin_attendee(talk_one, '[email protected]');
|
|
perform public.checkin_attendee(talk_one, '[email protected]');
|
|
perform pg_temp.assert_board((select points = 10 from public.registrations where contact_email = '[email protected]'), 'nonadmin speaker earns points');
|
|
foreach excluded_email in array array['[email protected]','[email protected]','[email protected]'] loop
|
|
perform public.checkin_attendee(talk_one, 'LB-' || excluded_email);
|
|
end loop;
|
|
begin
|
|
perform public.checkin_attendee('a0150000-0000-0000-0000-000000000013', '[email protected]');
|
|
raise exception 'service check-in accepted';
|
|
exception when raise_exception then
|
|
if sqlerrm <> 'service sessions cannot be checked in' then raise; end if;
|
|
end;
|
|
perform pg_temp.assert_board(true, 'service check-in rejected');
|
|
-- Front-desk gate: an attendee not checked in at the event cannot be scanned
|
|
-- into a session; the RPC raises a clear message.
|
|
begin
|
|
perform public.checkin_attendee(talk_one, '[email protected]');
|
|
raise exception 'front-desk gate allowed a session check-in';
|
|
exception when raise_exception then
|
|
if sqlerrm not like '%front desk%' then raise; end if;
|
|
end;
|
|
perform pg_temp.assert_board(
|
|
(select not exists (select 1 from public.checkins where contact_email = '[email protected]')),
|
|
'front-desk gate blocks session check-in');
|
|
-- The front-desk welcome check-in is a real check-in: it awards the same +10
|
|
-- as a session scan, once per person, and says whether it paid out.
|
|
select * into welcome_result from public.welcome_checkin('[email protected]');
|
|
perform pg_temp.assert_board(
|
|
welcome_result.already = false and welcome_result.points_awarded
|
|
and welcome_result.points = 10,
|
|
'welcome check-in awards ten');
|
|
perform pg_temp.assert_board(
|
|
(select points = 10 and checked_in from public.registrations where contact_email = '[email protected]'),
|
|
'welcome check-in lands on the registration');
|
|
select * into welcome_result from public.welcome_checkin('[email protected]');
|
|
perform pg_temp.assert_board(
|
|
welcome_result.already and welcome_result.points = 10,
|
|
'welcome check-in is idempotent');
|
|
-- The gate is open for that attendee now, and both awards stack.
|
|
perform public.checkin_attendee(talk_one, '[email protected]');
|
|
perform pg_temp.assert_board(
|
|
(select points = 20 from public.registrations where contact_email = '[email protected]'),
|
|
'session scan after welcome totals twenty');
|
|
-- Staff and Admin-domain contacts are welcomed too, but earn nothing and the
|
|
-- RPC must not claim otherwise (that silence is what made points look broken).
|
|
-- Clear the flag first so this exercises the award branch, not the
|
|
-- already-checked-in shortcut.
|
|
update public.registrations set checked_in = false
|
|
where contact_email in ('[email protected]','[email protected]','[email protected]');
|
|
foreach excluded_email in array array['[email protected]','[email protected]','[email protected]'] loop
|
|
select * into welcome_result from public.welcome_checkin('LB-' || excluded_email);
|
|
perform pg_temp.assert_board(
|
|
welcome_result.already = false and welcome_result.points_awarded = false
|
|
and welcome_result.points = 0,
|
|
excluded_email || ' welcome awards zero and reports it');
|
|
perform pg_temp.assert_board(
|
|
(select checked_in from public.registrations where contact_email = excluded_email),
|
|
excluded_email || ' is still marked checked in');
|
|
end loop;
|
|
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
|
foreach excluded_email in array array['[email protected]','[email protected]','[email protected]'] loop
|
|
perform set_config('request.jwt.claims', json_build_object('email', excluded_email)::text, true);
|
|
select * into feedback_result from public.submit_feedback(talk_one, 5, 'Review');
|
|
perform pg_temp.assert_board(feedback_result.earned = 0 and feedback_result.points = 0, excluded_email || ' feedback earns zero');
|
|
perform public._award_points(event_one, excluded_email, 7, 'connection');
|
|
perform public._award_points(event_one, excluded_email, 2, 'talk_networking');
|
|
perform public._award_points(event_one, excluded_email, 100, 'admin');
|
|
end loop;
|
|
perform pg_temp.assert_board(not exists (select 1 from public.points_log where contact_email in
|
|
('[email protected]','[email protected]','[email protected]')), 'no excluded award logs');
|
|
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
|
perform pg_temp.assert_board((select earned = 5 and points = 15 from public.submit_feedback(talk_one, 4, null)), 'rating awards five');
|
|
perform pg_temp.assert_board((select earned = 0 and points = 15 from public.submit_feedback(talk_one, 5, 'Updated')), 'feedback idempotent');
|
|
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
|
perform public.checkin_attendee(talk_two, '[email protected]');
|
|
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
|
perform pg_temp.assert_board((select earned = 10 and points = 35 from public.submit_feedback(talk_two, 5, 'Useful')), 'comment awards ten');
|
|
-- 20261020130000: the FIRST event feedback awards +5 to eligible attendees;
|
|
-- editing it afterwards earns nothing.
|
|
perform pg_temp.assert_board((select earned = 5 and points = 40 from public.submit_event_feedback(5, 'Good event')), 'first event feedback awards five');
|
|
perform pg_temp.assert_board((select earned = 0 and points = 40 from public.submit_event_feedback(4, 'Edited')), 'event feedback edit earns nothing');
|
|
end;
|
|
$$;
|
|
|
|
-- Clear any pairs the auto-connect rules created during the check-ins above so
|
|
-- the explicit fixture inserts below are deterministic.
|
|
delete from public.connections where event_id = 'a0150000-0000-0000-0000-000000000001';
|
|
|
|
insert into public.connections (id, event_id, from_email, to_email)
|
|
values ('a0150000-0000-0000-0000-000000000021', 'a0150000-0000-0000-0000-000000000001', '[email protected]', '[email protected]'),
|
|
('a0150000-0000-0000-0000-000000000022', 'a0150000-0000-0000-0000-000000000001', '[email protected]', '[email protected]'),
|
|
('a0150000-0000-0000-0000-000000000023', 'a0150000-0000-0000-0000-000000000001', '[email protected]', '[email protected]');
|
|
do $$
|
|
begin
|
|
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
|
perform pg_temp.assert_board((select shared_talk from public.accept_connection('a0150000-0000-0000-0000-000000000021')), 'shared talk detected');
|
|
perform pg_temp.assert_board((select points = 42 from public.registrations where contact_email = '[email protected]'), 'eligible peer earns seven with staff connection');
|
|
perform pg_temp.assert_board((select points = 0 from public.registrations where contact_email = '[email protected]'), 'staff connection earns zero');
|
|
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
|
perform public.accept_connection('a0150000-0000-0000-0000-000000000022');
|
|
perform pg_temp.assert_board((select points = 49 from public.registrations where contact_email = '[email protected]'), 'sender earns connection plus bonus');
|
|
perform pg_temp.assert_board((select points = 17 from public.registrations where contact_email = '[email protected]' and event_id = 'a0150000-0000-0000-0000-000000000001'), 'recipient earns connection plus bonus');
|
|
begin
|
|
perform public.accept_connection('a0150000-0000-0000-0000-000000000022');
|
|
raise exception 'repeated acceptance allowed';
|
|
exception when raise_exception then
|
|
if sqlerrm <> 'request already resolved' then raise; end if;
|
|
end;
|
|
perform pg_temp.assert_board(true, 'connection one-shot');
|
|
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
|
perform pg_temp.assert_board((select not shared_talk from public.accept_connection('a0150000-0000-0000-0000-000000000023')), 'no shared-talk bonus without shared check-in');
|
|
perform pg_temp.assert_board((select points = 5 from public.registrations where contact_email = '[email protected]'), 'ordinary connection earns five');
|
|
perform pg_temp.assert_board((select points = 50 from public.registrations where event_id = 'a0150000-0000-0000-0000-000000000002'), 'other event untouched');
|
|
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
|
|
perform public.adjust_points('[email protected]', -999, 'Floor test');
|
|
perform pg_temp.assert_board((select points = 0 from public.registrations where contact_email = '[email protected]'), 'points floored at zero');
|
|
perform public.adjust_points('[email protected]', 3, 'Correction');
|
|
perform pg_temp.assert_board((select points = 57 from public.registrations where contact_email = '[email protected]'), 'admin adjustment updates player');
|
|
perform public.adjust_points('[email protected]', 100, 'Must not award');
|
|
perform public.adjust_points('[email protected]', 100, 'Must not award');
|
|
perform public._award_points('a0150000-0000-0000-0000-000000000001', '[email protected]', 100, 'admin');
|
|
perform pg_temp.assert_board(not exists (select 1 from public.points_log where contact_email in
|
|
('[email protected]','[email protected]','[email protected]','[email protected]')), 'adjustments cannot award excluded or missing registrations');
|
|
perform pg_temp.assert_board(not exists (select 1 from public.leaderboard l join public.registrations r using (event_id, contact_email) where l.points <> r.points), 'leaderboard matches registration totals');
|
|
perform pg_temp.assert_board((select contact_email = '[email protected]' from public.get_leaderboard() limit 1), 'highest points rank first');
|
|
perform pg_temp.assert_board(not exists (select 1 from public.get_leaderboard() where event_id <> 'a0150000-0000-0000-0000-000000000001'), 'active event only');
|
|
end;
|
|
$$;
|
|
|
|
update public.registrations set points = 99 where contact_email = '[email protected]';
|
|
select pg_temp.assert_board((select points = 0 from public.registrations where contact_email = '[email protected]'), 'direct staff edits forced to zero');
|
|
update public.registrations set type = 'staff' where contact_email = '[email protected]';
|
|
select pg_temp.assert_board((select points = 0 from public.registrations where contact_email = '[email protected]'), 'staff promotion clears points');
|
|
select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = '[email protected]'), 'staff promotion removes ranking');
|
|
update public.contacts set is_admin = true where email = '[email protected]';
|
|
select pg_temp.assert_board((select bool_and(points = 0) from public.registrations where contact_email = '[email protected]'), 'admin promotion clears all event balances');
|
|
select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = '[email protected]'), 'admin promotion removes all rankings');
|
|
select pg_temp.assert_board(exists (select 1 from public.points_log where contact_email = '[email protected]'), 'historical audit retained');
|
|
update public.registrations set type = 'attendee', points = 7 where contact_email = '[email protected]';
|
|
update public.registrations set points = 7 where contact_email = '[email protected]';
|
|
update public.contacts set full_name = 'Tied' where email in ('[email protected]', '[email protected]');
|
|
select pg_temp.assert_board((select bool_and(full_name = 'Tied') from public.leaderboard where contact_email in ('[email protected]','[email protected]')), 'name changes refresh mirror');
|
|
select pg_temp.assert_board((select array_agg(contact_email) = array['[email protected]','[email protected]'] from public.get_leaderboard() where points = 7), 'deterministic tie order');
|
|
insert into public.leaderboard (event_id, contact_email, full_name, points)
|
|
values ('a0150000-0000-0000-0000-000000000001', '[email protected]', 'Stray staff', 999),
|
|
('a0150000-0000-0000-0000-000000000001', '[email protected]', 'Stray admin', 999),
|
|
('a0150000-0000-0000-0000-000000000001', '[email protected]', 'Stray orphan', 999);
|
|
select pg_temp.assert_board(not exists (select 1 from public.get_leaderboard() where contact_email in ('[email protected]','[email protected]','[email protected]')), 'read defense excludes stale staff admin and orphan rows');
|
|
select public._sync_leaderboard('a0150000-0000-0000-0000-000000000001', '[email protected]');
|
|
select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = '[email protected]'), 'sync removes stale ranking');
|
|
delete from public.registrations where contact_email = '[email protected]';
|
|
select pg_temp.assert_board(not exists (select 1 from public.leaderboard where contact_email = '[email protected]'), 'registration deletion removes ranking');
|
|
select pg_temp.assert_board(not has_function_privilege('authenticated','public._award_points(uuid,text,integer,text)','execute')
|
|
and not has_function_privilege('anon','public._award_points(uuid,text,integer,text)','execute'), 'API roles cannot forge awards');
|
|
select pg_temp.assert_board(not has_function_privilege('authenticated','public._sync_leaderboard(uuid,text)','execute'), 'API roles cannot call sync helper');
|
|
select pg_temp.assert_board(has_function_privilege('authenticated','public.get_leaderboard()','execute')
|
|
and not has_function_privilege('anon','public.get_leaderboard()','execute'), 'rankings require authentication');
|
|
select pg_temp.assert_board(exists (select 1 from pg_publication_tables where pubname = 'supabase_realtime' and tablename = 'leaderboard' and schemaname = 'public'), 'Realtime publication enabled');
|
|
|
|
grant insert on leaderboard_results to authenticated;
|
|
select set_config('request.jwt.claims', '{"email":"[email protected]","role":"authenticated"}', true);
|
|
set local role authenticated;
|
|
select pg_temp.assert_board(exists (select 1 from public.get_leaderboard()), 'authenticated API role can read rankings');
|
|
do $$
|
|
begin
|
|
begin
|
|
perform public._award_points('a0150000-0000-0000-0000-000000000001', '[email protected]', 9999, 'admin');
|
|
raise exception 'forged award accepted';
|
|
exception when insufficient_privilege then null;
|
|
end;
|
|
perform pg_temp.assert_board(true, 'direct helper invocation denied');
|
|
begin
|
|
perform public.adjust_points('[email protected]', 9999, 'Not admin');
|
|
raise exception 'unauthorized adjustment accepted';
|
|
exception when raise_exception then
|
|
if sqlerrm <> 'admin only' then raise; end if;
|
|
end;
|
|
perform pg_temp.assert_board(true, 'nonadmin adjustment rejected');
|
|
end;
|
|
$$;
|
|
reset role;
|
|
|
|
select count(*) as passed_assertions from leaderboard_results;
|
|
rollback; |