@pgpm/supabase 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/LICENSE +21 -0
  2. package/Makefile +7 -0
  3. package/README.md +37 -0
  4. package/__tests__/_realtime/_realtime-extensions.test.ts +129 -0
  5. package/__tests__/_realtime/_realtime-schema-migrations.test.ts +160 -0
  6. package/__tests__/_realtime/_realtime-tenants.test.ts +163 -0
  7. package/__tests__/auth/auth.audit-log-entries.test.ts +148 -0
  8. package/__tests__/auth/auth.flow-state.test.ts +114 -0
  9. package/__tests__/auth/auth.identities.test.ts +149 -0
  10. package/__tests__/auth/auth.instances.test.ts +134 -0
  11. package/__tests__/auth/auth.mfa-amr-claims.test.ts +142 -0
  12. package/__tests__/auth/auth.mfa-challenges.test.ts +117 -0
  13. package/__tests__/auth/auth.mfa-factors.test.ts +118 -0
  14. package/__tests__/auth/auth.oauth-authorizations.test.ts +123 -0
  15. package/__tests__/auth/auth.oauth-clients.test.ts +120 -0
  16. package/__tests__/auth/auth.oauth-consents.test.ts +122 -0
  17. package/__tests__/auth/auth.one-time-tokens.test.ts +194 -0
  18. package/__tests__/auth/auth.refresh-tokens.test.ts +134 -0
  19. package/__tests__/auth/auth.saml-providers.test.ts +154 -0
  20. package/__tests__/auth/auth.saml-relay-states.test.ts +145 -0
  21. package/__tests__/auth/auth.schema-migrations.test.ts +131 -0
  22. package/__tests__/auth/auth.sessions.test.ts +140 -0
  23. package/__tests__/auth/auth.sso-domains.test.ts +137 -0
  24. package/__tests__/auth/auth.sso-providers.test.ts +136 -0
  25. package/__tests__/auth/auth.users.test.ts +314 -0
  26. package/__tests__/conditional.test.ts +229 -0
  27. package/__tests__/functions.test.ts +252 -0
  28. package/__tests__/jsonb.test.ts +237 -0
  29. package/__tests__/net/net.http-request-queue.test.ts +159 -0
  30. package/__tests__/net/net.http-response.test.ts +163 -0
  31. package/__tests__/realtime/realtime.messages-2025-10-31.test.ts +168 -0
  32. package/__tests__/realtime/realtime.messages.test.ts +162 -0
  33. package/__tests__/realtime/realtime.schema-migrations.test.ts +139 -0
  34. package/__tests__/realtime/realtime.subscription.test.ts +159 -0
  35. package/__tests__/storage/storage.buckets-analytics.test.ts +134 -0
  36. package/__tests__/storage/storage.buckets.test.ts +146 -0
  37. package/__tests__/storage/storage.iceberg-namespaces.test.ts +132 -0
  38. package/__tests__/storage/storage.iceberg-tables.test.ts +142 -0
  39. package/__tests__/storage/storage.migrations.test.ts +133 -0
  40. package/__tests__/storage/storage.objects.test.ts +160 -0
  41. package/__tests__/storage/storage.prefixes.test.ts +138 -0
  42. package/__tests__/storage/storage.s3-multipart-uploads-parts.test.ts +133 -0
  43. package/__tests__/storage/storage.s3-multipart-uploads.test.ts +128 -0
  44. package/__tests__/supabase-functions/supabase.functions-hooks.test.ts +125 -0
  45. package/__tests__/supabase-functions/supabase.functions-migrations.test.ts +213 -0
  46. package/__tests__/temporal.test.ts +225 -0
  47. package/__tests__/vault-secrets.test.ts +145 -0
  48. package/deploy/LICENSE +9 -0
  49. package/deploy/supabase.sql +2271 -0
  50. package/jest.config.js +15 -0
  51. package/launchql.plan +4 -0
  52. package/package.json +23 -0
  53. package/revert/supabase.sql +3 -0
  54. package/sql/supabase--0.0.1.sql +1368 -0
  55. package/src/index.ts +3 -0
  56. package/supabase.control +7 -0
  57. package/verify/supabase.sql +3 -0
@@ -0,0 +1,160 @@
1
+ import { getConnections, PgTestClient } from 'supabase-test';
2
+
3
+ let pg: PgTestClient;
4
+ let db: PgTestClient;
5
+ let teardown: () => Promise<void>;
6
+
7
+ beforeAll(async () => {
8
+ ({ pg, db, teardown } = await getConnections());
9
+
10
+ // verify storage schema exists
11
+ const storageSchemaExists = await pg.any(
12
+ `SELECT EXISTS (
13
+ SELECT FROM information_schema.schemata
14
+ WHERE schema_name = 'storage'
15
+ ) as exists`
16
+ );
17
+ expect(storageSchemaExists[0].exists).toBe(true);
18
+
19
+ // grant access to storage schema for testing
20
+ await pg.any(
21
+ `GRANT USAGE ON SCHEMA storage TO public;
22
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO service_role;
23
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO authenticated;
24
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO anon;
25
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO service_role;
26
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO anon;`,
27
+ []
28
+ );
29
+
30
+ // assert storage.objects exists
31
+ const exists = await pg.any(
32
+ `SELECT EXISTS (
33
+ SELECT FROM information_schema.tables
34
+ WHERE table_schema = 'storage' AND table_name = 'objects'
35
+ ) as exists`
36
+ );
37
+ expect(exists[0].exists).toBe(true);
38
+ });
39
+
40
+ afterAll(async () => {
41
+ await teardown();
42
+ });
43
+
44
+ beforeEach(async () => {
45
+ await db.beforeEach();
46
+ });
47
+
48
+ afterEach(async () => {
49
+ await db.afterEach();
50
+ });
51
+
52
+ describe('tutorial: storage objects table access with rls', () => {
53
+
54
+ it('should verify objects table exists', async () => {
55
+ db.setContext({ role: 'service_role' });
56
+ const exists = await db.any(
57
+ `SELECT EXISTS (
58
+ SELECT FROM information_schema.tables
59
+ WHERE table_schema = 'storage' AND table_name = 'objects'
60
+ ) as exists`
61
+ );
62
+ expect(Array.isArray(exists)).toBe(true);
63
+ expect(exists[0].exists).toBe(true);
64
+ });
65
+
66
+ it('should verify rls is enabled on objects table', async () => {
67
+ db.setContext({ role: 'service_role' });
68
+
69
+ const rlsStatus = await db.any(
70
+ `SELECT c.relrowsecurity
71
+ FROM pg_class c
72
+ JOIN pg_namespace n ON n.oid = c.relnamespace
73
+ WHERE n.nspname = 'storage' AND c.relname = 'objects'`
74
+ );
75
+
76
+ expect(Array.isArray(rlsStatus)).toBe(true);
77
+ if (rlsStatus.length > 0) {
78
+ expect(typeof rlsStatus[0].relrowsecurity).toBe('boolean');
79
+ }
80
+ });
81
+
82
+ it('should verify service_role can read objects', async () => {
83
+ db.setContext({ role: 'service_role' });
84
+
85
+ const objects = await db.any(
86
+ `SELECT id, bucket_id, name, owner, created_at
87
+ FROM storage.objects
88
+ LIMIT 10`
89
+ );
90
+
91
+ expect(Array.isArray(objects)).toBe(true);
92
+ });
93
+
94
+ it('should verify table has primary key on id', async () => {
95
+ db.setContext({ role: 'service_role' });
96
+
97
+ const pk = await db.any(
98
+ `SELECT constraint_name
99
+ FROM information_schema.table_constraints
100
+ WHERE table_schema = 'storage' AND table_name = 'objects'
101
+ AND constraint_type = 'PRIMARY KEY'`
102
+ );
103
+
104
+ expect(Array.isArray(pk)).toBe(true);
105
+ if (pk.length > 0) {
106
+ expect(pk[0].constraint_name).toBeDefined();
107
+ }
108
+ });
109
+
110
+ it('should verify table has unique index on bucket_id and name', async () => {
111
+ db.setContext({ role: 'service_role' });
112
+
113
+ const indexes = await db.any(
114
+ `SELECT indexname
115
+ FROM pg_indexes
116
+ WHERE schemaname = 'storage' AND tablename = 'objects'
117
+ AND indexname = 'bucketid_objname'`
118
+ );
119
+
120
+ expect(Array.isArray(indexes)).toBe(true);
121
+ });
122
+
123
+ it('should verify table has foreign key to buckets', async () => {
124
+ db.setContext({ role: 'service_role' });
125
+
126
+ const fks = await db.any(
127
+ `SELECT tc.constraint_name, ccu.table_name AS foreign_table_name
128
+ FROM information_schema.table_constraints AS tc
129
+ JOIN information_schema.constraint_column_usage AS ccu
130
+ ON ccu.constraint_name = tc.constraint_name
131
+ WHERE tc.constraint_type = 'FOREIGN KEY'
132
+ AND tc.table_schema = 'storage'
133
+ AND tc.table_name = 'objects'
134
+ AND ccu.table_name = 'buckets'`
135
+ );
136
+
137
+ expect(Array.isArray(fks)).toBe(true);
138
+ });
139
+
140
+ it('should verify anon access to objects based on rls', async () => {
141
+ // check rls status
142
+ db.setContext({ role: 'service_role' });
143
+ const rlsStatus = await db.any(
144
+ `SELECT c.relrowsecurity
145
+ FROM pg_class c
146
+ JOIN pg_namespace n ON n.oid = c.relnamespace
147
+ WHERE n.nspname = 'storage' AND c.relname = 'objects'`
148
+ );
149
+ expect(Array.isArray(rlsStatus)).toBe(true);
150
+ expect(rlsStatus.length).toBeGreaterThan(0);
151
+
152
+ db.clearContext();
153
+ const result = await db.any(`SELECT * FROM storage.objects LIMIT 1`);
154
+ expect(Array.isArray(result)).toBe(true);
155
+ if (rlsStatus[0].relrowsecurity === true) {
156
+ expect(result.length).toBe(0);
157
+ }
158
+ });
159
+ });
160
+
@@ -0,0 +1,138 @@
1
+ import { getConnections, PgTestClient } from 'supabase-test';
2
+
3
+ let pg: PgTestClient;
4
+ let db: PgTestClient;
5
+ let teardown: () => Promise<void>;
6
+ let testUserId: string | null = null;
7
+
8
+ beforeAll(async () => {
9
+ ({ pg, db, teardown } = await getConnections());
10
+
11
+ // verify storage schema exists
12
+ const storageSchemaExists = await pg.any(
13
+ `SELECT EXISTS (
14
+ SELECT FROM information_schema.schemata
15
+ WHERE schema_name = 'storage'
16
+ ) as exists`
17
+ );
18
+ expect(storageSchemaExists[0].exists).toBe(true);
19
+
20
+ // grant access to storage schema for testing
21
+ await pg.any(
22
+ `GRANT USAGE ON SCHEMA storage TO public;
23
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO service_role;
24
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO authenticated;
25
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO anon;
26
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO service_role;
27
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO authenticated;
28
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO anon;`,
29
+ []
30
+ );
31
+
32
+ // ensure test user exists for authenticated context
33
+ const u = await pg.one(
34
+ `INSERT INTO auth.users (id, email)
35
+ VALUES (gen_random_uuid(), $1)
36
+ RETURNING id`,
37
+ ['storage-prefix-test@example.com']
38
+ );
39
+ testUserId = u.id;
40
+ });
41
+
42
+ afterAll(async () => {
43
+ await teardown();
44
+ });
45
+
46
+ beforeEach(async () => {
47
+ await db.beforeEach();
48
+ });
49
+
50
+ afterEach(async () => {
51
+ await db.afterEach();
52
+ });
53
+
54
+ describe('tutorial: storage prefixes table access', () => {
55
+
56
+ it('should verify prefixes table exists', async () => {
57
+ db.setContext({ role: 'service_role' });
58
+
59
+ // verify table exists in information schema
60
+ const exists = await db.any(
61
+ `SELECT EXISTS (
62
+ SELECT FROM information_schema.tables
63
+ WHERE table_schema = 'storage' AND table_name = 'prefixes'
64
+ ) as exists`
65
+ );
66
+
67
+ expect(Array.isArray(exists)).toBe(true);
68
+ expect(exists[0].exists).toBe(true);
69
+ });
70
+
71
+ it('should verify service_role can query prefixes', async () => {
72
+ db.setContext({ role: 'service_role' });
73
+
74
+ // service_role should be able to query prefixes
75
+ const prefixes = await db.any(
76
+ `SELECT bucket_id, created_at
77
+ FROM storage.prefixes
78
+ LIMIT 10`
79
+ );
80
+
81
+ expect(Array.isArray(prefixes)).toBe(true);
82
+ });
83
+
84
+ it('should verify table has proper structure', async () => {
85
+ db.setContext({ role: 'service_role' });
86
+
87
+ // query table column structure
88
+ const columns = await db.any(
89
+ `SELECT column_name
90
+ FROM information_schema.columns
91
+ WHERE table_schema = 'storage' AND table_name = 'prefixes'
92
+ ORDER BY ordinal_position`
93
+ );
94
+
95
+ expect(Array.isArray(columns)).toBe(true);
96
+ });
97
+
98
+ it('should verify authenticated access to prefixes based on rls', async () => {
99
+ // rls status
100
+ db.setContext({ role: 'service_role' });
101
+ const rlsStatus = await db.any(
102
+ `SELECT c.relrowsecurity
103
+ FROM pg_class c
104
+ JOIN pg_namespace n ON n.oid = c.relnamespace
105
+ WHERE n.nspname = 'storage' AND c.relname = 'prefixes'`
106
+ );
107
+ expect(Array.isArray(rlsStatus)).toBe(true);
108
+
109
+ // set context to authenticated user
110
+ db.setContext({
111
+ role: 'authenticated',
112
+ 'request.jwt.claim.sub': testUserId
113
+ });
114
+ const result = await db.any(`SELECT * FROM storage.prefixes LIMIT 1`);
115
+ expect(Array.isArray(result)).toBe(true);
116
+ if (rlsStatus[0]?.relrowsecurity === true) {
117
+ expect(result.length).toBe(0);
118
+ }
119
+ });
120
+
121
+ it('should verify anon access to prefixes based on rls', async () => {
122
+ db.setContext({ role: 'service_role' });
123
+ const rlsStatus = await db.any(
124
+ `SELECT c.relrowsecurity
125
+ FROM pg_class c
126
+ JOIN pg_namespace n ON n.oid = c.relnamespace
127
+ WHERE n.nspname = 'storage' AND c.relname = 'prefixes'`
128
+ );
129
+
130
+ db.clearContext();
131
+ const result = await db.any(`SELECT * FROM storage.prefixes LIMIT 1`);
132
+ expect(Array.isArray(result)).toBe(true);
133
+ if (rlsStatus[0]?.relrowsecurity === true) {
134
+ expect(result.length).toBe(0);
135
+ }
136
+ });
137
+ });
138
+
@@ -0,0 +1,133 @@
1
+ import { getConnections, PgTestClient } from 'supabase-test';
2
+
3
+ let pg: PgTestClient;
4
+ let db: PgTestClient;
5
+ let teardown: () => Promise<void>;
6
+ let testUserId: string | null = null;
7
+
8
+ beforeAll(async () => {
9
+ ({ pg, db, teardown } = await getConnections());
10
+
11
+ // verify storage schema exists
12
+ const storageSchemaExists = await pg.any(
13
+ `SELECT EXISTS (
14
+ SELECT FROM information_schema.schemata
15
+ WHERE schema_name = 'storage'
16
+ ) as exists`
17
+ );
18
+ expect(storageSchemaExists[0].exists).toBe(true);
19
+
20
+ // grant access to storage schema for testing
21
+ await pg.any(
22
+ `GRANT USAGE ON SCHEMA storage TO public;
23
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO service_role;
24
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO authenticated;
25
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO anon;
26
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO service_role;
27
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO authenticated;
28
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO anon;`,
29
+ []
30
+ );
31
+
32
+ // create a test user for authenticated context
33
+ const u = await pg.one(
34
+ `INSERT INTO auth.users (id, email)
35
+ VALUES (gen_random_uuid(), $1)
36
+ RETURNING id`,
37
+ ['storage-parts-test@example.com']
38
+ );
39
+ testUserId = u.id;
40
+ });
41
+
42
+ afterAll(async () => {
43
+ await teardown();
44
+ });
45
+
46
+ beforeEach(async () => {
47
+ await db.beforeEach();
48
+ });
49
+
50
+ afterEach(async () => {
51
+ await db.afterEach();
52
+ });
53
+
54
+ describe('tutorial: storage prefixes table access (renamed scenario)', () => {
55
+
56
+ it('should verify prefixes table exists', async () => {
57
+ db.setContext({ role: 'service_role' });
58
+
59
+ // verify table exists in information schema
60
+ const exists = await db.any(
61
+ `SELECT EXISTS (
62
+ SELECT FROM information_schema.tables
63
+ WHERE table_schema = 'storage' AND table_name = 'prefixes'
64
+ ) as exists`
65
+ );
66
+
67
+ expect(Array.isArray(exists)).toBe(true);
68
+ expect(exists[0].exists).toBe(true);
69
+ });
70
+
71
+ it('should verify service_role can query prefixes', async () => {
72
+ db.setContext({ role: 'service_role' });
73
+ // service_role should be able to query prefixes
74
+ const rows = await db.any(
75
+ `SELECT bucket_id, name, level, created_at, updated_at
76
+ FROM storage.prefixes
77
+ LIMIT 10`
78
+ );
79
+ expect(Array.isArray(rows)).toBe(true);
80
+ });
81
+
82
+ it('should verify table has foreign key to buckets', async () => {
83
+ db.setContext({ role: 'service_role' });
84
+ // check for foreign key constraints to buckets
85
+ const fks = await db.any(
86
+ `SELECT tc.constraint_name, ccu.table_name AS foreign_table_name
87
+ FROM information_schema.table_constraints AS tc
88
+ JOIN information_schema.constraint_column_usage AS ccu
89
+ ON ccu.constraint_name = tc.constraint_name
90
+ WHERE tc.constraint_type = 'FOREIGN KEY'
91
+ AND tc.table_schema = 'storage'
92
+ AND tc.table_name = 'prefixes'
93
+ AND ccu.table_name = 'buckets'`
94
+ );
95
+ expect(Array.isArray(fks)).toBe(true);
96
+ });
97
+
98
+ it('should verify authenticated access to prefixes based on rls', async () => {
99
+ db.setContext({ role: 'service_role' });
100
+ const rlsStatus = await db.any(
101
+ `SELECT c.relrowsecurity
102
+ FROM pg_class c
103
+ JOIN pg_namespace n ON n.oid = c.relnamespace
104
+ WHERE n.nspname = 'storage' AND c.relname = 'prefixes'`
105
+ );
106
+ expect(Array.isArray(rlsStatus)).toBe(true);
107
+
108
+ db.setContext({ role: 'authenticated', 'request.jwt.claim.sub': testUserId });
109
+ const result = await db.any(`SELECT * FROM storage.prefixes LIMIT 1`);
110
+ expect(Array.isArray(result)).toBe(true);
111
+ if (rlsStatus[0]?.relrowsecurity === true) {
112
+ expect(result.length).toBe(0);
113
+ }
114
+ });
115
+
116
+ it('should verify anon access to prefixes based on rls', async () => {
117
+ db.setContext({ role: 'service_role' });
118
+ const rlsStatus = await db.any(
119
+ `SELECT c.relrowsecurity
120
+ FROM pg_class c
121
+ JOIN pg_namespace n ON n.oid = c.relnamespace
122
+ WHERE n.nspname = 'storage' AND c.relname = 'prefixes'`
123
+ );
124
+
125
+ db.clearContext();
126
+ const result = await db.any(`SELECT * FROM storage.prefixes LIMIT 1`);
127
+ expect(Array.isArray(result)).toBe(true);
128
+ if (rlsStatus[0]?.relrowsecurity === true) {
129
+ expect(result.length).toBe(0);
130
+ }
131
+ });
132
+ });
133
+
@@ -0,0 +1,128 @@
1
+ import { getConnections, PgTestClient } from 'supabase-test';
2
+
3
+ let pg: PgTestClient;
4
+ let db: PgTestClient;
5
+ let teardown: () => Promise<void>;
6
+ let testUserId: string | null = null;
7
+
8
+ beforeAll(async () => {
9
+ ({ pg, db, teardown } = await getConnections());
10
+
11
+ // verify storage schema exists
12
+ const storageSchemaExists = await pg.any(
13
+ `SELECT EXISTS (
14
+ SELECT FROM information_schema.schemata
15
+ WHERE schema_name = 'storage'
16
+ ) as exists`
17
+ );
18
+ expect(storageSchemaExists[0].exists).toBe(true);
19
+
20
+ // grant access to storage schema for testing
21
+ await pg.any(
22
+ `GRANT USAGE ON SCHEMA storage TO public;
23
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO service_role;
24
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO authenticated;
25
+ GRANT SELECT ON ALL TABLES IN SCHEMA storage TO anon;
26
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO service_role;
27
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO authenticated;
28
+ ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO anon;`,
29
+ []
30
+ );
31
+
32
+ // create a test user for authenticated context
33
+ const u = await pg.one(
34
+ `INSERT INTO auth.users (id, email)
35
+ VALUES (gen_random_uuid(), $1)
36
+ RETURNING id`,
37
+ ['storage-upload-test@example.com']
38
+ );
39
+ testUserId = u.id;
40
+ });
41
+
42
+ afterAll(async () => {
43
+ await teardown();
44
+ });
45
+
46
+ beforeEach(async () => {
47
+ await db.beforeEach();
48
+ });
49
+
50
+ afterEach(async () => {
51
+ await db.afterEach();
52
+ });
53
+
54
+ describe('tutorial: storage objects table access (renamed scenario)', () => {
55
+
56
+ it('should verify objects table exists', async () => {
57
+ db.setContext({ role: 'service_role' });
58
+
59
+ // verify table exists in information schema
60
+ const exists = await db.any(
61
+ `SELECT EXISTS (
62
+ SELECT FROM information_schema.tables
63
+ WHERE table_schema = 'storage' AND table_name = 'objects'
64
+ ) as exists`
65
+ );
66
+
67
+ expect(Array.isArray(exists)).toBe(true);
68
+ expect(exists[0].exists).toBe(true);
69
+ });
70
+
71
+ it('should verify service_role can query objects', async () => {
72
+ db.setContext({ role: 'service_role' });
73
+
74
+ // service_role should be able to query objects
75
+ const rows = await db.any(
76
+ `SELECT id, bucket_id, name, owner, created_at, updated_at, last_accessed_at, metadata, level
77
+ FROM storage.objects
78
+ LIMIT 10`
79
+ );
80
+ expect(Array.isArray(rows)).toBe(true);
81
+ });
82
+
83
+ it('should verify objects table structure via information_schema', async () => {
84
+ db.setContext({ role: 'service_role' });
85
+ // query table column structure
86
+ const columns = await db.any(
87
+ `SELECT column_name, data_type
88
+ FROM information_schema.columns
89
+ WHERE table_schema = 'storage' AND table_name = 'objects'
90
+ ORDER BY ordinal_position`
91
+ );
92
+ expect(Array.isArray(columns)).toBe(true);
93
+ });
94
+
95
+ it('should verify authenticated access to objects based on rls', async () => {
96
+ db.setContext({ role: 'service_role' });
97
+ const rlsStatus = await db.any(
98
+ `SELECT c.relrowsecurity
99
+ FROM pg_class c
100
+ JOIN pg_namespace n ON n.oid = c.relnamespace
101
+ WHERE n.nspname = 'storage' AND c.relname = 'objects'`
102
+ );
103
+ expect(Array.isArray(rlsStatus)).toBe(true);
104
+ db.setContext({ role: 'authenticated', 'request.jwt.claim.sub': testUserId });
105
+ const result = await db.any(`SELECT * FROM storage.objects LIMIT 1`);
106
+ expect(Array.isArray(result)).toBe(true);
107
+ if (rlsStatus[0]?.relrowsecurity === true) {
108
+ expect(result.length).toBe(0);
109
+ }
110
+ });
111
+
112
+ it('should verify anon access to objects based on rls', async () => {
113
+ db.setContext({ role: 'service_role' });
114
+ const rlsStatus = await db.any(
115
+ `SELECT c.relrowsecurity
116
+ FROM pg_class c
117
+ JOIN pg_namespace n ON n.oid = c.relnamespace
118
+ WHERE n.nspname = 'storage' AND c.relname = 'objects'`
119
+ );
120
+ db.clearContext();
121
+ const result = await db.any(`SELECT * FROM storage.objects LIMIT 1`);
122
+ expect(Array.isArray(result)).toBe(true);
123
+ if (rlsStatus[0]?.relrowsecurity === true) {
124
+ expect(result.length).toBe(0);
125
+ }
126
+ });
127
+ });
128
+
@@ -0,0 +1,125 @@
1
+ import { getConnections, PgTestClient } from 'supabase-test';
2
+
3
+ let pg: PgTestClient;
4
+ let db: PgTestClient;
5
+ let teardown: () => Promise<void>;
6
+
7
+ beforeAll(async () => {
8
+ ({ pg, db, teardown } = await getConnections());
9
+
10
+ // grant access to supabase_functions schema for testing
11
+ await pg.any(
12
+ `GRANT USAGE ON SCHEMA supabase_functions TO public;
13
+ GRANT SELECT ON ALL TABLES IN SCHEMA supabase_functions TO service_role;
14
+ GRANT SELECT ON ALL TABLES IN SCHEMA supabase_functions TO authenticated;
15
+ GRANT SELECT ON ALL TABLES IN SCHEMA supabase_functions TO anon;
16
+ ALTER DEFAULT PRIVILEGES IN SCHEMA supabase_functions GRANT SELECT ON TABLES TO service_role;
17
+ ALTER DEFAULT PRIVILEGES IN SCHEMA supabase_functions GRANT SELECT ON TABLES TO authenticated;
18
+ ALTER DEFAULT PRIVILEGES IN SCHEMA supabase_functions GRANT SELECT ON TABLES TO anon;`,
19
+ []
20
+ );
21
+
22
+ // grant access to auth schema for auth.users inserts
23
+ await pg.any(
24
+ `GRANT USAGE ON SCHEMA auth TO public;
25
+ GRANT INSERT ON TABLE auth.users TO service_role;`,
26
+ []
27
+ );
28
+ });
29
+
30
+ afterAll(async () => {
31
+ await teardown();
32
+ });
33
+
34
+ beforeEach(async () => {
35
+ await db.beforeEach();
36
+ });
37
+
38
+ afterEach(async () => {
39
+ await db.afterEach();
40
+ });
41
+
42
+ describe('tutorial: supabase_functions hooks table access', () => {
43
+
44
+ it('should verify hooks table exists', async () => {
45
+ db.setContext({ role: 'service_role' });
46
+
47
+ // verify table exists in information schema
48
+ const exists = await db.any(
49
+ `SELECT EXISTS (
50
+ SELECT FROM information_schema.tables
51
+ WHERE table_schema = 'supabase_functions' AND table_name = 'hooks'
52
+ ) as exists`
53
+ );
54
+
55
+ expect(Array.isArray(exists)).toBe(true);
56
+ expect(exists[0].exists).toBe(true);
57
+ });
58
+
59
+ it('should verify service_role can query hooks', async () => {
60
+ db.setContext({ role: 'service_role' });
61
+
62
+ // service_role should be able to query hooks
63
+ const hooks = await db.any(
64
+ `SELECT id, hook_table_id, hook_name, created_at
65
+ FROM supabase_functions.hooks
66
+ LIMIT 10`
67
+ );
68
+
69
+ expect(Array.isArray(hooks)).toBe(true);
70
+ });
71
+
72
+ it('should verify table structure via information_schema', async () => {
73
+ db.setContext({ role: 'service_role' });
74
+
75
+ // query table column structure
76
+ const columns = await db.any(
77
+ `SELECT column_name, data_type
78
+ FROM information_schema.columns
79
+ WHERE table_schema = 'supabase_functions' AND table_name = 'hooks'
80
+ ORDER BY ordinal_position`
81
+ );
82
+
83
+ expect(Array.isArray(columns)).toBe(true);
84
+ });
85
+
86
+ it('should prevent authenticated users from accessing hooks without proper permissions', async () => {
87
+ // create a test user as admin
88
+ // using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
89
+ // use pg for auth.users insert since it requires superuser privileges
90
+ const user = await pg.one(
91
+ `INSERT INTO auth.users (id, email)
92
+ VALUES (gen_random_uuid(), $1)
93
+ RETURNING id`,
94
+ ['functions-hooks-test@example.com']
95
+ );
96
+
97
+ // set context to simulate authenticated user
98
+ db.setContext({
99
+ role: 'authenticated',
100
+ 'request.jwt.claim.sub': user.id
101
+ });
102
+
103
+ // authenticated users should not be able to access hooks (rls blocks)
104
+ const result = await db.any(
105
+ `SELECT * FROM supabase_functions.hooks LIMIT 1`
106
+ );
107
+
108
+ // rls should block access, result should be empty
109
+ expect(result.length).toBe(0);
110
+ });
111
+
112
+ it('should prevent anon from accessing hooks', async () => {
113
+ // clear context to anon role
114
+ db.clearContext();
115
+
116
+ // anon should not be able to access hooks (rls blocks)
117
+ const result = await db.any(
118
+ `SELECT * FROM supabase_functions.hooks LIMIT 1`
119
+ );
120
+
121
+ // rls should block access, result should be empty
122
+ expect(result.length).toBe(0);
123
+ });
124
+ });
125
+