From b0a4cbe58819cec32757b1dd2ef8fd70ee01eaa3 Mon Sep 17 00:00:00 2001 From: bchanot Date: Mon, 2 Nov 2026 16:19:48 +0100 Subject: [PATCH] feat(supabase): M7 admin read RPCs for official sheets (V1-2.5 B) list_official_sheets_admin and get_official_sheet_admin: badges, version and round states, named actors; capability holders at aal2, one answer for unknown or forbidden ids, no write path. pgTAP 022 (40); sandbox fresh and upgrade from M6: 1938 pass, types match. --- packages/supabase/src/database.types.ts | 5 + .../supabase/tests/db/schema-types.test.ts | 2 + .../20261102100000_sheet_admin_reads.sql | 190 ++++++++++++++++++ supabase/tests/022_sheet_admin_reads.sql | 183 +++++++++++++++++ 4 files changed, 380 insertions(+) create mode 100644 supabase/migrations/20261102100000_sheet_admin_reads.sql create mode 100644 supabase/tests/022_sheet_admin_reads.sql diff --git a/packages/supabase/src/database.types.ts b/packages/supabase/src/database.types.ts index ac6a44a0..106d1a43 100644 --- a/packages/supabase/src/database.types.ts +++ b/packages/supabase/src/database.types.ts @@ -1215,6 +1215,7 @@ export type Database = { Args: { code: string; lease_token: string; review_id: string } Returns: string } + get_official_sheet_admin: { Args: { sheet_id: string }; Returns: Json } get_sheet_asset_state: { Args: { asset_id: string }; Returns: Json } get_sheet_verification_input: { Args: { lease_token: string; review_id: string } @@ -1266,6 +1267,10 @@ export type Database = { }[] } list_official_catalog: { Args: Record; Returns: Json } + list_official_sheets_admin: { + Args: Record + Returns: Json + } lookup_catalog_account: { Args: { email: string }; Returns: Json } mark_quarantine_purged: { Args: { asset_id: string }; Returns: undefined } publish_sheet_version: { Args: { review_id: string }; Returns: string } diff --git a/packages/supabase/tests/db/schema-types.test.ts b/packages/supabase/tests/db/schema-types.test.ts index 18edfa3c..b0b0f972 100644 --- a/packages/supabase/tests/db/schema-types.test.ts +++ b/packages/supabase/tests/db/schema-types.test.ts @@ -259,12 +259,14 @@ const expectedFunctionArgs = { create_species: ['id', 'name_en', 'name_fr', 'sort_order'], create_sheet_working_copy: ['slug', 'source_version_id'], decide_sheet_review: ['approve', 'note', 'review_id'], + get_official_sheet_admin: ['sheet_id'], get_sheet_asset_state: ['asset_id'], get_sheet_version: ['version_id'], grant_catalog_right: ['expires_at', 'kind', 'reason', 'user_id', 'verification_note'], list_catalog_grants: [], list_catalog_species: [], list_official_catalog: [], + list_official_sheets_admin: [], lookup_catalog_account: ['email'], rename_species: ['id', 'name_en', 'name_fr'], revoke_catalog_right: ['grant_id', 'reason'], diff --git a/supabase/migrations/20261102100000_sheet_admin_reads.sql b/supabase/migrations/20261102100000_sheet_admin_reads.sql new file mode 100644 index 00000000..81a65588 --- /dev/null +++ b/supabase/migrations/20261102100000_sheet_admin_reads.sql @@ -0,0 +1,190 @@ +-- V1-2.5 M7 `sheet_admin_reads`: the read RPCs of the administration space. +-- docs/development/V1-2_ANATOMICAL_SHEETS.md §11.1 (`/app/admin/sheets`, its +-- overview), §12.3 (M7), §16.6; owner decision 2026-11-02 (BDR-097). +-- +-- The admin list shows the historical and working-copy badges, the version +-- and round states and the rounds awaiting review. The reader column grants +-- of M4 leave out `working_copy_source_version_id`, `last_version_number`, +-- `updated_at` and the version authors on purpose (§5.3), so the admin space +-- reads them here, never through a wider grant: +-- * list_official_sheets_admin: every official sheet that is not deleted, +-- one summary each; +-- * get_official_sheet_admin: one sheet's summary, its versions and its +-- rounds, with the names of the people who acted. +-- Capability holders at aal2 only (`caller_is_catalog_staff`, the predicate +-- of the draft reads); an unknown or forbidden sheet gets one answer. No +-- write, no grant, no policy and no rule changes here. + +-- =========================================================================== +-- 1. Helpers +-- =========================================================================== +-- An account as the admin space names it: id and display name. +create or replace function private.sheet_admin_person(p_user_id uuid) +returns jsonb +language sql +stable +security definer +set search_path = '' +as $$ + select case when p_user_id is null then null else jsonb_build_object( + 'user_id', p_user_id, + 'display_name', (select p.display_name from public.profiles p where p.id = p_user_id) + ) end; +$$; + +-- One official sheet as the admin list shows it. +create or replace function private.official_sheet_admin_summary(p_sheet_id uuid) +returns jsonb +language sql +stable +security definer +set search_path = '' +as $$ + select jsonb_build_object( + 'sheet_id', s.id, + 'species_id', s.species_id, + 'slug', s.slug, + 'status', s.status, + 'archived_at', s.archived_at, + 'created_at', s.created_at, + 'updated_at', s.updated_at, + 'name', coalesce(latest.content -> 'name', jsonb_build_object('fr', s.name, 'en', s.name)), + 'historical', s.id = private.historical_sheet_id(), + 'working_copy', private.is_historical_working_copy(s.id), + -- The single slot of §14.3, read where the action is offered. + 'working_copy_active', case when s.id = private.historical_sheet_id() + then private.historical_working_copy_active(null) end, + 'latest_version', case when latest.id is not null then jsonb_build_object( + 'version_id', latest.id, 'version_number', latest.version_number, + 'status', latest.status, 'revision', latest.revision) end, + 'current_version', case when cur.id is not null then jsonb_build_object( + 'version_id', cur.id, 'version_number', cur.version_number, 'published_at', cur.published_at) end, + 'draft_version', ( + select jsonb_build_object('version_id', d.id, 'version_number', d.version_number, 'revision', d.revision) + from public.anatomical_sheet_versions d + where d.sheet_id = s.id and d.status = 'draft' + order by d.version_number desc limit 1), + 'version_statuses', coalesce(( + select jsonb_agg(distinct v.status) from public.anatomical_sheet_versions v where v.sheet_id = s.id), + '[]'::jsonb), + 'open_rounds', ( + select count(*) from public.anatomical_sheet_reviews r + join public.anatomical_sheet_versions v on v.id = r.version_id + where v.sheet_id = s.id and r.status = 'open'), + 'latest_round', ( + select jsonb_build_object('review_id', r.id, 'version_number', v.version_number, 'round', r.round, + 'status', r.status, 'submitted_at', r.submitted_at) + from public.anatomical_sheet_reviews r + join public.anatomical_sheet_versions v on v.id = r.version_id + where v.sheet_id = s.id + order by r.submitted_at desc nulls last, v.version_number desc, r.round desc limit 1) + ) + from public.anatomical_sheets s + left join public.anatomical_sheet_versions cur on cur.id = s.current_version_id + left join lateral ( + select v.* from public.anatomical_sheet_versions v + where v.sheet_id = s.id order by v.version_number desc limit 1 + ) latest on true + where s.id = p_sheet_id; +$$; + +-- =========================================================================== +-- 2. Read RPCs +-- =========================================================================== +create or replace function public.list_official_sheets_admin() +returns jsonb +language plpgsql +stable +security definer +set search_path = '' +as $$ +begin + perform private.require_catalog_actor(); + if not private.caller_is_catalog_staff() then + raise exception 'rights.capability' using errcode = '42501'; + end if; + return coalesce(( + select jsonb_agg(private.official_sheet_admin_summary(s.id) order by s.species_id, s.slug) + from public.anatomical_sheets s + where s.is_official and s.status <> 'deleted' + ), '[]'::jsonb); +end; +$$; + +create or replace function public.get_official_sheet_admin(sheet_id uuid) +returns jsonb +language plpgsql +stable +security definer +set search_path = '' +as $$ +#variable_conflict use_column +begin + perform private.require_catalog_actor(); + -- Unknown, deleted, personal and forbidden sheets: one answer (§10). + if get_official_sheet_admin.sheet_id is null + or not private.caller_can_see_official_sheet(get_official_sheet_admin.sheet_id) then + raise exception 'not_found' using errcode = '42501'; + end if; + return private.official_sheet_admin_summary(get_official_sheet_admin.sheet_id) || jsonb_build_object( + 'working_copy_source', ( + select jsonb_build_object('version_id', src.id, 'version_number', src.version_number) + from public.anatomical_sheets s + join public.anatomical_sheet_versions src on src.id = s.working_copy_source_version_id + where s.id = get_official_sheet_admin.sheet_id), + 'versions', coalesce(( + select jsonb_agg(jsonb_build_object( + 'version_id', v.id, + 'version_number', v.version_number, + 'status', v.status, + 'revision', v.revision, + 'change_note', v.change_note, + 'created_at', v.created_at, + 'published_at', v.published_at, + 'deprecated_at', v.deprecated_at, + 'source_version_number', (select src.version_number from public.anatomical_sheet_versions src + where src.id = v.source_version_id), + 'authors', coalesce((select jsonb_agg(private.sheet_admin_person(a.id) order by a.ord) + from unnest(v.authors) with ordinality a(id, ord)), '[]'::jsonb) + ) order by v.version_number desc) + from public.anatomical_sheet_versions v + where v.sheet_id = get_official_sheet_admin.sheet_id), '[]'::jsonb), + 'rounds', coalesce(( + select jsonb_agg(jsonb_build_object( + 'review_id', r.id, + 'version_id', v.id, + 'version_number', v.version_number, + 'round', r.round, + 'revision', r.revision, + 'status', r.status, + 'submitted_by', private.sheet_admin_person(r.submitted_by), + 'submitted_at', r.submitted_at, + 'verified_at', r.verified_at, + 'failure_outcome', r.failure_outcome, + 'failure_code', r.failure_code, + 'decided_by', private.sheet_admin_person(r.decided_by), + 'decided_at', r.decided_at, + 'withdrawn_at', r.withdrawn_at + ) order by r.submitted_at desc nulls last, v.version_number desc, r.round desc) + from public.anatomical_sheet_reviews r + join public.anatomical_sheet_versions v on v.id = r.version_id + where v.sheet_id = get_official_sheet_admin.sheet_id), '[]'::jsonb) + ); +end; +$$; + +comment on function public.list_official_sheets_admin() is + 'Capability holders, live aal2 session: every official sheet that is not deleted, with its historical and working-copy badges, version and round states (V1-2 contract §11.1, M7).'; +comment on function public.get_official_sheet_admin(uuid) is + 'Capability holders, live aal2 session: one official sheet''s summary, versions (authors named) and rounds (actors named); unknown and forbidden ids answer not_found (V1-2 contract §11.1, M7).'; + +-- =========================================================================== +-- 3. Privileges +-- =========================================================================== +revoke all on function private.sheet_admin_person(uuid) from public, anon, authenticated, service_role; +revoke all on function private.official_sheet_admin_summary(uuid) from public, anon, authenticated, service_role; + +revoke all on function public.list_official_sheets_admin() from public, anon, service_role; +revoke all on function public.get_official_sheet_admin(uuid) from public, anon, service_role; +grant execute on function public.list_official_sheets_admin() to authenticated; +grant execute on function public.get_official_sheet_admin(uuid) to authenticated; diff --git a/supabase/tests/022_sheet_admin_reads.sql b/supabase/tests/022_sheet_admin_reads.sql new file mode 100644 index 00000000..82ae7ef7 --- /dev/null +++ b/supabase/tests/022_sheet_admin_reads.sql @@ -0,0 +1,183 @@ +-- 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(40); + +-- 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'), + ('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')); +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); +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,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,WC,h', 'reviewer at aal2: the same list'); +select pg_temp.as_user('P', 'aal2'); +select is(pg_temp.listed(), 'SA,SP,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'); + +-- ============ 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'); + +select * from finish(); +rollback;