Files
4ec03410ff feat: add admin raffle with confetti and top-3/staff exclusion
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-15 21:39:57 +02:00

128 lines
5.8 KiB
PL/PgSQL

\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
('[email protected]', 'Top One', false),
('[email protected]', 'Top Two', false),
('[email protected]', 'Top Three', false),
('[email protected]', 'Low One', false),
('[email protected]', 'Low Two', false),
('[email protected]', 'Staff Person', false),
('[email protected]', 'Admin Person', true),
('[email protected]', 'Domain Person', false),
('[email protected]', 'Plain Viewer', false);
insert into public.registrations (event_id, contact_email, type, points) values
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'attendee', 100),
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'attendee', 90),
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'attendee', 80),
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'attendee', 10),
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'attendee', 5),
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'staff', 999),
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'attendee', 500),
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'attendee', 500);
-- Act as the raffle admin.
select set_config('request.jwt.claims', '{"email":"[email protected]","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['[email protected]','[email protected]'] 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 ('[email protected]','[email protected]') 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 ('[email protected]','[email protected]') 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 ('[email protected]','[email protected]'))
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":"[email protected]","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;