Files
Convivo/supabase/tests/admin_magic_link_eligibility_tests.sql
fbc827c2b8 feat: finalize ticket merging and speaker-aware imports
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-10 13:28:18 +02:00

67 lines
5.0 KiB
PL/PgSQL

\set ON_ERROR_STOP on
begin;
create function pg_temp.assert_admin_entry(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 temporary table admin_entry_identity as
select kind, gen_random_uuid() as user_id,
'entry-' || kind || '-' || gen_random_uuid() ||
case when kind = 'domain' then '@appe.at' else '@example.invalid' end as email
from unnest(array['admin', 'domain', 'attendee', 'speaker', 'noauth', 'authonly']) as kind;
grant select on admin_entry_identity to anon, authenticated;
create temporary table admin_entry_before as
select
(select coalesce(jsonb_agg(to_jsonb(registration) order by registration.id), '[]') from public.registrations registration) as registrations,
(select coalesce(jsonb_agg(to_jsonb(checkin) order by checkin.id), '[]') from public.checkins checkin) as checkins,
(select coalesce(jsonb_agg(to_jsonb(points) order by points.id), '[]') from public.points_log points) as points_log;
insert into auth.users(id, instance_id, aud, role, email, encrypted_password, email_confirmed_at, raw_app_meta_data, raw_user_meta_data, created_at, updated_at)
select user_id, '00000000-0000-0000-0000-000000000000'::uuid, 'authenticated', 'authenticated', email,
'', now(), '{}'::jsonb, '{"is_admin":true}'::jsonb, now(), now()
from admin_entry_identity where kind <> 'noauth';
insert into public.contacts(email, full_name, is_admin)
select email, 'Entry regression ' || kind, kind in ('admin', 'noauth')
from admin_entry_identity where kind <> 'authonly';
select pg_temp.assert_admin_entry(has_function_privilege('anon', 'public.can_request_admin_magic_link(text)', 'execute'), 'anonymous eligibility lookup allowed');
select pg_temp.assert_admin_entry(has_function_privilege('authenticated', 'public.can_request_admin_magic_link(text)', 'execute'), 'authenticated eligibility lookup allowed');
select pg_temp.assert_admin_entry(not exists (
select 1 from pg_proc as routine, lateral aclexplode(routine.proacl) as privilege
where routine.oid = 'public.can_request_admin_magic_link(text)'::regprocedure and privilege.grantee = 0
), 'no PUBLIC execution grant');
set local role anon;
select pg_temp.assert_admin_entry(public.can_request_admin_magic_link(email), 'flagged admin with Auth user eligible without registration') from admin_entry_identity where kind = 'admin';
select pg_temp.assert_admin_entry(public.can_request_admin_magic_link(email), 'existing domain admin eligible') from admin_entry_identity where kind = 'domain';
select pg_temp.assert_admin_entry(public.can_request_admin_magic_link(' ' || upper(email) || ' '), 'email normalization supported') from admin_entry_identity where kind = 'admin';
select pg_temp.assert_admin_entry(not public.can_request_admin_magic_link(email), 'non-admin attendee denied despite user metadata') from admin_entry_identity where kind = 'attendee';
select pg_temp.assert_admin_entry(not public.can_request_admin_magic_link(email), 'non-admin speaker denied despite user metadata') from admin_entry_identity where kind = 'speaker';
select pg_temp.assert_admin_entry(not public.can_request_admin_magic_link(email), 'admin contact without Auth user denied') from admin_entry_identity where kind = 'noauth';
select pg_temp.assert_admin_entry(not public.can_request_admin_magic_link(email), 'Auth user without contact denied') from admin_entry_identity where kind = 'authonly';
select pg_temp.assert_admin_entry(not public.can_request_admin_magic_link(null), 'null denied');
select pg_temp.assert_admin_entry(not public.can_request_admin_magic_link(''), 'empty denied');
select pg_temp.assert_admin_entry(not public.can_request_admin_magic_link('[email protected]'), 'unknown denied');
reset role;
select set_config('request.jwt.claims', json_build_object('email', email, 'role', 'authenticated', 'user_metadata', json_build_object('is_admin', true))::text, true)
from admin_entry_identity where kind = 'attendee';
set local role authenticated;
select pg_temp.assert_admin_entry(not public.is_admin(), 'callback authorization ignores editable metadata');
select pg_temp.assert_admin_entry(not public.can_request_admin_magic_link(email), 'authenticated non-admin still ineligible') from admin_entry_identity where kind = 'attendee';
reset role;
select pg_temp.assert_admin_entry(
(select registrations from admin_entry_before) = (select coalesce(jsonb_agg(to_jsonb(registration) order by registration.id), '[]') from public.registrations registration), 'registrations and attendance unchanged');
select pg_temp.assert_admin_entry(
(select checkins from admin_entry_before) = (select coalesce(jsonb_agg(to_jsonb(checkin) order by checkin.id), '[]') from public.checkins checkin), 'session checkins unchanged');
select pg_temp.assert_admin_entry(
(select points_log from admin_entry_before) = (select coalesce(jsonb_agg(to_jsonb(points) order by points.id), '[]') from public.points_log points), 'points log unchanged');
rollback;