-- V1-2.1 M4/M5: who reads and calls what (contract §5.3, §5.4, §5.6, §15), -- after the independent audit (M3). Table-driven: every catalogue RPC is -- called by every actor class that must be refused (owner, admin, moderator, -- support and a plain account without capability at aal2; capability and -- reader holders at aal1; deactivated and unconfirmed accounts), each policy -- is read by every actor class, and the capabilities are crossed. The refused -- calls write nothing. Fixtures are scoped to the ids created here. begin; select plan(337); -- Accounts. Suffixes: O owner, A admin, M moderator, S support, E admin + -- editor, V reviewer, P admin + publisher, R reader, RX expired reader, RR -- revoked reader, RD deactivated reader, RU unconfirmed reader, N no right. create table pg_temp.who (label text primary key, id uuid not null); insert into pg_temp.who values ('O', '18000000-0000-0000-0000-000000000001'), ('A', '18000000-0000-0000-0000-00000000000a'), ('M', '18000000-0000-0000-0000-00000000000b'), ('S', '18000000-0000-0000-0000-00000000000c'), ('E', '18000000-0000-0000-0000-00000000000e'), ('V', '18000000-0000-0000-0000-0000000000f1'), ('P', '18000000-0000-0000-0000-00000000000f'), ('R', '18000000-0000-0000-0000-0000000000cc'), ('RX', '18000000-0000-0000-0000-0000000000c1'), ('RR', '18000000-0000-0000-0000-0000000000c2'), ('RD', '18000000-0000-0000-0000-0000000000c3'), ('RU', '18000000-0000-0000-0000-0000000000c4'), ('N', '18000000-0000-0000-0000-0000000000c5'); insert into auth.users (id, email, email_confirmed_at) select id, lower(label) || '@m18.local', case when label = 'RU' then null else now() end from pg_temp.who; insert into auth.mfa_factors (id, user_id, factor_type, status, created_at, updated_at) select gen_random_uuid(), id, 'totp', 'verified', now(), now() from pg_temp.who; insert into auth.sessions (id, user_id, aal) select ('18100000' || substr(id::text, 9))::uuid, id, 'aal2' from pg_temp.who; update public.profiles set active = false where id = '18000000-0000-0000-0000-0000000000c3'; select set_config('request.jwt.claims', '', true); select private.assign_platform_role(null, '18000000-0000-0000-0000-000000000001', 'owner'); select private.assign_platform_role(null, '18000000-0000-0000-0000-00000000000a', 'admin'); select private.assign_platform_role(null, '18000000-0000-0000-0000-00000000000b', 'moderator'); select private.assign_platform_role(null, '18000000-0000-0000-0000-00000000000c', 'support'); select private.assign_platform_role(null, '18000000-0000-0000-0000-00000000000e', 'admin'); select private.assign_platform_role(null, '18000000-0000-0000-0000-00000000000f', 'admin'); insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by, granted_at, expires_at, revoked_at, revoked_by, revocation_reason) values ('18000000-0000-0000-0000-00000000000e', 'sheet_editor', 'a', 'c', '18000000-0000-0000-0000-000000000001', now(), null, null, null, null), ('18000000-0000-0000-0000-0000000000f1', 'sheet_reviewer', 'a', 'c', '18000000-0000-0000-0000-000000000001', now(), null, null, null, null), ('18000000-0000-0000-0000-00000000000f', 'sheet_publisher', 'a', 'c', '18000000-0000-0000-0000-000000000001', now(), null, null, null, null), ('18000000-0000-0000-0000-0000000000cc', 'catalog_reader', 'a', null, '18000000-0000-0000-0000-000000000001', now(), null, null, null, null), ('18000000-0000-0000-0000-0000000000c1', 'catalog_reader', 'a', null, '18000000-0000-0000-0000-000000000001', now() - interval '2 days', now() - interval '1 day', null, null, null), ('18000000-0000-0000-0000-0000000000c2', 'catalog_reader', 'a', null, '18000000-0000-0000-0000-000000000001', now(), null, now(), '18000000-0000-0000-0000-000000000001', 'ended'), ('18000000-0000-0000-0000-0000000000c3', 'catalog_reader', 'a', null, '18000000-0000-0000-0000-000000000001', now(), null, null, null, null), ('18000000-0000-0000-0000-0000000000c4', 'catalog_reader', 'a', null, '18000000-0000-0000-0000-000000000001', now(), null, null, null, null); -- Catalogue fixtures, labelled: SP active sheet with a deprecated v1 and a -- published v2 (drawing a1), a draft v3 (drawing a2) under an open round with a -- comment and an acknowledgment, a pending drawing a3; SD a draft-only sheet; -- SA an archived sheet with a published v1 (drawing a4). Every drawing has an -- object; a3's row is pending. create table pg_temp.k (key text primary key, id uuid not null); insert into pg_temp.k values ('SP', '18200000-0000-0000-0000-000000000001'), ('SD', '18200000-0000-0000-0000-000000000002'), ('SA', '18200000-0000-0000-0000-000000000003'), ('SP.v1', '18300000-0000-0000-0000-000000000011'), ('SP.v2', '18300000-0000-0000-0000-000000000012'), ('SP.v3', '18300000-0000-0000-0000-000000000013'), ('SD.v1', '18300000-0000-0000-0000-000000000021'), ('SA.v1', '18300000-0000-0000-0000-000000000031'), ('a1', '18400000-0000-0000-0000-000000000001'), ('a2', '18400000-0000-0000-0000-000000000002'), ('a3', '18400000-0000-0000-0000-000000000003'), ('a4', '18400000-0000-0000-0000-000000000004'), ('SP.r', '18500000-0000-0000-0000-000000000001'); create function pg_temp.id(p_key text) returns uuid language sql stable as $$ select id from pg_temp.k where key = p_key $$; create function pg_temp.doc(p_asset uuid) returns jsonb language sql as $$ select jsonb_build_object('schema_version', 2, 'species_id', 'equine', 'name', jsonb_build_object('fr', 'M', 'en', 'M'), 'description', jsonb_build_object('fr', 'd', 'en', 'd'), 'scope_disclaimer', jsonb_build_object('fr', 'x', 'en', 'x'), 'attribution', jsonb_build_object('authors', '[]'::jsonb, 'license', null, 'notes', null), 'vertebral_config', jsonb_build_object('regions', '[]'::jsonb), 'references', '[]'::jsonb, 'categories', '[]'::jsonb, 'views', jsonb_build_array(jsonb_build_object('id', 'main', 'kind', 'main', 'name', jsonb_build_object('fr', 'V', 'en', 'V'), 'svg_asset_id', p_asset, 'view_box', null)), 'annotations', '[]'::jsonb, 'finding_zones', '[]'::jsonb); $$; select set_config('request.jwt.claims', '{"sub":"18000000-0000-0000-0000-00000000000e"}', true); insert into public.anatomical_sheets (id, species_id, name, slug, is_official, visibility, status, archived_at) values (pg_temp.id('SP'), 'equine', 'SP', 'm18-sp', true, 'private', 'active', null), (pg_temp.id('SD'), 'equine', 'SD', 'm18-sd', true, 'private', 'active', null), (pg_temp.id('SA'), 'equine', 'SA', 'm18-sa', true, 'private', 'archived', now()); insert into public.anatomical_sheet_assets (id, sheet_id, sha256, byte_size, file_name, uploaded_at) values (pg_temp.id('a1'), pg_temp.id('SP'), repeat('c1', 32), 100, 'a1.svg', now()), (pg_temp.id('a2'), pg_temp.id('SP'), repeat('c2', 32), 100, 'a2.svg', now()), (pg_temp.id('a3'), pg_temp.id('SP'), repeat('c3', 32), 100, 'a3.svg', null), (pg_temp.id('a4'), pg_temp.id('SA'), repeat('c4', 32), 100, 'a4.svg', now()); insert into storage.objects (bucket_id, name, metadata) select 'anatomical-sheet-assets', repeat(c, 32) || '.svg', '{"size":100}' from unnest(array['c1', 'c2', 'c3', 'c4']) c on conflict do nothing; insert into public.anatomical_sheet_versions (id, sheet_id, version_number, status, document_schema_version, content, published_at, deprecated_at, document_fingerprint) values (pg_temp.id('SP.v1'), pg_temp.id('SP'), 1, 'deprecated', 2, pg_temp.doc(pg_temp.id('a1')), now(), now(), repeat('0', 64)), (pg_temp.id('SP.v2'), pg_temp.id('SP'), 2, 'published', 2, pg_temp.doc(pg_temp.id('a1')), now(), null, repeat('0', 64)), (pg_temp.id('SP.v3'), pg_temp.id('SP'), 3, 'draft', 2, pg_temp.doc(pg_temp.id('a2')), null, null, null), (pg_temp.id('SD.v1'), pg_temp.id('SD'), 1, 'draft', 2, pg_temp.doc(null), null, null, null), (pg_temp.id('SA.v1'), pg_temp.id('SA'), 1, 'published', 2, pg_temp.doc(pg_temp.id('a4')), now(), null, repeat('0', 64)); update public.anatomical_sheets set current_version_id = pg_temp.id('SP.v2'), last_version_number = 3 where id = pg_temp.id('SP'); update public.anatomical_sheets set last_version_number = 1 where id = pg_temp.id('SD'); update public.anatomical_sheets set current_version_id = pg_temp.id('SA.v1'), last_version_number = 1 where id = pg_temp.id('SA'); insert into public.anatomical_sheet_reviews (id, version_id, round, revision, authors, submitted_by, status, verdict, verified_at) values (pg_temp.id('SP.r'), pg_temp.id('SP.v3'), 1, 0, array['18000000-0000-0000-0000-00000000000e'::uuid], '18000000-0000-0000-0000-00000000000e', 'open', jsonb_build_object('fingerprint', repeat('a', 64), 'drawings', '[]'::jsonb, 'groups', jsonb_build_array(jsonb_build_object('key', 'general', 'digest', repeat('1', 64), 'coverable', true))), now()); insert into public.anatomical_sheet_review_comments (review_id, author, body) values (pg_temp.id('SP.r'), '18000000-0000-0000-0000-0000000000f1', 'c'); insert into public.anatomical_sheet_review_acknowledgments (review_id, group_key, group_digest, reviewer) values (pg_temp.id('SP.r'), 'general', repeat('1', 64), '18000000-0000-0000-0000-0000000000f1'); insert into public.species (id, name_fr, name_en, sort_order, active) values ('m18inactive', 'I', 'I', 99, false); insert into pg_temp.k select 'h.v', id from public.anatomical_sheet_versions where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916'; -- The calling session of an account at an assurance level (aal2 names its -- live session row; aal1 a session that never reached aal2). create function pg_temp.as_user(p text, p_aal text) returns void language plpgsql as $$ declare v_id uuid := (select id from pg_temp.who where label = p); begin perform set_config('role', 'authenticated', true); perform set_config('request.jwt.claims', jsonb_build_object('sub', v_id, 'role', 'authenticated', 'aal', p_aal, 'session_id', case when p_aal = 'aal2' then ('18100000' || substr(v_id::text, 9))::uuid else gen_random_uuid() end)::text, true); end $$; -- The outcome of a statement: its single value, or its SQLSTATE and message. create function pg_temp.val(p_sql text) returns text language plpgsql as $$ declare v text; begin execute p_sql into v; return coalesce(v, 'ok'); exception when others then return sqlstate || ' ' || sqlerrm; end $$; -- What the caller reads of the fixtures, through the policies. create function pg_temp.visible() returns text language sql as $$ select 'sheets=' || coalesce((select string_agg(k.key, ',' order by k.key collate "C") from public.anatomical_sheets s join pg_temp.k k on k.id = s.id), '') || ';versions=' || coalesce((select string_agg(k.key, ',' order by k.key collate "C") from public.anatomical_sheet_versions v join pg_temp.k k on k.id = v.id and k.key <> 'h.v'), '') || ';assets=' || coalesce((select string_agg(k.key, ',' order by k.key collate "C") from public.anatomical_sheet_assets a join pg_temp.k k on k.id = a.id), '') || ';objects=' || coalesce((select string_agg(k.key, ',' order by k.key collate "C") from storage.objects o join pg_temp.k k on k.key in ('a1', 'a2', 'a3', 'a4') and o.name = repeat('c' || right(k.key, 1), 32) || '.svg' where o.bucket_id = 'anatomical-sheet-assets'), '') || ';reviews=' || (select count(*) from public.anatomical_sheet_reviews where id = pg_temp.id('SP.r')) || ';comments=' || (select count(*) from public.anatomical_sheet_review_comments where review_id = pg_temp.id('SP.r')) || ';acks=' || (select count(*) from public.anatomical_sheet_review_acknowledgments where review_id = pg_temp.id('SP.r')); $$; -- The fixture sheets the catalogue list names, or the refusal. create function pg_temp.catalogue() returns text language plpgsql as $$ begin return coalesce((select string_agg(k.key, ',' order by k.key collate "C") from jsonb_array_elements(public.list_official_catalog()) e join pg_temp.k k on k.id = (e ->> 'sheet_id')::uuid), ''); exception when others then return sqlstate || ' ' || sqlerrm; end $$; -- Every catalogue RPC an actor without the matching right must be refused. create table pg_temp.calls (n int primary key, name text not null, sql text not null); insert into pg_temp.calls values (1, 'create_official_sheet', $$ select public.create_official_sheet('m18-new', pg_temp.doc(null)) $$), (2, 'create_sheet_draft', $$ select public.create_sheet_draft(pg_temp.id('SP'), null, 'x') $$), (3, 'save_sheet_draft', $$ select public.save_sheet_draft(pg_temp.id('SP.v3'), 0, pg_temp.doc(null), 'x') $$), (4, 'submit_sheet_review', $$ select public.submit_sheet_review(pg_temp.id('SP.v3'), 0, 'x') $$), (5, 'withdraw_sheet_review', $$ select public.withdraw_sheet_review(pg_temp.id('SP.r'), 'x') $$), (6, 'comment_sheet_review', $$ select public.comment_sheet_review(pg_temp.id('SP.r'), 'x', null) $$), (7, 'acknowledge_sheet_review_group', $$ select public.acknowledge_sheet_review_group(pg_temp.id('SP.r'), 'general', repeat('1', 64)) $$), (8, 'decide_sheet_review', $$ select public.decide_sheet_review(pg_temp.id('SP.r'), true, 'x') $$), (9, 'publish_sheet_version', $$ select public.publish_sheet_version(pg_temp.id('SP.r')) $$), (10, 'deprecate_sheet_version', $$ select public.deprecate_sheet_version(pg_temp.id('SP.v2'), 'x') $$), (11, 'archive_anatomical_sheet', $$ select public.archive_anatomical_sheet(pg_temp.id('SP')) $$), (12, 'restore_anatomical_sheet', $$ select public.restore_anatomical_sheet(pg_temp.id('SA')) $$), (13, 'delete_anatomical_sheet', $$ select public.delete_anatomical_sheet(pg_temp.id('SD')) $$), (14, 'delete_sheet_version', $$ select public.delete_sheet_version(pg_temp.id('SD.v1')) $$), (15, 'create_sheet_working_copy', $$ select public.create_sheet_working_copy(pg_temp.id('h.v'), 'm18-wc') $$), (16, 'create_species', $$ select public.create_species('m18new', 'N', 'N', 1) $$), (17, 'update_inactive_species', $$ select public.update_inactive_species('m18inactive', 'N', 'N', 1) $$), (18, 'list_catalog_species', $$ select case when public.list_catalog_species() is not null then 'ok' end $$), (19, 'register_sheet_asset', $$ select public.register_sheet_asset(pg_temp.id('SP'), repeat('c9', 32), 100, 'x.svg') $$), (20, 'confirm_sheet_asset_upload', $$ select public.confirm_sheet_asset_upload(pg_temp.id('a3')) $$), (21, 'delete_sheet_asset', $$ select public.delete_sheet_asset(pg_temp.id('a3')) $$); -- Every call, by one actor, with one expected outcome (the owner lists species). create function pg_temp.matrix(p text, p_aal text, p_expect text) returns setof text language plpgsql as $$ declare c record; begin for c in select * from pg_temp.calls order by n loop perform pg_temp.as_user(p, p_aal); return next is(pg_temp.val(c.sql), case when p = 'O' and p_aal = 'aal2' and c.name = 'list_catalog_species' then 'ok' else p_expect end, p || ' at ' || p_aal || ': ' || c.name); end loop; end $$; -- Named calls by an actor that holds another capability: each refused. create function pg_temp.crossed(p text, p_names text[]) returns setof text language plpgsql as $$ declare c record; begin for c in select * from pg_temp.calls where name = any (p_names) order by n loop perform pg_temp.as_user(p, 'aal2'); return next is(pg_temp.val(c.sql), '42501 rights.capability', p || ' holds another capability: ' || c.name); end loop; end $$; -- The four reads of one actor: fixtures through the policies, the catalogue -- list, an archived sheet's published version by id, a draft by id. create function pg_temp.reads(p text, p_aal text, p_visible text, p_catalogue text, p_archived text, p_draft text) returns setof text language plpgsql as $f$ begin perform pg_temp.as_user(p, p_aal); return next is(pg_temp.visible(), p_visible, p || ' at ' || p_aal || ': rows through the policies'); return next is(pg_temp.catalogue(), p_catalogue, p || ' at ' || p_aal || ': catalogue list'); return next is(pg_temp.val($$ select public.get_sheet_version(pg_temp.id('SA.v1')) ->> 'status' $$), p_archived, p || ' at ' || p_aal || ': published version of an archived sheet by id'); return next is(pg_temp.val($$ select public.get_sheet_version(pg_temp.id('SP.v3')) ->> 'status' $$), p_draft, p || ' at ' || p_aal || ': draft by id'); end $f$; grant all on pg_temp.who, pg_temp.k, pg_temp.calls to authenticated; grant execute on all functions in schema pg_temp to authenticated; -- ============ §5.3 catalogue access and the read policies, per actor ============ create table pg_temp.views (label text primary key, visible text not null); insert into pg_temp.views values ('none', 'sheets=;versions=;assets=;objects=;reviews=0;comments=0;acks=0'), ('reader', 'sheets=SA,SP;versions=SA.v1,SP.v1,SP.v2;assets=a1,a4;objects=a1,a4;reviews=0;comments=0;acks=0'), ('staff', 'sheets=SA,SD,SP;versions=SA.v1,SD.v1,SP.v1,SP.v2,SP.v3;assets=a1,a2,a3,a4;objects=a1,a2,a4;reviews=1;comments=1;acks=1'); grant select on pg_temp.views to authenticated; select pg_temp.reads(a.p, a.aal, (select visible from pg_temp.views where label = 'none'), '42501 rights.catalog', '42501 not_found', '42501 not_found') from (values ('O', 'aal2'), ('A', 'aal2'), ('M', 'aal2'), ('S', 'aal2'), ('N', 'aal2'), ('RX', 'aal1'), ('RR', 'aal1'), ('RD', 'aal1'), ('RU', 'aal1')) a(p, aal); select pg_temp.reads(a.p, a.aal, (select visible from pg_temp.views where label = 'reader'), 'SP', 'published', '42501 not_found') from (values ('R', 'aal1'), ('E', 'aal1'), ('V', 'aal1'), ('P', 'aal1')) a(p, aal); select pg_temp.reads(a.p, 'aal2', (select visible from pg_temp.views where label = 'staff'), 'SP', 'published', 'draft') from (values ('E'), ('V'), ('P')) a(p); -- Anonymous: no table privilege, no catalogue RPC. reset role; set local role anon; select set_config('request.jwt.claims', '{"role":"anon"}', true); select throws_ok($$ select id from public.anatomical_sheets $$, '42501', null, 'anon cannot read sheets'); select throws_ok($$ select id from public.anatomical_sheet_versions $$, '42501', null, 'anon cannot read versions'); select throws_ok($$ select public.get_sheet_version('18300000-0000-0000-0000-000000000031') $$, '42501', null, 'anon cannot call the read RPCs'); reset role; select is( (select count(*) from pg_proc p join pg_namespace n on n.oid = p.pronamespace where n.nspname = 'public' and p.proname ~ '(sheet|species|catalog|platform_role)' and has_function_privilege('anon', p.oid, 'execute')), 0::bigint, 'anon executes no catalogue or role RPC'); select is( (select string_agg(p.proname::text, ',' order by p.proname::text collate "C") from pg_proc p join pg_namespace n on n.oid = p.pronamespace where n.nspname = 'public' and p.proname ~ '(sheet|species|catalog|platform_role)' and has_function_privilege('service_role', p.oid, 'execute')), 'confirm_sheet_asset_upload,get_sheet_asset_state', 'service_role executes only the upload confirmation and the operator read (§5.4)'); -- ============ F14 and §5.6: every catalogue RPC refused without the right ============ select pg_temp.matrix('O', 'aal2', '42501 rights.capability'); select pg_temp.matrix('A', 'aal2', '42501 rights.capability'); select pg_temp.matrix('M', 'aal2', '42501 rights.capability'); select pg_temp.matrix('S', 'aal2', '42501 rights.capability'); select pg_temp.matrix('N', 'aal2', '42501 rights.capability'); select pg_temp.matrix('R', 'aal1', '42501 rights.mfa'); select pg_temp.matrix('E', 'aal1', '42501 rights.mfa'); select pg_temp.matrix('V', 'aal1', '42501 rights.mfa'); select pg_temp.matrix('P', 'aal1', '42501 rights.mfa'); select pg_temp.matrix('RD', 'aal2', '42501 rights.account'); select pg_temp.matrix('RU', 'aal2', '42501 rights.account'); -- ============ §5.4: one capability never stands for another ============ select pg_temp.crossed('E', array['acknowledge_sheet_review_group', 'decide_sheet_review', 'publish_sheet_version', 'deprecate_sheet_version', 'archive_anatomical_sheet', 'restore_anatomical_sheet']); select pg_temp.crossed('V', array['create_official_sheet', 'create_sheet_draft', 'save_sheet_draft', 'submit_sheet_review', 'withdraw_sheet_review', 'publish_sheet_version', 'deprecate_sheet_version', 'archive_anatomical_sheet', 'delete_anatomical_sheet', 'delete_sheet_version', 'create_sheet_working_copy', 'create_species', 'register_sheet_asset', 'confirm_sheet_asset_upload', 'delete_sheet_asset']); select pg_temp.crossed('P', array['create_official_sheet', 'save_sheet_draft', 'submit_sheet_review', 'withdraw_sheet_review', 'acknowledge_sheet_review_group', 'decide_sheet_review', 'register_sheet_asset', 'create_species']); -- ============ Species activation and renaming: the owner at aal2 only (§7.2) ============ select pg_temp.as_user('A', 'aal2'); select is(pg_temp.val($$ select public.activate_species('m18inactive') $$), '42501 rights.owner', 'an admin cannot activate a species'); select pg_temp.as_user('O', 'aal1'); select is(pg_temp.val($$ select public.activate_species('m18inactive') $$), '42501 rights.mfa', 'the owner at aal1 cannot activate a species'); select pg_temp.as_user('O', 'aal2'); select is(pg_temp.val($$ select public.activate_species('m18inactive') $$), 'm18inactive', 'the owner at aal2 activates a species'); select pg_temp.as_user('A', 'aal2'); select is(pg_temp.val($$ select public.rename_species('m18inactive', 'X', 'X') $$), '42501 rights.owner', 'an admin cannot rename a species'); -- ============ The grants table: own rows, the owner, staff (§5.2) ============ create function pg_temp.grants_seen() returns bigint language sql as $$ select count(*) from public.anatomical_catalog_grants g join pg_temp.who w on w.id = g.user_id; $$; grant execute on function pg_temp.grants_seen() to authenticated; select pg_temp.as_user('R', 'aal1'); select is(pg_temp.grants_seen(), 1::bigint, 'a reader sees only its own grant'); select pg_temp.as_user('A', 'aal2'); select is(pg_temp.grants_seen(), 0::bigint, 'an admin without a grant sees none'); select pg_temp.as_user('E', 'aal2'); select is(pg_temp.grants_seen(), 3::bigint, 'a capability holder at aal2 sees the capability grants, not the reader grants'); select pg_temp.as_user('O', 'aal2'); select is(pg_temp.grants_seen(), 8::bigint, 'the owner at aal2 sees every grant'); select * from finish(); rollback;