639 lines
36 KiB
PL/PgSQL
639 lines
36 KiB
PL/PgSQL
-- Anatomical sheets: schema contract, lifecycle (allowed path), immutability,
|
|
-- copies with provenance, deletion preconditions, invalid documents, every
|
|
-- denied path (other user, anon, direct writes, official sheets).
|
|
-- Counts are scoped to fixture ids: the real stack also carries seed rows.
|
|
begin;
|
|
select plan(127);
|
|
|
|
-- ---------------------------------------------------------------------------
|
|
-- Fixtures
|
|
-- ---------------------------------------------------------------------------
|
|
insert into auth.users (id, email) values
|
|
('20000000-0000-0000-0000-000000000001', 'root-sheets@test.local'),
|
|
('20000000-0000-0000-0000-00000000000a', 'alice-sheets@test.local'),
|
|
('20000000-0000-0000-0000-00000000000b', 'bob-sheets@test.local');
|
|
select private.assign_platform_role(null, '20000000-0000-0000-0000-000000000001', 'owner');
|
|
|
|
-- A small but complete document: two views (detail without its own asset),
|
|
-- a trigger, a bilateral joint, a spine zone, one finding zone.
|
|
create function pg_temp.sheet_doc(p_asset uuid)
|
|
returns jsonb
|
|
language sql
|
|
as $$
|
|
select jsonb_build_object(
|
|
'schema_version', 2,
|
|
'species_id', 'equine',
|
|
'name', jsonb_build_object('fr', 'Cheval test', 'en', 'Test horse'),
|
|
'description', '{}'::jsonb,
|
|
'scope_disclaimer', '{}'::jsonb,
|
|
'attribution', jsonb_build_object('authors', '[]'::jsonb, 'license', null, 'notes', null),
|
|
'vertebral_config', jsonb_build_object('regions', jsonb_build_array(
|
|
jsonb_build_object('code', 'C', 'name', jsonb_build_object('fr', 'Cervicales', 'en', 'Cervical'), 'count', 7)
|
|
)),
|
|
'references', '[]'::jsonb,
|
|
'categories', jsonb_build_array(
|
|
jsonb_build_object('id', 'articulations', 'name', jsonb_build_object('fr', 'Articulations', 'en', 'Joints'), 'color', null)
|
|
),
|
|
'views', jsonb_build_array(
|
|
jsonb_build_object('id', 'view-main', 'kind', 'main',
|
|
'name', jsonb_build_object('fr', 'Vue principale', 'en', 'Main view'),
|
|
'svg_asset_id', p_asset, 'view_box', null),
|
|
jsonb_build_object('id', 'view-head', 'kind', 'detail', 'parent_view_id', 'view-main',
|
|
'name', jsonb_build_object('fr', 'Tête', 'en', 'Head'),
|
|
'svg_asset_id', null,
|
|
'view_box', jsonb_build_object('min_x', 60, 'min_y', 100, 'width', 500, 'height', 380))
|
|
),
|
|
'annotations', jsonb_build_array(
|
|
jsonb_build_object('id', 'hip', 'kind', 'point', 'view_id', 'view-main', 'category_id', 'articulations',
|
|
'laterality', 'bilateral', 'geometry', null,
|
|
'name', jsonb_build_object('fr', 'Hanche', 'en', 'Hip joint'),
|
|
'finding_zone_id', 'hip', 'vertebral_region_code', null),
|
|
jsonb_build_object('id', 'cervical', 'kind', 'zone', 'view_id', 'view-main', 'category_id', 'articulations',
|
|
'laterality', 'unpaired', 'geometry', null,
|
|
'name', jsonb_build_object('fr', 'Vertèbres cervicales', 'en', 'Cervical vertebrae'),
|
|
'finding_zone_id', null, 'vertebral_region_code', 'C'),
|
|
jsonb_build_object('id', 'head-trigger', 'kind', 'detail_trigger', 'view_id', 'view-main',
|
|
'category_id', 'articulations', 'laterality', 'unpaired', 'geometry', null,
|
|
'name', null, 'opens_view_id', 'view-head')
|
|
),
|
|
'finding_zones', jsonb_build_array(
|
|
jsonb_build_object('id', 'hip', 'name', jsonb_build_object('fr', 'Hanche', 'en', 'Hip'),
|
|
'categories', jsonb_build_array(
|
|
jsonb_build_object('id', 'hip-dysfunctions', 'kind', 'dysfunction',
|
|
'name', jsonb_build_object('fr', 'Dysfonctions', 'en', 'Dysfunctions'),
|
|
'children', jsonb_build_array(
|
|
jsonb_build_object('id', 'articular', 'name', jsonb_build_object('fr', 'Articulaire', 'en', 'Articular'))
|
|
))
|
|
))
|
|
)
|
|
);
|
|
$$;
|
|
|
|
create table pg_temp.ids (key text primary key, id uuid not null);
|
|
create function pg_temp.id(p_key text) returns uuid language sql stable
|
|
as $$ select id from pg_temp.ids where key = p_key $$;
|
|
-- The fixtures are read while impersonating the API roles.
|
|
grant all on pg_temp.ids to anon, authenticated;
|
|
grant execute on function pg_temp.id(text), pg_temp.sheet_doc(uuid) to anon, authenticated;
|
|
|
|
-- ---------------------------------------------------------------------------
|
|
-- 1. Schema contract
|
|
-- ---------------------------------------------------------------------------
|
|
select has_table('public', 'species', 'table species exists');
|
|
select has_table('public', 'anatomical_sheets', 'table anatomical_sheets exists');
|
|
select has_table('public', 'anatomical_sheet_versions', 'table anatomical_sheet_versions exists');
|
|
select has_table('public', 'anatomical_sheet_assets', 'table anatomical_sheet_assets exists');
|
|
select enum_has_labels('public', 'sheet_visibility', array['private', 'public'], 'sheet_visibility labels');
|
|
select enum_has_labels('public', 'sheet_status', array['active', 'archived', 'deleted'], 'sheet_status labels');
|
|
select enum_has_labels('public', 'sheet_version_status', array['draft', 'published', 'deprecated'], 'sheet_version_status labels');
|
|
select is(
|
|
(select count(*) from public.species where id = 'equine' and active), 1::bigint,
|
|
'species equine is seeded by the migration'
|
|
);
|
|
select ok(
|
|
(select bool_and(relrowsecurity) from pg_class
|
|
where oid in ('public.species'::regclass, 'public.anatomical_sheets'::regclass,
|
|
'public.anatomical_sheet_versions'::regclass, 'public.anatomical_sheet_assets'::regclass)),
|
|
'RLS enabled on the four tables'
|
|
);
|
|
select ok(not has_table_privilege('anon', 'public.anatomical_sheets', 'select'), 'anon cannot select sheets');
|
|
select ok(not has_table_privilege('anon', 'public.anatomical_sheet_versions', 'select'), 'anon cannot select versions');
|
|
select ok(not has_table_privilege('anon', 'public.species', 'select'), 'anon cannot select species');
|
|
select ok(not has_table_privilege('authenticated', 'public.anatomical_sheets', 'insert'), 'authenticated cannot insert sheets directly');
|
|
select ok(not has_table_privilege('authenticated', 'public.anatomical_sheets', 'delete'), 'authenticated cannot delete sheets directly');
|
|
select ok(not has_column_privilege('authenticated', 'public.anatomical_sheets', 'status', 'update'), 'authenticated cannot update sheets.status');
|
|
select ok(not has_column_privilege('authenticated', 'public.anatomical_sheets', 'is_official', 'update'), 'authenticated cannot update sheets.is_official');
|
|
select ok(not has_table_privilege('authenticated', 'public.anatomical_sheet_versions', 'insert'), 'authenticated cannot insert versions directly');
|
|
select ok(not has_column_privilege('authenticated', 'public.anatomical_sheet_versions', 'status', 'update'), 'authenticated cannot update versions.status');
|
|
select ok(has_column_privilege('authenticated', 'public.anatomical_sheet_versions', 'content', 'update'), 'authenticated may update versions.content (RLS: own draft)');
|
|
select ok(not has_table_privilege('authenticated', 'public.anatomical_sheet_assets', 'insert'), 'authenticated cannot insert assets directly');
|
|
select ok(
|
|
not (has_column_privilege('authenticated', 'public.anatomical_sheets', 'visibility', 'update')
|
|
or has_column_privilege('authenticated', 'public.anatomical_sheets', 'creator_user_id', 'update')
|
|
or has_column_privilege('authenticated', 'public.anatomical_sheets', 'current_version_id', 'update')
|
|
or has_column_privilege('authenticated', 'public.anatomical_sheets', 'based_on_sheet_id', 'update')
|
|
or has_column_privilege('authenticated', 'public.anatomical_sheets', 'based_on_version_id', 'update')
|
|
or has_column_privilege('authenticated', 'public.anatomical_sheets', 'species_id', 'update')),
|
|
'visibility, ownership, provenance, current version and species are not user-updatable'
|
|
);
|
|
select ok(not has_table_privilege('authenticated', 'public.anatomical_sheet_versions', 'delete'), 'authenticated cannot delete versions directly (RPC only)');
|
|
select is(
|
|
(select count(*) from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
|
where n.nspname = 'public' and p.prokind = 'f'
|
|
and not exists (select 1 from pg_depend d where d.objid = p.oid and d.deptype = 'e')
|
|
and has_function_privilege('anon', p.oid, 'execute')),
|
|
0::bigint, 'anon can execute no public RPC'
|
|
);
|
|
|
|
-- ---------------------------------------------------------------------------
|
|
-- 2. Alice creates a private sheet (draft v1), registers an asset, publishes
|
|
-- ---------------------------------------------------------------------------
|
|
set local role authenticated;
|
|
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
|
|
|
insert into pg_temp.ids select 'sheet', public.create_anatomical_sheet(
|
|
'equine', 'Mon cheval', 'mon-cheval', pg_temp.sheet_doc(null), 'perso'
|
|
);
|
|
select results_eq(
|
|
$$ select creator_user_id, status::text, visibility::text, current_version_id, is_official
|
|
from public.anatomical_sheets where id = pg_temp.id('sheet') $$,
|
|
$$ values ('20000000-0000-0000-0000-00000000000a'::uuid, 'active', 'private', null::uuid, false) $$,
|
|
'created sheet: creator alice, active, private, no current version'
|
|
);
|
|
insert into pg_temp.ids select 'v1', id from public.anatomical_sheet_versions
|
|
where sheet_id = pg_temp.id('sheet') and version_number = 1;
|
|
select is(
|
|
(select status::text from public.anatomical_sheet_versions where id = pg_temp.id('v1')),
|
|
'draft', 'version 1 is a draft'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.create_anatomical_sheet('unicorn', 'x', 'x-x', pg_temp.sheet_doc(null)) $$,
|
|
'P0002', null, 'unknown species is refused'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.create_anatomical_sheet('equine', 'Dup', 'mon-cheval', pg_temp.sheet_doc(null)) $$,
|
|
'23505', null, 'slug is unique per owner'
|
|
);
|
|
|
|
select throws_ok(
|
|
$$ select public.create_anatomical_sheet('equine', 'Bad', 'bad-doc', '{"schema_version":"abc","species_id":"equine"}'::jsonb) $$,
|
|
'23514', null, 'a non-numeric schema_version is refused'
|
|
);
|
|
select throws_ok(
|
|
$$ update public.anatomical_sheet_versions
|
|
set content = jsonb_build_object('schema_version', 2, 'species_id', 'equine', 'pad', repeat('x', 2200000))
|
|
where id = pg_temp.id('v1') $$,
|
|
'23514', null, 'a document over 2 MiB is refused'
|
|
);
|
|
|
|
-- Publishing a draft whose main view has no asset is refused with the rule code
|
|
select throws_ok(
|
|
$$ select public.publish_sheet_version(pg_temp.id('v1')) $$,
|
|
'23514', 'document not publishable: view.asset.missing', 'publish refuses a main view without asset'
|
|
);
|
|
|
|
insert into pg_temp.ids select 'asset', public.register_sheet_asset(
|
|
pg_temp.id('sheet'), repeat('a', 64), 142400, 'Horse.svg'
|
|
);
|
|
select is(
|
|
public.register_sheet_asset(pg_temp.id('sheet'), repeat('a', 64), 142400, 'Horse.svg'),
|
|
pg_temp.id('asset'), 'registering the same sha256 again returns the same asset'
|
|
);
|
|
|
|
-- Draft autosave: direct UPDATE of content through the policy
|
|
update public.anatomical_sheet_versions
|
|
set content = pg_temp.sheet_doc(pg_temp.id('asset')), change_note = 'asset wired'
|
|
where id = pg_temp.id('v1');
|
|
select is(
|
|
(select content -> 'views' -> 0 ->> 'svg_asset_id' from public.anatomical_sheet_versions where id = pg_temp.id('v1')),
|
|
pg_temp.id('asset')::text, 'editor updated the draft content in place'
|
|
);
|
|
|
|
-- Unknown asset uuid is refused at publish
|
|
update public.anatomical_sheet_versions
|
|
set content = pg_temp.sheet_doc('ffffffff-ffff-ffff-ffff-ffffffffffff')
|
|
where id = pg_temp.id('v1');
|
|
select throws_ok(
|
|
$$ select public.publish_sheet_version(pg_temp.id('v1')) $$,
|
|
'23514', null, 'publish refuses a view pointing at an asset not registered for the sheet'
|
|
);
|
|
update public.anatomical_sheet_versions
|
|
set content = pg_temp.sheet_doc(pg_temp.id('asset')) where id = pg_temp.id('v1');
|
|
|
|
-- Document species must match the sheet species (trigger)
|
|
select throws_ok(
|
|
$$ update public.anatomical_sheet_versions
|
|
set content = jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{species_id}', '"canine"')
|
|
where id = pg_temp.id('v1') $$,
|
|
'23514', null, 'a document for another species is refused'
|
|
);
|
|
-- Dangling references are refused at publish
|
|
update public.anatomical_sheet_versions
|
|
set content = jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{annotations,0,finding_zone_id}', '"nowhere"')
|
|
where id = pg_temp.id('v1');
|
|
select throws_ok(
|
|
$$ select public.publish_sheet_version(pg_temp.id('v1')) $$,
|
|
'23514', 'document not publishable: annotation.finding_zone.missing',
|
|
'publish refuses an annotation pointing at a missing finding zone'
|
|
);
|
|
update public.anatomical_sheet_versions
|
|
set content = jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{annotations,1,id}', '"hip"')
|
|
where id = pg_temp.id('v1');
|
|
select throws_ok(
|
|
$$ select public.publish_sheet_version(pg_temp.id('v1')) $$,
|
|
'23514', 'document not publishable: annotation.id.duplicate',
|
|
'publish refuses duplicate annotation ids'
|
|
);
|
|
create function pg_temp.publish_issue(p_doc jsonb) returns text language plpgsql as $$
|
|
begin
|
|
update public.anatomical_sheet_versions set content = p_doc where id = pg_temp.id('v1');
|
|
perform public.publish_sheet_version(pg_temp.id('v1'));
|
|
return 'published';
|
|
exception when check_violation then
|
|
return sqlerrm;
|
|
end $$;
|
|
grant execute on function pg_temp.publish_issue(jsonb) to authenticated;
|
|
select is(
|
|
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{views,0,kind}', '"detail"')),
|
|
'document not publishable: view.main.count, view.parent.missing', 'SQL gate: no main view'
|
|
);
|
|
select is(
|
|
pg_temp.publish_issue(pg_temp.sheet_doc(pg_temp.id('asset')) #- '{annotations,2}'),
|
|
'document not publishable: view.unreachable', 'SQL gate: detail view without trigger'
|
|
);
|
|
select is(
|
|
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{views,1,parent_view_id}', '"nowhere"')),
|
|
'document not publishable: view.parent.missing, annotation.opens_view.invalid', 'SQL gate: dangling parent view'
|
|
);
|
|
select is(
|
|
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{views,1,id}', '"view-main"')),
|
|
'document not publishable: view.unreachable, view.id.duplicate, annotation.opens_view.invalid', 'SQL gate: duplicate view id'
|
|
);
|
|
select is(
|
|
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{annotations,0,view_id}', '"nowhere"')),
|
|
'document not publishable: annotation.view.missing', 'SQL gate: annotation on a missing view'
|
|
);
|
|
select is(
|
|
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{annotations,0,category_id}', '"nowhere"')),
|
|
'document not publishable: annotation.category.missing', 'SQL gate: missing category'
|
|
);
|
|
select is(
|
|
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{annotations,1,vertebral_region_code}', '"Z"')),
|
|
'document not publishable: annotation.region.missing', 'SQL gate: unknown vertebral region'
|
|
);
|
|
select is(
|
|
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{annotations,2,opens_view_id}', '"view-main"')),
|
|
'document not publishable: view.unreachable, annotation.opens_view.invalid', 'SQL gate: trigger opening a non-child view'
|
|
);
|
|
select is(
|
|
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{vertebral_config,regions}',
|
|
'[{"code":"C","name":{"fr":"a","en":"a"},"count":7},{"code":"C","name":{"fr":"b","en":"b"},"count":7}]')),
|
|
'document not publishable: region.code.duplicate', 'SQL gate: duplicate region code'
|
|
);
|
|
select is(
|
|
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{finding_zones}',
|
|
'[{"id":"hip","name":{"fr":"a","en":"a"},"categories":[]},{"id":"hip","name":{"fr":"b","en":"b"},"categories":[]}]')),
|
|
'document not publishable: finding_zone.id.duplicate', 'SQL gate: duplicate finding zone id'
|
|
);
|
|
select is(
|
|
pg_temp.publish_issue(jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{views}', '"not-an-array"')),
|
|
'document not publishable: document.shape', 'SQL gate: views not an array'
|
|
);
|
|
update public.anatomical_sheet_versions
|
|
set content = pg_temp.sheet_doc(pg_temp.id('asset')) where id = pg_temp.id('v1');
|
|
|
|
select lives_ok(
|
|
$$ select public.publish_sheet_version(pg_temp.id('v1')) $$,
|
|
'alice publishes version 1'
|
|
);
|
|
select results_eq(
|
|
$$ select status::text, published_by, published_at is not null
|
|
from public.anatomical_sheet_versions where id = pg_temp.id('v1') $$,
|
|
$$ values ('published', '20000000-0000-0000-0000-00000000000a'::uuid, true) $$,
|
|
'version 1 is published by alice'
|
|
);
|
|
select is(
|
|
(select current_version_id from public.anatomical_sheets where id = pg_temp.id('sheet')),
|
|
pg_temp.id('v1'), 'sheet current version is v1'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.publish_sheet_version(pg_temp.id('v1')) $$,
|
|
'55000', null, 'publishing twice is refused'
|
|
);
|
|
|
|
-- ---------------------------------------------------------------------------
|
|
-- 3. Published rows are immutable (editor, then table owner)
|
|
-- ---------------------------------------------------------------------------
|
|
-- Through the API role the policies hide published rows from UPDATE/DELETE:
|
|
-- the statement touches zero rows instead of raising.
|
|
update public.anatomical_sheet_versions set content = '{}'::jsonb where id = pg_temp.id('v1');
|
|
select is(
|
|
(select content from public.anatomical_sheet_versions where id = pg_temp.id('v1')),
|
|
pg_temp.sheet_doc(pg_temp.id('asset')), 'editor UPDATE of published content touches no row'
|
|
);
|
|
update public.anatomical_sheet_versions set change_note = 'x' where id = pg_temp.id('v1');
|
|
select is(
|
|
(select change_note from public.anatomical_sheet_versions where id = pg_temp.id('v1')),
|
|
'asset wired', 'editor UPDATE of a published change note touches no row'
|
|
);
|
|
select throws_ok(
|
|
$$ delete from public.anatomical_sheet_versions where id = pg_temp.id('v1') $$,
|
|
'42501', null, 'editor has no DELETE privilege on versions (RPC only)'
|
|
);
|
|
reset role;
|
|
select throws_ok(
|
|
$$ update public.anatomical_sheet_versions
|
|
set content = pg_temp.sheet_doc(pg_temp.id('asset')) || '{"name":{"fr":"X","en":"X"}}'
|
|
where id = pg_temp.id('v1') $$,
|
|
'42501', null, 'even the table owner cannot change published content'
|
|
);
|
|
select throws_ok(
|
|
$$ update public.anatomical_sheet_versions set status = 'draft' where id = pg_temp.id('v1') $$,
|
|
'42501', null, 'a published version cannot go back to draft'
|
|
);
|
|
select throws_ok(
|
|
$$ update public.anatomical_sheet_versions set version_number = 9 where id = pg_temp.id('v1') $$,
|
|
'42501', null, 'version_number is immutable'
|
|
);
|
|
select throws_ok(
|
|
$$ delete from public.anatomical_sheet_versions where id = pg_temp.id('v1') $$,
|
|
'55000', null, 'owner role cannot delete a published version either'
|
|
);
|
|
|
|
-- ---------------------------------------------------------------------------
|
|
-- 4. New version leaves the old one intact
|
|
-- ---------------------------------------------------------------------------
|
|
set local role authenticated;
|
|
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
|
create table pg_temp.v1_snapshot as
|
|
select * from public.anatomical_sheet_versions where id = pg_temp.id('v1');
|
|
|
|
insert into pg_temp.ids select 'v2', public.create_sheet_draft(pg_temp.id('sheet'), null, 'second edition');
|
|
select results_eq(
|
|
$$ select version_number, status::text, change_note, content = (select content from pg_temp.v1_snapshot)
|
|
from public.anatomical_sheet_versions where id = pg_temp.id('v2') $$,
|
|
$$ values (2, 'draft', 'second edition', true) $$,
|
|
'draft v2 starts from the current version content'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.create_sheet_draft(pg_temp.id('sheet')) $$,
|
|
'23505', null, 'one draft per sheet'
|
|
);
|
|
update public.anatomical_sheet_versions
|
|
set content = jsonb_set(pg_temp.sheet_doc(pg_temp.id('asset')), '{name,fr}', '"Cheval v2"')
|
|
where id = pg_temp.id('v2');
|
|
select lives_ok($$ select public.publish_sheet_version(pg_temp.id('v2')) $$, 'alice publishes v2');
|
|
select is(
|
|
(select current_version_id from public.anatomical_sheets where id = pg_temp.id('sheet')),
|
|
pg_temp.id('v2'), 'current version moved to v2'
|
|
);
|
|
select is(
|
|
(select count(*) from public.anatomical_sheet_versions v, pg_temp.v1_snapshot s
|
|
where v.id = s.id and v.content = s.content and v.status = s.status
|
|
and v.published_at = s.published_at and v.version_number = s.version_number
|
|
and v.change_note = s.change_note),
|
|
1::bigint, 'v1 row is byte-identical after v2 was published'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.delete_sheet_version(pg_temp.id('v2')) $$,
|
|
'55000', null, 'the current version cannot be deleted'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.delete_sheet_version(pg_temp.id('v1')) $$,
|
|
'55000', null, 'a published version cannot be deleted before deprecation'
|
|
);
|
|
|
|
-- ---------------------------------------------------------------------------
|
|
-- 5. Bob: private sheet invisible; public sheet usable; copy keeps provenance
|
|
-- ---------------------------------------------------------------------------
|
|
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
|
|
select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('sheet')), 0::bigint, 'bob does not see the private sheet');
|
|
select is((select count(*) from public.anatomical_sheet_versions where sheet_id = pg_temp.id('sheet')), 0::bigint, 'bob does not see its versions');
|
|
select is((select count(*) from public.anatomical_sheet_assets where sheet_id = pg_temp.id('sheet')), 0::bigint, 'bob does not see its assets');
|
|
select throws_ok(
|
|
$$ select public.fork_anatomical_sheet(pg_temp.id('v2'), 'Copie', 'copie-bob') $$,
|
|
'42501', null, 'bob cannot copy a private sheet'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.create_sheet_draft(pg_temp.id('sheet')) $$,
|
|
'42501', null, 'bob cannot open a draft on alice sheet'
|
|
);
|
|
update public.anatomical_sheets set name = 'hijack' where id = pg_temp.id('sheet');
|
|
select throws_ok(
|
|
$$ insert into public.anatomical_sheets (species_id, name, slug, creator_user_id)
|
|
values ('equine', 'direct', 'direct', '20000000-0000-0000-0000-00000000000b') $$,
|
|
'42501', null, 'bob cannot insert a sheet row directly'
|
|
);
|
|
select throws_ok(
|
|
$$ insert into public.anatomical_sheet_versions (sheet_id, version_number, document_schema_version, content)
|
|
values (pg_temp.id('sheet'), 9, 2, pg_temp.sheet_doc(null)) $$,
|
|
'42501', null, 'bob cannot insert a version row directly'
|
|
);
|
|
|
|
-- alice makes the sheet public through the identity-column policy
|
|
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
|
select is((select name from public.anatomical_sheets where id = pg_temp.id('sheet')), 'Mon cheval', 'bob update on alice sheet changed nothing');
|
|
select lives_ok($$ select public.set_sheet_visibility(pg_temp.id('sheet'), 'public') $$, 'alice makes the sheet public (audited RPC)');
|
|
insert into pg_temp.ids select 'v3', public.create_sheet_draft(pg_temp.id('sheet'), null, 'work in progress');
|
|
insert into pg_temp.ids select 'asset-unused', public.register_sheet_asset(
|
|
pg_temp.id('sheet'), repeat('b', 64), 1000, 'Unused.svg'
|
|
);
|
|
|
|
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
|
|
select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('sheet')), 1::bigint, 'bob sees the public sheet');
|
|
select results_eq(
|
|
$$ select version_number from public.anatomical_sheet_versions where sheet_id = pg_temp.id('sheet') order by 1 $$,
|
|
$$ values (1), (2) $$,
|
|
'bob sees the published versions, not the draft'
|
|
);
|
|
select is(
|
|
(select count(*) from public.anatomical_sheet_versions where id = pg_temp.id('v3') and content = '{}'::jsonb),
|
|
0::bigint, 'bob cannot read the draft'
|
|
);
|
|
update public.anatomical_sheet_versions set content = '{"schema_version":2}'::jsonb where id = pg_temp.id('v3');
|
|
select throws_ok(
|
|
$$ select public.fork_anatomical_sheet(pg_temp.id('v3'), 'Copie', 'copie-bob') $$,
|
|
'42501', null, 'a draft cannot be copied (hidden from non-editors)'
|
|
);
|
|
update public.anatomical_sheets set name = 'hijack-public' where id = pg_temp.id('sheet');
|
|
select is((select name from public.anatomical_sheets where id = pg_temp.id('sheet')), 'Mon cheval', 'bob cannot rename a public sheet he does not edit');
|
|
-- Every editor-gated RPC refuses a non-editor, even on a public sheet
|
|
select throws_ok($$ select public.publish_sheet_version(pg_temp.id('v3')) $$, '42501', null, 'non-editor cannot publish');
|
|
select throws_ok($$ select public.deprecate_sheet_version(pg_temp.id('v2'), 'x') $$, '42501', null, 'non-editor cannot deprecate');
|
|
select throws_ok($$ select public.delete_sheet_version(pg_temp.id('v3')) $$, '42501', null, 'non-editor cannot delete a version');
|
|
select throws_ok($$ select public.archive_anatomical_sheet(pg_temp.id('sheet')) $$, '42501', null, 'non-editor cannot archive');
|
|
select throws_ok($$ select public.restore_anatomical_sheet(pg_temp.id('sheet')) $$, '42501', null, 'non-editor cannot restore');
|
|
select throws_ok($$ select public.delete_anatomical_sheet(pg_temp.id('sheet')) $$, '42501', null, 'non-editor cannot delete a sheet');
|
|
select throws_ok($$ select public.set_sheet_visibility(pg_temp.id('sheet'), 'private') $$, '42501', null, 'non-editor cannot change visibility');
|
|
select throws_ok($$ select public.register_sheet_asset(pg_temp.id('sheet'), repeat('c', 64), 10, 'x.svg') $$, '42501', null, 'non-editor cannot register an asset');
|
|
select throws_ok($$ select public.delete_sheet_asset(pg_temp.id('asset-unused')) $$, '42501', null, 'non-editor cannot delete an asset');
|
|
select throws_ok($$ select public.publish_sheet_version(gen_random_uuid()) $$, '42501', null, 'unknown version gets the same answer as a foreign one');
|
|
-- A platform admin is not an editor of a personal sheet
|
|
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-000000000001","role":"authenticated"}', true);
|
|
select throws_ok($$ select public.create_sheet_draft(pg_temp.id('sheet')) $$, '42501', null, 'platform owner cannot edit a personal sheet');
|
|
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
|
|
|
|
create table pg_temp.source_snapshot as
|
|
select s.updated_at as sheet_updated_at, s.current_version_id, v.content, v.updated_at as version_updated_at,
|
|
(select count(*) from public.anatomical_sheet_assets where sheet_id = s.id) as asset_count
|
|
from public.anatomical_sheets s join public.anatomical_sheet_versions v on v.id = s.current_version_id
|
|
where s.id = pg_temp.id('sheet');
|
|
|
|
insert into pg_temp.ids select 'fork', public.fork_anatomical_sheet(pg_temp.id('v2'), 'Copie de Bob', 'copie-bob');
|
|
select results_eq(
|
|
$$ select creator_user_id, based_on_sheet_id, based_on_version_id, species_id, visibility::text, is_official
|
|
from public.anatomical_sheets where id = pg_temp.id('fork') $$,
|
|
$$ values ('20000000-0000-0000-0000-00000000000b'::uuid, pg_temp.id('sheet'), pg_temp.id('v2'), 'equine', 'private', false) $$,
|
|
'copy records provenance (source sheet + version) and belongs to bob'
|
|
);
|
|
insert into pg_temp.ids select 'fork-v1', id from public.anatomical_sheet_versions where sheet_id = pg_temp.id('fork');
|
|
insert into pg_temp.ids select 'fork-asset', id from public.anatomical_sheet_assets where sheet_id = pg_temp.id('fork');
|
|
select results_eq(
|
|
$$ select version_number, status::text, created_by from public.anatomical_sheet_versions where id = pg_temp.id('fork-v1') $$,
|
|
$$ values (1, 'draft', '20000000-0000-0000-0000-00000000000b'::uuid) $$,
|
|
'copy starts as draft v1 owned by bob'
|
|
);
|
|
select results_eq(
|
|
$$ select sha256, byte_size, id <> pg_temp.id('asset') from public.anatomical_sheet_assets where sheet_id = pg_temp.id('fork') $$,
|
|
$$ values (repeat('a', 64), 142400, true) $$,
|
|
'only the asset referenced by the copied version is copied (same content, new id)'
|
|
);
|
|
select is(
|
|
(select content -> 'views' -> 0 ->> 'svg_asset_id' from public.anatomical_sheet_versions where id = pg_temp.id('fork-v1')),
|
|
pg_temp.id('fork-asset')::text, 'copied document points at the copied asset'
|
|
);
|
|
select is(
|
|
(select content #- '{views,0,svg_asset_id}' from public.anatomical_sheet_versions where id = pg_temp.id('fork-v1')),
|
|
(select content #- '{views,0,svg_asset_id}' from pg_temp.source_snapshot),
|
|
'copied document is otherwise identical to the source'
|
|
);
|
|
|
|
-- bob edits his copy; the source stays untouched
|
|
update public.anatomical_sheet_versions
|
|
set content = jsonb_set(content, '{name,fr}', '"Ma copie"') where id = pg_temp.id('fork-v1');
|
|
select lives_ok($$ select public.publish_sheet_version(pg_temp.id('fork-v1')) $$, 'bob publishes his copy');
|
|
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
|
select is(
|
|
(select count(*) from public.anatomical_sheets s join public.anatomical_sheet_versions v on v.id = s.current_version_id,
|
|
pg_temp.source_snapshot snap
|
|
where s.id = pg_temp.id('sheet') and s.updated_at = snap.sheet_updated_at and s.current_version_id = snap.current_version_id
|
|
and v.content = snap.content and v.updated_at = snap.version_updated_at
|
|
and (select count(*) from public.anatomical_sheet_assets where sheet_id = s.id) = snap.asset_count),
|
|
1::bigint, 'source sheet, version and assets are unchanged by the copy and its edits'
|
|
);
|
|
|
|
-- ---------------------------------------------------------------------------
|
|
-- 6. Deprecation, deletion preconditions, archive, soft delete
|
|
-- ---------------------------------------------------------------------------
|
|
select lives_ok($$ select public.deprecate_sheet_version(pg_temp.id('v2'), 'superseded') $$, 'alice deprecates v2');
|
|
select results_eq(
|
|
$$ select status::text, deprecated_by from public.anatomical_sheet_versions where id = pg_temp.id('v2') $$,
|
|
$$ values ('deprecated', '20000000-0000-0000-0000-00000000000a'::uuid) $$,
|
|
'v2 is deprecated'
|
|
);
|
|
select is(
|
|
(select current_version_id from public.anatomical_sheets where id = pg_temp.id('sheet')),
|
|
pg_temp.id('v1'), 'current version falls back to the latest published one (v1)'
|
|
);
|
|
update public.anatomical_sheet_versions set content = '{}'::jsonb where id = pg_temp.id('v2');
|
|
select is(
|
|
(select content ->> 'schema_version' from public.anatomical_sheet_versions where id = pg_temp.id('v2')),
|
|
'2', 'a deprecated version is hidden from editor UPDATE'
|
|
);
|
|
reset role;
|
|
select throws_ok(
|
|
$$ update public.anatomical_sheet_versions set change_note = 'late edit' where id = pg_temp.id('v2') $$,
|
|
'42501', null, 'a deprecated version is frozen for the table owner too'
|
|
);
|
|
set local role authenticated;
|
|
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
|
select throws_ok(
|
|
$$ select public.delete_sheet_version(pg_temp.id('v2')) $$,
|
|
'55000', null, 'v2 cannot be deleted while bob copy derives from it'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.delete_anatomical_sheet(pg_temp.id('sheet')) $$,
|
|
'55000', null, 'the sheet cannot be deleted while a copy is active'
|
|
);
|
|
select lives_ok($$ select public.archive_anatomical_sheet(pg_temp.id('sheet')) $$, 'alice archives the sheet');
|
|
select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('sheet')), 1::bigint, 'the editor still reads her archived sheet');
|
|
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
|
|
select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('sheet')), 0::bigint, 'an archived sheet is no longer usable by others');
|
|
select throws_ok(
|
|
$$ select public.delete_sheet_version(pg_temp.id('fork-v1')) $$,
|
|
'55000', null, 'bob cannot delete the published version of his copy'
|
|
);
|
|
select lives_ok($$ select public.delete_anatomical_sheet(pg_temp.id('fork')) $$, 'bob soft-deletes his copy');
|
|
select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('fork')), 0::bigint, 'a deleted sheet is invisible to its creator');
|
|
|
|
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
|
select lives_ok($$ select public.restore_anatomical_sheet(pg_temp.id('sheet')) $$, 'alice restores the sheet');
|
|
select lives_ok($$ select public.delete_sheet_version(pg_temp.id('v2')) $$, 'deprecated v2 is deleted once no active copy depends on it');
|
|
select is((select count(*) from public.anatomical_sheet_versions where id = pg_temp.id('v2')), 0::bigint, 'v2 row is gone');
|
|
select lives_ok($$ select public.delete_sheet_version(pg_temp.id('v3')) $$, 'editor drops her own draft through the RPC');
|
|
insert into pg_temp.ids select 'v4', public.create_sheet_draft(pg_temp.id('sheet'), null, 'after deletion');
|
|
select is(
|
|
(select version_number from public.anatomical_sheet_versions where id = pg_temp.id('v4')),
|
|
4, 'a deleted version number is never reused'
|
|
);
|
|
select lives_ok($$ select public.delete_sheet_asset(pg_temp.id('asset-unused')) $$, 'an unreferenced asset can be deleted');
|
|
select throws_ok(
|
|
$$ select public.delete_sheet_asset(pg_temp.id('asset')) $$,
|
|
'55000', null, 'an asset referenced by a version cannot be deleted'
|
|
);
|
|
|
|
-- ---------------------------------------------------------------------------
|
|
-- 7. Deactivated account
|
|
-- ---------------------------------------------------------------------------
|
|
reset role;
|
|
update public.profiles set active = false where id = '20000000-0000-0000-0000-00000000000b';
|
|
set local role authenticated;
|
|
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
|
|
select throws_ok(
|
|
$$ select public.create_anatomical_sheet('equine', 'Nope', 'nope', pg_temp.sheet_doc(null)) $$,
|
|
'42501', null, 'a deactivated account cannot write'
|
|
);
|
|
reset role;
|
|
update public.profiles set active = true where id = '20000000-0000-0000-0000-00000000000b';
|
|
set local role authenticated;
|
|
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
|
|
|
-- ---------------------------------------------------------------------------
|
|
-- 8. Official sheets: platform admins only
|
|
-- ---------------------------------------------------------------------------
|
|
select throws_ok(
|
|
$$ select public.create_anatomical_sheet('equine', 'Officielle', 'officielle', pg_temp.sheet_doc(null), '', 'public', true) $$,
|
|
'42501', null, 'a plain user cannot create an official sheet'
|
|
);
|
|
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-000000000001","role":"authenticated"}', true);
|
|
insert into pg_temp.ids select 'official', public.create_anatomical_sheet(
|
|
'equine', 'Officielle test', 'officielle-test', pg_temp.sheet_doc(null), 'Fiche officielle', 'public', true
|
|
);
|
|
select results_eq(
|
|
$$ select creator_user_id, is_official, visibility::text from public.anatomical_sheets where id = pg_temp.id('official') $$,
|
|
$$ values (null::uuid, true, 'public') $$,
|
|
'official sheet is platform-owned'
|
|
);
|
|
select set_config('request.jwt.claims', '{"sub":"20000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
|
select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('official')), 1::bigint, 'alice sees the public official sheet');
|
|
select is(
|
|
(select count(*) from public.anatomical_sheet_versions where sheet_id = pg_temp.id('official')),
|
|
0::bigint, 'alice does not see the official draft'
|
|
);
|
|
select throws_ok(
|
|
$$ select public.create_sheet_draft(pg_temp.id('official')) $$,
|
|
'42501', null, 'alice cannot edit the official sheet'
|
|
);
|
|
|
|
-- ---------------------------------------------------------------------------
|
|
-- 9. Anonymous
|
|
-- ---------------------------------------------------------------------------
|
|
set local role anon;
|
|
select set_config('request.jwt.claims', '', true);
|
|
select throws_ok($$ select * from public.species $$, '42501', null, 'anon cannot read species');
|
|
select throws_ok($$ select * from public.anatomical_sheets $$, '42501', null, 'anon cannot read sheets');
|
|
select throws_ok(
|
|
$$ select public.create_anatomical_sheet('equine', 'x', 'x-x', '{}'::jsonb) $$,
|
|
'42501', null, 'anon cannot call create_anatomical_sheet'
|
|
);
|
|
|
|
-- ---------------------------------------------------------------------------
|
|
-- 10. Audit trail
|
|
-- ---------------------------------------------------------------------------
|
|
reset role;
|
|
select results_eq(
|
|
$$ select action from audit.audit_logs
|
|
where resource_id in (pg_temp.id('sheet'), pg_temp.id('v1'), pg_temp.id('v2'), pg_temp.id('v3'), pg_temp.id('fork'),
|
|
pg_temp.id('asset'), pg_temp.id('asset-unused'))
|
|
group by action order by action collate "C" $$,
|
|
$$ values ('anatomical_sheet.archived'), ('anatomical_sheet.created'), ('anatomical_sheet.deleted'),
|
|
('anatomical_sheet.forked'), ('anatomical_sheet.restored'), ('anatomical_sheet.visibility_changed'),
|
|
('anatomical_sheet_asset.deleted'), ('anatomical_sheet_asset.registered'),
|
|
('anatomical_sheet_version.created'), ('anatomical_sheet_version.deleted'),
|
|
('anatomical_sheet_version.deprecated'), ('anatomical_sheet_version.published') $$,
|
|
'every lifecycle step left an audit event'
|
|
);
|
|
|
|
select * from finish();
|
|
rollback;
|