156 lines
12 KiB
PL/PgSQL
156 lines
12 KiB
PL/PgSQL
-- V1-2.6 M8: `get_official_sheet_admin` names who saved the draft's current
|
|
-- revision (contract §12.3, §16.7). Null on a fresh draft, on a sheet without
|
|
-- draft and after a content write that is not a save; same refusals as M7, no
|
|
-- new privilege. Fixtures are scoped to the ids created here.
|
|
begin;
|
|
select plan(27);
|
|
|
|
-- Accounts. O owner, A admin, E and F admin + editor, R reader, N no right.
|
|
create table pg_temp.who (label text primary key, id uuid not null);
|
|
insert into pg_temp.who values
|
|
('O', '23000000-0000-0000-0000-000000000001'), ('A', '23000000-0000-0000-0000-00000000000a'),
|
|
('E', '23000000-0000-0000-0000-00000000000e'), ('F', '23000000-0000-0000-0000-00000000000f'),
|
|
('R', '23000000-0000-0000-0000-0000000000cc'), ('N', '23000000-0000-0000-0000-0000000000c5');
|
|
insert into auth.users (id, email, email_confirmed_at, raw_user_meta_data)
|
|
select id, lower(label) || '@m23.local', now(), jsonb_build_object('display_name', 'M23 ' || 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 ('23100000' || substr(id::text, 9))::uuid, id, 'aal2' from pg_temp.who;
|
|
|
|
select set_config('request.jwt.claims', '', true);
|
|
select private.assign_platform_role(null, '23000000-0000-0000-0000-000000000001', 'owner');
|
|
select private.assign_platform_role(null, '23000000-0000-0000-0000-00000000000a', 'admin');
|
|
select private.assign_platform_role(null, '23000000-0000-0000-0000-00000000000e', 'admin');
|
|
select private.assign_platform_role(null, '23000000-0000-0000-0000-00000000000f', 'admin');
|
|
insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by) values
|
|
('23000000-0000-0000-0000-00000000000e', 'sheet_editor', 'a', 'c', '23000000-0000-0000-0000-000000000001'),
|
|
('23000000-0000-0000-0000-00000000000f', 'sheet_editor', 'a', 'c', '23000000-0000-0000-0000-000000000001'),
|
|
('23000000-0000-0000-0000-0000000000cc', 'catalog_reader', 'a', null, '23000000-0000-0000-0000-000000000001');
|
|
|
|
-- Sheets: SP with a published v1 and a draft v2, SA with a published v1 only,
|
|
-- SD deleted, PS personal.
|
|
create table pg_temp.k (key text primary key, id uuid not null);
|
|
insert into pg_temp.k values
|
|
('SP', '23200000-0000-0000-0000-000000000001'), ('SA', '23200000-0000-0000-0000-000000000002'),
|
|
('SD', '23200000-0000-0000-0000-000000000003'), ('PS', '23200000-0000-0000-0000-000000000004'),
|
|
('SP.v1', '23300000-0000-0000-0000-000000000011'), ('SP.v2', '23300000-0000-0000-0000-000000000012'),
|
|
('SA.v1', '23300000-0000-0000-0000-000000000021');
|
|
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":"23000000-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', 'm23-sp', true, 'private', 'active', null, null, null, null),
|
|
(pg_temp.id('SA'), 'equine', 'SA', 'm23-sa', true, 'private', 'active', null, null, null, null),
|
|
(pg_temp.id('SD'), 'equine', 'SD', 'm23-sd', true, 'private', 'deleted', null, now(), null, null),
|
|
(pg_temp.id('PS'), 'equine', 'PS', 'm23-ps', false, 'private', 'active', null, null, '23000000-0000-0000-0000-00000000000e', 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));
|
|
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');
|
|
|
|
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 ('23100000' || 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 $$;
|
|
-- A path of the sheet's admin overview, as text ('null' for a JSON null).
|
|
create function pg_temp.last_save(p_key text, p_path text default '{}') returns text language plpgsql as $$
|
|
begin
|
|
return coalesce((public.get_official_sheet_admin(pg_temp.id(p_key)) -> 'draft_last_save') #>> p_path::text[],
|
|
(public.get_official_sheet_admin(pg_temp.id(p_key)) -> 'draft_last_save')::text);
|
|
exception when others then
|
|
return sqlstate || ' ' || sqlerrm;
|
|
end $$;
|
|
-- The saved_at of the overview equals the audit row's time.
|
|
create function pg_temp.audit_time(p_key text, p_revision int) returns text language sql as $$
|
|
select max(occurred_at)::text from audit.audit_logs where resource_id = pg_temp.id(p_key)
|
|
and action = 'anatomical_sheet_version.saved' and metadata ->> 'revision' = p_revision::text;
|
|
$$;
|
|
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: the refusals of M7 ============
|
|
select pg_temp.as_user('O', 'aal2');
|
|
select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 not_found', 'owner without a capability: refused');
|
|
select pg_temp.as_user('A', 'aal2');
|
|
select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 not_found', 'admin without a capability: refused');
|
|
select pg_temp.as_user('R', 'aal2');
|
|
select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 not_found', 'catalogue reader: refused');
|
|
select pg_temp.as_user('N', 'aal2');
|
|
select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 not_found', 'account without a right: refused');
|
|
select pg_temp.as_user('E', 'aal1');
|
|
select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 rights.mfa', 'editor at aal1: refused');
|
|
select pg_temp.as_user('E', 'aal2');
|
|
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('23200000-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');
|
|
|
|
-- ============ Null cases ============
|
|
select is(pg_temp.last_save('SP'), 'null', 'fresh draft: draft_last_save is null');
|
|
select is(pg_temp.val($$ select (public.get_official_sheet_admin(pg_temp.id('SP')) ? 'draft_last_save')::text $$), 'true', 'the key is present, null');
|
|
select is(pg_temp.last_save('SA'), 'null', 'sheet without a draft: null');
|
|
|
|
-- ============ After a save ============
|
|
select is(pg_temp.val($$ select public.save_sheet_draft(pg_temp.id('SP.v2'), 0, pg_temp.doc('Un'), 'one') ->> 'revision' $$), '1', 'E saves revision 1');
|
|
select is(pg_temp.last_save('SP', '{revision}'), '1', 'after a save: the revision saved');
|
|
select is(pg_temp.last_save('SP', '{saved_by,display_name}'), 'M23 E', 'after a save: the actor named');
|
|
select is(pg_temp.last_save('SP', '{saved_by,user_id}'), '23000000-0000-0000-0000-00000000000e', 'after a save: the actor id');
|
|
select is(pg_temp.val($$ select (public.get_official_sheet_admin(pg_temp.id('SP')) #>> '{draft_last_save,saved_at}')::timestamptz::text $$),
|
|
pg_temp.audit_time('SP.v2', 1), 'after a save: the time is the audit row''s');
|
|
|
|
-- ============ A second editor saves ============
|
|
select pg_temp.as_user('F', 'aal2');
|
|
select is(pg_temp.last_save('SP', '{saved_by,display_name}'), 'M23 E', 'F reads the save of E');
|
|
select is(pg_temp.val($$ select public.save_sheet_draft(pg_temp.id('SP.v2'), 1, pg_temp.doc('Deux'), 'two') ->> 'revision' $$), '2', 'F saves revision 2');
|
|
select is(pg_temp.last_save('SP', '{revision}') || '/' || pg_temp.last_save('SP', '{saved_by,display_name}'), '2/M23 F', 'after a second save: the second editor');
|
|
select pg_temp.as_user('E', 'aal2');
|
|
select is(pg_temp.last_save('SP', '{saved_by,display_name}'), 'M23 F', 'E reads the save of F');
|
|
|
|
-- ============ A content write that is not a save ============
|
|
reset role;
|
|
select set_config('request.jwt.claims', '{"sub":"23000000-0000-0000-0000-00000000000e"}', true);
|
|
update public.anatomical_sheet_versions set content = pg_temp.doc('Directe') where id = pg_temp.id('SP.v2');
|
|
select pg_temp.as_user('E', 'aal2');
|
|
select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) #>> '{draft_version,revision}' $$), '3', 'the draft moved to revision 3');
|
|
select is(pg_temp.last_save('SP'), 'null', 'revision written outside a save: null');
|
|
select is(pg_temp.val($$ select public.save_sheet_draft(pg_temp.id('SP.v2'), 3, pg_temp.doc('Quatre'), 'four') ->> 'revision' $$), '4', 'E saves revision 4');
|
|
select is(pg_temp.last_save('SP', '{revision}') || '/' || pg_temp.last_save('SP', '{saved_by,display_name}'), '4/M23 E', 'a save after it names the saver again');
|
|
|
|
-- ============ Privileges and nature ============
|
|
reset role;
|
|
select ok(not has_function_privilege('anon', 'public.get_official_sheet_admin(uuid)', 'execute')
|
|
and not has_function_privilege('service_role', 'public.get_official_sheet_admin(uuid)', 'execute')
|
|
and has_function_privilege('authenticated', 'public.get_official_sheet_admin(uuid)', 'execute'), 'privileges as M7: authenticated only');
|
|
select is((select p.provolatile::text || '/' || p.prosecdef::text || '/' || (p.proconfig = array['search_path=""'])::text
|
|
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
|
where n.nspname = 'public' and p.proname = 'get_official_sheet_admin'), 's/true/true',
|
|
'stable, security definer, empty search_path');
|
|
|
|
select * from finish();
|
|
rollback;
|