\set ON_ERROR_STOP on begin; create temporary table badge_results (name text); grant insert on badge_results to authenticated, anon; create function pg_temp.assert_badge(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 badge_results values (p_name); end; $$; -- Two isolated events so per-event scoping can be checked. update public.events set is_active = false where is_active; insert into public.events (id, name, start_date, end_date, is_active) values ('b0de0001-0000-0000-0000-000000000001', 'Badge event A', current_date, current_date, true), ('b0de0002-0000-0000-0000-000000000002', 'Badge event B', current_date, current_date, false); insert into public.contacts (email, full_name, is_admin) values ('bd-admin@example.test', 'Badge Admin', true), ('bd-viewer@example.test', 'Plain Viewer', false); -- Act as the badge admin. select set_config('request.jwt.claims', '{"email":"bd-admin@example.test","role":"authenticated"}', true); -- Save a default layout for event A. select pg_temp.assert_badge( (select is_default from public.save_badge_layout( 'b0de0001-0000-0000-0000-000000000001'::uuid, 'Default', 100, 70, '[{"id":"a","type":"text","x":10,"y":10,"width":60,"height":10,"rotation":0,"content":"attendee_name","text":"","font":"Open Sans","font_size":13,"line_height":1,"color":"#000000","bold":false,"italic":false,"align_h":"left","align_v":"middle","transform":"none"}]'::jsonb, true)), 'save creates a default layout'); -- Upsert the same (event, name) updates in place rather than duplicating. select public.save_badge_layout('b0de0001-0000-0000-0000-000000000001'::uuid, 'Default', 105, 75, '[]'::jsonb, true); select pg_temp.assert_badge( (select count(*) = 1 from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'Default'), 're-saving the same name upserts'); select pg_temp.assert_badge( (select page_width_mm = 105 from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'Default'), 'upsert updates page size'); -- A second default layout demotes the first (only one default per event). select public.save_badge_layout('b0de0001-0000-0000-0000-000000000001'::uuid, 'VIP', 90, 50, '[]'::jsonb, true); select pg_temp.assert_badge( (select count(*) = 1 from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and is_default), 'only one default per event after saving a new default'); select pg_temp.assert_badge( (select is_default from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'VIP'), 'newest default is the VIP layout'); -- Layouts are scoped per event: event B has none of its own. select public.save_badge_layout('b0de0002-0000-0000-0000-000000000002'::uuid, 'Default', 100, 70, '[]'::jsonb, true); select pg_temp.assert_badge( (select count(*) = 1 from public.badge_layouts where event_id = 'b0de0002-0000-0000-0000-000000000002'), 'event B has its own layout'); select pg_temp.assert_badge( (select count(*) = 2 from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001'), 'event A still has two layouts'); -- set_default_badge_layout promotes the target and demotes the rest. select public.set_default_badge_layout( (select id from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'Default')); select pg_temp.assert_badge( (select is_default from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'Default'), 'set_default promotes the chosen layout'); select pg_temp.assert_badge( (select not is_default from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'VIP'), 'set_default demotes the previous default'); -- The default layout cannot be deleted while other layouts exist. do $$ begin begin perform public.delete_badge_layout( (select id from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'Default')); raise exception 'default layout delete allowed'; exception when raise_exception then if sqlerrm not like 'cannot delete the default layout%' then raise; end if; end; perform pg_temp.assert_badge(true, 'cannot delete default while others exist'); end; $$; -- A non-default layout deletes cleanly. select public.delete_badge_layout( (select id from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'VIP')); select pg_temp.assert_badge( (select count(*) = 1 from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001'), 'non-default layout deletes'); -- Empty / blank names are rejected. do $$ begin begin perform public.save_badge_layout('b0de0001-0000-0000-0000-000000000001'::uuid, ' ', 100, 70, '[]'::jsonb, false); raise exception 'blank name allowed'; exception when raise_exception then if sqlerrm <> 'layout name is required' then raise; end if; end; perform pg_temp.assert_badge(true, 'blank layout name rejected'); end; $$; -- Non-admins cannot write. select set_config('request.jwt.claims', '{"email":"bd-viewer@example.test","role":"authenticated"}', true); do $$ begin begin perform public.save_badge_layout('b0de0001-0000-0000-0000-000000000001'::uuid, 'Hacked', 100, 70, '[]'::jsonb, false); raise exception 'non-admin save allowed'; exception when insufficient_privilege then if sqlerrm <> 'admin only' then raise; end if; end; begin perform public.delete_badge_layout( (select id from public.badge_layouts limit 1)); raise exception 'non-admin delete allowed'; exception when insufficient_privilege then if sqlerrm <> 'admin only' then raise; end if; end; perform pg_temp.assert_badge(true, 'non-admin cannot save or delete'); end; $$; -- Non-admins cannot read layouts either (admin-read RLS). Run as the real -- `authenticated` role so RLS is enforced (the superuser would bypass it). set local role authenticated; select set_config('request.jwt.claims', '{"email":"bd-viewer@example.test","role":"authenticated"}', true); select pg_temp.assert_badge( (select count(*) = 0 from public.badge_layouts), 'non-admin sees no layouts via RLS'); reset role; -- 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.save_badge_layout('b0de0001-0000-0000-0000-000000000001'::uuid, 'Anon', 100, 70, '[]'::jsonb, false); raise exception 'anon save allowed'; exception when insufficient_privilege then null; end; perform pg_temp.assert_badge(true, 'anon cannot execute badge functions'); end; $$; reset role; select count(*) as passed_assertions from badge_results; rollback;