diff --git a/supabase/migrations/20261028110000_sheet_review_workflow.sql b/supabase/migrations/20261028110000_sheet_review_workflow.sql index bf323ddf..e1492209 100644 --- a/supabase/migrations/20261028110000_sheet_review_workflow.sql +++ b/supabase/migrations/20261028110000_sheet_review_workflow.sql @@ -469,7 +469,8 @@ declare v_sheet uuid; begin perform private.check_document_structure(document); - v_name := left(coalesce(document -> 'name' ->> 'fr', document -> 'name' ->> 'en', 'Sans nom'), 120); + v_name := left(coalesce(nullif(btrim(document -> 'name' ->> 'fr'), ''), nullif(btrim(document -> 'name' ->> 'en'), ''), + 'Sans nom'), 120); v_description := left(coalesce(document -> 'description' ->> 'fr', ''), 2000); -- An official sheet accepts an active or inactive species, never `other` (§7.2). if v_species is null or v_species = 'other' @@ -523,6 +524,9 @@ begin where v.sheet_id = create_sheet_draft.sheet_id and v.status = 'draft') then raise exception 'draft.exists' using errcode = 'P0001'; end if; + if char_length(create_sheet_draft.change_note) > 1000 then + raise exception 'invalid_input' using errcode = 'P0001'; + end if; perform private.consume_catalog_write_budget(v_actor); -- Base only from a version still `published` that was published through a @@ -554,11 +558,15 @@ declare v_actor uuid := private.require_catalog_actor(); v_row public.anatomical_sheet_versions%rowtype; begin - select v.* into v_row from public.anatomical_sheet_versions v where v.id = save_sheet_draft.version_id for update; - if v_row.id is null then + select v.sheet_id into v_row.sheet_id from public.anatomical_sheet_versions v where v.id = save_sheet_draft.version_id; + if v_row.sheet_id is null then raise exception 'not_found' using errcode = '42501'; end if; perform private.require_sheet_author(v_row.sheet_id); + -- Lock order sheet, then version (§6.1): an archive waits for this write or + -- is seen by it, so nothing lands on a sheet archived in the meantime. + perform 1 from public.anatomical_sheets s where s.id = v_row.sheet_id for share; + select v.* into v_row from public.anatomical_sheet_versions v where v.id = save_sheet_draft.version_id for update; if v_row.status <> 'draft' then raise exception 'draft.not_a_draft' using errcode = '55000'; end if; @@ -574,6 +582,9 @@ begin raise exception 'draft_conflict' using errcode = 'P0001'; end if; perform private.check_document_structure(document); + if char_length(save_sheet_draft.change_note) > 1000 then + raise exception 'invalid_input' using errcode = 'P0001'; + end if; if (document ->> 'species_id') is distinct from (select species_id from public.anatomical_sheets where id = v_row.sheet_id) then raise exception 'document.species' using errcode = '23514'; end if; @@ -615,11 +626,15 @@ declare v_round integer; v_id uuid; begin - select v.* into v_row from public.anatomical_sheet_versions v where v.id = submit_sheet_review.version_id for update; - if v_row.id is null then + select v.sheet_id into v_row.sheet_id from public.anatomical_sheet_versions v where v.id = submit_sheet_review.version_id; + if v_row.sheet_id is null then raise exception 'not_found' using errcode = '42501'; end if; perform private.require_sheet_author(v_row.sheet_id); + -- Lock order sheet, then version (§6.1): an archive waits for this write or + -- is seen by it, so nothing lands on a sheet archived in the meantime. + perform 1 from public.anatomical_sheets s where s.id = v_row.sheet_id for share; + select v.* into v_row from public.anatomical_sheet_versions v where v.id = submit_sheet_review.version_id for update; if v_row.status <> 'draft' then raise exception 'draft.not_a_draft' using errcode = '55000'; end if; @@ -1086,7 +1101,7 @@ begin if v_fingerprint is null or v_fingerprint !~ '^[0-9a-f]{64}$' then raise exception 'verdict.fingerprint' using errcode = 'P0001'; end if; - v_name := left(coalesce(v_version.content -> 'name' ->> 'fr', v_sheet.name), 120); + v_name := left(coalesce(nullif(btrim(v_version.content -> 'name' ->> 'fr'), ''), v_sheet.name), 120); v_description := left(coalesce(v_version.content -> 'description' ->> 'fr', ''), 2000); perform private.consume_catalog_write_budget(v_actor); @@ -1368,7 +1383,7 @@ begin end if; perform private.consume_catalog_write_budget(v_actor); - select private.insert_official_sheet(s.species_id, left(coalesce(v_source.content -> 'name' ->> 'fr', s.name), 120), + select private.insert_official_sheet(s.species_id, left(coalesce(nullif(btrim(v_source.content -> 'name' ->> 'fr'), ''), s.name), 120), create_sheet_working_copy.slug, s.description, v_source.id) into v_sheet from public.anatomical_sheets s where s.id = v_source.sheet_id; diff --git a/supabase/tests/015_catalog_access.sql b/supabase/tests/015_catalog_access.sql index 9ea51701..dec381b5 100644 --- a/supabase/tests/015_catalog_access.sql +++ b/supabase/tests/015_catalog_access.sql @@ -5,7 +5,7 @@ -- proven in packages/supabase/tests/sandbox/sheets.sandbox.ts, the seed owner's -- factors on an upgraded stack in seed-owner-mfa.sandbox.ts. begin; -select plan(36); +select plan(37); insert into auth.users (id, email, email_confirmed_at) values ('d0000000-0000-0000-0000-000000000001', 'own@t.local', now()), -- owner @@ -158,6 +158,10 @@ select lives_ok( select lives_ok( $$ select public.grant_catalog_right('d0000000-0000-0000-0000-00000000000c','catalog_reader','beta') $$, 'catalog_reader needs no note or factor'); +-- a capability never goes to an account without a verified factor (§5.2) +select throws_ok( + $$ select public.grant_catalog_right('d0000000-0000-0000-0000-00000000000c','sheet_reviewer','partner','DVM verified') $$, + 'P0001', 'grant.ineligible', 'a capability is refused to an account without a verified factor'); -- the owner cannot grant itself sheet_reviewer select throws_ok( $$ select public.grant_catalog_right('d0000000-0000-0000-0000-000000000001','sheet_reviewer','self','x') $$, diff --git a/supabase/tests/016_sheet_review_workflow.sql b/supabase/tests/016_sheet_review_workflow.sql index aead7086..dc1e59c1 100644 --- a/supabase/tests/016_sheet_review_workflow.sql +++ b/supabase/tests/016_sheet_review_workflow.sql @@ -155,7 +155,7 @@ insert into auth.users (id,email,email_confirmed_at) values ('e0000000-0000-0000 insert into public.anatomical_catalog_grants (user_id,grant_kind,reason,granted_by) values ('e0000000-0000-0000-0000-0000000000cc','catalog_reader','beta','e0000000-0000-0000-0000-000000000001'); --- A catalogue reader sees the published version and its drawing; not the grants. +-- A catalogue reader sees the published version (the grants table rules are in 018). set local role authenticated; select pg_temp.act('e0000000-0000-0000-0000-0000000000cc', gen_random_uuid()); select is((select count(*) from jsonb_array_elements(public.list_official_catalog()) e diff --git a/supabase/tests/017_sheet_review_rules.sql b/supabase/tests/017_sheet_review_rules.sql index 5b32f709..9383b620 100644 --- a/supabase/tests/017_sheet_review_rules.sql +++ b/supabase/tests/017_sheet_review_rules.sql @@ -8,7 +8,7 @@ -- ids created here, so the file also runs on a stack that already holds -- sheets, rounds or a working copy. begin; -select plan(78); +select plan(85); -- Accounts: O owner, E admin + editor + publisher, V reviewer, V2 admin + -- editor + reviewer (an author of a later round), V3 reviewer, R reader. @@ -502,6 +502,17 @@ select throws_ok($$ select public.delete_sheet_asset(pg_temp.id('s13.asset')) $$ select throws_ok($$ select public.delete_sheet_asset((select id from public.anatomical_sheet_assets where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' limit 1)) $$, '42501', 'sheet.historical_read_only', 'the historical sheet''s drawing row is never deleted'); +select throws_ok($$ select public.delete_anatomical_sheet('503dc301-a8b1-50d4-9e44-0919b2fc2916') $$, + '42501', 'sheet.historical_read_only', 'the historical sheet is never deleted through the RPC'); +select throws_ok($$ select public.delete_sheet_version(pg_temp.id('h.v')) $$, + '42501', 'sheet.historical_read_only', 'the historical draft is never deleted through the RPC'); +select throws_ok($$ select public.restore_anatomical_sheet('503dc301-a8b1-50d4-9e44-0919b2fc2916') $$, + '42501', 'sheet.historical_read_only', 'the historical sheet is never restored through the RPC'); +select throws_ok($$ select public.create_sheet_draft('503dc301-a8b1-50d4-9e44-0919b2fc2916', null, 'x') $$, + '42501', 'sheet.historical_read_only', 'no draft is opened on the historical sheet'); +select throws_ok($$ select public.confirm_sheet_asset_upload((select id from public.anatomical_sheet_assets + where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916' limit 1)) $$, + '42501', 'sheet.historical_read_only', 'an editor never confirms a drawing of the historical sheet'); select throws_ok($$ select public.create_official_sheet('r17-s13', pg_temp.doc(null, 'reviewed')) $$, 'P0001', 'sheet.slug_taken', 'a slug already used by an official sheet'); select throws_ok($$ select public.create_official_sheet('Bad Slug', pg_temp.doc(null, 'reviewed')) $$, @@ -522,6 +533,13 @@ select throws_ok($$ select public.save_sheet_draft(pg_temp.id('s13.v'), 1, select throws_ok($$ select public.save_sheet_draft(pg_temp.id('s13.v'), 1, jsonb_set(pg_temp.doc(pg_temp.id('s13.asset'), 'reviewed'), '{schema_version}', '3'), 'x') $$, '23514', 'document.schema_version', 'save refuses another schema version'); +select throws_ok($$ select public.save_sheet_draft(pg_temp.id('s13.v'), 1, + jsonb_set(pg_temp.doc(pg_temp.id('s13.asset'), 'reviewed'), '{description,fr}', '"n"'), repeat('x', 1001)) $$, + 'P0001', 'invalid_input', 'a change note over 1 000 characters is invalid input, not a raw constraint error'); +insert into pg_temp.k select 'blank', public.create_official_sheet('r17-blank', + jsonb_set(pg_temp.doc(null, 'reviewed'), '{name,fr}', '" "')); +select is((select name from public.anatomical_sheets where id = pg_temp.id('blank')), 'R horse', + 'a blank FR name falls back to the EN name at creation'); select throws_ok($$ select public.create_sheet_draft(pg_temp.id('m3'), null, 'x') $$, 'P0001', 'draft.exists', 'one draft per sheet'); select public.publish_sheet_version(pg_temp.id('m3b.r'));