\set ON_ERROR_STOP on begin; set local storage.allow_delete_query = 'true'; create function pg_temp.assert_speaker(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; raise notice 'PASS: %', p_name; end; $$; create function pg_temp.expect_speaker_denied(p_statement text, p_name text) returns void language plpgsql as $$ begin begin execute p_statement; exception when insufficient_privilege then raise notice 'PASS: %', p_name; return; end; raise exception 'FAIL: % was allowed', p_name; end; $$; create temporary table speaker_fixture as select kind, gen_random_uuid() as event_id, gen_random_uuid() as session_id, (select least(coalesce(min(id), 0), 0) from public.slots) - row_number() over ()::integer as slot_id, case when kind like '%future' then current_date + 365 else current_date - 365 end as event_date from unnest(array['past', 'future', 'active', 'other', 'service', 'attendee', 'unregistered', 'staff_past', 'staff_future', 'staff_active', 'unassigned_staff', 'wrong_event_staff', 'admin_past', 'admin_future', 'admin_active', 'admin_service']) as kind; create temporary table speaker_identity as select 'portal-owner-' || gen_random_uuid() || '@example.invalid' as owner_email, 'portal-other-' || gen_random_uuid() || '@example.invalid' as other_email, 'portal-admin-' || gen_random_uuid() || '@appe.at' as admin_email; grant select on speaker_fixture, speaker_identity to authenticated; insert into public.contacts(email, full_name) select owner_email, 'Portal Owner' from speaker_identity union all select other_email, 'Other Speaker' from speaker_identity union all select admin_email, 'Portal Admin' from speaker_identity; insert into public.events(id, name, start_date, end_date, is_active) select event_id, 'Portal regression ' || kind, event_date, event_date, false from speaker_fixture; insert into public.slots(id, event_id, name, type, slot_date, start_time, end_time) select slot_id, event_id, 'Portal slot', (case when kind in ('service', 'admin_service') then 'service_session' else 'session' end)::public.slot_type, event_date, '09:00', '10:00' from speaker_fixture; insert into public.sessions(id, event_id, slot_id, title) select session_id, event_id, slot_id, 'Portal talk ' || kind from speaker_fixture; insert into public.registrations(event_id, contact_email, type, points, checked_in) select event_id, case when kind in ('other', 'wrong_event_staff') then other_email when kind like 'admin_%' then admin_email else owner_email end, (case when kind like 'staff_%' or kind in ('service', 'unassigned_staff', 'wrong_event_staff') then 'staff' else 'speaker' end)::public.registration_type, 37, false from speaker_fixture cross join speaker_identity where kind <> 'unregistered'; insert into public.session_speakers(session_id, contact_email) select session_id, case when kind in ('other', 'unassigned_staff') then other_email when kind like 'admin_%' then admin_email else owner_email end from speaker_fixture cross join speaker_identity where kind not in ('service', 'admin_service'); select public._assign_service_session_speakers(session_id, event_id) from speaker_fixture where kind in ('service', 'admin_service'); update public.registrations set type = 'attendee' where event_id = (select event_id from speaker_fixture where kind = 'attendee'); update public.events set is_active = false where is_active; update public.events set is_active = true where id = (select event_id from speaker_fixture where kind = 'active'); create temporary table speaker_registration_before as select registration.* from public.registrations registration join speaker_fixture using (event_id); create temporary table speaker_points_before as select * from public.points_log; create temporary table speaker_checkins_before as select * from public.checkins; create temporary table speaker_leaderboard_before as select * from public.leaderboard; select pg_temp.assert_speaker(not has_function_privilege('anon', 'public.get_my_speaker_sessions()', 'execute'), 'anonymous portal RPC revoked'); select pg_temp.assert_speaker(not has_function_privilege('anon', 'public.set_session_material(uuid,text)', 'execute'), 'anonymous material RPC revoked'); select set_config('request.jwt.claims', json_build_object('email', owner_email, 'role', 'authenticated')::text, true) from speaker_identity; set local role authenticated; select pg_temp.assert_speaker( (select count(*) = 6 from public.get_my_speaker_sessions()), 'only own registered non-service talks across active, past and future events'); select pg_temp.assert_speaker( (select count(*) = 5 from public.get_my_speaker_sessions() where not is_active), 'inactive events remain accessible without physical check-in'); select pg_temp.assert_speaker(not public.is_admin(), 'staff material restrictions tested without Admin privileges'); reset role; update public.events set is_active = false where is_active; update public.events set is_active = true where id = (select event_id from speaker_fixture where kind = 'staff_active'); set local role authenticated; select pg_temp.assert_speaker( (select count(*) = 1 from public.get_my_speaker_sessions() where is_active), 'assigned non-admin staff can access an active-event talk without check-in'); select pg_temp.assert_speaker( not public.can_write_session_material(path), 'reject malformed path: ' || coalesce(path, 'NULL')) from unnest(array[null, '', 'sessions/not-a-uuid/slides.pdf', 'foreign/slides.pdf', 'sessions/' || (select session_id from speaker_fixture where kind = 'past') || '/', 'sessions/' || (select session_id from speaker_fixture where kind = 'past') || '/nested/slides.pdf']) as path; select pg_temp.expect_speaker_denied( format('insert into storage.objects(bucket_id,name) values (''materials'',%L)', path), 'malformed storage insert denied: ' || path) from unnest(array['sessions/not-a-uuid/slides.pdf', 'foreign/slides.pdf', 'sessions/' || (select session_id from speaker_fixture where kind = 'past') || '/nested/slides.pdf']) as path; do $$ declare fixture record; material text; affected integer; begin for fixture in select * from speaker_fixture where kind in ('past', 'future', 'active', 'staff_past', 'staff_future', 'staff_active') loop material := 'sessions/' || fixture.session_id || '/slides.pdf'; perform pg_temp.assert_speaker(public.owns_speaker_session(fixture.session_id), 'ownership: ' || fixture.kind); perform pg_temp.expect_speaker_denied( format('select public.set_session_material(%L::uuid, %L)', fixture.session_id, material), 'nonexistent object cannot be attached: ' || fixture.kind); insert into storage.objects(bucket_id, name, metadata) values ('materials', material, '{"version":1}'); perform public.set_session_material(fixture.session_id, material); perform pg_temp.assert_speaker( (select material_path = material from public.sessions where id = fixture.session_id), 'upload attached: ' || fixture.kind); update storage.objects set metadata = '{"version":2}' where bucket_id = 'materials' and name = material; get diagnostics affected = row_count; perform pg_temp.assert_speaker(affected = 1, 'storage replacement allowed: ' || fixture.kind); insert into storage.objects(bucket_id, name) values ('materials', replace(material, 'slides.pdf', 'replacement.pdf')); perform public.set_session_material(fixture.session_id, replace(material, 'slides.pdf', 'replacement.pdf')); perform pg_temp.assert_speaker( (select material_path = replace(material, 'slides.pdf', 'replacement.pdf') from public.sessions where id = fixture.session_id), 'replacement attached: ' || fixture.kind); perform pg_temp.expect_speaker_denied( format('update storage.objects set name = %L where bucket_id = ''materials'' and name = %L', 'sessions/' || (select session_id from speaker_fixture where kind = 'other') || '/stolen.pdf', material), 'UPDATE WITH CHECK prevents reassignment: ' || fixture.kind); perform public.set_session_material(fixture.session_id, null); delete from storage.objects where bucket_id = 'materials' and name in (material, replace(material, 'slides.pdf', 'replacement.pdf')); get diagnostics affected = row_count; perform pg_temp.assert_speaker(affected = 2, 'storage delete allowed: ' || fixture.kind); end loop; for fixture in select * from speaker_fixture where kind in ('other', 'service', 'attendee', 'unregistered', 'unassigned_staff', 'wrong_event_staff', 'admin_past') loop material := 'sessions/' || fixture.session_id || '/slides.pdf'; perform pg_temp.assert_speaker(not public.owns_speaker_session(fixture.session_id), 'ownership denied: ' || fixture.kind); perform pg_temp.assert_speaker(not public.can_write_session_material(material), 'storage ownership denied: ' || fixture.kind); perform pg_temp.expect_speaker_denied( format('insert into storage.objects(bucket_id,name) values (''materials'',%L)', material), 'storage insert denied: ' || fixture.kind); perform pg_temp.expect_speaker_denied( format('select public.set_session_material(%L::uuid,null)', fixture.session_id), 'material detach denied: ' || fixture.kind); end loop; perform pg_temp.expect_speaker_denied( format('select public.set_session_material(%L::uuid,%L)', (select session_id from speaker_fixture where kind = 'past'), 'sessions/' || (select session_id from speaker_fixture where kind = 'future') || '/slides.pdf'), 'another owned session path cannot be attached'); end; $$; reset role; insert into storage.objects(bucket_id, name) select 'materials', 'sessions/' || session_id || '/foreign.pdf' from speaker_fixture where kind = 'other'; set local role authenticated; do $$ declare affected integer; begin update storage.objects set metadata = '{"changed":true}' where bucket_id = 'materials' and name = 'sessions/' || (select session_id from speaker_fixture where kind = 'other') || '/foreign.pdf'; get diagnostics affected = row_count; perform pg_temp.assert_speaker(affected = 0, 'foreign storage update denied'); delete from storage.objects where bucket_id = 'materials' and name = 'sessions/' || (select session_id from speaker_fixture where kind = 'other') || '/foreign.pdf'; get diagnostics affected = row_count; perform pg_temp.assert_speaker(affected = 0, 'foreign storage delete denied'); end; $$; select set_config('request.jwt.claims', json_build_object('email', other_email, 'role', 'authenticated')::text, true) from speaker_identity; select pg_temp.assert_speaker( (select count(*) = 1 from public.get_my_speaker_sessions()), 'other speaker sees only own assignment'); reset role; update public.registrations set type = 'attendee' where event_id = (select event_id from speaker_fixture where kind = 'other'); set local role authenticated; select pg_temp.assert_speaker( not exists (select 1 from public.get_my_speaker_sessions()), 'attendee with assignment has empty access'); reset role; update public.registrations set type = 'speaker' where event_id = (select event_id from speaker_fixture where kind = 'other'); select set_config('request.jwt.claims', '{"role":"authenticated"}', true); set local role authenticated; select pg_temp.assert_speaker( not exists (select 1 from public.get_my_speaker_sessions()), 'missing identity has empty access'); select pg_temp.expect_speaker_denied( format('select public.set_session_material(%L::uuid,null)', (select session_id from speaker_fixture where kind = 'past')), 'missing identity cannot detach material'); reset role; select set_config('request.jwt.claims', json_build_object('email', admin_email, 'role', 'authenticated')::text, true) from speaker_identity; update public.events set is_active = false where is_active; update public.events set is_active = true where id = (select event_id from speaker_fixture where kind = 'admin_active'); set local role authenticated; select pg_temp.assert_speaker(public.is_admin(), 'assigned Admin retains Admin access'); select pg_temp.assert_speaker( (select count(*) = 3 from public.get_my_speaker_sessions()), 'Admin portal lists only own assigned talks across event dates'); select pg_temp.assert_speaker( (select count(*) = 1 from public.get_my_speaker_sessions() where is_active), 'assigned Admin staff can access an active-event talk without check-in'); select pg_temp.assert_speaker( public.owns_speaker_session(session_id), 'Admin staff owns assigned talk: ' || kind) from speaker_fixture where kind in ('admin_past', 'admin_future', 'admin_active'); select pg_temp.assert_speaker( not public.owns_speaker_session(session_id), 'Admin portal ownership denied: ' || kind) from speaker_fixture where kind in ('admin_service', 'other'); select pg_temp.assert_speaker( public.can_write_session_material('sessions/' || session_id || '/admin.pdf'), 'Admin global material permission preserved') from speaker_fixture where kind = 'other'; select public.set_session_material(session_id, null) from speaker_fixture where kind = 'other'; reset role; select pg_temp.assert_speaker( not exists (select * from speaker_registration_before except select registration.* from public.registrations registration join speaker_fixture using (event_id)) and not exists (select registration.* from public.registrations registration join speaker_fixture using (event_id) except select * from speaker_registration_before), 'registrations, points, QR tokens and physical check-in unchanged'); select pg_temp.assert_speaker( not exists (select * from public.points_log except select * from speaker_points_before) and not exists (select * from speaker_points_before except select * from public.points_log), 'no points-log mutation'); select pg_temp.assert_speaker( not exists (select * from public.checkins except select * from speaker_checkins_before) and not exists (select * from speaker_checkins_before except select * from public.checkins), 'no check-in mutation'); select pg_temp.assert_speaker( not exists (select * from public.leaderboard except select * from speaker_leaderboard_before) and not exists (select * from speaker_leaderboard_before except select * from public.leaderboard), 'no leaderboard mutation'); rollback;