Files
faunosteo/supabase/tests/001_schema.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

71 lines
3.8 KiB
PL/PgSQL

-- Schema contract: objects exist, RLS is on, API roles start from zero.
begin;
select plan(26);
-- Schemas
select has_schema('private', 'schema private exists');
select has_schema('audit', 'schema audit exists');
-- Tables
select has_table('public', 'profiles', 'table public.profiles exists');
select has_table('public', 'profile_roles', 'table public.profile_roles exists');
select has_table('audit', 'audit_logs', 'table audit.audit_logs exists');
-- RLS enabled everywhere (pgTAP has no helper: read pg_class)
select ok(
(select relrowsecurity from pg_class where oid = 'public.profiles'::regclass),
'RLS enabled on profiles'
);
select ok(
(select relrowsecurity from pg_class where oid = 'public.profile_roles'::regclass),
'RLS enabled on profile_roles'
);
select ok(
(select relrowsecurity from pg_class where oid = 'audit.audit_logs'::regclass),
'RLS enabled on audit_logs'
);
-- Enum
select enum_has_labels(
'public', 'platform_role',
array['owner', 'admin', 'moderator', 'support'],
'platform_role has exactly the four platform roles (no professional status)'
);
-- Functions
select has_function('private', 'log_event', 'private.log_event exists');
select has_function('private', 'is_platform_owner', array['uuid'], 'private.is_platform_owner exists');
select has_function('private', 'is_platform_admin', array['uuid'], 'private.is_platform_admin exists');
select has_function('private', 'is_platform_moderator', array['uuid'], 'private.is_platform_moderator exists');
select has_function('private', 'is_support', array['uuid'], 'private.is_support exists');
select has_function('public', 'grant_platform_role', array['uuid', 'platform_role'], 'public.grant_platform_role exists');
select has_function('public', 'revoke_platform_role', array['uuid', 'platform_role', 'text'], 'public.revoke_platform_role exists');
-- Privileges: anon has nothing, authenticated has only what RLS then filters
select ok(not has_table_privilege('anon', 'public.profiles', 'select'), 'anon cannot select profiles');
select ok(not has_table_privilege('anon', 'public.profile_roles', 'select'), 'anon cannot select profile_roles');
select ok(not has_table_privilege('authenticated', 'public.profiles', 'insert'), 'authenticated cannot insert profiles');
select ok(not has_table_privilege('authenticated', 'public.profiles', 'delete'), 'authenticated cannot delete profiles');
select ok(not has_table_privilege('authenticated', 'public.profile_roles', 'insert'), 'authenticated cannot insert profile_roles');
select ok(not has_column_privilege('authenticated', 'public.profiles', 'active', 'update'), 'authenticated cannot update profiles.active');
select ok(not has_schema_privilege('anon', 'private', 'usage'), 'anon has no usage on schema private');
select ok(not has_function_privilege('anon', 'public.grant_platform_role(uuid, public.platform_role)', 'execute'), 'anon cannot execute grant_platform_role');
-- Private helpers: EXECUTE revoked from everyone except the predicates the policies evaluate.
-- (has_function_privilege is true for any role while proacl is null, i.e. the built-in PUBLIC default.)
select is(
(select count(*) from pg_proc p join pg_namespace n on n.oid = p.pronamespace
where n.nspname in ('private', 'audit') and has_function_privilege('anon', p.oid, 'execute')),
0::bigint, 'anon can execute nothing in private/audit'
);
select is(
(select string_agg(p.proname::text, ',' order by p.proname::text collate "C")
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
where n.nspname in ('private', 'audit') and has_function_privilege('authenticated', p.oid, 'execute')),
'can_edit_sheet,can_read_avatar_object,can_read_sheet_asset_object,can_upload_avatar_object,can_use_sheet,has_platform_role,is_platform_admin,is_platform_moderator,is_platform_owner,is_support',
'authenticated executes only the policy predicates in private'
);
select * from finish();
rollback;