-- V1-2.1 M4/M5: what no role changes, the table owner included (contract §6.8, -- §6.9, §9.1, §12.1 F13 F16, §14.2), and the Storage rules the rewrite of 005, -- 006 and 007 had stopped testing (§9.3, §9.4, §9.5), after the independent -- audit (M3). Most statements run as the table owner on purpose: the guards are -- triggers and constraints, not privileges. Fixtures are scoped to this file. begin; select plan(38); insert into auth.users (id, email, email_confirmed_at) values ('19000000-0000-0000-0000-000000000001', 'own@i19.local', now()), ('19000000-0000-0000-0000-00000000000e', 'ed@i19.local', now()), ('19000000-0000-0000-0000-0000000000cc', 'reader@i19.local', now()); 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 auth.users where email like '%@i19.local'; insert into auth.sessions (id, user_id, aal) select ('19100000' || substr(id::text, 9))::uuid, id, 'aal2' from auth.users where email like '%@i19.local'; select set_config('request.jwt.claims', '', true); select private.assign_platform_role(null, '19000000-0000-0000-0000-000000000001', 'owner'); select private.assign_platform_role(null, '19000000-0000-0000-0000-00000000000e', 'admin'); insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by) values ('19000000-0000-0000-0000-00000000000e', 'sheet_editor', 'a', 'c', '19000000-0000-0000-0000-000000000001'), ('19000000-0000-0000-0000-0000000000cc', 'catalog_reader', 'a', null, '19000000-0000-0000-0000-000000000001'); create function pg_temp.act(p uuid) returns void language sql as $$ select set_config('request.jwt.claims', jsonb_build_object('sub', p, 'role', 'authenticated', 'aal', 'aal2', 'session_id', ('19100000' || substr(p::text, 9))::uuid)::text, true); $$; create function pg_temp.doc(p_asset uuid) returns jsonb language sql as $$ select jsonb_build_object('schema_version', 2, 'species_id', 'equine', 'name', jsonb_build_object('fr', 'I', 'en', 'I'), 'description', jsonb_build_object('fr', 'd', 'en', 'd'), 'scope_disclaimer', jsonb_build_object('fr', 'x', 'en', 'x'), 'attribution', jsonb_build_object('authors', '[]'::jsonb, 'license', null, 'notes', null), 'vertebral_config', jsonb_build_object('regions', '[]'::jsonb), 'references', '[]'::jsonb, 'categories', '[]'::jsonb, 'views', jsonb_build_array(jsonb_build_object('id', 'main', 'kind', 'main', 'name', jsonb_build_object('fr', 'V', 'en', 'V'), 'svg_asset_id', p_asset, 'view_box', null)), 'annotations', '[]'::jsonb, 'finding_zones', '[]'::jsonb); $$; grant execute on all functions in schema pg_temp to authenticated, service_role; -- SP: published v1 (drawing d1), draft v2; pending drawings d2 (no transition -- yet), d3 (object present), d4 (no object). SX: a deleted sheet that had a -- published version. select set_config('request.jwt.claims', '{"sub":"19000000-0000-0000-0000-00000000000e"}', true); insert into public.anatomical_sheets (id, species_id, name, slug, is_official, visibility, status) values ('19200000-0000-0000-0000-000000000001', 'equine', 'SP', 'i19-sp', true, 'private', 'active'), ('19200000-0000-0000-0000-000000000002', 'equine', 'SX', 'i19-sx', true, 'private', 'active'); insert into public.anatomical_sheet_assets (id, sheet_id, sha256, byte_size, file_name, uploaded_at, created_by) values ('19400000-0000-0000-0000-000000000001', '19200000-0000-0000-0000-000000000001', repeat('d1', 32), 100, 'd1.svg', now(), null), ('19400000-0000-0000-0000-000000000002', '19200000-0000-0000-0000-000000000001', repeat('d2', 32), 100, 'd2.svg', null, null), ('19400000-0000-0000-0000-000000000003', '19200000-0000-0000-0000-000000000001', repeat('d3', 32), 100, 'd3.svg', null, null), ('19400000-0000-0000-0000-000000000004', '19200000-0000-0000-0000-000000000001', repeat('d4', 32), 100, 'd4.svg', null, null); insert into storage.objects (bucket_id, name, metadata) select 'anatomical-sheet-assets', repeat(c, 32) || '.svg', '{"size":100}' from unnest(array['d1', 'd3']) c on conflict do nothing; insert into public.anatomical_sheet_versions (id, sheet_id, version_number, status, document_schema_version, content, published_at, document_fingerprint) values ('19300000-0000-0000-0000-000000000001', '19200000-0000-0000-0000-000000000001', 1, 'published', 2, pg_temp.doc('19400000-0000-0000-0000-000000000001'), now(), repeat('0', 64)), ('19300000-0000-0000-0000-000000000002', '19200000-0000-0000-0000-000000000001', 2, 'draft', 2, pg_temp.doc('19400000-0000-0000-0000-000000000001'), null, null), ('19300000-0000-0000-0000-000000000003', '19200000-0000-0000-0000-000000000002', 1, 'published', 2, pg_temp.doc(null), now(), repeat('0', 64)); update public.anatomical_sheets set current_version_id = '19300000-0000-0000-0000-000000000001', last_version_number = 2 where id = '19200000-0000-0000-0000-000000000001'; update public.anatomical_sheets set current_version_id = '19300000-0000-0000-0000-000000000003', last_version_number = 1, status = 'deleted', deleted_at = now() where id = '19200000-0000-0000-0000-000000000002'; -- ============ A published version, for the table owner (§6.8) ============ select throws_ok($$ update public.anatomical_sheet_versions set content = content || '{"name":{"fr":"X","en":"X"}}' where id = '19300000-0000-0000-0000-000000000001' $$, '42501', null, 'the table owner cannot change published content'); select throws_ok($$ update public.anatomical_sheet_versions set status = 'draft' where id = '19300000-0000-0000-0000-000000000001' $$, '42501', null, 'a published version never goes back to draft'); select throws_ok($$ update public.anatomical_sheet_versions set version_number = 9 where id = '19300000-0000-0000-0000-000000000001' $$, '42501', null, 'version_number is immutable'); select throws_ok($$ update public.anatomical_sheet_versions set document_fingerprint = repeat('1', 64) where id = '19300000-0000-0000-0000-000000000001' $$, '42501', null, 'the published fingerprint is immutable'); select throws_ok($$ update public.anatomical_sheet_versions set authors = '{}' where id = '19300000-0000-0000-0000-000000000001' $$, '42501', null, 'the published author set is immutable'); select throws_ok($$ update public.anatomical_sheet_versions set published_review_id = gen_random_uuid() where id = '19300000-0000-0000-0000-000000000001' $$, '42501', null, 'the published review link is immutable'); select throws_ok($$ delete from public.anatomical_sheet_versions where id = '19300000-0000-0000-0000-000000000001' $$, '55000', null, 'the table owner cannot delete a published version'); -- The deprecation transition changes no V1-2 column (§6.8). select throws_ok($$ update public.anatomical_sheet_versions set status = 'deprecated', deprecated_at = now(), document_fingerprint = repeat('1', 64) where id = '19300000-0000-0000-0000-000000000001' $$, '42501', null, 'a deprecation that also changes the fingerprint is refused'); select throws_ok($$ update public.anatomical_sheet_versions set status = 'deprecated', deprecated_at = now(), source_version_id = '19300000-0000-0000-0000-000000000002' where id = '19300000-0000-0000-0000-000000000001' $$, '42501', null, 'a deprecation that also changes the base is refused'); select lives_ok($$ update public.anatomical_sheet_versions set status = 'deprecated', deprecated_at = now() where id = '19300000-0000-0000-0000-000000000001' $$, 'the bare deprecation transition is allowed'); -- ============ A deprecated version is frozen and kept (F13) ============ select throws_ok($$ update public.anatomical_sheet_versions set change_note = 'late' where id = '19300000-0000-0000-0000-000000000001' $$, '42501', null, 'a deprecated version is frozen for the table owner'); select throws_ok($$ update public.anatomical_sheet_versions set status = 'published' where id = '19300000-0000-0000-0000-000000000001' $$, '42501', null, 'a deprecated version is never published again'); select throws_ok($$ delete from public.anatomical_sheet_versions where id = '19300000-0000-0000-0000-000000000001' $$, '55000', null, 'the table owner cannot delete a deprecated version (F13)'); -- ============ Drafts: one per sheet, species of the sheet, no direct deprecation ============ select throws_ok($$ update public.anatomical_sheet_versions set status = 'deprecated', deprecated_at = now() where id = '19300000-0000-0000-0000-000000000002' $$, '55000', null, 'a draft is never deprecated directly'); select throws_ok($$ insert into public.anatomical_sheet_versions (sheet_id, version_number, document_schema_version, content) values ('19200000-0000-0000-0000-000000000001', 7, 2, pg_temp.doc(null)) $$, '23505', null, 'one draft per sheet, for every role'); select throws_ok($$ update public.anatomical_sheet_versions set content = jsonb_set(content, '{species_id}', '"canine"') where id = '19300000-0000-0000-0000-000000000002' $$, '23514', null, 'a document of another species is refused for every role'); -- No API role publishes by a direct UPDATE: neither has the privilege (F12). set local role service_role; select throws_ok($$ update public.anatomical_sheet_versions set status = 'published' where id = '19300000-0000-0000-0000-000000000002' $$, '42501', null, 'service_role cannot update a version (F12)'); set local role authenticated; select pg_temp.act('19000000-0000-0000-0000-00000000000e'); select throws_ok($$ update public.anatomical_sheet_versions set status = 'published' where id = '19300000-0000-0000-0000-000000000002' $$, '42501', null, 'an editor cannot update a version directly (F9)'); reset role; -- ============ Drawing rows (§9.1, F16) ============ select throws_ok($$ insert into public.anatomical_sheet_assets (sheet_id, sha256, byte_size, file_name) values ('19200000-0000-0000-0000-000000000001', repeat('d5', 32), 2097153, 'big.svg') $$, '23514', null, 'a drawing row over 2 MiB is refused'); select throws_ok($$ update public.anatomical_sheet_assets set byte_size = 99 where id = '19400000-0000-0000-0000-000000000001' $$, '42501', null, 'a drawing row''s size is immutable (F16)'); select throws_ok($$ update public.anatomical_sheet_assets set uploaded_at = now() - interval '1 day' where id = '19400000-0000-0000-0000-000000000001' $$, '42501', null, 'a confirmation time never moves once set'); select lives_ok($$ update public.anatomical_sheet_assets set uploaded_at = now() where id = '19400000-0000-0000-0000-000000000002' $$, 'a pending row is confirmed once'); set local role authenticated; select pg_temp.act('19000000-0000-0000-0000-00000000000e'); select throws_ok($$ select public.register_sheet_asset('19200000-0000-0000-0000-000000000001', repeat('d5', 32), 2097153, 'big.svg') $$, 'P0001', 'invalid_input', 'register refuses a drawing over 2 MiB'); reset role; -- ============ The historical sheet, for the table owner (§14.2) ============ select throws_ok($$ update public.anatomical_sheets set status = 'archived', archived_at = now() where id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' $$, '42501', 'sheet.historical_read_only', 'the historical sheet is never archived'); select throws_ok($$ update public.anatomical_sheets set name = 'x' where id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' $$, '42501', 'sheet.historical_read_only', 'the historical sheet row is never edited'); select throws_ok($$ delete from public.anatomical_sheets where id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' $$, '42501', 'sheet.historical_read_only', 'the historical sheet is never deleted'); select throws_ok($$ delete from public.anatomical_sheet_assets where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' $$, '42501', 'sheet.historical_read_only', 'the historical drawing row is never deleted'); select set_config('request.jwt.claims', '', true); select lives_ok($$ update public.anatomical_sheet_versions set content = jsonb_set(content, '{description,fr}', '"seed"') where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' $$, 'a content write without an actor stays allowed on the historical sheet only (the converging seed)'); -- ============ Storage (§9.1, §9.3, §9.4) ============ select is((select allowed_mime_types from storage.buckets where id = 'anatomical-sheet-assets'), array['image/svg+xml'], 'the bucket accepts SVG only'); -- Objects under names that resemble a confirmed key: only the exact -- `.svg` is ever readable. insert into storage.objects (bucket_id, name, metadata) select 'anatomical-sheet-assets', n, '{"size":100}' from unnest(array[upper(repeat('d1', 32)) || '.svg', repeat('d1', 32), 'x/' || repeat('d1', 32) || '.svg', repeat('d1', 32) || '.svg.bak', repeat('d1', 32) || '.SVG']) n on conflict do nothing; set local role authenticated; select pg_temp.act('19000000-0000-0000-0000-00000000000e'); select is((select string_agg(name, ',') from storage.objects where bucket_id = 'anatomical-sheet-assets' and name ilike '%' || repeat('d1', 32) || '%'), repeat('d1', 32) || '.svg', 'an editor reads the exact key only, no variant'); select pg_temp.act('19000000-0000-0000-0000-0000000000cc'); select is((select string_agg(name, ',') from storage.objects where bucket_id = 'anatomical-sheet-assets' and name ilike '%' || repeat('d1', 32) || '%'), repeat('d1', 32) || '.svg', 'a reader reads the exact key of a catalogue drawing only, no variant'); -- A deleted sheet is read by nobody, its published version included. select is((select count(*) from public.anatomical_sheets where id = '19200000-0000-0000-0000-000000000002'), 0::bigint, 'a reader does not see a deleted sheet'); select throws_ok($$ select public.get_sheet_version('19300000-0000-0000-0000-000000000003') $$, '42501', 'not_found', 'nor a published version of a deleted sheet by id'); select pg_temp.act('19000000-0000-0000-0000-00000000000e'); select is((select count(*) from public.anatomical_sheet_versions where sheet_id = '19200000-0000-0000-0000-000000000002'), 0::bigint, 'an editor does not see the versions of a deleted sheet'); -- The operator tool confirms an upload with the service key (§9.3 step 3, §9.5). reset role; set local role service_role; select set_config('request.jwt.claims', '{"role":"service_role"}', true); select is(public.confirm_sheet_asset_upload('19400000-0000-0000-0000-000000000003'), '19400000-0000-0000-0000-000000000003'::uuid, 'the service role confirms an upload whose object exists with the recorded size'); select throws_ok($$ select public.confirm_sheet_asset_upload('19400000-0000-0000-0000-000000000004') $$, 'P0001', 'upload_missing', 'the service role cannot confirm a row without its object (F7)'); select throws_ok($$ select public.register_sheet_asset('19200000-0000-0000-0000-000000000001', repeat('d6', 32), 100, 'x.svg') $$, '42501', null, 'the service role cannot register a drawing'); reset role; select is((select actor_user_id is null from audit.audit_logs where action = 'anatomical_sheet_asset.uploaded' and resource_id = '19400000-0000-0000-0000-000000000003'), true, 'the operator confirmation is audited with a null actor'); select * from finish(); rollback;