-- V1-2.5 M7: the read RPCs of the administration space (contract §11.1, §12.3, -- BDR-097). Capability holders at aal2 read every official sheet that is not -- deleted, with its badges and states; every other caller is refused, and an -- unknown, deleted, personal or forbidden sheet gets one answer. Fixtures are -- scoped to the ids created here. begin; select plan(60); -- Accounts. O owner, A admin, E admin + editor, V reviewer, P admin + -- publisher, R reader, N no right. Every one has a verified factor and a live -- aal2 session row. create table pg_temp.who (label text primary key, id uuid not null); insert into pg_temp.who values ('O', '22000000-0000-0000-0000-000000000001'), ('A', '22000000-0000-0000-0000-00000000000a'), ('E', '22000000-0000-0000-0000-00000000000e'), ('V', '22000000-0000-0000-0000-0000000000f1'), ('P', '22000000-0000-0000-0000-00000000000f'), ('R', '22000000-0000-0000-0000-0000000000cc'), ('N', '22000000-0000-0000-0000-0000000000c5'); insert into auth.users (id, email, email_confirmed_at, raw_user_meta_data) select id, lower(label) || '@m22.local', now(), jsonb_build_object('display_name', 'M22 ' || label) 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 ('22100000' || substr(id::text, 9))::uuid, id, 'aal2' from pg_temp.who; select set_config('request.jwt.claims', '', true); select private.assign_platform_role(null, '22000000-0000-0000-0000-000000000001', 'owner'); select private.assign_platform_role(null, '22000000-0000-0000-0000-00000000000a', 'admin'); select private.assign_platform_role(null, '22000000-0000-0000-0000-00000000000e', 'admin'); select private.assign_platform_role(null, '22000000-0000-0000-0000-00000000000f', 'admin'); insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by) values ('22000000-0000-0000-0000-00000000000e', 'sheet_editor', 'a', 'c', '22000000-0000-0000-0000-000000000001'), ('22000000-0000-0000-0000-0000000000f1', 'sheet_reviewer', 'a', 'c', '22000000-0000-0000-0000-000000000001'), ('22000000-0000-0000-0000-00000000000f', 'sheet_publisher', 'a', 'c', '22000000-0000-0000-0000-000000000001'), ('22000000-0000-0000-0000-0000000000cc', 'catalog_reader', 'a', null, '22000000-0000-0000-0000-000000000001'); -- Sheets, labelled: SP active with a published v1 and a draft v2 under an -- open round; SA archived with a published v1; SD deleted; PS personal; WC a -- working copy of the historical sheet h (seeded). create table pg_temp.k (key text primary key, id uuid not null); insert into pg_temp.k values ('SP', '22200000-0000-0000-0000-000000000001'), ('SA', '22200000-0000-0000-0000-000000000002'), ('SD', '22200000-0000-0000-0000-000000000003'), ('PS', '22200000-0000-0000-0000-000000000004'), ('WC', '22200000-0000-0000-0000-000000000005'), ('SX', '22200000-0000-0000-0000-000000000006'), ('SX.v1', '22300000-0000-0000-0000-000000000061'), ('SX.v2', '22300000-0000-0000-0000-000000000062'), ('SP.v1', '22300000-0000-0000-0000-000000000011'), ('SP.v2', '22300000-0000-0000-0000-000000000012'), ('SA.v1', '22300000-0000-0000-0000-000000000021'), ('WC.v1', '22300000-0000-0000-0000-000000000051'), ('SP.r', '22500000-0000-0000-0000-000000000001'), ('h', '503dc301-a8b1-50d4-9e44-0919b2fc2916'); insert into pg_temp.k select 'h.v', id from public.anatomical_sheet_versions where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' order by version_number desc limit 1; 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_name text) returns jsonb language sql as $$ select jsonb_build_object('schema_version', 2, 'species_id', 'equine', 'name', jsonb_build_object('fr', p_name, 'en', p_name || ' EN'), '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', null, 'view_box', null)), 'annotations', '[]'::jsonb, 'finding_zones', '[]'::jsonb); $$; select set_config('request.jwt.claims', '{"sub":"22000000-0000-0000-0000-00000000000e"}', true); insert into public.anatomical_sheets (id, species_id, name, slug, is_official, visibility, status, archived_at, deleted_at, creator_user_id, working_copy_source_version_id) values (pg_temp.id('SP'), 'equine', 'SP', 'm22-sp', true, 'private', 'active', null, null, null, null), (pg_temp.id('SA'), 'equine', 'SA', 'm22-sa', true, 'private', 'archived', now(), null, null, null), (pg_temp.id('SD'), 'equine', 'SD', 'm22-sd', true, 'private', 'deleted', null, now(), null, null), (pg_temp.id('PS'), 'equine', 'PS', 'm22-ps', false, 'private', 'active', null, null, '22000000-0000-0000-0000-00000000000e', null), (pg_temp.id('WC'), 'equine', 'WC', 'm22-wc', true, 'private', 'active', null, null, null, pg_temp.id('h.v')), (pg_temp.id('SX'), 'equine', 'SX', 'm22-sx', true, 'private', 'active', null, null, null, null); insert into public.anatomical_sheet_versions (id, sheet_id, version_number, status, document_schema_version, content, published_at, document_fingerprint) values (pg_temp.id('SP.v1'), pg_temp.id('SP'), 1, 'published', 2, pg_temp.doc('Publiée'), now(), repeat('0', 64)), (pg_temp.id('SP.v2'), pg_temp.id('SP'), 2, 'draft', 2, pg_temp.doc('Brouillon'), null, null), (pg_temp.id('SA.v1'), pg_temp.id('SA'), 1, 'published', 2, pg_temp.doc('Archivée'), now(), repeat('0', 64)), (pg_temp.id('WC.v1'), pg_temp.id('WC'), 1, 'draft', 2, pg_temp.doc('Copie'), null, null), (pg_temp.id('SX.v2'), pg_temp.id('SX'), 2, 'published', 2, pg_temp.doc('Courante'), now(), repeat('e', 64)); 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('SX.v1'), pg_temp.id('SX'), 1, 'deprecated', 2, pg_temp.doc('Retirée'), now(), now(), repeat('d', 64)); update public.anatomical_sheets set current_version_id = pg_temp.id('SX.v2'), last_version_number = 2 where id = pg_temp.id('SX'); update public.anatomical_sheet_versions set authors = array['22000000-0000-0000-0000-00000000000e'::uuid] where id = pg_temp.id('SP.v2'); update public.anatomical_sheets set current_version_id = pg_temp.id('SP.v1'), last_version_number = 2 where id = pg_temp.id('SP'); update public.anatomical_sheets set current_version_id = pg_temp.id('SA.v1'), last_version_number = 1 where id = pg_temp.id('SA'); update public.anatomical_sheets set last_version_number = 1 where id = pg_temp.id('WC'); insert into public.anatomical_sheet_reviews (id, version_id, round, revision, authors, submitted_by, submitted_at, status, verdict, verified_at) values (pg_temp.id('SP.r'), pg_temp.id('SP.v2'), 1, 0, array['22000000-0000-0000-0000-00000000000e'::uuid], '22000000-0000-0000-0000-00000000000e', now(), '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()); -- The calling session of an account at an assurance level. 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 ('22100000' || 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 $$; -- The fixture sheets the admin list names (the seeded historical sheet included), or the refusal. create function pg_temp.listed() 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_sheets_admin()) e join pg_temp.k k on k.id = (e ->> 'sheet_id')::uuid), ''); exception when others then return sqlstate || ' ' || sqlerrm; end $$; -- One field of a sheet's admin summary, as the list returns it. create function pg_temp.summary(p_key text, p_path text[]) returns text language sql as $$ select e #>> p_path from jsonb_array_elements(public.list_official_sheets_admin()) e where (e ->> 'sheet_id')::uuid = pg_temp.id(p_key); $$; grant all on pg_temp.who, pg_temp.k to authenticated; grant execute on all functions in schema pg_temp to authenticated; -- ============ Who may call ============ select pg_temp.as_user('O', 'aal2'); select is(pg_temp.listed(), '42501 rights.capability', 'owner without a capability: list refused'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 not_found', 'owner without a capability: sheet refused'); select pg_temp.as_user('A', 'aal2'); select is(pg_temp.listed(), '42501 rights.capability', 'admin without a capability: list refused'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 not_found', 'admin without a capability: sheet refused'); select pg_temp.as_user('R', 'aal2'); select is(pg_temp.listed(), '42501 rights.capability', 'catalogue reader: list refused'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 not_found', 'catalogue reader: sheet refused'); select pg_temp.as_user('N', 'aal2'); select is(pg_temp.listed(), '42501 rights.capability', 'account without a right: list refused'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 not_found', 'account without a right: sheet refused'); select pg_temp.as_user('E', 'aal1'); select is(pg_temp.listed(), '42501 rights.mfa', 'editor at aal1: list refused'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 rights.mfa', 'editor at aal1: sheet refused'); select pg_temp.as_user('E', 'aal2'); select is(pg_temp.listed(), 'SA,SP,SX,WC,h', 'editor at aal2: every official sheet but the deleted one'); select pg_temp.as_user('V', 'aal2'); select is(pg_temp.listed(), 'SA,SP,SX,WC,h', 'reviewer at aal2: the same list'); select pg_temp.as_user('P', 'aal2'); select is(pg_temp.listed(), 'SA,SP,SX,WC,h', 'publisher at aal2: the same list'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SD')) ->> 'slug' $$), '42501 not_found', 'deleted sheet: not_found'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('PS')) ->> 'slug' $$), '42501 not_found', 'personal sheet: not_found'); select is(pg_temp.val($$ select public.get_official_sheet_admin('22200000-0000-0000-0000-0000000000ff') ->> 'slug' $$), '42501 not_found', 'unknown id: not_found'); select is(pg_temp.val($$ select public.get_official_sheet_admin(null) ->> 'slug' $$), '42501 not_found', 'null id: not_found'); -- ============ What the list says ============ select pg_temp.as_user('V', 'aal2'); select is(pg_temp.summary('SP', '{name,fr}'), 'Brouillon', 'the name is the latest version''s'); select is(pg_temp.summary('SP', '{current_version,version_number}'), '1', 'current version: the published v1'); select is(pg_temp.summary('SP', '{draft_version,version_number}'), '2', 'draft version: v2'); select is(pg_temp.summary('SP', '{version_statuses}'), '["draft", "published"]', 'version states present'); select is(pg_temp.summary('SP', '{open_rounds}'), '1', 'one round awaiting review'); select is(pg_temp.summary('SP', '{latest_round,status}'), 'open', 'latest round: open'); select is(pg_temp.summary('SP', '{historical}') || '/' || pg_temp.summary('SP', '{working_copy}'), 'false/false', 'no badge on an ordinary sheet'); select is(pg_temp.summary('SA', '{status}'), 'archived', 'archived sheet listed with its state'); select is(pg_temp.summary('h', '{historical}'), 'true', 'historical badge'); select is(pg_temp.summary('h', '{working_copy_active}'), 'true', 'the historical sheet says its copy slot is taken'); select is(pg_temp.summary('WC', '{working_copy}'), 'true', 'working-copy badge'); select is(pg_temp.summary('WC', '{working_copy_active}'), null, 'the slot is read on the historical sheet only'); -- ============ What the overview says ============ select is(pg_temp.val($$ select jsonb_array_length(public.get_official_sheet_admin(pg_temp.id('SP')) -> 'versions')::text $$), '2', 'overview: two versions'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) #>> '{versions,0,version_number}' $$), '2', 'overview: latest version first'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) #>> '{versions,0,authors,0,display_name}' $$), 'M22 E', 'overview: authors named'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) #>> '{rounds,0,submitted_by,display_name}' $$), 'M22 E', 'overview: who submitted the round'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) #>> '{rounds,0,status}' $$), 'open', 'overview: round state'); select is(pg_temp.val($$ select (public.get_official_sheet_admin(pg_temp.id('SA')) ->> 'status') $$), 'archived', 'overview of an archived sheet'); select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('WC')) #>> '{working_copy_source,version_id}' $$), pg_temp.id('h.v')::text, 'overview: the working copy names its source'); -- ============ get_sheet_version: the fingerprint of a deprecated version (BDR-098) ============ select pg_temp.as_user('R', 'aal1'); select is(pg_temp.val($$ select public.get_sheet_version(pg_temp.id('SX.v1')) ->> 'fingerprint' $$), repeat('d', 64), 'reader: a deprecated version carries its publication fingerprint'); select is(pg_temp.val($$ select public.get_sheet_version(pg_temp.id('SX.v2')) ->> 'fingerprint' $$), repeat('e', 64), 'reader: a published version carries its fingerprint'); select pg_temp.as_user('E', 'aal2'); select is(pg_temp.val($$ select public.get_sheet_version(pg_temp.id('SP.v2')) ->> 'fingerprint' $$), 'ok', 'editor: a draft has no fingerprint'); -- ============ Privileges and nature ============ reset role; select ok(not has_function_privilege('anon', 'public.list_official_sheets_admin()', 'execute') and not has_function_privilege('anon', 'public.get_official_sheet_admin(uuid)', 'execute'), 'anon executes neither read'); select ok(not has_function_privilege('service_role', 'public.list_official_sheets_admin()', 'execute') and not has_function_privilege('service_role', 'public.get_official_sheet_admin(uuid)', 'execute'), 'service_role executes neither read'); select ok(not has_function_privilege('authenticated', 'private.official_sheet_admin_summary(uuid)', 'execute') and not has_function_privilege('authenticated', 'private.sheet_admin_person(uuid)', 'execute'), 'the helpers are not callable'); select is((select string_agg(p.provolatile::text, ',' order by p.proname) from pg_proc p join pg_namespace n on n.oid = p.pronamespace where n.nspname = 'public' and p.proname in ('list_official_sheets_admin', 'get_official_sheet_admin')), 's,s', 'both reads are stable: no write path'); -- ============ Species order range (owner 2026-11-02) ============ -- The stored order of an inactive species, read with the owner's rights (RLS hides inactive rows). create function pg_temp.stored_order(p_id text) returns text language plpgsql security definer as $$ begin return (select sort_order::text from public.species where id = p_id); end $$; grant execute on function pg_temp.stored_order(text) to authenticated; select pg_temp.as_user('E', 'aal2'); select is(pg_temp.val($$ select public.create_species('m22-neg', 'Négatif', 'Negative', -1) $$), 'P0001 invalid_input', 'create_species: -1 refused'); select is(pg_temp.val($$ select public.create_species('m22-big', 'Grand', 'Big', 10000) $$), 'P0001 invalid_input', 'create_species: 10 000 refused'); select is(pg_temp.val($$ select public.create_species('m22-zero', 'Zéro', 'Zero', 0) $$), 'm22-zero', 'create_species: 0 accepted'); select is(pg_temp.val($$ select public.create_species('m22-max', 'Max', 'Max', 9999) $$), 'm22-max', 'create_species: 9 999 accepted'); select is(pg_temp.val($$ select public.create_species('m22-null', 'Nul', 'Null', null) $$), 'm22-null', 'create_species: null accepted'); select is(coalesce(pg_temp.stored_order('m22-neg'), 'none') || '/' || coalesce(pg_temp.stored_order('m22-big'), 'none'), 'none/none', 'create_species: refused orders store nothing'); select is(pg_temp.stored_order('m22-zero') || ',' || pg_temp.stored_order('m22-max') || ',' || pg_temp.stored_order('m22-null'), '0,9999,100', 'create_species: stored orders, null keeps the default 100'); select is(pg_temp.val($$ select public.update_inactive_species('m22-zero', 'Zéro', 'Zero', -1) $$), 'P0001 invalid_input', 'update_inactive_species: -1 refused'); select is(pg_temp.val($$ select public.update_inactive_species('m22-zero', 'Zéro', 'Zero', 10000) $$), 'P0001 invalid_input', 'update_inactive_species: 10 000 refused'); select is(pg_temp.val($$ select public.update_inactive_species('m22-zero', 'Zéro', 'Zero', 9999) $$), 'm22-zero', 'update_inactive_species: 9 999 accepted'); select is(pg_temp.stored_order('m22-zero'), '9999', 'update_inactive_species: 9 999 stored'); select is(pg_temp.val($$ select public.update_inactive_species('m22-zero', 'Zéro', 'Zero', 0) $$), 'm22-zero', 'update_inactive_species: 0 accepted'); select is(pg_temp.stored_order('m22-zero'), '0', 'update_inactive_species: 0 stored'); select is(pg_temp.val($$ select public.update_inactive_species('m22-max', 'Max', 'Max', null) $$), 'm22-max', 'update_inactive_species: null accepted'); select is(pg_temp.stored_order('m22-max'), '9999', 'update_inactive_species: null keeps the stored order'); select pg_temp.as_user('V', 'aal2'); select is(pg_temp.val($$ select public.create_species('m22-rev', 'Rev', 'Rev', 5) $$), '42501 rights.capability', 'create_species: a reviewer is still refused'); reset role; select is((select string_agg(p.prosecdef::text || '/' || (p.proconfig = array['search_path=""'])::text, ',' order by p.proname) from pg_proc p join pg_namespace n on n.oid = p.pronamespace where n.nspname = 'public' and p.proname in ('create_species', 'update_inactive_species')), 'true/true,true/true', 'both species RPCs: security definer, empty search_path'); select * from finish(); rollback;