161 lines
6.3 KiB
PL/PgSQL
161 lines
6.3 KiB
PL/PgSQL
-- Platform roles: bootstrap, delegation rules, every denied path, visibility.
|
|
begin;
|
|
select plan(28);
|
|
|
|
insert into auth.users (id, email) values
|
|
('00000000-0000-0000-0000-000000000001', 'root@test.local'),
|
|
('00000000-0000-0000-0000-00000000000a', 'alice@test.local'),
|
|
('00000000-0000-0000-0000-00000000000b', 'bob@test.local'),
|
|
('00000000-0000-0000-0000-00000000000c', 'carol@test.local');
|
|
|
|
-- 1. Nobody has a role; bootstrap the first owner from a privileged session
|
|
select ok(not private.is_platform_owner('00000000-0000-0000-0000-000000000001'), 'fresh account has no owner role');
|
|
select lives_ok(
|
|
$$ select private.assign_platform_role(null, '00000000-0000-0000-0000-000000000001', 'owner') $$,
|
|
'system bootstraps the first owner (actor null)'
|
|
);
|
|
select ok(private.is_platform_owner('00000000-0000-0000-0000-000000000001'), 'root is owner');
|
|
select ok(private.is_platform_admin('00000000-0000-0000-0000-000000000001'), 'owner counts as admin');
|
|
select ok(private.is_platform_moderator('00000000-0000-0000-0000-000000000001'), 'owner counts as moderator');
|
|
select ok(not private.is_support('00000000-0000-0000-0000-000000000001'), 'owner is not support');
|
|
|
|
-- 2. Owner grants admin to alice through the RPC
|
|
set local role authenticated;
|
|
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-000000000001","role":"authenticated"}', true);
|
|
select lives_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000a', 'admin') $$,
|
|
'owner grants admin'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000a', 'admin') $$,
|
|
'23505',
|
|
null,
|
|
'granting an active role twice is refused'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-000000000001', 'admin') $$,
|
|
'42501',
|
|
null,
|
|
'owner cannot change their own roles'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role(gen_random_uuid(), 'support') $$,
|
|
'P0002',
|
|
null,
|
|
'unknown target is refused'
|
|
);
|
|
|
|
-- 3. Alice (admin) may delegate moderator/support, nothing above
|
|
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
|
select lives_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000b', 'moderator') $$,
|
|
'admin grants moderator'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000b', 'admin') $$,
|
|
'42501',
|
|
null,
|
|
'admin cannot grant admin'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000b', 'owner') $$,
|
|
'42501',
|
|
null,
|
|
'admin cannot grant owner'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000a', 'owner') $$,
|
|
'42501',
|
|
null,
|
|
'admin cannot promote herself to owner'
|
|
);
|
|
|
|
-- 4. Direct writes are impossible for the API role
|
|
select throws_ok(
|
|
$$ insert into public.profile_roles (user_id, role) values ('00000000-0000-0000-0000-00000000000a', 'owner') $$,
|
|
'42501',
|
|
null,
|
|
'admin cannot insert a role row directly'
|
|
);
|
|
select throws_ok(
|
|
$$ update public.profile_roles set role = 'owner' where user_id = '00000000-0000-0000-0000-00000000000a' $$,
|
|
'42501',
|
|
null,
|
|
'admin cannot update role rows directly'
|
|
);
|
|
select throws_ok(
|
|
$$ delete from public.profile_roles $$,
|
|
'42501',
|
|
null,
|
|
'admin cannot delete role rows directly'
|
|
);
|
|
|
|
-- 5. Bob (moderator) cannot delegate; sees only his own grants; carol sees nothing
|
|
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000c', 'support') $$,
|
|
'42501',
|
|
null,
|
|
'moderator cannot grant any role'
|
|
);
|
|
select results_eq(
|
|
$$ select user_id, role::text from public.profile_roles $$,
|
|
$$ values ('00000000-0000-0000-0000-00000000000b'::uuid, 'moderator'::text) $$,
|
|
'moderator sees only his own grant rows'
|
|
);
|
|
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
|
|
select is((select count(*) from public.profile_roles), 0::bigint, 'plain user sees no role rows');
|
|
select is((select count(*) from public.profiles), 1::bigint, 'plain user still sees one profile (own)');
|
|
|
|
-- 6. Admin sees every grant and every profile (staff read)
|
|
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
|
select is(
|
|
(select count(*) from public.profile_roles
|
|
where user_id in ('00000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-00000000000a', '00000000-0000-0000-0000-00000000000b')),
|
|
3::bigint,
|
|
'admin sees all three fixture grant rows'
|
|
);
|
|
select is(
|
|
(select count(*) from public.profiles where id in (
|
|
'00000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-00000000000a',
|
|
'00000000-0000-0000-0000-00000000000b', '00000000-0000-0000-0000-00000000000c')),
|
|
4::bigint,
|
|
'admin reads every fixture profile'
|
|
);
|
|
|
|
-- 7. Anonymous
|
|
set local role anon;
|
|
select set_config('request.jwt.claims', '', true);
|
|
select throws_ok(
|
|
$$ select public.grant_platform_role('00000000-0000-0000-0000-00000000000c', 'support') $$,
|
|
'42501',
|
|
null,
|
|
'anon cannot call grant_platform_role'
|
|
);
|
|
|
|
-- 8. Revocation and deactivation
|
|
set local role authenticated;
|
|
select set_config('request.jwt.claims', '{"sub":"00000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
|
select lives_ok(
|
|
$$ select public.revoke_platform_role('00000000-0000-0000-0000-00000000000b', 'moderator', 'test') $$,
|
|
'admin revokes moderator'
|
|
);
|
|
select ok(not private.is_platform_moderator('00000000-0000-0000-0000-00000000000b'), 'revoked role no longer counts');
|
|
|
|
reset role;
|
|
update public.profiles set active = false where id = '00000000-0000-0000-0000-00000000000a';
|
|
select ok(not private.is_platform_admin('00000000-0000-0000-0000-00000000000a'), 'deactivated account loses its roles');
|
|
|
|
-- 9. Last-owner guard on the system path (neutralise owners seeded outside the fixtures)
|
|
update public.profile_roles set revoked_at = now()
|
|
where role = 'owner' and revoked_at is null and user_id <> '00000000-0000-0000-0000-000000000001';
|
|
select throws_ok(
|
|
$$ select private.withdraw_platform_role(null, '00000000-0000-0000-0000-000000000001', 'owner', 'oops') $$,
|
|
'42501',
|
|
null,
|
|
'the last owner cannot be revoked'
|
|
);
|
|
|
|
select * from finish();
|
|
rollback;
|