Files
faunosteo/supabase/tests/006_sheet_asset_storage.sql
T
bchanot d05f75da84 fix(supabase): V1-2.1 audit M1 M2 M3 and LOW fixes in M5
Approval needs an active sheet (lock sheet, version, round); a base only
from a still-published round version; rights.mfa before rights.capability;
withdrawal of a published round refused; approver separation at publish;
deprecate and working copy serialised; closed codes; append-only acks;
upload policy bound to the registering editor. pgTAP 017-019 added.
2026-10-28 18:14:09 +01:00

195 lines
13 KiB
PL/PgSQL

-- Sheet-asset Storage after V1-2.1 (M4, M5): the bucket is private and 2 MiB,
-- the read predicate follows the catalogue rules (contract §9.4), the editor
-- upload INSERT policy exists, no UPDATE/DELETE policy. Rows are seeded
-- directly (table owner) to exercise the policy, not the RPCs (those are 016).
begin;
select plan(16);
-- Bucket
select is((select public from storage.buckets where id = 'anatomical-sheet-assets'), false, 'the bucket is private');
select is((select file_size_limit from storage.buckets where id = 'anatomical-sheet-assets'), 2097152::bigint, 'the bucket limit is 2 MiB (M5)');
-- Policies on storage.objects for the bucket: read + editor insert, nothing else.
select is(
(select count(*) from pg_policies where schemaname='storage' and tablename='objects'
and (coalesce(qual,'')||coalesce(with_check,'')) like '%anatomical-sheet-assets%'),
2::bigint, 'two policies name the bucket (read, editor insert)'
);
select is(
(select count(*) from pg_policies where schemaname='storage' and tablename='objects' and cmd in ('UPDATE','DELETE')
and (coalesce(qual,'')||coalesce(with_check,'')) like '%anatomical-sheet-assets%'),
0::bigint, 'no UPDATE or DELETE policy on the bucket'
);
-- Accounts: owner, two editors (admin + capability), reader (catalog_reader), plain.
insert into auth.users (id, email, email_confirmed_at) values
('30000000-0000-0000-0000-000000000001', 'own-st@test.local', now()),
('30000000-0000-0000-0000-00000000000e', 'ed-st@test.local', now()),
('30000000-0000-0000-0000-0000000000e2', 'ed2-st@test.local', now()),
('30000000-0000-0000-0000-0000000000d0', 'rd-st@test.local', now()),
('30000000-0000-0000-0000-0000000000d1', 'pl-st@test.local', now());
insert into auth.mfa_factors (id, user_id, factor_type, status, created_at, updated_at)
values (gen_random_uuid(), '30000000-0000-0000-0000-00000000000e', 'totp', 'verified', now(), now()),
(gen_random_uuid(), '30000000-0000-0000-0000-0000000000e2', 'totp', 'verified', now(), now());
insert into auth.sessions (id, user_id, aal) values
('31000000-0000-0000-0000-000000000001', '30000000-0000-0000-0000-000000000001', 'aal2'),
('31000000-0000-0000-0000-00000000000e', '30000000-0000-0000-0000-00000000000e', 'aal2'),
('31000000-0000-0000-0000-0000000000e2', '30000000-0000-0000-0000-0000000000e2', 'aal2');
select set_config('request.jwt.claims', '', true);
select private.assign_platform_role(null, '30000000-0000-0000-0000-000000000001', 'owner');
select private.assign_platform_role(null, '30000000-0000-0000-0000-00000000000e', 'admin');
select private.assign_platform_role(null, '30000000-0000-0000-0000-0000000000e2', 'admin');
insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by) values
('30000000-0000-0000-0000-00000000000e', 'sheet_editor', 'author', 'checked', '30000000-0000-0000-0000-000000000001'),
('30000000-0000-0000-0000-0000000000e2', 'sheet_editor', 'author', 'checked', '30000000-0000-0000-0000-000000000001'),
('30000000-0000-0000-0000-0000000000d0', 'catalog_reader', 'beta', null, '30000000-0000-0000-0000-000000000001');
-- An official sheet with a published version (asset A), a deprecated version
-- (asset B), and a draft (asset C, confirmed). A and B are referenced by
-- non-draft versions; C only by the draft.
insert into public.anatomical_sheets (id, species_id, name, slug, is_official, visibility, status, current_version_id)
values ('32000000-0000-0000-0000-000000000001', 'equine', 'Pub', 'pub-storage', true, 'private', 'active', null);
insert into public.anatomical_sheet_assets (id, sheet_id, sha256, byte_size, file_name, uploaded_at) values
('32000000-0000-0000-0000-00000000000a', '32000000-0000-0000-0000-000000000001', repeat('a',64), 100, 'a.svg', now()),
('32000000-0000-0000-0000-00000000000b', '32000000-0000-0000-0000-000000000001', repeat('b',64), 100, 'b.svg', now()),
('32000000-0000-0000-0000-00000000000c', '32000000-0000-0000-0000-000000000001', repeat('c',64), 100, 'c.svg', now());
-- The content trigger needs an authenticated actor for a non-historical
-- sheet, so the direct version seeding carries the owner's claim.
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-000000000001"}', 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','P','en','P'),'description','{}'::jsonb,'scope_disclaimer','{}'::jsonb,
'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);
$$;
insert into public.anatomical_sheet_versions (id, sheet_id, version_number, status, document_schema_version, content, published_at, document_fingerprint)
values ('33000000-0000-0000-0000-000000000001', '32000000-0000-0000-0000-000000000001', 1, 'published', 2, pg_temp.doc('32000000-0000-0000-0000-00000000000a'), now(), repeat('0',64));
insert into public.anatomical_sheet_versions (id, sheet_id, version_number, status, document_schema_version, content, published_at, deprecated_at, document_fingerprint)
values ('33000000-0000-0000-0000-000000000002', '32000000-0000-0000-0000-000000000001', 2, 'deprecated', 2, pg_temp.doc('32000000-0000-0000-0000-00000000000b'), now(), now(), repeat('0',64));
insert into public.anatomical_sheet_versions (id, sheet_id, version_number, status, document_schema_version, content)
values ('33000000-0000-0000-0000-000000000003', '32000000-0000-0000-0000-000000000001', 3, 'draft', 2, pg_temp.doc('32000000-0000-0000-0000-00000000000c'));
update public.anatomical_sheets set current_version_id = '33000000-0000-0000-0000-000000000001' where id = '32000000-0000-0000-0000-000000000001';
insert into storage.objects (bucket_id, name, metadata) values
('anatomical-sheet-assets', repeat('a',64)||'.svg', '{"size":100}'),
('anatomical-sheet-assets', repeat('b',64)||'.svg', '{"size":100}'),
('anatomical-sheet-assets', repeat('c',64)||'.svg', '{"size":100}');
-- A fourth object whose row is pending (never confirmed): readable by nobody.
insert into public.anatomical_sheet_assets (id, sheet_id, sha256, byte_size, file_name)
values ('32000000-0000-0000-0000-00000000000d', '32000000-0000-0000-0000-000000000001', repeat('d',64), 100, 'd.svg');
insert into storage.objects (bucket_id, name, metadata) values ('anatomical-sheet-assets', repeat('d',64)||'.svg', '{"size":100}');
create function pg_temp.act(p_uid uuid, p_sid uuid, p_aal text) returns void language sql as $$
select set_config('request.jwt.claims', jsonb_build_object('sub',p_uid,'role','authenticated','aal',p_aal,'session_id',p_sid)::text, true);
$$;
grant execute on function pg_temp.act(uuid,uuid,text) to authenticated;
-- The objects of this file's keys the caller reads: the stack may hold other
-- drawings (sandbox suites, DEV after its first publications, LRN-093).
create function pg_temp.objects_seen() returns table (name text) language sql as $$
select o.name from storage.objects o
where o.bucket_id = 'anatomical-sheet-assets'
and o.name in (select repeat(c, 64) || '.svg' from unnest(array['a', 'b', 'c', 'd', 'e']) c)
order by o.name;
$$;
grant execute on function pg_temp.objects_seen() to authenticated;
-- Reader (catalog_reader, aal1): the published and deprecated drawings only.
set local role authenticated;
select pg_temp.act('30000000-0000-0000-0000-0000000000d0', gen_random_uuid(), 'aal1');
select results_eq(
$$ select name from pg_temp.objects_seen() $$,
$$ values (repeat('a',64)||'.svg'), (repeat('b',64)||'.svg') $$,
'a catalogue reader reads the published and deprecated drawings, not the draft or pending ones'
);
-- Editor at aal2: every uploaded drawing of the official sheet (a, b, c), not the pending d.
select pg_temp.act('30000000-0000-0000-0000-00000000000e', '31000000-0000-0000-0000-00000000000e', 'aal2');
select results_eq(
$$ select name from pg_temp.objects_seen() $$,
$$ values (repeat('a',64)||'.svg'), (repeat('b',64)||'.svg'), (repeat('c',64)||'.svg') $$,
'an editor at aal2 reads every confirmed drawing of the official sheet, not the pending one'
);
-- Editor at aal1 (no live aal2 session): the capability branch does not apply,
-- but a capability holder also has catalogue access, so the reader branch still
-- shows the published and deprecated drawings (not the draft's).
select pg_temp.act('30000000-0000-0000-0000-00000000000e', gen_random_uuid(), 'aal1');
select results_eq(
$$ select name from pg_temp.objects_seen() $$,
$$ values (repeat('a',64)||'.svg'), (repeat('b',64)||'.svg') $$,
'the editor at aal1 falls back to the reader branch: published and deprecated only'
);
-- A plain confirmed account with no right: nothing.
select pg_temp.act('30000000-0000-0000-0000-0000000000d1', gen_random_uuid(), 'aal1');
select is((select count(*) from pg_temp.objects_seen()), 0::bigint,
'a confirmed account without any right reads nothing');
-- The INSERT policy only allows a pending key of one''s own sheet: a confirmed
-- key is refused by its WITH CHECK (no UPDATE/DELETE policy exists at all, per
-- the census above; the runtime refusal of those is a storage-API concern, not
-- RLS, so it is not asserted here).
select pg_temp.act('30000000-0000-0000-0000-00000000000e', '31000000-0000-0000-0000-00000000000e', 'aal2');
select throws_ok(
$$ insert into storage.objects (bucket_id, name) values ('anatomical-sheet-assets', repeat('a',64)||'.svg') $$,
'42501', null, 'an editor cannot insert a confirmed (non-pending) object key'
);
-- A fifth asset, pending, registered by the first editor, with no object yet:
-- only the editor who registered it uploads its key (§9.3 step 2).
reset role;
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-000000000001"}', true);
insert into public.anatomical_sheet_assets (id, sheet_id, sha256, byte_size, file_name, created_by)
values ('32000000-0000-0000-0000-00000000000e', '32000000-0000-0000-0000-000000000001', repeat('e',64), 100, 'e.svg',
'30000000-0000-0000-0000-00000000000e');
set local role authenticated;
select pg_temp.act('30000000-0000-0000-0000-0000000000e2', '31000000-0000-0000-0000-0000000000e2', 'aal2');
select throws_ok(
$$ insert into storage.objects (bucket_id, name, metadata) values ('anatomical-sheet-assets', repeat('e',64)||'.svg', '{"size":100}') $$,
'42501', null, 'another editor cannot upload a pending key it did not register'
);
select pg_temp.act('30000000-0000-0000-0000-00000000000e', '31000000-0000-0000-0000-00000000000e', 'aal2');
select lives_ok(
$$ insert into storage.objects (bucket_id, name, metadata) values ('anatomical-sheet-assets', repeat('e',64)||'.svg', '{"size":100}') $$,
'the editor who registered the pending row uploads its key'
);
-- confirm with the storage check: a wrong size is refused, the right one works.
reset role;
update storage.objects set metadata = '{"size":999}' where bucket_id='anatomical-sheet-assets' and name = repeat('d',64)||'.svg';
set local role authenticated;
select pg_temp.act('30000000-0000-0000-0000-00000000000e', '31000000-0000-0000-0000-00000000000e', 'aal2');
select throws_ok(
$$ select public.confirm_sheet_asset_upload('32000000-0000-0000-0000-00000000000d') $$,
'P0001', 'size_mismatch', 'confirm refuses a size that does not match the row (F7)'
);
reset role;
update storage.objects set metadata = '{"size":100}' where bucket_id='anatomical-sheet-assets' and name = repeat('d',64)||'.svg';
set local role authenticated;
select pg_temp.act('30000000-0000-0000-0000-00000000000e', '31000000-0000-0000-0000-00000000000e', 'aal2');
select lives_ok(
$$ select public.confirm_sheet_asset_upload('32000000-0000-0000-0000-00000000000d') $$,
'confirm succeeds when the object exists and the size matches'
);
select is((select uploaded_at is not null from public.anatomical_sheet_assets where id='32000000-0000-0000-0000-00000000000d'),
true, 'the row is now confirmed');
-- The operator read RPC: service role only.
select throws_ok(
$$ select public.get_sheet_asset_state('32000000-0000-0000-0000-00000000000a') $$,
'42501', null, 'an editor cannot call the operator read RPC (EXECUTE is service_role only)'
);
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
select is(
(select public.get_sheet_asset_state('32000000-0000-0000-0000-00000000000a') ->> 'sha256'),
repeat('a',64), 'the operator read RPC returns the asset state for the service role'
);
select * from finish();
rollback;