@pgpm/supabase 0.5.1 → 1.0.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 (69) hide show
  1. package/LICENSE +2 -1
  2. package/Makefile +2 -3
  3. package/README.md +45 -59
  4. package/deploy/roles/anon.sql +15 -0
  5. package/deploy/schemas/supabase_integration/procedures/pre_request.sql +26 -0
  6. package/deploy/schemas/supabase_integration/schema.sql +9 -0
  7. package/package.json +27 -12
  8. package/pgpm-supabase.control +7 -0
  9. package/pgpm.plan +6 -3
  10. package/revert/roles/anon.sql +7 -0
  11. package/revert/schemas/supabase_integration/procedures/pre_request.sql +7 -0
  12. package/revert/schemas/supabase_integration/schema.sql +7 -0
  13. package/sql/pgpm-supabase--1.0.0.bundle.tar.gz +0 -0
  14. package/sql/pgpm-supabase--1.0.0.sql +32 -0
  15. package/verify/roles/anon.sql +12 -0
  16. package/verify/schemas/supabase_integration/procedures/pre_request.sql +19 -0
  17. package/verify/schemas/supabase_integration/schema.sql +18 -0
  18. package/__tests__/_realtime/_realtime-extensions.test.ts +0 -129
  19. package/__tests__/_realtime/_realtime-schema-migrations.test.ts +0 -160
  20. package/__tests__/_realtime/_realtime-tenants.test.ts +0 -163
  21. package/__tests__/auth/auth.audit-log-entries.test.ts +0 -148
  22. package/__tests__/auth/auth.flow-state.test.ts +0 -114
  23. package/__tests__/auth/auth.identities.test.ts +0 -149
  24. package/__tests__/auth/auth.instances.test.ts +0 -134
  25. package/__tests__/auth/auth.mfa-amr-claims.test.ts +0 -142
  26. package/__tests__/auth/auth.mfa-challenges.test.ts +0 -117
  27. package/__tests__/auth/auth.mfa-factors.test.ts +0 -118
  28. package/__tests__/auth/auth.oauth-authorizations.test.ts +0 -123
  29. package/__tests__/auth/auth.oauth-clients.test.ts +0 -120
  30. package/__tests__/auth/auth.oauth-consents.test.ts +0 -122
  31. package/__tests__/auth/auth.one-time-tokens.test.ts +0 -194
  32. package/__tests__/auth/auth.refresh-tokens.test.ts +0 -134
  33. package/__tests__/auth/auth.saml-providers.test.ts +0 -154
  34. package/__tests__/auth/auth.saml-relay-states.test.ts +0 -145
  35. package/__tests__/auth/auth.schema-migrations.test.ts +0 -131
  36. package/__tests__/auth/auth.sessions.test.ts +0 -140
  37. package/__tests__/auth/auth.sso-domains.test.ts +0 -137
  38. package/__tests__/auth/auth.sso-providers.test.ts +0 -136
  39. package/__tests__/auth/auth.users.test.ts +0 -314
  40. package/__tests__/conditional.test.ts +0 -229
  41. package/__tests__/functions.test.ts +0 -252
  42. package/__tests__/jsonb.test.ts +0 -237
  43. package/__tests__/net/net.http-request-queue.test.ts +0 -159
  44. package/__tests__/net/net.http-response.test.ts +0 -163
  45. package/__tests__/realtime/realtime.messages-2025-10-31.test.ts +0 -168
  46. package/__tests__/realtime/realtime.messages.test.ts +0 -162
  47. package/__tests__/realtime/realtime.schema-migrations.test.ts +0 -139
  48. package/__tests__/realtime/realtime.subscription.test.ts +0 -159
  49. package/__tests__/storage/storage.buckets-analytics.test.ts +0 -134
  50. package/__tests__/storage/storage.buckets.test.ts +0 -146
  51. package/__tests__/storage/storage.iceberg-namespaces.test.ts +0 -132
  52. package/__tests__/storage/storage.iceberg-tables.test.ts +0 -142
  53. package/__tests__/storage/storage.migrations.test.ts +0 -133
  54. package/__tests__/storage/storage.objects.test.ts +0 -160
  55. package/__tests__/storage/storage.prefixes.test.ts +0 -138
  56. package/__tests__/storage/storage.s3-multipart-uploads-parts.test.ts +0 -133
  57. package/__tests__/storage/storage.s3-multipart-uploads.test.ts +0 -128
  58. package/__tests__/supabase-functions/supabase.functions-hooks.test.ts +0 -125
  59. package/__tests__/supabase-functions/supabase.functions-migrations.test.ts +0 -213
  60. package/__tests__/temporal.test.ts +0 -225
  61. package/__tests__/vault-secrets.test.ts +0 -145
  62. package/deploy/LICENSE +0 -9
  63. package/deploy/supabase.sql +0 -2271
  64. package/jest.config.js +0 -15
  65. package/revert/supabase.sql +0 -3
  66. package/sql/supabase--0.0.1.sql +0 -1368
  67. package/src/index.ts +0 -3
  68. package/supabase.control +0 -7
  69. package/verify/supabase.sql +0 -3
@@ -1,252 +0,0 @@
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 auth schema exists
11
- const authSchemaExists = await pg.any(
12
- `SELECT EXISTS (
13
- SELECT FROM information_schema.schemata
14
- WHERE schema_name = 'auth'
15
- ) as exists`
16
- );
17
- expect(authSchemaExists[0].exists).toBe(true);
18
-
19
- // grant access to auth and storage schemas for testing
20
- // grant INSERT on auth.users to service_role so we can create test users
21
- await pg.any(
22
- `GRANT USAGE ON SCHEMA auth TO public;
23
- GRANT USAGE ON SCHEMA storage TO public;
24
- GRANT INSERT ON TABLE auth.users TO service_role;
25
- GRANT EXECUTE ON FUNCTION auth.uid() TO public;
26
- GRANT EXECUTE ON FUNCTION auth.role() TO public;
27
- GRANT EXECUTE ON FUNCTION auth.email() TO public;`,
28
- []
29
- );
30
- });
31
-
32
- afterAll(async () => {
33
- await teardown();
34
- });
35
-
36
- beforeEach(async () => {
37
- await db.beforeEach();
38
- });
39
-
40
- afterEach(async () => {
41
- await db.afterEach();
42
- });
43
-
44
- describe('tutorial: rls with supabase functions', () => {
45
- it('should verify auth.uid() function exists and works', async () => {
46
- // verify function exists first
47
- db.setContext({ role: 'service_role' });
48
- const funcExists = await db.any(
49
- `SELECT EXISTS (
50
- SELECT FROM pg_proc p
51
- JOIN pg_namespace n ON n.oid = p.pronamespace
52
- WHERE n.nspname = 'auth' AND p.proname = 'uid'
53
- ) as exists`
54
- );
55
- expect(funcExists[0].exists).toBe(true);
56
-
57
- // using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
58
- // set service_role context to insert into auth.users (requires INSERT permission)
59
- const user = await pg.one(
60
- `INSERT INTO auth.users (id, email)
61
- VALUES (gen_random_uuid(), $1)
62
- RETURNING id`,
63
- ['func1@example.com']
64
- );
65
-
66
- db.setContext({
67
- role: 'authenticated',
68
- 'request.jwt.claim.sub': user.id
69
- });
70
-
71
- // verify auth.uid() returns correct user id
72
- const uidResult = await db.one(`SELECT auth.uid() as uid`);
73
- expect(uidResult.uid).toBe(user.id);
74
- });
75
-
76
- it('should verify auth.role() function exists and works', async () => {
77
- // verify function exists first
78
- db.setContext({ role: 'service_role' });
79
- const funcExists = await db.any(
80
- `SELECT EXISTS (
81
- SELECT FROM pg_proc p
82
- JOIN pg_namespace n ON n.oid = p.pronamespace
83
- WHERE n.nspname = 'auth' AND p.proname = 'role'
84
- ) as exists`
85
- );
86
- expect(funcExists[0].exists).toBe(true);
87
-
88
- db.setContext({
89
- role: 'authenticated',
90
- 'request.jwt.claim.role': 'authenticated'
91
- });
92
-
93
- const roleResult = await db.one(`SELECT auth.role() as role`);
94
- expect(roleResult.role).toBe('authenticated');
95
- });
96
-
97
- it('should verify auth.email() function exists and works', async () => {
98
- // verify function exists first
99
- db.setContext({ role: 'service_role' });
100
- const funcExists = await db.any(
101
- `SELECT EXISTS (
102
- SELECT FROM pg_proc p
103
- JOIN pg_namespace n ON n.oid = p.pronamespace
104
- WHERE n.nspname = 'auth' AND p.proname = 'email'
105
- ) as exists`
106
- );
107
- expect(funcExists[0].exists).toBe(true);
108
-
109
- // using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
110
- // use pg for auth.users insert since it requires superuser privileges
111
- const user = await pg.one(
112
- `INSERT INTO auth.users (id, email)
113
- VALUES (gen_random_uuid(), $1)
114
- RETURNING id, email`,
115
- ['emailfunc@example.com']
116
- );
117
-
118
-
119
-
120
- db.setContext({
121
- role: 'authenticated',
122
- 'request.jwt.claim.sub': user.id,
123
- 'request.jwt.claim.email': user.email
124
- });
125
-
126
- const emailResult = await db.one(`SELECT auth.email() as email`);
127
- expect(emailResult.email).toBe(user.email);
128
- });
129
-
130
- it('should verify functions work in rls policy context', async () => {
131
- // verify function exists first
132
- db.setContext({ role: 'service_role' });
133
- const funcExists = await db.any(
134
- `SELECT EXISTS (
135
- SELECT FROM pg_proc p
136
- JOIN pg_namespace n ON n.oid = p.pronamespace
137
- WHERE n.nspname = 'auth' AND p.proname = 'uid'
138
- ) as exists`
139
- );
140
- expect(funcExists[0].exists).toBe(true);
141
-
142
- // using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
143
- // use pg for auth.users insert since it requires superuser privileges
144
- const user = await pg.one(
145
- `INSERT INTO auth.users (id, email)
146
- VALUES (gen_random_uuid(), $1)
147
- RETURNING id`,
148
- ['policyfunc@example.com']
149
- );
150
-
151
-
152
- });
153
-
154
- it('should verify functions return null for anon users', async () => {
155
- // verify function exists first
156
- db.setContext({ role: 'service_role' });
157
- const funcExists = await db.any(
158
- `SELECT EXISTS (
159
- SELECT FROM pg_proc p
160
- JOIN pg_namespace n ON n.oid = p.pronamespace
161
- WHERE n.nspname = 'auth' AND p.proname = 'uid'
162
- ) as exists`
163
- );
164
- expect(funcExists[0].exists).toBe(true);
165
-
166
- db.clearContext();
167
-
168
- const uidResult = await db.any(`SELECT auth.uid() as uid`);
169
- expect(uidResult.length).toBe(1);
170
- // auth.uid() should return null for anon
171
- expect(uidResult[0].uid).toBeNull();
172
- });
173
-
174
- it('should verify storage functions exist and are callable', async () => {
175
- db.setContext({ role: 'service_role' });
176
-
177
- // check if storage.search function exists
178
- const funcExists = await db.any(
179
- `SELECT EXISTS (
180
- SELECT FROM pg_proc p
181
- JOIN pg_namespace n ON n.oid = p.pronamespace
182
- WHERE n.nspname = 'storage' AND p.proname = 'search'
183
- ) as exists`
184
- );
185
-
186
- expect(Array.isArray(funcExists)).toBe(true);
187
- expect(funcExists.length).toBe(1);
188
-
189
- // if function exists, verify we can check its signature
190
- if (funcExists[0].exists) {
191
- const funcInfo = await db.any(
192
- `SELECT p.proname, n.nspname
193
- FROM pg_proc p
194
- JOIN pg_namespace n ON n.oid = p.pronamespace
195
- WHERE n.nspname = 'storage' AND p.proname = 'search'
196
- LIMIT 1`
197
- );
198
- expect(funcInfo.length).toBe(1);
199
- expect(funcInfo[0].proname).toBe('search');
200
- expect(funcInfo[0].nspname).toBe('storage');
201
- }
202
- });
203
-
204
- it('should verify functions can be used in where clauses with rls', async () => {
205
- // verify function exists first
206
- db.setContext({ role: 'service_role' });
207
- const funcExists = await db.any(
208
- `SELECT EXISTS (
209
- SELECT FROM pg_proc p
210
- JOIN pg_namespace n ON n.oid = p.pronamespace
211
- WHERE n.nspname = 'auth' AND p.proname = 'uid'
212
- ) as exists`
213
- );
214
- expect(funcExists[0].exists).toBe(true);
215
-
216
- // using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
217
- // use pg for auth.users insert since it requires superuser privileges
218
- const user = await pg.one(
219
- `INSERT INTO auth.users (id, email)
220
- VALUES (gen_random_uuid(), $1)
221
- RETURNING id`,
222
- ['wherefunc@example.com']
223
- );
224
-
225
-
226
- });
227
-
228
- it('should verify functions work in subqueries with rls', async () => {
229
- // verify function exists first
230
- db.setContext({ role: 'service_role' });
231
- const funcExists = await db.any(
232
- `SELECT EXISTS (
233
- SELECT FROM pg_proc p
234
- JOIN pg_namespace n ON n.oid = p.pronamespace
235
- WHERE n.nspname = 'auth' AND p.proname = 'uid'
236
- ) as exists`
237
- );
238
- expect(funcExists[0].exists).toBe(true);
239
-
240
- // using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
241
- // use pg for auth.users insert since it requires superuser privileges
242
- const user = await pg.one(
243
- `INSERT INTO auth.users (id, email)
244
- VALUES (gen_random_uuid(), $1)
245
- RETURNING id`,
246
- ['subfunc@example.com']
247
- );
248
-
249
-
250
- });
251
- });
252
-
@@ -1,237 +0,0 @@
1
- import { getConnections, PgTestClient } from 'supabase-test';
2
-
3
- let pg: PgTestClient;
4
- let db: PgTestClient;
5
- let teardown: () => Promise<void>;
6
-
7
- let testBucketId: string;
8
-
9
- beforeAll(async () => {
10
-
11
-
12
- ({ pg, db, teardown } = await getConnections());
13
-
14
- // verify storage schema exists
15
- const storageSchemaExists = await pg.any(
16
- `SELECT EXISTS (
17
- SELECT FROM information_schema.schemata
18
- WHERE schema_name = 'storage'
19
- ) as exists`
20
- );
21
- expect(storageSchemaExists[0].exists).toBe(true);
22
-
23
- // grant access to storage schema for testing
24
- await pg.any(
25
- `GRANT USAGE ON SCHEMA storage TO public;
26
- GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA storage TO service_role;
27
- GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA storage TO authenticated;
28
- GRANT SELECT ON ALL TABLES IN SCHEMA storage TO anon;
29
- ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT, INSERT, UPDATE ON TABLES TO service_role;
30
- ALTER DEFAULT PRIVILEGES IN SCHEMA storage GRANT SELECT ON TABLES TO anon;`,
31
- []
32
- );
33
-
34
- // grant insert on buckets table for bucket creation (using pg for setup)
35
- await pg.any(
36
- `GRANT INSERT ON TABLE storage.buckets TO service_role;`,
37
- []
38
- );
39
-
40
- // verify storage.objects table exists (required for jsonb tests)
41
- const objectsExists = await pg.any(
42
- `SELECT EXISTS (
43
- SELECT FROM information_schema.tables
44
- WHERE table_schema = 'storage' AND table_name = 'objects'
45
- ) as exists`
46
- );
47
- expect(objectsExists[0].exists).toBe(true);
48
-
49
- // verify buckets table exists (required for storage.objects)
50
- const bucketsExists = await pg.any(
51
- `SELECT EXISTS (
52
- SELECT FROM information_schema.tables
53
- WHERE table_schema = 'storage' AND table_name = 'buckets'
54
- ) as exists`
55
- );
56
- expect(bucketsExists[0].exists).toBe(true);
57
-
58
- // ensure we have a bucket for testing (create if none exists)
59
- // storage.objects requires a valid bucket_id, so we need a bucket
60
- const buckets = await pg.any(
61
- `SELECT id FROM storage.buckets LIMIT 1`
62
- );
63
-
64
- if (buckets.length > 0) {
65
- testBucketId = buckets[0].id;
66
- } else {
67
- // create a test bucket if none exists (using pg for setup in beforeAll)
68
- const newBucket = await pg.any(
69
- `INSERT INTO storage.buckets (id, name)
70
- VALUES ($1, $2)
71
- RETURNING id`,
72
- ['jsonb-test-bucket', 'jsonb-test-bucket']
73
- );
74
- expect(newBucket.length).toBeGreaterThan(0);
75
- testBucketId = newBucket[0].id;
76
- }
77
-
78
- // verify we have a bucket id (required for testing)
79
- expect(testBucketId).toBeDefined();
80
- });
81
-
82
- afterAll(async () => {
83
- await teardown();
84
- });
85
-
86
- beforeEach(async () => {
87
- await db.beforeEach();
88
- });
89
-
90
- afterEach(async () => {
91
- await db.afterEach();
92
- });
93
-
94
- describe('tutorial: rls with jsonb columns on supabase tables', () => {
95
-
96
- it('should verify storage.objects has metadata jsonb column', async () => {
97
- db.setContext({ role: 'service_role' });
98
-
99
- const columns = await db.any(
100
- `SELECT column_name, data_type
101
- FROM information_schema.columns
102
- WHERE table_schema = 'storage'
103
- AND table_name = 'objects'
104
- AND column_name = 'metadata'`
105
- );
106
-
107
- expect(columns.length).toBe(1);
108
- expect(columns[0].data_type).toBe('jsonb');
109
- });
110
-
111
- it('should verify service_role can query jsonb metadata fields', async () => {
112
- db.setContext({ role: 'service_role' });
113
-
114
- // query metadata jsonb field
115
- const objects = await db.any(
116
- `SELECT id, name, metadata
117
- FROM storage.objects
118
- WHERE metadata IS NOT NULL
119
- LIMIT 10`
120
- );
121
-
122
- expect(Array.isArray(objects)).toBe(true);
123
-
124
- // if objects exist with metadata, verify jsonb structure
125
- if (objects.length > 0 && objects[0].metadata) {
126
- expect(typeof objects[0].metadata).toBe('object');
127
- }
128
- });
129
-
130
- it('should verify jsonb path queries work with rls', async () => {
131
- db.setContext({ role: 'service_role' });
132
-
133
- // test jsonb path queries (e.g., metadata->>'key')
134
- const objects = await db.any(
135
- `SELECT id, name,
136
- metadata->>'contentType' as content_type,
137
- metadata->>'size' as size
138
- FROM storage.objects
139
- WHERE metadata IS NOT NULL
140
- LIMIT 10`
141
- );
142
-
143
- expect(Array.isArray(objects)).toBe(true);
144
- });
145
-
146
- it('should verify jsonb filtering works with rls', async () => {
147
- db.setContext({ role: 'service_role' });
148
-
149
- // test filtering by jsonb field values
150
- const objects = await db.any(
151
- `SELECT id, name, metadata
152
- FROM storage.objects
153
- WHERE metadata->>'contentType' = 'image/jpeg'
154
- OR metadata ? 'customField'
155
- LIMIT 10`
156
- );
157
-
158
- expect(Array.isArray(objects)).toBe(true);
159
- });
160
-
161
- it('should verify jsonb array operations with rls', async () => {
162
- db.setContext({ role: 'service_role' });
163
-
164
- // test jsonb array operations (if metadata has arrays)
165
- const objects = await db.any(
166
- `SELECT id, name,
167
- jsonb_array_length(metadata->'tags') as tag_count
168
- FROM storage.objects
169
- WHERE metadata ? 'tags'
170
- AND jsonb_typeof(metadata->'tags') = 'array'
171
- LIMIT 10`
172
- );
173
-
174
- expect(Array.isArray(objects)).toBe(true);
175
- });
176
-
177
- it('should verify anon cannot access jsonb metadata', async () => {
178
- db.setContext({ role: 'anon' });
179
-
180
- const result = await db.any(
181
- `SELECT id, metadata
182
- FROM storage.objects
183
- WHERE metadata IS NOT NULL
184
- LIMIT 1`
185
- );
186
-
187
- // rls should block access, result should be empty
188
- expect(result.length).toBe(0);
189
- });
190
-
191
- it('should verify jsonb updates respect rls', async () => {
192
- db.setContext({ role: 'service_role' });
193
-
194
- // insert an object with jsonb metadata
195
- const testObject = await db.any(
196
- `INSERT INTO storage.objects (bucket_id, name, metadata)
197
- VALUES ($1, $2, $3::jsonb)
198
- RETURNING id, name, metadata`,
199
- [
200
- testBucketId,
201
- 'test-jsonb-object.json',
202
- JSON.stringify({ contentType: 'application/json', size: 100, customField: 'test' })
203
- ]
204
- );
205
-
206
- expect(testObject.length).toBeGreaterThan(0);
207
- expect(testObject[0].metadata).toBeDefined();
208
- expect(typeof testObject[0].metadata).toBe('object');
209
-
210
- // verify jsonb path query works on inserted data
211
- const retrieved = await db.any(
212
- `SELECT id, metadata->>'contentType' as content_type
213
- FROM storage.objects
214
- WHERE id = $1`,
215
- [testObject[0].id]
216
- );
217
-
218
- expect(retrieved.length).toBeGreaterThan(0);
219
- expect(retrieved[0].content_type).toBe('application/json');
220
- });
221
-
222
- it('should verify jsonb nested field queries with rls', async () => {
223
- db.setContext({ role: 'service_role' });
224
-
225
- // test nested jsonb field queries (e.g., metadata->'nested'->>'field')
226
- const objects = await db.any(
227
- `SELECT id, name,
228
- metadata->'nested'->>'field' as nested_field
229
- FROM storage.objects
230
- WHERE metadata->'nested' IS NOT NULL
231
- LIMIT 10`
232
- );
233
-
234
- expect(Array.isArray(objects)).toBe(true);
235
- });
236
- });
237
-
@@ -1,159 +0,0 @@
1
- import { getConnections, PgTestClient } from 'supabase-test';
2
-
3
- let pg: PgTestClient;
4
- let db: PgTestClient;
5
- let teardown: () => Promise<void>;
6
-
7
- let tableExists = false;
8
-
9
- beforeAll(async () => {
10
- ({ pg, db, teardown } = await getConnections());
11
-
12
- // verify net schema exists (optional schema)
13
- const netSchemaExists = await pg.any(
14
- `SELECT EXISTS (
15
- SELECT FROM information_schema.schemata
16
- WHERE schema_name = 'net'
17
- ) as exists`
18
- );
19
-
20
- if (netSchemaExists[0]?.exists === true) {
21
- // grant access to net schema for testing
22
- await pg.any(
23
- `GRANT USAGE ON SCHEMA net TO public;
24
- GRANT SELECT ON ALL TABLES IN SCHEMA net TO service_role;
25
- GRANT SELECT ON ALL TABLES IN SCHEMA net TO authenticated;
26
- GRANT SELECT ON ALL TABLES IN SCHEMA net TO anon;
27
- ALTER DEFAULT PRIVILEGES IN SCHEMA net GRANT SELECT ON TABLES TO service_role;
28
- ALTER DEFAULT PRIVILEGES IN SCHEMA net GRANT SELECT ON TABLES TO authenticated;
29
- ALTER DEFAULT PRIVILEGES IN SCHEMA net GRANT SELECT ON TABLES TO anon;`,
30
- []
31
- );
32
-
33
- // check if net.http_request_queue table exists (using pg in beforeAll only)
34
- const exists = await pg.any(
35
- `SELECT EXISTS (
36
- SELECT FROM information_schema.tables
37
- WHERE table_schema = 'net' AND table_name = 'http_request_queue'
38
- ) as exists`
39
- );
40
- tableExists = exists[0]?.exists === true;
41
- }
42
- });
43
-
44
- afterAll(async () => {
45
- await teardown();
46
- });
47
-
48
- beforeEach(async () => {
49
- await db.beforeEach();
50
- });
51
-
52
- afterEach(async () => {
53
- await db.afterEach();
54
- });
55
-
56
- describe('tutorial: net http_request_queue table access', () => {
57
-
58
- it('should verify http_request_queue table exists', async () => {
59
- db.setContext({ role: 'service_role' });
60
-
61
- // verify table exists in information schema
62
- const exists = await db.any(
63
- `SELECT EXISTS (
64
- SELECT FROM information_schema.tables
65
- WHERE table_schema = 'net' AND table_name = 'http_request_queue'
66
- ) as exists`
67
- );
68
-
69
- expect(Array.isArray(exists)).toBe(true);
70
- if (exists[0]?.exists === false) {
71
- expect(exists[0].exists).toBe(false);
72
- return;
73
- }
74
- expect(exists[0].exists).toBe(true);
75
- });
76
-
77
- it('should verify service_role can query http_request_queue', async () => {
78
- if (!tableExists) {
79
- return;
80
- }
81
-
82
- db.setContext({ role: 'service_role' });
83
-
84
- // service_role should be able to query http_request_queue
85
- const queue = await db.any(
86
- `SELECT id, url, method, status
87
- FROM net.http_request_queue
88
- LIMIT 10`
89
- );
90
-
91
- expect(Array.isArray(queue)).toBe(true);
92
- });
93
-
94
- it('should verify table has proper structure', async () => {
95
- if (!tableExists) {
96
- return;
97
- }
98
-
99
- db.setContext({ role: 'service_role' });
100
-
101
- // query table column structure
102
- const columns = await db.any(
103
- `SELECT column_name
104
- FROM information_schema.columns
105
- WHERE table_schema = 'net' AND table_name = 'http_request_queue'
106
- ORDER BY ordinal_position`
107
- );
108
-
109
- expect(Array.isArray(columns)).toBe(true);
110
- });
111
-
112
- it('should prevent authenticated users from accessing http_request_queue without proper permissions', async () => {
113
- if (!tableExists) {
114
- return;
115
- }
116
-
117
- // create a test user as admin using db with service_role context
118
- // using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
119
- db.setContext({ role: 'service_role' });
120
- const user = await db.one(
121
- `INSERT INTO auth.users (id, email)
122
- VALUES (gen_random_uuid(), $1)
123
- RETURNING id`,
124
- ['net-request-queue-test@example.com']
125
- );
126
-
127
- // set context to simulate authenticated user
128
- db.setContext({
129
- role: 'authenticated',
130
- 'request.jwt.claim.sub': user.id
131
- });
132
-
133
- // authenticated users should not be able to access http_request_queue (rls blocks)
134
- const result = await db.any(
135
- `SELECT * FROM net.http_request_queue LIMIT 1`
136
- );
137
-
138
- // rls should block access, result should be empty
139
- expect(result.length).toBe(0);
140
- });
141
-
142
- it('should prevent anon from accessing http_request_queue', async () => {
143
- if (!tableExists) {
144
- return;
145
- }
146
-
147
- // clear context to anon role
148
- db.clearContext();
149
-
150
- // anon should not be able to access http_request_queue (rls blocks)
151
- const result = await db.any(
152
- `SELECT * FROM net.http_request_queue LIMIT 1`
153
- );
154
-
155
- // rls should block access, result should be empty
156
- expect(result.length).toBe(0);
157
- });
158
- });
159
-