\set ON_ERROR_STOP on begin; create temporary table raffle_results (name text); grant insert on raffle_results to authenticated, anon; create function pg_temp.assert_raffle(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 raffle_results values (p_name); end; $$; -- Isolate a single active event for the run. update public.events set is_active = false where is_active; insert into public.events (id, name, start_date, end_date, is_active) values ('b0ff1e00-0000-0000-0000-000000000001', 'Raffle regression', current_date, current_date, true); insert into public.contacts (email, full_name, is_admin) values ('rf-top1@example.test', 'Top One', false), ('rf-top2@example.test', 'Top Two', false), ('rf-top3@example.test', 'Top Three', false), ('rf-low1@example.test', 'Low One', false), ('rf-low2@example.test', 'Low Two', false), ('rf-staff@example.test', 'Staff Person', false), ('rf-admin@example.test', 'Admin Person', true), ('rf-domain@appe.at', 'Domain Person', false), ('rf-viewer@example.test', 'Plain Viewer', false); insert into public.registrations (event_id, contact_email, type, points) values ('b0ff1e00-0000-0000-0000-000000000001', 'rf-top1@example.test', 'attendee', 100), ('b0ff1e00-0000-0000-0000-000000000001', 'rf-top2@example.test', 'attendee', 90), ('b0ff1e00-0000-0000-0000-000000000001', 'rf-top3@example.test', 'attendee', 80), ('b0ff1e00-0000-0000-0000-000000000001', 'rf-low1@example.test', 'attendee', 10), ('b0ff1e00-0000-0000-0000-000000000001', 'rf-low2@example.test', 'attendee', 5), ('b0ff1e00-0000-0000-0000-000000000001', 'rf-staff@example.test', 'staff', 999), ('b0ff1e00-0000-0000-0000-000000000001', 'rf-admin@example.test', 'attendee', 500), ('b0ff1e00-0000-0000-0000-000000000001', 'rf-domain@appe.at', 'attendee', 500); -- Act as the raffle admin. select set_config('request.jwt.claims', '{"email":"rf-admin@example.test","role":"authenticated"}', true); -- The pool excludes staff, Admin, the @appe.at domain and the leaderboard top 3, -- leaving exactly the two low-point eligible attendees. select pg_temp.assert_raffle( (select array_agg(contact_email order by contact_email) = array['rf-low1@example.test','rf-low2@example.test'] from public.get_raffle_pool()), 'pool excludes staff, admin, domain and top 3'); -- Draw one: winner is an eligible non-top attendee, recorded with draw_number 1. create temporary table draw1 as select * from public.draw_raffle_winner(); select pg_temp.assert_raffle( (select contact_email in ('rf-low1@example.test','rf-low2@example.test') from draw1), 'first draw returns an eligible attendee'); select pg_temp.assert_raffle((select draw_number = 1 from draw1), 'first draw number is one'); select pg_temp.assert_raffle( (select count(*) = 1 from public.get_raffle_pool()), 'drawn attendee leaves the pool'); -- Draw two: the remaining eligible attendee, draw_number 2, pool now empty. create temporary table draw2 as select * from public.draw_raffle_winner(); select pg_temp.assert_raffle( (select contact_email <> (select contact_email from draw1) and contact_email in ('rf-low1@example.test','rf-low2@example.test') from draw2), 'second draw is a different eligible attendee'); select pg_temp.assert_raffle((select draw_number = 2 from draw2), 'second draw number is two'); select pg_temp.assert_raffle((select count(*) = 0 from public.get_raffle_pool()), 'pool empty after all eligible drawn'); -- A third draw with no eligible attendees left fails clearly. do $$ begin begin perform public.draw_raffle_winner(); raise exception 'over-draw allowed'; exception when raise_exception then if sqlerrm <> 'No eligible attendees left to draw' then raise; end if; end; perform pg_temp.assert_raffle(true, 'over-draw rejected'); end; $$; -- Winners persisted, none excluded, sequential numbers. select pg_temp.assert_raffle( (select count(*) = 2 and bool_and(contact_email in ('rf-low1@example.test','rf-low2@example.test')) and count(distinct draw_number) = 2 from public.raffle_draws), 'winners persisted without excluded attendees'); -- Reset clears the run so the same attendees become eligible again. select public.reset_raffle(); select pg_temp.assert_raffle( (select count(*) = 0 from public.raffle_draws), 'reset clears winners'); select pg_temp.assert_raffle( (select count(*) = 2 from public.get_raffle_pool()), 'reset restores the pool'); -- Non-admin authenticated users cannot see the pool or draw. select set_config('request.jwt.claims', '{"email":"rf-viewer@example.test","role":"authenticated"}', true); select pg_temp.assert_raffle((select count(*) = 0 from public.get_raffle_pool()), 'non-admin sees empty pool'); select pg_temp.assert_raffle((select count(*) = 0 from public.raffle_draws), 'non-admin cannot read winners'); do $$ begin begin perform public.draw_raffle_winner(); raise exception 'non-admin draw allowed'; exception when raise_exception then if sqlerrm <> 'admin only' then raise; end if; end; begin perform public.reset_raffle(); raise exception 'non-admin reset allowed'; exception when raise_exception then if sqlerrm <> 'admin only' then raise; end if; end; perform pg_temp.assert_raffle(true, 'non-admin cannot draw or reset'); end; $$; -- The anonymous API role has no execute grant at all. reset role; select set_config('request.jwt.claims', '{"role":"anon"}', true); set local role anon; do $$ begin begin perform public.draw_raffle_winner(); raise exception 'anon draw allowed'; exception when insufficient_privilege then null; end; perform pg_temp.assert_raffle(true, 'anon cannot execute raffle functions'); end; $$; reset role; select count(*) as passed_assertions from raffle_results; rollback;