-- profiles: trigger creation, own-row read/update, every denied path. begin; select plan(17); -- Fixtures (privileged session = migration owner) insert into auth.users (id, email, raw_user_meta_data) values ('00000000-0000-0000-0000-00000000000a', 'alice@test.local', '{"display_name":"Alice","locale":"en"}'), ('00000000-0000-0000-0000-00000000000b', 'bob@test.local', '{}'), ('00000000-0000-0000-0000-00000000000c', 'carol.long.name@test.local', '{"locale":"xx"}'); -- 1. Trigger builds the profile select results_eq( $$ select display_name, locale from public.profiles where id = '00000000-0000-0000-0000-00000000000a' $$, $$ values ('Alice'::text, 'en'::text) $$, 'profile created from metadata (display_name + locale)' ); select results_eq( $$ select display_name, locale from public.profiles where id = '00000000-0000-0000-0000-00000000000b' $$, $$ values ('bob'::text, 'fr'::text) $$, 'profile falls back to the email local part and locale fr' ); select is( (select locale from public.profiles where id = '00000000-0000-0000-0000-00000000000c'), 'fr', 'unsupported locale in metadata falls back to fr' ); -- 2. Alice, authenticated set local role authenticated; select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000a","role":"authenticated"}', true); select results_eq( $$ select id from public.profiles order by id $$, $$ values ('00000000-0000-0000-0000-00000000000a'::uuid) $$, 'alice sees exactly her own profile' ); select is( (select count(*) from public.profiles where id = '00000000-0000-0000-0000-00000000000b'), 0::bigint, 'alice cannot read bob by id (no IDOR)' ); select lives_ok( $$ update public.profiles set display_name = 'Alice B.', timezone = 'Europe/Brussels' where id = '00000000-0000-0000-0000-00000000000a' $$, 'alice updates her own display_name and timezone' ); select is( (select display_name from public.profiles where id = '00000000-0000-0000-0000-00000000000a'), 'Alice B.', 'own update is visible' ); -- Update on another row: RLS filters it out silently (0 rows) update public.profiles set display_name = 'hacked' where id = '00000000-0000-0000-0000-00000000000b'; select throws_ok( $$ update public.profiles set active = false where id = '00000000-0000-0000-0000-00000000000a' $$, '42501', null, 'alice cannot deactivate herself (column privilege)' ); select throws_ok( $$ update public.profiles set phone_verified_at = now() where id = '00000000-0000-0000-0000-00000000000a' $$, '42501', null, 'alice cannot self-verify her phone (column privilege)' ); select throws_ok( $$ insert into public.profiles (id, display_name) values (gen_random_uuid(), 'ghost') $$, '42501', null, 'alice cannot insert a profile' ); select throws_ok( $$ delete from public.profiles where id = '00000000-0000-0000-0000-00000000000a' $$, '42501', null, 'alice cannot delete her profile' ); select throws_ok( $$ update public.profiles set phone = '0612345678' where id = '00000000-0000-0000-0000-00000000000a' $$, '23514', null, 'phone must be E.164' ); -- 3. Bob sees his row untouched select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000b","role":"authenticated"}', true); select is( (select display_name from public.profiles where id = '00000000-0000-0000-0000-00000000000b'), 'bob', 'alice''s cross-row update did not touch bob' ); -- 4. Phone change resets verification reset role; update public.profiles set phone = '+33612345678', phone_verified_at = now() where id = '00000000-0000-0000-0000-00000000000b'; set local role authenticated; select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000b","role":"authenticated"}', true); select lives_ok( $$ update public.profiles set phone = '+33698765432' where id = '00000000-0000-0000-0000-00000000000b' $$, 'bob changes his phone' ); select is( (select phone_verified_at from public.profiles where id = '00000000-0000-0000-0000-00000000000b'), null, 'changing the phone clears phone_verified_at' ); -- 5. Anonymous: no access at all set local role anon; select set_config('request.jwt.claims', '', true); select throws_ok( $$ select * from public.profiles $$, '42501', null, 'anon cannot select profiles' ); -- 6. updated_at trigger overrides whatever the writer sends reset role; update public.profiles set updated_at = '2000-01-01' where id = '00000000-0000-0000-0000-00000000000a'; select is( (select updated_at from public.profiles where id = '00000000-0000-0000-0000-00000000000a'), now(), 'updated_at is stamped by the trigger, not by the writer' ); select * from finish(); rollback;