Files
faunosteo/supabase/tests/006_sheet_asset_storage.sql

156 lines
10 KiB
PL/PgSQL

-- Storage read policy on bucket anatomical-sheet-assets: an object is readable
-- through a sheet (published reference for users, any version for editors),
-- never through its hash alone. No API writes.
begin;
select plan(28);
insert into auth.users (id, email) values
('30000000-0000-0000-0000-00000000000a', 'alice-assets@test.local'),
('30000000-0000-0000-0000-00000000000b', 'bob-assets@test.local');
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 $$;
grant all on pg_temp.ids to anon, authenticated, service_role;
grant execute on function pg_temp.id(text) to anon, authenticated, service_role;
-- Minimal publishable document with one main view on a given asset.
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', 'A', 'en', 'A'),
'description', '{}'::jsonb, 'scope_disclaimer', '{}'::jsonb,
'attribution', jsonb_build_object('authors', '[]'::jsonb, 'license', null, 'notes', null),
'vertebral_config', jsonb_build_object('regions', '[]'::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 function pg_temp.doc(uuid) to authenticated, service_role;
-- 1. Bucket contract
select is((select public from storage.buckets where id = 'anatomical-sheet-assets'), false, 'bucket is private');
select is(
(select allowed_mime_types from storage.buckets where id = 'anatomical-sheet-assets'),
array['image/svg+xml'], 'bucket accepts SVG only'
);
select is(
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'
and policyname = 'anatomical_sheet_assets_read' and cmd = 'SELECT'),
1::bigint, 'one SELECT policy on storage.objects for the bucket'
);
select is(
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'
and policyname like 'anatomical_sheet_assets%' and cmd <> 'SELECT'),
0::bigint, 'no write policy on the bucket for API roles'
);
-- 2. Alice: sheet with published v1 on asset A, draft v2 on asset B; C is registered but unreferenced
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
insert into pg_temp.ids select 'sheet', public.create_anatomical_sheet('equine', 'Assets', 'assets-test', pg_temp.doc(null));
insert into pg_temp.ids select 'a', public.register_sheet_asset(pg_temp.id('sheet'), repeat('a', 64), 100, 'A.svg');
insert into pg_temp.ids select 'b', public.register_sheet_asset(pg_temp.id('sheet'), repeat('b', 64), 100, 'B.svg');
insert into pg_temp.ids select 'c', public.register_sheet_asset(pg_temp.id('sheet'), repeat('c', 64), 100, 'C.svg');
insert into pg_temp.ids select 'v1', id from public.anatomical_sheet_versions where sheet_id = pg_temp.id('sheet');
update public.anatomical_sheet_versions set content = pg_temp.doc(pg_temp.id('a')) where id = pg_temp.id('v1');
select lives_ok($$ select public.publish_sheet_version(pg_temp.id('v1')) $$, 'v1 published on asset A');
insert into pg_temp.ids select 'v2', public.create_sheet_draft(pg_temp.id('sheet'));
update public.anatomical_sheet_versions set content = pg_temp.doc(pg_temp.id('b')) where id = pg_temp.id('v2');
-- Objects uploaded by the operator (owner session), then confirmed through the
-- service-role RPC for A and B only; C stays metadata without bytes.
reset role;
insert into storage.objects (bucket_id, name) values
('anatomical-sheet-assets', repeat('a', 64) || '.svg'),
('anatomical-sheet-assets', repeat('b', 64) || '.svg'),
('anatomical-sheet-assets', repeat('c', 64) || '.svg'),
('anatomical-sheet-assets', repeat('d', 64) || '.svg');
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'nothing is readable before the upload is confirmed');
select throws_ok(
$$ select public.confirm_sheet_asset_upload(pg_temp.id('a')) $$,
'42501', null, 'an editor cannot confirm an upload (service role only)'
);
set local role service_role;
select lives_ok($$ select public.confirm_sheet_asset_upload(pg_temp.id('a')) $$, 'service role confirms asset A');
select lives_ok($$ select public.confirm_sheet_asset_upload(pg_temp.id('b')) $$, 'service role confirms asset B');
select is(public.confirm_sheet_asset_upload(pg_temp.id('a')), pg_temp.id('a'), 'confirming twice is idempotent');
reset role;
-- 3. Editor reads every object of her sheet (published, draft, unreferenced), not foreign ones
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select results_eq(
$$ select name from storage.objects where bucket_id = 'anatomical-sheet-assets' order by name $$,
$$ values (repeat('a', 64) || '.svg'), (repeat('b', 64) || '.svg') $$,
'editor reads the confirmed objects of her sheet; unconfirmed and foreign objects stay hidden'
);
-- 4. Bob: private sheet → nothing; claiming the hash on his own sheet opens nothing
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'other user reads nothing from a private sheet');
insert into pg_temp.ids select 'bob-sheet', public.create_anatomical_sheet('equine', 'Claim', 'claim-test', pg_temp.doc(null));
select lives_ok(
$$ select public.register_sheet_asset(pg_temp.id('bob-sheet'), repeat('a', 64), 100, 'A.svg') $$,
'bob registers the known hash on his own sheet'
);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'a self-registered hash without a confirmed upload opens no object');
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select lives_ok($$ select public.set_sheet_visibility(pg_temp.id('sheet'), 'public') $$, 'alice makes the sheet public');
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select results_eq(
$$ select name from storage.objects where bucket_id = 'anatomical-sheet-assets' order by name $$,
$$ values (repeat('a', 64) || '.svg') $$,
'other user reads only the object a published version references (not the draft asset, not the unreferenced one)'
);
select throws_ok(
$$ insert into storage.objects (bucket_id, name) values ('anatomical-sheet-assets', 'x.svg') $$,
'42501', null, 'authenticated cannot upload into the bucket'
);
-- The real image also carries storage.protect_delete (statement trigger), so a
-- direct DELETE is asserted through the policy census, not by running it.
select is(
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'
and cmd in ('INSERT', 'UPDATE', 'DELETE')
and (coalesce(qual, '') like '%anatomical-sheet-assets%' or coalesce(with_check, '') like '%anatomical-sheet-assets%')),
0::bigint, 'no API role has an insert/update/delete policy on the bucket objects'
);
-- Name variants never match the strict key
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select ok(
not private.can_read_sheet_asset_object('30000000-0000-0000-0000-00000000000a', repeat('a', 64))
and not private.can_read_sheet_asset_object('30000000-0000-0000-0000-00000000000a', upper(repeat('a', 64)) || '.svg')
and not private.can_read_sheet_asset_object('30000000-0000-0000-0000-00000000000a', 'x/' || repeat('a', 64) || '.svg')
and not private.can_read_sheet_asset_object('30000000-0000-0000-0000-00000000000a', repeat('a', 64) || '.svg/'),
'object name must be exactly <sha256>.svg'
);
-- A deprecated version still publishes its asset
select lives_ok($$ select public.publish_sheet_version(pg_temp.id('v2')) $$, 'v2 (asset B) published');
select lives_ok($$ select public.deprecate_sheet_version(pg_temp.id('v1'), 'old') $$, 'v1 (asset A) deprecated');
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select results_eq(
$$ select name from storage.objects where bucket_id = 'anatomical-sheet-assets' order by name $$,
$$ values (repeat('a', 64) || '.svg'), (repeat('b', 64) || '.svg') $$,
'published and deprecated references both open the object for users of a public sheet'
);
-- 5. Archived sheet hides objects from others; anon sees nothing
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select lives_ok($$ select public.archive_anatomical_sheet(pg_temp.id('sheet')) $$, 'alice archives');
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'archived sheet: others read nothing');
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select lives_ok($$ select public.restore_anatomical_sheet(pg_temp.id('sheet')) $$, 'alice restores');
select lives_ok($$ select public.delete_anatomical_sheet(pg_temp.id('sheet')) $$, 'alice soft-deletes the sheet');
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'deleted sheet: even the creator reads nothing');
set local role anon;
select set_config('request.jwt.claims', '', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'anon reads nothing');
select * from finish();
rollback;