test(supabase): pgTAP 007 and 010 independent of the base's data

This commit is contained in:
bchanot committed 2026-10-27 14:44:03 +01:00
1 parent 6c36e2f22d
commit 7244648b4c
3 files changed
+80 -13

No files matched your search

+22 -6
View File
@@ -2,7 +2,7 @@
-- present as a platform-owned DRAFT, never published by the seed, publishable
-- by platform admins only, content accepted by the SQL referential gate.
begin;
select plan(19);
select plan(20);
insert into auth.users (id, email) values
('40000000-0000-0000-0000-000000000001', 'root-seed@test.local'),
@@ -81,13 +81,27 @@ select is((select count(*) from public.anatomical_sheet_versions where id = pg_t
select lives_ok($$ select public.publish_sheet_version(pg_temp.id('draft')) $$, 'a platform admin could publish it (rolled back here)');
-- 3. Storage: the seed registers metadata only; after the operator upload + confirmation,
-- a platform admin reads the object and a plain user none.
-- a platform admin reads the object and a plain user none. A base where the operator
-- already uploaded and confirmed the drawing (DEV) is brought back to the seed's state
-- inside this transaction: the confirmation is cleared, and the object already in the
-- bucket stands for the upload (content-addressed key, never written twice). Reads are
-- counted on the seed's own key: other objects of the bucket do not count.
reset role;
create table pg_temp.seed_objects as
select a.sha256 || '.svg' as name from public.anatomical_sheet_assets a where a.sheet_id = pg_temp.id('sheet');
grant select on pg_temp.seed_objects to authenticated, service_role;
update public.anatomical_sheet_assets set uploaded_at = null where sheet_id = pg_temp.id('sheet');
insert into storage.objects (bucket_id, name)
select 'anatomical-sheet-assets', a.sha256 || '.svg' from public.anatomical_sheet_assets a where a.sheet_id = pg_temp.id('sheet');
select 'anatomical-sheet-assets', k.name from pg_temp.seed_objects k
where not exists (select 1 from storage.objects o where o.bucket_id = 'anatomical-sheet-assets' and o.name = k.name);
select is(
(select count(*) from storage.objects o join pg_temp.seed_objects k on k.name = o.name where o.bucket_id = 'anatomical-sheet-assets'),
1::bigint, 'the drawing is in the bucket once, under its content address'
);
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-000000000001","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'seed objects are not readable before confirmation');
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets' and name in (select name from pg_temp.seed_objects)),
0::bigint, 'seed objects are not readable before confirmation');
set local role service_role;
select is(
(select count(*) from public.anatomical_sheet_assets a where a.sheet_id = pg_temp.id('sheet')
@@ -96,9 +110,11 @@ select is(
);
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-000000000001","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 1::bigint, 'a platform admin reads the official SVG');
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets' and name in (select name from pg_temp.seed_objects)),
1::bigint, 'a platform admin reads the official SVG');
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'a plain user reads no official draft SVG');
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets' and name in (select name from pg_temp.seed_objects)),
0::bigint, 'a plain user reads no official draft SVG');
select * from finish();
rollback;
+38 -5
View File
@@ -2,7 +2,7 @@
-- browser and worker RPCs, the purge queue. Every allowed path has its denial
-- twin; the eight scenarios of the Step 8 brief are named in the descriptions.
begin;
select plan(140);
select plan(141);
insert into auth.users (id, email) values
('40000000-0000-0000-0000-00000000000a', 'alice-files@test.local'),
@@ -18,6 +18,32 @@ grant all on pg_temp.ids to anon, authenticated, service_role;
grant execute on function pg_temp.id(text) to anon, authenticated, service_role;
grant execute on function pg_temp.row(text) to anon, authenticated, service_role;
-- ---------------------------------------------------------------------------
-- 0. Work the base already holds
-- ---------------------------------------------------------------------------
-- A used stack (DEV) can hold due purge entries, uploaded avatars waiting for
-- the worker and uploads begun more than 24 h ago. The worker RPCs serve the
-- whole queue, oldest first: that work is leased (or expired) here, inside
-- this transaction and rolled back with it, so the queue assertions below
-- count this file's rows only. A clean base has nothing to lease.
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
do $$
begin
for i in 1..1000 loop
exit when not exists (select 1 from public.claim_purges(100))
and not exists (select 1 from public.claim_avatar_jobs(10));
end loop;
perform public.expire_pending_uploads(86400);
end $$;
select ok(
not exists (select 1 from public.claim_purges(100))
and not exists (select 1 from public.claim_avatar_jobs(10))
and public.expire_pending_uploads(86400) = 0,
'setup: the base holds no claimable purge entry, avatar job or stale upload of its own'
);
reset role;
-- ---------------------------------------------------------------------------
-- 1. Buckets and the policy census
-- ---------------------------------------------------------------------------
@@ -414,12 +440,19 @@ select is(
-- ---------------------------------------------------------------------------
-- Objects still present under quarantine/: a1 (its row no longer references
-- it), a2, a3, a4, a5, a6, a7 (terminal rows). None pending or uploaded.
-- The sweep reads the whole bucket and holds no lease: leftovers of the base
-- (a used stack) are left out of the counts by this file's key prefix.
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
select is((select count(*) from public.claim_quarantine_leftovers(0, 100)), 7::bigint, 'every quarantine object without a waiting row is a leftover');
select is((select count(*) from public.claim_quarantine_leftovers(0, 100) l where l.asset_id is null), 1::bigint,
'a leftover whose row forgot the key (purged earlier) has no asset_id');
select is((select count(*) from public.claim_quarantine_leftovers(3600, 100)), 0::bigint, 'nothing younger than the grace is a leftover');
select is((select count(*) from public.claim_quarantine_leftovers(0, 100) l
where l.object_name like 'quarantine/40000000-0000-0000-0000-00000000000_/%'),
7::bigint, 'every quarantine object without a waiting row is a leftover');
select is((select count(*) from public.claim_quarantine_leftovers(0, 100) l
where l.object_name like 'quarantine/40000000-0000-0000-0000-00000000000_/%' and l.asset_id is null),
1::bigint, 'a leftover whose row forgot the key (purged earlier) has no asset_id');
select is((select count(*) from public.claim_quarantine_leftovers(3600, 100) l
where l.object_name like 'quarantine/40000000-0000-0000-0000-00000000000_/%'),
0::bigint, 'nothing younger than the grace is a leftover');
reset role;
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);