fix(supabase): V1-2.1 owner self-review grant and the text size measure
M6 option A: grant_catalog_right no longer refuses an owner granting itself sheet_reviewer; authors stay refused at acknowledgment, approval and publication. The document cap is the UTF-8 bytes of the JSON text: check_document_structure drops pg_column_size and M5 replaces the base table CHECK by octet_length(content::text). pgTAP 015 and 017 cover the owner-reviewer journey and cap - 1, cap, cap + 1, multi-byte, dense.
This commit is contained in:
1 parent
2af61e72cc
commit
b1de87b5d0
6 files changed
+131
-34
No files matched your search
@@ -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(85);
|
||||
select plan(104);
|
||||
|
||||
-- Accounts: O owner, E admin + editor + publisher, V reviewer, V2 admin +
|
||||
-- editor + reviewer (an author of a later round), V3 reviewer, R reader.
|
||||
@@ -144,6 +144,18 @@ begin
|
||||
end if;
|
||||
perform pg_temp.as_user('E');
|
||||
end $$;
|
||||
-- A document whose JSON text (`::text`, the contract's measure, §12.2) is
|
||||
-- exactly p_bytes UTF-8 bytes: the base plus a `pad` string of p_unit
|
||||
-- characters, completed with ASCII.
|
||||
create function pg_temp.sized(p_base jsonb, p_bytes int, p_unit text) returns jsonb language sql as $$
|
||||
select p_base || jsonb_build_object('pad', repeat(p_unit, f / octet_length(p_unit)) || repeat('x', f % octet_length(p_unit)))
|
||||
from (select p_bytes - octet_length((p_base || '{"pad": ""}'::jsonb)::text) as f) s;
|
||||
$$;
|
||||
-- Saves <doc> on the draft <p> at its current revision.
|
||||
create function pg_temp.save_now(p text, p_doc jsonb) returns void language sql as $$
|
||||
select public.save_sheet_draft(pg_temp.id(p || '.v'),
|
||||
(select revision from public.anatomical_sheet_versions where id = pg_temp.id(p || '.v')), p_doc, 'size');
|
||||
$$;
|
||||
grant all on pg_temp.k to authenticated;
|
||||
grant execute on all functions in schema pg_temp to authenticated;
|
||||
|
||||
@@ -517,13 +529,6 @@ select throws_ok($$ select public.create_official_sheet('r17-s13', pg_temp.doc(n
|
||||
'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')) $$,
|
||||
'P0001', 'invalid_input', 'a slug outside the format');
|
||||
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'), '{pad}', to_jsonb(repeat('x', 2100000))), 'x') $$,
|
||||
'23514', 'document.too_large', 'save refuses a document over 2 MiB serialised');
|
||||
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'), '{pad}',
|
||||
(select jsonb_agg(jsonb_build_object('a', 1)) from generate_series(1, 100000))), 'x') $$,
|
||||
'23514', 'document.too_large', 'a document under 2 MiB as text but over it as stored jsonb gets the closed code');
|
||||
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'), '{species_id}', '"canine"'), 'x') $$,
|
||||
'23514', 'document.species', 'save refuses a document of another species');
|
||||
@@ -562,6 +567,94 @@ select lives_ok($$ select public.delete_anatomical_sheet(pg_temp.id('s13')) $$,
|
||||
select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('s13')), 0::bigint,
|
||||
'a deleted sheet is invisible, capability holders included');
|
||||
|
||||
-- ============ M6: an owner may review, never a round it authored (§5.2, §5.5) ============
|
||||
select pg_temp.as_user('O');
|
||||
select lives_ok($$ select public.grant_catalog_right(pg_temp.u('O'), 'sheet_reviewer', 'owner reviews', 'DVM, owner') $$,
|
||||
'M6: the owner at aal2 grants itself sheet_reviewer');
|
||||
select public.grant_catalog_right(pg_temp.u('O'), 'sheet_editor', 'owner edits', 'owner');
|
||||
-- o1: written by E only; the owner acknowledges, approves, E publishes.
|
||||
select pg_temp.sheet('o1', repeat('c1', 32));
|
||||
select pg_temp.round('o1', 'o1.r', 1, repeat('c1', 32), pg_temp.groups3());
|
||||
select pg_temp.as_user('O');
|
||||
select lives_ok($$ select public.acknowledge_sheet_review_group(pg_temp.id('o1.r'), g ->> 'key', g ->> 'digest')
|
||||
from jsonb_array_elements(pg_temp.groups3()) g $$,
|
||||
'M6: the owner-reviewer acknowledges the groups of a draft written by another author');
|
||||
select lives_ok($$ select public.decide_sheet_review(pg_temp.id('o1.r'), true, 'ok') $$,
|
||||
'M6: the owner-reviewer approves a version written by another author');
|
||||
select pg_temp.as_user('E');
|
||||
select is(public.publish_sheet_version(pg_temp.id('o1.r')), pg_temp.id('o1.v'),
|
||||
'M6: the owner''s approval publishes when every other rule is met');
|
||||
-- o2: the owner saves a variant, so it is an author of the round.
|
||||
select pg_temp.sheet('o2', repeat('b6', 32));
|
||||
select pg_temp.as_user('O');
|
||||
select public.save_sheet_draft(pg_temp.id('o2.v'), 1,
|
||||
jsonb_set(pg_temp.doc(pg_temp.id('o2.asset'), 'reviewed'), '{description,fr}', '"o"'), 'owner edit');
|
||||
select pg_temp.round('o2', 'o2.r', 2, repeat('b6', 32), pg_temp.groups3());
|
||||
select pg_temp.as_user('O');
|
||||
select throws_ok($$ select public.acknowledge_sheet_review_group(pg_temp.id('o2.r'), 'general', repeat('1', 64)) $$,
|
||||
'42501', 'review.separation', 'M6: the owner-reviewer cannot acknowledge a round it authored');
|
||||
select throws_ok($$ select public.decide_sheet_review(pg_temp.id('o2.r'), true, 'ok') $$,
|
||||
'42501', 'review.separation', 'M6: the owner-reviewer cannot approve its own version');
|
||||
-- An approval by an author (written as the table owner, the RPC refuses it) is
|
||||
-- refused again at publication.
|
||||
select pg_temp.as_user('E');
|
||||
select public.withdraw_sheet_review(pg_temp.id('o2.r'), 'next');
|
||||
select pg_temp.fixture_round('o2', 'o2.f', 'c',
|
||||
jsonb_set(pg_temp.doc(pg_temp.id('o2.asset'), 'reviewed'), '{description,fr}', '"o2"'), 'O', 'O', 'V');
|
||||
select throws_ok($$ select public.publish_sheet_version(pg_temp.id('o2.f')) $$,
|
||||
'P0001', 'publish.approval.invalid', 'M6: publication refused after an approval by an author (the owner)');
|
||||
|
||||
-- ============ Document size: UTF-8 bytes of the JSON text (§6.2, §12.2) ============
|
||||
select pg_temp.sheet('sz', repeat('c3', 32));
|
||||
select lives_ok($$ select pg_temp.save_now('sz', pg_temp.sized(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), 2097151, 'x')) $$,
|
||||
'size: a document of cap − 1 bytes is saved');
|
||||
reset role;
|
||||
select is((select octet_length(content::text) from public.anatomical_sheet_versions where id = pg_temp.id('sz.v')), 2097151,
|
||||
'size: the stored document measures cap − 1 bytes');
|
||||
select pg_temp.as_user('E');
|
||||
select lives_ok($$ select pg_temp.save_now('sz', pg_temp.sized(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), 2097152, 'x')) $$,
|
||||
'size: a document of exactly 2 MiB is saved');
|
||||
reset role;
|
||||
select is((select octet_length(content::text) from public.anatomical_sheet_versions where id = pg_temp.id('sz.v')), 2097152,
|
||||
'size: the stored document measures exactly the cap');
|
||||
select pg_temp.as_user('E');
|
||||
select throws_ok($$ select pg_temp.save_now('sz', pg_temp.sized(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), 2097153, 'x')) $$,
|
||||
'23514', 'document.too_large', 'size: a document of cap + 1 bytes gets the closed code');
|
||||
-- Multi-byte text: the cap counts bytes, not characters.
|
||||
select lives_ok($$ select pg_temp.save_now('sz', pg_temp.sized(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), 2097152, '€')) $$,
|
||||
'size: a document of exactly 2 MiB written in 3-byte characters is saved');
|
||||
reset role;
|
||||
select ok((select octet_length(content::text) = 2097152 and char_length(content::text) < 800000
|
||||
from public.anatomical_sheet_versions where id = pg_temp.id('sz.v')),
|
||||
'size: that document is 2 MiB in bytes and far fewer characters');
|
||||
select ok(char_length(pg_temp.sized(pg_temp.doc(null, 'reviewed'), 2097153, '𝄞')::text) < 600000,
|
||||
'size: the 4-byte document below is far under the cap in characters');
|
||||
select pg_temp.as_user('E');
|
||||
select throws_ok($$ select pg_temp.save_now('sz', pg_temp.sized(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), 2097153, '𝄞')) $$,
|
||||
'23514', 'document.too_large', 'size: one byte over the cap in 4-byte characters gets the closed code');
|
||||
-- A dense document: within the cap as text, over it as binary jsonb (`|| '{}'`
|
||||
-- measures a fresh, uncompressed value, as the RPC receives it).
|
||||
reset role;
|
||||
create table pg_temp.sz_dense as select jsonb_set(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), '{pad}',
|
||||
(select jsonb_agg(jsonb_build_object('a', 1)) from generate_series(1, 100000))) as d;
|
||||
grant select on pg_temp.sz_dense to authenticated;
|
||||
select ok((select octet_length(d::text) <= 2097152 and pg_column_size(d || '{}'::jsonb) > 2097152 from pg_temp.sz_dense),
|
||||
'size: the dense fixture fits the cap as text and exceeds it as binary jsonb');
|
||||
select pg_temp.as_user('E');
|
||||
select lives_ok($$ select pg_temp.save_now('sz', (select d from pg_temp.sz_dense)) $$,
|
||||
'size: a dense document within the cap as text is saved (the binary jsonb size is not the measure)');
|
||||
reset role;
|
||||
select ok((select octet_length(content::text) <= 2097152 and jsonb_array_length(content -> 'pad') = 100000
|
||||
from public.anatomical_sheet_versions where id = pg_temp.id('sz.v')),
|
||||
'size: the dense document is stored whole');
|
||||
select pg_temp.as_user('E');
|
||||
select throws_ok($$ select public.create_official_sheet('r17-szbig', pg_temp.sized(pg_temp.doc(null, 'reviewed'), 2097153, 'x')) $$,
|
||||
'23514', 'document.too_large', 'size: creation refuses a document of cap + 1 bytes with the closed code');
|
||||
reset role;
|
||||
select is((select pg_get_constraintdef(oid) from pg_constraint where conname = 'anatomical_sheet_versions_content_size'),
|
||||
'CHECK ((octet_length((content)::text) <= 2097152))',
|
||||
'size: the table CHECK uses the same measure as the RPCs');
|
||||
|
||||
-- ============ Audit (§13): every lifecycle step of a sheet is recorded ============
|
||||
reset role;
|
||||
select results_eq(
|
||||
|
||||
Reference in new issue
Block a user