@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.
- package/LICENSE +21 -0
- package/Makefile +7 -0
- package/README.md +37 -0
- package/__tests__/_realtime/_realtime-extensions.test.ts +129 -0
- package/__tests__/_realtime/_realtime-schema-migrations.test.ts +160 -0
- package/__tests__/_realtime/_realtime-tenants.test.ts +163 -0
- package/__tests__/auth/auth.audit-log-entries.test.ts +148 -0
- package/__tests__/auth/auth.flow-state.test.ts +114 -0
- package/__tests__/auth/auth.identities.test.ts +149 -0
- package/__tests__/auth/auth.instances.test.ts +134 -0
- package/__tests__/auth/auth.mfa-amr-claims.test.ts +142 -0
- package/__tests__/auth/auth.mfa-challenges.test.ts +117 -0
- package/__tests__/auth/auth.mfa-factors.test.ts +118 -0
- package/__tests__/auth/auth.oauth-authorizations.test.ts +123 -0
- package/__tests__/auth/auth.oauth-clients.test.ts +120 -0
- package/__tests__/auth/auth.oauth-consents.test.ts +122 -0
- package/__tests__/auth/auth.one-time-tokens.test.ts +194 -0
- package/__tests__/auth/auth.refresh-tokens.test.ts +134 -0
- package/__tests__/auth/auth.saml-providers.test.ts +154 -0
- package/__tests__/auth/auth.saml-relay-states.test.ts +145 -0
- package/__tests__/auth/auth.schema-migrations.test.ts +131 -0
- package/__tests__/auth/auth.sessions.test.ts +140 -0
- package/__tests__/auth/auth.sso-domains.test.ts +137 -0
- package/__tests__/auth/auth.sso-providers.test.ts +136 -0
- package/__tests__/auth/auth.users.test.ts +314 -0
- package/__tests__/conditional.test.ts +229 -0
- package/__tests__/functions.test.ts +252 -0
- package/__tests__/jsonb.test.ts +237 -0
- package/__tests__/net/net.http-request-queue.test.ts +159 -0
- package/__tests__/net/net.http-response.test.ts +163 -0
- package/__tests__/realtime/realtime.messages-2025-10-31.test.ts +168 -0
- package/__tests__/realtime/realtime.messages.test.ts +162 -0
- package/__tests__/realtime/realtime.schema-migrations.test.ts +139 -0
- package/__tests__/realtime/realtime.subscription.test.ts +159 -0
- package/__tests__/storage/storage.buckets-analytics.test.ts +134 -0
- package/__tests__/storage/storage.buckets.test.ts +146 -0
- package/__tests__/storage/storage.iceberg-namespaces.test.ts +132 -0
- package/__tests__/storage/storage.iceberg-tables.test.ts +142 -0
- package/__tests__/storage/storage.migrations.test.ts +133 -0
- package/__tests__/storage/storage.objects.test.ts +160 -0
- package/__tests__/storage/storage.prefixes.test.ts +138 -0
- package/__tests__/storage/storage.s3-multipart-uploads-parts.test.ts +133 -0
- package/__tests__/storage/storage.s3-multipart-uploads.test.ts +128 -0
- package/__tests__/supabase-functions/supabase.functions-hooks.test.ts +125 -0
- package/__tests__/supabase-functions/supabase.functions-migrations.test.ts +213 -0
- package/__tests__/temporal.test.ts +225 -0
- package/__tests__/vault-secrets.test.ts +145 -0
- package/deploy/LICENSE +9 -0
- package/deploy/supabase.sql +2271 -0
- package/jest.config.js +15 -0
- package/launchql.plan +4 -0
- package/package.json +23 -0
- package/revert/supabase.sql +3 -0
- package/sql/supabase--0.0.1.sql +1368 -0
- package/src/index.ts +3 -0
- package/supabase.control +7 -0
- package/verify/supabase.sql +3 -0
|
@@ -0,0 +1,237 @@
|
|
|
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
|
+
|
|
@@ -0,0 +1,159 @@
|
|
|
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
|
+
|
|
@@ -0,0 +1,163 @@
|
|
|
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_response 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_response'
|
|
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_response table access', () => {
|
|
57
|
+
|
|
58
|
+
it('should verify _http_response 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_response'
|
|
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_response structure', async () => {
|
|
78
|
+
if (!tableExists) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
db.setContext({ role: 'service_role' });
|
|
83
|
+
|
|
84
|
+
// query table column structure
|
|
85
|
+
const columns = await db.any(
|
|
86
|
+
`SELECT column_name, data_type
|
|
87
|
+
FROM information_schema.columns
|
|
88
|
+
WHERE table_schema = 'net' AND table_name = '_http_response'
|
|
89
|
+
ORDER BY ordinal_position`
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
expect(Array.isArray(columns)).toBe(true);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should verify table exists in net schema', async () => {
|
|
96
|
+
if (!tableExists) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
db.setContext({ role: 'service_role' });
|
|
101
|
+
|
|
102
|
+
// verify table is in the net schema
|
|
103
|
+
const schema = await db.any(
|
|
104
|
+
`SELECT table_schema
|
|
105
|
+
FROM information_schema.tables
|
|
106
|
+
WHERE table_schema = 'net' AND table_name = '_http_response'`
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
if (schema.length > 0) {
|
|
110
|
+
expect(schema[0].table_schema).toBe('net');
|
|
111
|
+
} else {
|
|
112
|
+
expect(Array.isArray(schema)).toBe(true);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should prevent authenticated users from accessing _http_response without proper permissions', async () => {
|
|
117
|
+
if (!tableExists) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// create a test user as admin using db with service_role context
|
|
122
|
+
// using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
|
|
123
|
+
db.setContext({ role: 'service_role' });
|
|
124
|
+
const user = await db.one(
|
|
125
|
+
`INSERT INTO auth.users (id, email)
|
|
126
|
+
VALUES (gen_random_uuid(), $1)
|
|
127
|
+
RETURNING id`,
|
|
128
|
+
['net-http-response-test@example.com']
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
// set context to simulate authenticated user
|
|
132
|
+
db.setContext({
|
|
133
|
+
role: 'authenticated',
|
|
134
|
+
'request.jwt.claim.sub': user.id
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// authenticated users should not be able to access _http_response (rls blocks)
|
|
138
|
+
const result = await db.any(
|
|
139
|
+
`SELECT * FROM net._http_response LIMIT 1`
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
// rls should block access, result should be empty
|
|
143
|
+
expect(result.length).toBe(0);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('should prevent anon from accessing _http_response', async () => {
|
|
147
|
+
if (!tableExists) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// clear context to anon role
|
|
152
|
+
db.clearContext();
|
|
153
|
+
|
|
154
|
+
// anon should not be able to access _http_response (rls blocks)
|
|
155
|
+
const result = await db.any(
|
|
156
|
+
`SELECT * FROM net._http_response LIMIT 1`
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
// rls should block access, result should be empty
|
|
160
|
+
expect(result.length).toBe(0);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|