Files
faunosteo/supabase/tests/010_files_foundation.sql
T
bchanot 614887470c feat(supabase): files foundation — assets table, private buckets, avatar policies through assets, purge queue, browser and worker RPCs
Migration 20261013100000: asset_status enum, public.assets (server-built keys,
lease token, closed rejection reasons), seven private buckets (avatars open with
cap and MIME list, six closed), storage.objects SELECT/INSERT policies for
avatars evaluated through assets, private.object_purge_queue fed by status and
delete triggers (48 h / 1 h grace), storage ceiling, RPCs begin_avatar_upload /
confirm_asset_upload / remove_avatar (authenticated) and claim_avatar_jobs /
complete_avatar_job / reject_avatar_job / mark_quarantine_purged / claim_purges /
ack_purge / claim_quarantine_leftovers / expire_pending_uploads (service_role),
explicit revokes and column grants. pgTAP 010 (125 checks: allowed and denied
twins, policy census, stale lease, supersede, removal, leftovers, cascade,
ceiling), 001 census extended, database.types.ts regenerated from the real
stack, schema-types test counts input arguments only.
2026-10-13 19:32:15 +02:00

492 lines
32 KiB
PL/PgSQL

-- Files foundation: buckets, assets, avatar policies through assets, the
-- 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(125);
insert into auth.users (id, email) values
('40000000-0000-0000-0000-00000000000a', 'alice-files@test.local'),
('40000000-0000-0000-0000-00000000000b', 'bob-files@test.local'),
('40000000-0000-0000-0000-00000000000c', 'carol-files@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 $$;
create function pg_temp.row(p_key text) returns public.assets language sql stable security definer
as $$ select a from public.assets a where a.id = pg_temp.id(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;
grant execute on function pg_temp.row(text) to anon, authenticated, service_role;
-- ---------------------------------------------------------------------------
-- 1. Buckets and the policy census
-- ---------------------------------------------------------------------------
select is(
(select count(*) from storage.buckets where id in ('avatars', 'logos', 'professional-documents',
'client-files', 'animal-files', 'report-files', 'anatomical-sheet-assets', 'imports')),
8::bigint, 'the eight buckets exist'
);
select is(
(select count(*) from storage.buckets where public and id in ('avatars', 'logos', 'professional-documents',
'client-files', 'animal-files', 'report-files', 'anatomical-sheet-assets', 'imports')),
0::bigint, 'every bucket is private'
);
select is((select file_size_limit from storage.buckets where id = 'avatars'), 5242880::bigint, 'avatars capped at 5 MiB');
select is(
(select allowed_mime_types from storage.buckets where id = 'avatars'),
array['image/jpeg', 'image/png', 'image/webp'], 'avatars accepts jpeg, png, webp only (never SVG)'
);
select is(
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'
and (select count(*) from regexp_matches(coalesce(qual, '') || coalesce(with_check, ''), 'bucket_id = ''[a-z-]+''', 'g')) <> 1),
0::bigint, 'census: every storage.objects policy pins exactly one bucket literal'
);
select is(
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'
and (coalesce(qual, '') || coalesce(with_check, '')) like '%bucket_id = ''avatars''%'),
2::bigint, 'census: two policies name avatars'
);
select is(
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'
and (coalesce(qual, '') || coalesce(with_check, '')) like '%bucket_id = ''anatomical-sheet-assets''%'),
1::bigint, 'census: one policy names anatomical-sheet-assets'
);
select is(
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'),
3::bigint, 'census: three policies in total, so the six closed buckets have none'
);
select is(
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'
and cmd in ('UPDATE', 'DELETE')),
0::bigint, 'no UPDATE / DELETE policy exists on storage.objects for any API role'
);
-- ---------------------------------------------------------------------------
-- 2. Schema and privileges of assets
-- ---------------------------------------------------------------------------
select has_table('public', 'assets', 'table public.assets exists');
select ok((select relrowsecurity from pg_class where oid = 'public.assets'::regclass), 'RLS enabled on assets');
select enum_has_labels('public', 'asset_status',
array['pending_upload', 'uploaded', 'clean', 'rejected', 'deleted'], 'asset_status labels');
select ok(not has_table_privilege('anon', 'public.assets', 'select'), 'anon cannot select assets');
select ok(not has_table_privilege('authenticated', 'public.assets', 'insert'), 'authenticated cannot insert assets');
select ok(not has_table_privilege('authenticated', 'public.assets', 'update'), 'authenticated cannot update assets');
select ok(not has_table_privilege('authenticated', 'public.assets', 'delete'), 'authenticated cannot delete assets');
select ok(has_column_privilege('authenticated', 'public.assets', 'status', 'select'), 'authenticated reads status');
select ok(not has_column_privilege('authenticated', 'public.assets', 'lock_token', 'select'), 'lock_token stays server-side');
select ok(not has_column_privilege('authenticated', 'public.assets', 'quarantine_key', 'select'), 'quarantine_key stays server-side');
select ok(not has_function_privilege('authenticated', 'public.claim_avatar_jobs(integer)', 'execute'), 'authenticated cannot claim jobs');
select ok(not has_function_privilege('authenticated', 'public.complete_avatar_job(uuid, uuid, integer, text)', 'execute'), 'authenticated cannot complete a job');
select ok(not has_function_privilege('authenticated', 'public.claim_purges(integer)', 'execute'), 'authenticated cannot claim purges');
select ok(not has_function_privilege('anon', 'public.begin_avatar_upload(text, integer)', 'execute'), 'anon cannot begin an upload');
select is(private.storage_ceiling(), 10737418240::bigint, 'storage ceiling is 10 GiB');
-- ---------------------------------------------------------------------------
-- 3. Alice begins an upload
-- ---------------------------------------------------------------------------
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select throws_ok($$ select public.begin_avatar_upload('image/gif', 1000) $$, 'P0001', 'unsupported_type', 'a GIF is refused at begin');
select throws_ok($$ select public.begin_avatar_upload('image/svg+xml', 1000) $$, 'P0001', 'unsupported_type', 'an SVG is refused at begin');
select throws_ok($$ select public.begin_avatar_upload('image/jpeg', 5242881) $$, 'P0001', 'too_large', 'over 5 MiB is refused at begin');
select throws_ok($$ select public.begin_avatar_upload('image/jpeg', 0) $$, 'P0001', 'too_large', 'an empty file is refused at begin');
insert into pg_temp.ids select 'a1', (public.begin_avatar_upload('image/jpeg', 1000) ->> 'asset_id')::uuid;
select is((pg_temp.row('a1')).status, 'pending_upload', 'a1 is pending_upload');
select is((pg_temp.row('a1')).object_key,
'profiles/40000000-0000-0000-0000-00000000000a/' || pg_temp.id('a1') || '/avatar.webp', 'final key built from uuids');
select is((pg_temp.row('a1')).quarantine_key,
'quarantine/40000000-0000-0000-0000-00000000000a/' || pg_temp.id('a1') || '/upload', 'quarantine key built from uuids');
select results_eq(
$$ select id, status::text, object_key from public.assets order by created_at $$,
$$ select pg_temp.id('a1'), 'pending_upload'::text, (pg_temp.row('a1')).object_key $$,
'alice reads her own row through RLS'
);
select throws_ok($$ select lock_token from public.assets $$, '42501', null, 'alice cannot read the lease column');
-- ---------------------------------------------------------------------------
-- 4. Upload policy: only the own quarantine key of a pending row
-- ---------------------------------------------------------------------------
select throws_ok(
$$ insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a1')).object_key, '{"size":1000}') $$,
'42501', null, 'scenario: the final key cannot be written by the browser'
);
select throws_ok(
$$ insert into storage.objects (bucket_id, name, metadata) values ('avatars', 'quarantine/40000000-0000-0000-0000-00000000000a/00000000-0000-0000-0000-000000000000/upload', '{"size":1000}') $$,
'42501', null, 'an unknown quarantine key is refused'
);
select throws_ok(
$$ insert into storage.objects (bucket_id, name, metadata) values ('logos', 'anything', '{"size":1000}') $$,
'42501', null, 'a closed bucket refuses every upload'
);
select throws_ok($$ select public.confirm_asset_upload(pg_temp.id('a1')) $$, 'P0001', 'upload_missing', 'confirm before the bytes landed is refused');
-- Bob at Alice's key
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select throws_ok(
$$ insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a1')).quarantine_key, '{"size":1000}') $$,
'42501', null, 'scenario: Bob cannot upload at Alice''s quarantine key'
);
select throws_ok($$ select public.confirm_asset_upload(pg_temp.id('a1')) $$, 'P0001', 'not_found', 'Bob cannot confirm Alice''s row');
select is((select count(*) from public.assets), 0::bigint, 'Bob sees none of Alice''s rows');
-- Alice uploads and confirms
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select lives_ok(
$$ insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a1')).quarantine_key, '{"size":1000}') $$,
'Alice uploads at her quarantine key'
);
select is((select count(*) from storage.objects where bucket_id = 'avatars'), 0::bigint,
'scenario: a quarantine object is never readable, not even by its uploader');
select lives_ok($$ select public.confirm_asset_upload(pg_temp.id('a1')) $$, 'Alice confirms');
select is((pg_temp.row('a1')).status, 'uploaded', 'a1 is uploaded');
select throws_ok($$ select public.confirm_asset_upload(pg_temp.id('a1')) $$, 'P0001', 'invalid_state', 'confirming twice is refused');
select throws_ok(
$$ insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a1')).quarantine_key, '{"size":1000}') $$,
'42501', null, 'once uploaded, the quarantine key accepts no second upload'
);
select is((select count(*) from storage.objects where bucket_id = 'avatars'), 0::bigint, 'nothing readable while uploaded');
select throws_ok($$ select * from public.claim_avatar_jobs(1) $$, '42501', null, 'the browser cannot claim jobs');
-- ---------------------------------------------------------------------------
-- 5. The worker claims, the parent writes the clean object, completes
-- ---------------------------------------------------------------------------
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
insert into pg_temp.ids select 'tok1', lock_token from public.claim_avatar_jobs(1);
select is((select count(*) from public.claim_avatar_jobs(1)), 0::bigint, 'a live lease is skipped by the next claim');
select is((pg_temp.row('a1')).attempts, 1::smallint, 'claim counts one attempt');
select throws_ok(
$$ select public.complete_avatar_job(pg_temp.id('a1'), gen_random_uuid(), 30000, repeat('a', 64)) $$,
'P0001', 'invalid_state', 'scenario: a complete with a foreign lease token is refused'
);
select throws_ok(
$$ select public.complete_avatar_job(pg_temp.id('a1'), pg_temp.id('tok1'), 30000, 'nope') $$,
'P0001', 'invalid_output', 'a malformed sha256 is refused'
);
select throws_ok(
$$ select public.complete_avatar_job(pg_temp.id('a1'), pg_temp.id('tok1'), 600000, repeat('a', 64)) $$,
'P0001', 'invalid_output', 'an output over 512 KiB is refused'
);
reset role;
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a1')).object_key, '{"size":30000}');
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
select lives_ok($$ select public.complete_avatar_job(pg_temp.id('a1'), pg_temp.id('tok1'), 30000, repeat('a', 64)) $$, 'worker completes a1');
select is((pg_temp.row('a1')).status, 'clean', 'a1 is clean');
select is((pg_temp.row('a1')).byte_size, 30000, 'byte_size recorded');
select is((pg_temp.row('a1')).lock_token, null, 'lease cleared on complete');
select throws_ok(
$$ select public.complete_avatar_job(pg_temp.id('a1'), pg_temp.id('tok1'), 30000, repeat('a', 64)) $$,
'P0001', 'invalid_state', 'a clean row cannot be completed again'
);
select lives_ok($$ select public.mark_quarantine_purged(pg_temp.id('a1')) $$, 'worker marks the quarantine purged');
select is((pg_temp.row('a1')).quarantine_key, null, 'quarantine_key cleared');
reset role;
select is((select count(*) from private.object_purge_queue where object_name = (pg_temp.row('a1')).object_key), 0::bigint,
'a clean row''s object_key never enters the purge queue');
-- ---------------------------------------------------------------------------
-- 6. Read policy: the owner reads exactly the clean object
-- ---------------------------------------------------------------------------
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select results_eq(
$$ select name from storage.objects where bucket_id = 'avatars' $$,
$$ select (pg_temp.row('a1')).object_key $$,
'Alice reads her clean avatar object and nothing else (the quarantine object still exists)'
);
select ok(
not private.can_read_avatar_object('40000000-0000-0000-0000-00000000000a', (pg_temp.row('a1')).object_key || '/')
and not private.can_read_avatar_object('40000000-0000-0000-0000-00000000000a', upper((pg_temp.row('a1')).object_key))
and not private.can_read_avatar_object('40000000-0000-0000-0000-00000000000a', 'quarantine/40000000-0000-0000-0000-00000000000a/' || pg_temp.id('a1') || '/upload'),
'name variants and the quarantine key never match the read predicate'
);
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'avatars'), 0::bigint, 'scenario: Bob reads none of Alice''s objects');
select is((select count(*) from public.assets), 0::bigint, 'Bob reads none of Alice''s rows');
select is(public.remove_avatar(), 0, 'scenario: Bob''s remove_avatar touches nothing of Alice');
select is((pg_temp.row('a1')).status, 'clean', 'Alice''s avatar survives Bob''s removal call');
set local role anon;
select set_config('request.jwt.claims', '', true);
select is((select count(*) from storage.objects where bucket_id = 'avatars'), 0::bigint, 'scenario: anon reads no private object');
select throws_ok($$ select public.begin_avatar_upload('image/jpeg', 1000) $$, '42501', null, 'anon cannot begin an upload');
select throws_ok($$ select public.remove_avatar() $$, '42501', null, 'anon cannot remove an avatar');
reset role;
-- ---------------------------------------------------------------------------
-- 7. Replacement: a2 clean → a1 deleted and enqueued with the 48 h grace
-- ---------------------------------------------------------------------------
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
insert into pg_temp.ids select 'a2', (public.begin_avatar_upload('image/png', 2000) ->> 'asset_id')::uuid;
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a2')).quarantine_key, '{"size":2000}');
select public.confirm_asset_upload(pg_temp.id('a2'));
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
insert into pg_temp.ids select 'tok2', lock_token from public.claim_avatar_jobs(1);
reset role;
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a2')).object_key, '{"size":31000}');
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
select lives_ok($$ select public.complete_avatar_job(pg_temp.id('a2'), pg_temp.id('tok2'), 31000, repeat('b', 64)) $$, 'worker completes a2');
select is((pg_temp.row('a2')).status, 'clean', 'a2 is clean');
select is((pg_temp.row('a1')).status, 'deleted', 'the previous clean avatar is deleted in the same transaction');
select is((select count(*) from public.claim_purges(100)), 0::bigint, 'nothing is due before its grace elapsed');
reset role;
select is(
(select count(*) from private.object_purge_queue
where object_name = (pg_temp.row('a1')).object_key and not_before > now() + interval '47 hours'),
1::bigint, 'the replaced object is enqueued with the 48 h grace (backup invariant)'
);
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select results_eq(
$$ select name from storage.objects where bucket_id = 'avatars' $$,
$$ select (pg_temp.row('a2')).object_key $$,
'scenario: a replaced object is no longer readable although it still exists'
);
reset role;
-- ---------------------------------------------------------------------------
-- 8. Supersede race: a claimed job whose row was superseded cannot complete
-- ---------------------------------------------------------------------------
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
insert into pg_temp.ids select 'a3', (public.begin_avatar_upload('image/jpeg', 3000) ->> 'asset_id')::uuid;
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a3')).quarantine_key, '{"size":3000}');
select public.confirm_asset_upload(pg_temp.id('a3'));
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
insert into pg_temp.ids select 'tok3', lock_token from public.claim_avatar_jobs(1);
reset role;
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
insert into pg_temp.ids select 'a4', (public.begin_avatar_upload('image/jpeg', 4000) ->> 'asset_id')::uuid;
select is((pg_temp.row('a3')).status, 'deleted', 'a new pick supersedes the in-flight row');
reset role;
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
select throws_ok(
$$ select public.complete_avatar_job(pg_temp.id('a3'), pg_temp.id('tok3'), 30000, repeat('c', 64)) $$,
'P0001', 'invalid_state', 'scenario: an old task cannot complete a superseded row'
);
select throws_ok(
$$ select public.reject_avatar_job(pg_temp.id('a3'), pg_temp.id('tok3'), 'decode_failed') $$,
'P0001', 'invalid_state', 'nor reject it'
);
select is((pg_temp.row('a2')).status, 'clean', 'the current avatar is untouched by the stale task');
reset role;
select is(
(select count(*) from private.object_purge_queue where object_name = (pg_temp.row('a3')).quarantine_key and not_before < now() + interval '2 hours'),
1::bigint, 'the superseded quarantine key is enqueued with the 1 h grace'
);
-- ---------------------------------------------------------------------------
-- 9. Stale lease: an expired lease is re-claimed, the old token is refused
-- ---------------------------------------------------------------------------
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a4')).quarantine_key, '{"size":4000}');
select public.confirm_asset_upload(pg_temp.id('a4'));
reset role;
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
insert into pg_temp.ids select 'tok4a', lock_token from public.claim_avatar_jobs(1);
reset role;
update public.assets set locked_at = now() - interval '11 minutes' where id = pg_temp.id('a4');
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
insert into pg_temp.ids select 'tok4b', lock_token from public.claim_avatar_jobs(1);
select is((pg_temp.row('a4')).attempts, 2::smallint, 'an expired lease is re-claimed (second attempt)');
select throws_ok(
$$ select public.complete_avatar_job(pg_temp.id('a4'), pg_temp.id('tok4a'), 30000, repeat('d', 64)) $$,
'P0001', 'invalid_state', 'scenario: the worker whose lease expired cannot overwrite with its stale token'
);
reset role;
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a4')).object_key, '{"size":32000}');
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
select lives_ok($$ select public.complete_avatar_job(pg_temp.id('a4'), pg_temp.id('tok4b'), 32000, repeat('d', 64)) $$, 'the current lease holder completes');
select is((pg_temp.row('a4')).status, 'clean', 'a4 is clean');
select is((pg_temp.row('a2')).status, 'deleted', 'a2 replaced');
reset role;
-- ---------------------------------------------------------------------------
-- 10. Removal: clean and in-flight rows go, a later complete is refused
-- ---------------------------------------------------------------------------
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
insert into pg_temp.ids select 'a5', (public.begin_avatar_upload('image/webp', 5000) ->> 'asset_id')::uuid;
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a5')).quarantine_key, '{"size":5000}');
select public.confirm_asset_upload(pg_temp.id('a5'));
reset role;
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
insert into pg_temp.ids select 'tok5', lock_token from public.claim_avatar_jobs(1);
reset role;
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select is(public.remove_avatar(), 2, 'remove_avatar deletes the clean row and the in-flight one');
select is((pg_temp.row('a4')).status, 'deleted', 'a4 deleted');
select is((pg_temp.row('a5')).status, 'deleted', 'a5 deleted');
select is((select count(*) from storage.objects where bucket_id = 'avatars'), 0::bigint, 'nothing readable after removal');
reset role;
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
select throws_ok(
$$ select public.complete_avatar_job(pg_temp.id('a5'), pg_temp.id('tok5'), 30000, repeat('e', 64)) $$,
'P0001', 'invalid_state', 'scenario: a removed avatar does not reappear through a late complete'
);
reset role;
-- ---------------------------------------------------------------------------
-- 11. Rejections: closed codes, and the third expired lease
-- ---------------------------------------------------------------------------
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
insert into pg_temp.ids select 'a6', (public.begin_avatar_upload('image/jpeg', 6000) ->> 'asset_id')::uuid;
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a6')).quarantine_key, '{"size":6000}');
select public.confirm_asset_upload(pg_temp.id('a6'));
reset role;
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
insert into pg_temp.ids select 'tok6', lock_token from public.claim_avatar_jobs(1);
select throws_ok(
$$ select public.reject_avatar_job(pg_temp.id('a6'), pg_temp.id('tok6'), 'virus') $$,
'P0001', 'invalid_reason', 'a reason outside the closed set is refused'
);
select lives_ok($$ select public.reject_avatar_job(pg_temp.id('a6'), pg_temp.id('tok6'), 'decode_failed') $$, 'worker rejects a6');
select is((pg_temp.row('a6')).status, 'rejected', 'a6 rejected');
select is((pg_temp.row('a6')).rejection_reason, 'decode_failed', 'reason stored');
reset role;
select is(
(select count(*) from private.object_purge_queue where object_name in ((pg_temp.row('a6')).object_key, (pg_temp.row('a6')).quarantine_key)),
2::bigint, 'both keys of a rejected row are enqueued'
);
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select is((select rejection_reason from public.assets where id = pg_temp.id('a6')), 'decode_failed', 'the owner reads the rejection code');
insert into pg_temp.ids select 'a7', (public.begin_avatar_upload('image/jpeg', 7000) ->> 'asset_id')::uuid;
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a7')).quarantine_key, '{"size":7000}');
select public.confirm_asset_upload(pg_temp.id('a7'));
reset role;
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
select is((select count(*) from public.claim_avatar_jobs(1)), 1::bigint, 'lease 1 of a7');
reset role;
update public.assets set locked_at = now() - interval '11 minutes' where id = pg_temp.id('a7');
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
select is((select count(*) from public.claim_avatar_jobs(1)), 1::bigint, 'lease 2 of a7');
reset role;
update public.assets set locked_at = now() - interval '11 minutes' where id = pg_temp.id('a7');
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
select is((select count(*) from public.claim_avatar_jobs(1)), 1::bigint, 'lease 3 of a7');
reset role;
update public.assets set locked_at = now() - interval '11 minutes' where id = pg_temp.id('a7');
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
select is((select count(*) from public.claim_avatar_jobs(1)), 0::bigint, 'no fourth lease');
select is((pg_temp.row('a7')).status, 'rejected', 'the third expired lease rejects the row');
select is((pg_temp.row('a7')).rejection_reason, 'processing_error', 'with processing_error');
reset role;
-- ---------------------------------------------------------------------------
-- 12. Purge queue: due entries are leased, acked entries disappear
-- ---------------------------------------------------------------------------
update private.object_purge_queue set not_before = now() - interval '1 second'
where object_name in ((pg_temp.row('a6')).object_key, (pg_temp.row('a6')).quarantine_key);
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
create table pg_temp.purges as select * from public.claim_purges(100);
select is((select count(*) from pg_temp.purges), 2::bigint, 'the two due entries are claimed');
select is((select count(*) from public.claim_purges(100)), 0::bigint, 'a leased purge entry is not claimed again');
select lives_ok($$ select public.ack_purge(purge_id) from pg_temp.purges $$, 'worker acks both');
reset role;
select is(
(select count(*) from private.object_purge_queue where object_name in ((pg_temp.row('a6')).object_key, (pg_temp.row('a6')).quarantine_key)),
0::bigint, 'acked entries are gone'
);
-- ---------------------------------------------------------------------------
-- 13. Quarantine leftovers and expiry of never-confirmed uploads
-- ---------------------------------------------------------------------------
-- Objects still present under quarantine/: a1 (its row no longer references
-- it), a2, a3, a4, a5, a6, a7 (terminal rows). None pending or uploaded.
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');
reset role;
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
insert into pg_temp.ids select 'a8', (public.begin_avatar_upload('image/jpeg', 8000) ->> 'asset_id')::uuid;
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a8')).quarantine_key, '{"size":8000}');
reset role;
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) l where l.object_name = (pg_temp.row('a8')).quarantine_key), 0::bigint,
'scenario: bytes of a pending row are not a leftover');
select is(public.expire_pending_uploads(86400), 0, 'a fresh pending row is not expired');
reset role;
update public.assets set created_at = now() - interval '25 hours' where id = pg_temp.id('a8');
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
select is(public.expire_pending_uploads(86400), 1, 'a pending row older than 24 h is expired');
select is((pg_temp.row('a8')).status, 'deleted', 'expired row deleted');
select is((select count(*) from public.claim_quarantine_leftovers(0, 100) l where l.object_name = (pg_temp.row('a8')).quarantine_key), 1::bigint,
'scenario: bytes landed for a row that moved on are swept as a leftover');
reset role;
-- ---------------------------------------------------------------------------
-- 14. Rate limit, audit, account cascade, ceiling
-- ---------------------------------------------------------------------------
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
-- a1..a7 = 7 rows this hour (a8 was aged in §13); three more pass, the 11th is refused.
select lives_ok($$ select public.begin_avatar_upload('image/jpeg', 100) $$, 'begin 8 of the hour');
select lives_ok($$ select public.begin_avatar_upload('image/jpeg', 100) $$, 'begin 9 of the hour');
select lives_ok($$ select public.begin_avatar_upload('image/jpeg', 100) $$, 'begin 10 of the hour');
select throws_ok($$ select public.begin_avatar_upload('image/jpeg', 100) $$, 'P0001', 'rate_limited', 'the 11th begin within the hour is refused');
reset role;
select is(
(select count(*) from audit.audit_logs where target_user_id = '40000000-0000-0000-0000-00000000000a' and action = 'asset.avatar_set'),
3::bigint, 'three avatar_set audit rows (a1, a2, a4)'
);
select is(
(select count(*) from audit.audit_logs where target_user_id = '40000000-0000-0000-0000-00000000000a' and action = 'asset.avatar_removed'),
1::bigint, 'one avatar_removed audit row'
);
select is(
(select count(*) from audit.audit_logs where target_user_id = '40000000-0000-0000-0000-00000000000a' and action = 'asset.rejected'),
2::bigint, 'two rejected audit rows (worker refusal, third expired lease)'
);
-- Bob's account disappears: his keys are enqueued by the delete trigger.
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
insert into pg_temp.ids select 'b1', (public.begin_avatar_upload('image/jpeg', 1000) ->> 'asset_id')::uuid;
reset role;
delete from auth.users where id = '40000000-0000-0000-0000-00000000000b';
select is((select count(*) from public.assets where owner_id = '40000000-0000-0000-0000-00000000000b'), 0::bigint, 'assets cascade with the profile');
select is(
(select count(*) from private.object_purge_queue where object_name like 'profiles/40000000-0000-0000-0000-00000000000b/%' or object_name like 'quarantine/40000000-0000-0000-0000-00000000000b/%'),
2::bigint, 'the cascade enqueues both keys of the deleted row'
);
-- Ceiling: 2048 rejected rows of 5 MiB owned by Alice fill the 10 GiB.
insert into public.assets (owner_id, kind, bucket_id, object_key, quarantine_key, status, declared_mime, declared_size, rejection_reason, created_at)
select '40000000-0000-0000-0000-00000000000a', 'profile_avatar', 'avatars',
'profiles/40000000-0000-0000-0000-00000000000a/' || g || '/avatar.webp',
'quarantine/40000000-0000-0000-0000-00000000000a/' || g || '/upload',
'rejected', 'image/jpeg', 5242880, 'decode_failed', now() - interval '2 hours'
from (select gen_random_uuid() as g from generate_series(1, 2048)) s;
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
select throws_ok($$ select public.begin_avatar_upload('image/jpeg', 1000) $$, 'P0001', 'storage_full', 'the global ceiling refuses a new upload');
reset role;
select * from finish();
rollback;