@pgpm/supabase 0.5.0 → 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.
- package/LICENSE +2 -1
- package/Makefile +2 -3
- package/README.md +45 -32
- package/deploy/roles/anon.sql +15 -0
- package/deploy/schemas/supabase_integration/procedures/pre_request.sql +26 -0
- package/deploy/schemas/supabase_integration/schema.sql +9 -0
- package/package.json +27 -12
- package/pgpm-supabase.control +7 -0
- package/pgpm.plan +6 -3
- package/revert/roles/anon.sql +7 -0
- package/revert/schemas/supabase_integration/procedures/pre_request.sql +7 -0
- package/revert/schemas/supabase_integration/schema.sql +7 -0
- package/sql/pgpm-supabase--1.0.0.bundle.tar.gz +0 -0
- package/sql/pgpm-supabase--1.0.0.sql +32 -0
- package/verify/roles/anon.sql +12 -0
- package/verify/schemas/supabase_integration/procedures/pre_request.sql +19 -0
- package/verify/schemas/supabase_integration/schema.sql +18 -0
- package/__tests__/_realtime/_realtime-extensions.test.ts +0 -129
- package/__tests__/_realtime/_realtime-schema-migrations.test.ts +0 -160
- package/__tests__/_realtime/_realtime-tenants.test.ts +0 -163
- package/__tests__/auth/auth.audit-log-entries.test.ts +0 -148
- package/__tests__/auth/auth.flow-state.test.ts +0 -114
- package/__tests__/auth/auth.identities.test.ts +0 -149
- package/__tests__/auth/auth.instances.test.ts +0 -134
- package/__tests__/auth/auth.mfa-amr-claims.test.ts +0 -142
- package/__tests__/auth/auth.mfa-challenges.test.ts +0 -117
- package/__tests__/auth/auth.mfa-factors.test.ts +0 -118
- package/__tests__/auth/auth.oauth-authorizations.test.ts +0 -123
- package/__tests__/auth/auth.oauth-clients.test.ts +0 -120
- package/__tests__/auth/auth.oauth-consents.test.ts +0 -122
- package/__tests__/auth/auth.one-time-tokens.test.ts +0 -194
- package/__tests__/auth/auth.refresh-tokens.test.ts +0 -134
- package/__tests__/auth/auth.saml-providers.test.ts +0 -154
- package/__tests__/auth/auth.saml-relay-states.test.ts +0 -145
- package/__tests__/auth/auth.schema-migrations.test.ts +0 -131
- package/__tests__/auth/auth.sessions.test.ts +0 -140
- package/__tests__/auth/auth.sso-domains.test.ts +0 -137
- package/__tests__/auth/auth.sso-providers.test.ts +0 -136
- package/__tests__/auth/auth.users.test.ts +0 -314
- package/__tests__/conditional.test.ts +0 -229
- package/__tests__/functions.test.ts +0 -252
- package/__tests__/jsonb.test.ts +0 -237
- package/__tests__/net/net.http-request-queue.test.ts +0 -159
- package/__tests__/net/net.http-response.test.ts +0 -163
- package/__tests__/realtime/realtime.messages-2025-10-31.test.ts +0 -168
- package/__tests__/realtime/realtime.messages.test.ts +0 -162
- package/__tests__/realtime/realtime.schema-migrations.test.ts +0 -139
- package/__tests__/realtime/realtime.subscription.test.ts +0 -159
- package/__tests__/storage/storage.buckets-analytics.test.ts +0 -134
- package/__tests__/storage/storage.buckets.test.ts +0 -146
- package/__tests__/storage/storage.iceberg-namespaces.test.ts +0 -132
- package/__tests__/storage/storage.iceberg-tables.test.ts +0 -142
- package/__tests__/storage/storage.migrations.test.ts +0 -133
- package/__tests__/storage/storage.objects.test.ts +0 -160
- package/__tests__/storage/storage.prefixes.test.ts +0 -138
- package/__tests__/storage/storage.s3-multipart-uploads-parts.test.ts +0 -133
- package/__tests__/storage/storage.s3-multipart-uploads.test.ts +0 -128
- package/__tests__/supabase-functions/supabase.functions-hooks.test.ts +0 -125
- package/__tests__/supabase-functions/supabase.functions-migrations.test.ts +0 -213
- package/__tests__/temporal.test.ts +0 -225
- package/__tests__/vault-secrets.test.ts +0 -145
- package/deploy/LICENSE +0 -9
- package/deploy/supabase.sql +0 -2271
- package/jest.config.js +0 -15
- package/revert/supabase.sql +0 -3
- package/sql/supabase--0.0.1.sql +0 -1368
- package/src/index.ts +0 -3
- package/supabase.control +0 -7
- package/verify/supabase.sql +0 -3
|
@@ -1,314 +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
|
-
|
|
9
|
-
|
|
10
|
-
({ pg, db, teardown } = await getConnections());
|
|
11
|
-
|
|
12
|
-
// verify auth schema exists
|
|
13
|
-
const authSchemaExists = await pg.any(
|
|
14
|
-
`SELECT EXISTS (
|
|
15
|
-
SELECT FROM information_schema.schemata
|
|
16
|
-
WHERE schema_name = 'auth'
|
|
17
|
-
) as exists`
|
|
18
|
-
);
|
|
19
|
-
expect(authSchemaExists[0].exists).toBe(true);
|
|
20
|
-
|
|
21
|
-
// grant access to auth schema for testing
|
|
22
|
-
await pg.any(
|
|
23
|
-
`GRANT USAGE ON SCHEMA auth TO public;
|
|
24
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO service_role;
|
|
25
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO authenticated;
|
|
26
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO anon;
|
|
27
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO service_role;
|
|
28
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO authenticated;
|
|
29
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO anon;`,
|
|
30
|
-
[]
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
// assert auth.users exists
|
|
34
|
-
const exists = await pg.any(
|
|
35
|
-
`SELECT EXISTS (
|
|
36
|
-
SELECT FROM information_schema.tables
|
|
37
|
-
WHERE table_schema = 'auth' AND table_name = 'users'
|
|
38
|
-
) as exists`
|
|
39
|
-
);
|
|
40
|
-
expect(exists[0].exists).toBe(true);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
afterAll(async () => {
|
|
44
|
-
await teardown();
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
beforeEach(async () => {
|
|
48
|
-
await db.beforeEach();
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
afterEach(async () => {
|
|
52
|
-
await db.afterEach();
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
describe('tutorial: auth users table access (supabase system)', () => {
|
|
56
|
-
|
|
57
|
-
it('should verify auth.users table exists', async () => {
|
|
58
|
-
db.setContext({ role: 'service_role' });
|
|
59
|
-
const exists = await db.any(
|
|
60
|
-
`SELECT EXISTS (
|
|
61
|
-
SELECT FROM information_schema.tables
|
|
62
|
-
WHERE table_schema = 'auth' AND table_name = 'users'
|
|
63
|
-
) as exists`
|
|
64
|
-
);
|
|
65
|
-
expect(Array.isArray(exists)).toBe(true);
|
|
66
|
-
expect(exists[0].exists).toBe(true);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('should verify service_role can query auth.users table structure', async () => {
|
|
70
|
-
db.setContext({ role: 'service_role' });
|
|
71
|
-
|
|
72
|
-
const columns = await db.any(
|
|
73
|
-
`SELECT column_name, data_type
|
|
74
|
-
FROM information_schema.columns
|
|
75
|
-
WHERE table_schema = 'auth' AND table_name = 'users'
|
|
76
|
-
ORDER BY ordinal_position`
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
expect(Array.isArray(columns)).toBe(true);
|
|
80
|
-
if (columns.length > 0) {
|
|
81
|
-
expect(columns[0].column_name).toBeDefined();
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('should verify service_role can read auth.users', async () => {
|
|
86
|
-
db.setContext({ role: 'service_role' });
|
|
87
|
-
|
|
88
|
-
const users = await db.any(
|
|
89
|
-
`SELECT id, email, created_at, updated_at
|
|
90
|
-
FROM auth.users
|
|
91
|
-
LIMIT 10`
|
|
92
|
-
);
|
|
93
|
-
|
|
94
|
-
expect(Array.isArray(users)).toBe(true);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it('should verify service_role can count auth.users', async () => {
|
|
98
|
-
db.setContext({ role: 'service_role' });
|
|
99
|
-
|
|
100
|
-
const count = await db.any(
|
|
101
|
-
`SELECT COUNT(*)::integer as count FROM auth.users`
|
|
102
|
-
);
|
|
103
|
-
|
|
104
|
-
expect(Array.isArray(count)).toBe(true);
|
|
105
|
-
expect(count[0].count).toBeDefined();
|
|
106
|
-
expect(typeof Number(count[0].count)).toBe('number');
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it('should verify service_role can query specific user fields', async () => {
|
|
110
|
-
db.setContext({ role: 'service_role' });
|
|
111
|
-
|
|
112
|
-
// query basic fields that definitely exist in auth.users
|
|
113
|
-
const users = await db.any(
|
|
114
|
-
`SELECT id, email, created_at, updated_at
|
|
115
|
-
FROM auth.users
|
|
116
|
-
LIMIT 5`
|
|
117
|
-
);
|
|
118
|
-
|
|
119
|
-
expect(Array.isArray(users)).toBe(true);
|
|
120
|
-
if (users.length > 0) {
|
|
121
|
-
expect(users[0].id).toBeDefined();
|
|
122
|
-
expect(users[0].email).toBeDefined();
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it('should verify service_role can query by email', async () => {
|
|
127
|
-
db.setContext({ role: 'service_role' });
|
|
128
|
-
|
|
129
|
-
const users = await db.any(
|
|
130
|
-
`SELECT id, email
|
|
131
|
-
FROM auth.users
|
|
132
|
-
WHERE email IS NOT NULL
|
|
133
|
-
LIMIT 5`
|
|
134
|
-
);
|
|
135
|
-
|
|
136
|
-
expect(Array.isArray(users)).toBe(true);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it('should verify authenticated access based on rls', async () => {
|
|
140
|
-
// check rls status first
|
|
141
|
-
db.setContext({ role: 'service_role' });
|
|
142
|
-
const rlsStatus = await db.any(
|
|
143
|
-
`SELECT c.relrowsecurity
|
|
144
|
-
FROM pg_class c
|
|
145
|
-
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
146
|
-
WHERE n.nspname = 'auth' AND c.relname = 'users'`
|
|
147
|
-
);
|
|
148
|
-
expect(Array.isArray(rlsStatus)).toBe(true);
|
|
149
|
-
expect(rlsStatus.length).toBeGreaterThan(0);
|
|
150
|
-
|
|
151
|
-
db.setContext({ role: 'authenticated' });
|
|
152
|
-
const result = await db.any(`SELECT * FROM auth.users LIMIT 1`);
|
|
153
|
-
expect(Array.isArray(result)).toBe(true);
|
|
154
|
-
if (rlsStatus[0].relrowsecurity === true) {
|
|
155
|
-
expect(result.length).toBe(0);
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
it('should verify authenticated count based on rls', async () => {
|
|
160
|
-
db.setContext({ role: 'service_role' });
|
|
161
|
-
const rlsStatus = await db.any(
|
|
162
|
-
`SELECT c.relrowsecurity
|
|
163
|
-
FROM pg_class c
|
|
164
|
-
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
165
|
-
WHERE n.nspname = 'auth' AND c.relname = 'users'`
|
|
166
|
-
);
|
|
167
|
-
expect(Array.isArray(rlsStatus)).toBe(true);
|
|
168
|
-
|
|
169
|
-
db.setContext({ role: 'authenticated' });
|
|
170
|
-
const count = await db.any(`SELECT COUNT(*)::integer as count FROM auth.users`);
|
|
171
|
-
expect(Array.isArray(count)).toBe(true);
|
|
172
|
-
if (rlsStatus[0]?.relrowsecurity === true) {
|
|
173
|
-
expect(Number(count[0].count)).toBe(0);
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
it('should verify table has primary key on id', async () => {
|
|
178
|
-
db.setContext({ role: 'service_role' });
|
|
179
|
-
|
|
180
|
-
const pk = await db.any(
|
|
181
|
-
`SELECT constraint_name
|
|
182
|
-
FROM information_schema.table_constraints
|
|
183
|
-
WHERE table_schema = 'auth' AND table_name = 'users'
|
|
184
|
-
AND constraint_type = 'PRIMARY KEY'`
|
|
185
|
-
);
|
|
186
|
-
|
|
187
|
-
expect(Array.isArray(pk)).toBe(true);
|
|
188
|
-
if (pk.length > 0) {
|
|
189
|
-
expect(pk[0].constraint_name).toBeDefined();
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
it('should verify table has unique constraint on email', async () => {
|
|
194
|
-
db.setContext({ role: 'service_role' });
|
|
195
|
-
|
|
196
|
-
const unique = await db.any(
|
|
197
|
-
`SELECT constraint_name
|
|
198
|
-
FROM information_schema.table_constraints
|
|
199
|
-
WHERE table_schema = 'auth' AND table_name = 'users'
|
|
200
|
-
AND constraint_type = 'UNIQUE'`
|
|
201
|
-
);
|
|
202
|
-
|
|
203
|
-
expect(Array.isArray(unique)).toBe(true);
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
it('should verify table has indexes on instance_id', async () => {
|
|
207
|
-
db.setContext({ role: 'service_role' });
|
|
208
|
-
|
|
209
|
-
const indexes = await db.any(
|
|
210
|
-
`SELECT indexname
|
|
211
|
-
FROM pg_indexes
|
|
212
|
-
WHERE schemaname = 'auth' AND tablename = 'users'
|
|
213
|
-
AND indexname LIKE '%instance_id%'`
|
|
214
|
-
);
|
|
215
|
-
|
|
216
|
-
expect(Array.isArray(indexes)).toBe(true);
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
it('should verify rls status on auth.users table', async () => {
|
|
220
|
-
db.setContext({ role: 'service_role' });
|
|
221
|
-
|
|
222
|
-
const rlsStatus = await db.any(
|
|
223
|
-
`SELECT c.relrowsecurity
|
|
224
|
-
FROM pg_class c
|
|
225
|
-
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
226
|
-
WHERE n.nspname = 'auth' AND c.relname = 'users'`
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
expect(Array.isArray(rlsStatus)).toBe(true);
|
|
230
|
-
if (rlsStatus.length > 0) {
|
|
231
|
-
expect(typeof rlsStatus[0].relrowsecurity).toBe('boolean');
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
it('should verify anon access based on rls', async () => {
|
|
236
|
-
db.setContext({ role: 'service_role' });
|
|
237
|
-
const rlsStatus = await db.any(
|
|
238
|
-
`SELECT c.relrowsecurity
|
|
239
|
-
FROM pg_class c
|
|
240
|
-
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
241
|
-
WHERE n.nspname = 'auth' AND c.relname = 'users'`
|
|
242
|
-
);
|
|
243
|
-
expect(Array.isArray(rlsStatus)).toBe(true);
|
|
244
|
-
|
|
245
|
-
db.clearContext();
|
|
246
|
-
const result = await db.any(`SELECT * FROM auth.users LIMIT 1`);
|
|
247
|
-
expect(Array.isArray(result)).toBe(true);
|
|
248
|
-
if (rlsStatus[0]?.relrowsecurity === true) {
|
|
249
|
-
expect(result.length).toBe(0);
|
|
250
|
-
}
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
it('should verify anon count based on rls', async () => {
|
|
254
|
-
db.setContext({ role: 'service_role' });
|
|
255
|
-
const rlsStatus = await db.any(
|
|
256
|
-
`SELECT c.relrowsecurity
|
|
257
|
-
FROM pg_class c
|
|
258
|
-
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
259
|
-
WHERE n.nspname = 'auth' AND c.relname = 'users'`
|
|
260
|
-
);
|
|
261
|
-
expect(Array.isArray(rlsStatus)).toBe(true);
|
|
262
|
-
|
|
263
|
-
db.clearContext();
|
|
264
|
-
const count = await db.any(`SELECT COUNT(*)::integer as count FROM auth.users`);
|
|
265
|
-
expect(Array.isArray(count)).toBe(true);
|
|
266
|
-
if (rlsStatus[0]?.relrowsecurity === true) {
|
|
267
|
-
expect(Number(count[0].count)).toBe(0);
|
|
268
|
-
}
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
it('should verify anon query by email based on rls', async () => {
|
|
272
|
-
db.setContext({ role: 'service_role' });
|
|
273
|
-
const rlsStatus = await db.any(
|
|
274
|
-
`SELECT c.relrowsecurity
|
|
275
|
-
FROM pg_class c
|
|
276
|
-
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
277
|
-
WHERE n.nspname = 'auth' AND c.relname = 'users'`
|
|
278
|
-
);
|
|
279
|
-
|
|
280
|
-
db.clearContext();
|
|
281
|
-
const result = await db.any(`SELECT id, email FROM auth.users WHERE email = 'test@example.com'`);
|
|
282
|
-
expect(Array.isArray(result)).toBe(true);
|
|
283
|
-
if (rlsStatus[0]?.relrowsecurity === true) {
|
|
284
|
-
expect(result.length).toBe(0);
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
it('should verify context switching between roles', async () => {
|
|
289
|
-
// start as service_role - should see data
|
|
290
|
-
db.setContext({ role: 'service_role' });
|
|
291
|
-
const serviceRoleResult = await db.any(`SELECT COUNT(*)::integer as count FROM auth.users`);
|
|
292
|
-
expect(Array.isArray(serviceRoleResult)).toBe(true);
|
|
293
|
-
const initialCount = Number(serviceRoleResult[0].count);
|
|
294
|
-
expect(typeof initialCount).toBe('number');
|
|
295
|
-
|
|
296
|
-
// switch to authenticated
|
|
297
|
-
db.setContext({ role: 'authenticated' });
|
|
298
|
-
const authenticatedResult = await db.any(`SELECT COUNT(*)::integer as count FROM auth.users`);
|
|
299
|
-
expect(Array.isArray(authenticatedResult)).toBe(true);
|
|
300
|
-
|
|
301
|
-
// switch to anon
|
|
302
|
-
db.clearContext();
|
|
303
|
-
const anonResult = await db.any(`SELECT COUNT(*)::integer as count FROM auth.users`);
|
|
304
|
-
expect(Array.isArray(anonResult)).toBe(true);
|
|
305
|
-
|
|
306
|
-
// switch back to service_role - should see data again
|
|
307
|
-
db.setContext({ role: 'service_role' });
|
|
308
|
-
const serviceRoleResult2 = await db.any(`SELECT COUNT(*)::integer as count FROM auth.users`);
|
|
309
|
-
expect(Array.isArray(serviceRoleResult2)).toBe(true);
|
|
310
|
-
const finalCount = Number(serviceRoleResult2[0].count);
|
|
311
|
-
expect(typeof finalCount).toBe('number');
|
|
312
|
-
});
|
|
313
|
-
});
|
|
314
|
-
|
|
@@ -1,229 +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
|
-
// minimal grants for tests
|
|
20
|
-
await pg.any(
|
|
21
|
-
`GRANT USAGE ON SCHEMA auth TO public;
|
|
22
|
-
GRANT USAGE ON SCHEMA storage TO public;
|
|
23
|
-
GRANT SELECT ON TABLE auth.users TO service_role;
|
|
24
|
-
GRANT SELECT ON TABLE storage.buckets TO service_role;
|
|
25
|
-
GRANT EXECUTE ON FUNCTION auth.uid() TO public;
|
|
26
|
-
GRANT EXECUTE ON FUNCTION auth.role() TO public;`,
|
|
27
|
-
[]
|
|
28
|
-
);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
afterAll(async () => {
|
|
32
|
-
await teardown();
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
beforeEach(async () => {
|
|
36
|
-
await db.beforeEach();
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
afterEach(async () => {
|
|
40
|
-
await db.afterEach();
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
describe('tutorial: rls with conditional policies', () => {
|
|
44
|
-
it('should verify rls policies can use case statements', async () => {
|
|
45
|
-
db.setContext({ role: 'service_role' });
|
|
46
|
-
|
|
47
|
-
// insert into auth.users using admin connection
|
|
48
|
-
const user1 = await pg.one(
|
|
49
|
-
`INSERT INTO auth.users (id, email)
|
|
50
|
-
VALUES (gen_random_uuid(), $1)
|
|
51
|
-
RETURNING id`,
|
|
52
|
-
['case1@example.com']
|
|
53
|
-
);
|
|
54
|
-
// ensure auth.role() sees service_role via jwt claim
|
|
55
|
-
db.setContext({
|
|
56
|
-
role: 'service_role',
|
|
57
|
-
'request.jwt.claim.role': 'service_role'
|
|
58
|
-
});
|
|
59
|
-
// case over email and auth.role()
|
|
60
|
-
const rows = await db.any(
|
|
61
|
-
`SELECT
|
|
62
|
-
CASE WHEN email LIKE '%@example.com' THEN 'example' ELSE 'other' END AS grp,
|
|
63
|
-
CASE WHEN auth.role() = 'service_role' THEN 'visible' ELSE 'hidden' END AS visibility
|
|
64
|
-
FROM auth.users
|
|
65
|
-
WHERE id = $1`,
|
|
66
|
-
[user1.id]
|
|
67
|
-
);
|
|
68
|
-
expect(Array.isArray(rows)).toBe(true);
|
|
69
|
-
expect(rows.length).toBe(1);
|
|
70
|
-
expect(rows[0].grp).toBe('example');
|
|
71
|
-
expect(rows[0].visibility).toBe('visible');
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it('should verify rls policies work with multiple conditions', async () => {
|
|
75
|
-
db.setContext({ role: 'service_role' });
|
|
76
|
-
|
|
77
|
-
// insert into auth.users using admin connection
|
|
78
|
-
const user = await pg.one(
|
|
79
|
-
`INSERT INTO auth.users (id, email)
|
|
80
|
-
VALUES (gen_random_uuid(), $1)
|
|
81
|
-
RETURNING id`,
|
|
82
|
-
['multi1@example.com']
|
|
83
|
-
);
|
|
84
|
-
// ensure auth.role() sees service_role via jwt claim
|
|
85
|
-
db.setContext({
|
|
86
|
-
role: 'service_role',
|
|
87
|
-
'request.jwt.claim.role': 'service_role'
|
|
88
|
-
});
|
|
89
|
-
// and/or combinations with auth.role()
|
|
90
|
-
const rows = await db.any(
|
|
91
|
-
`SELECT id
|
|
92
|
-
FROM auth.users
|
|
93
|
-
WHERE (email = $1 AND (auth.role() = 'service_role' OR auth.role() = 'authenticated'))
|
|
94
|
-
AND id = $2`,
|
|
95
|
-
['multi1@example.com', user.id]
|
|
96
|
-
);
|
|
97
|
-
expect(Array.isArray(rows)).toBe(true);
|
|
98
|
-
expect(rows.length).toBe(1);
|
|
99
|
-
expect(rows[0].id).toBe(user.id);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('should verify rls policies work with or conditions', async () => {
|
|
103
|
-
db.setContext({ role: 'service_role' });
|
|
104
|
-
|
|
105
|
-
// insert into auth.users using admin connection
|
|
106
|
-
const user = await pg.one(
|
|
107
|
-
`INSERT INTO auth.users (id, email)
|
|
108
|
-
VALUES (gen_random_uuid(), $1)
|
|
109
|
-
RETURNING id`,
|
|
110
|
-
['or1@example.com']
|
|
111
|
-
);
|
|
112
|
-
const user2 = await pg.one(
|
|
113
|
-
`INSERT INTO auth.users (id, email)
|
|
114
|
-
VALUES (gen_random_uuid(), $1)
|
|
115
|
-
RETURNING id`,
|
|
116
|
-
['or2@example.com']
|
|
117
|
-
);
|
|
118
|
-
|
|
119
|
-
const rows = await db.any(
|
|
120
|
-
`SELECT COUNT(*)::integer AS count
|
|
121
|
-
FROM auth.users
|
|
122
|
-
WHERE email = $1 OR email = $2`,
|
|
123
|
-
['or1@example.com', 'or2@example.com']
|
|
124
|
-
);
|
|
125
|
-
expect(Array.isArray(rows)).toBe(true);
|
|
126
|
-
expect(Number(rows[0].count)).toBe(2);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
it('should verify rls policies work with subqueries', async () => {
|
|
130
|
-
db.setContext({ role: 'service_role' });
|
|
131
|
-
|
|
132
|
-
// insert into auth.users using admin connection
|
|
133
|
-
const user1 = await pg.one(
|
|
134
|
-
`INSERT INTO auth.users (id, email)
|
|
135
|
-
VALUES (gen_random_uuid(), $1)
|
|
136
|
-
RETURNING id`,
|
|
137
|
-
['sub1@example.com']
|
|
138
|
-
);
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
const user2 = await pg.one(
|
|
142
|
-
`INSERT INTO auth.users (id, email)
|
|
143
|
-
VALUES (gen_random_uuid(), $1)
|
|
144
|
-
RETURNING id`,
|
|
145
|
-
['sub2@example.com']
|
|
146
|
-
);
|
|
147
|
-
// use a subquery to resolve ids by email
|
|
148
|
-
const viaSub = await db.any(
|
|
149
|
-
`SELECT u.id
|
|
150
|
-
FROM auth.users u
|
|
151
|
-
WHERE u.id IN (SELECT id FROM auth.users WHERE email = $1)`,
|
|
152
|
-
['sub1@example.com']
|
|
153
|
-
);
|
|
154
|
-
expect(viaSub.length).toBe(1);
|
|
155
|
-
expect(viaSub[0].id).toBe(user1.id);
|
|
156
|
-
|
|
157
|
-
// correlated exists
|
|
158
|
-
const existsRows = await db.any(
|
|
159
|
-
`SELECT u.id
|
|
160
|
-
FROM auth.users u
|
|
161
|
-
WHERE EXISTS (
|
|
162
|
-
SELECT 1 FROM auth.users i WHERE i.id = u.id AND i.email = $1
|
|
163
|
-
)`,
|
|
164
|
-
['sub2@example.com']
|
|
165
|
-
);
|
|
166
|
-
expect(existsRows.length).toBeGreaterThan(0);
|
|
167
|
-
const found = existsRows.find((r: any) => r.id === user2.id);
|
|
168
|
-
expect(Boolean(found)).toBe(true);
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
it('should verify rls policies work with related table checks', async () => {
|
|
172
|
-
db.setContext({ role: 'service_role' });
|
|
173
|
-
|
|
174
|
-
// insert into auth.users using admin connection
|
|
175
|
-
const user = await pg.one(
|
|
176
|
-
`INSERT INTO auth.users (id, email)
|
|
177
|
-
VALUES (gen_random_uuid(), $1)
|
|
178
|
-
RETURNING id`,
|
|
179
|
-
['rel1@example.com']
|
|
180
|
-
);
|
|
181
|
-
// left join storage.buckets by fk owner -> auth.users(id)
|
|
182
|
-
const rows = await db.any(
|
|
183
|
-
`SELECT u.id, b.id AS bucket_id
|
|
184
|
-
FROM auth.users u
|
|
185
|
-
LEFT JOIN storage.buckets b ON b.owner = u.id
|
|
186
|
-
WHERE u.id = $1`,
|
|
187
|
-
[user.id]
|
|
188
|
-
);
|
|
189
|
-
expect(rows.length).toBe(1);
|
|
190
|
-
// no bucket yet, so join should be null
|
|
191
|
-
expect(rows[0].bucket_id === null || rows[0].bucket_id === undefined).toBe(true);
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it('should verify rls policies work with null checks', async () => {
|
|
195
|
-
db.setContext({ role: 'service_role' });
|
|
196
|
-
|
|
197
|
-
// insert into auth.users using admin connection
|
|
198
|
-
const user = await pg.one(
|
|
199
|
-
`INSERT INTO auth.users (id, email)
|
|
200
|
-
VALUES (gen_random_uuid(), $1)
|
|
201
|
-
RETURNING id, email`,
|
|
202
|
-
[null]
|
|
203
|
-
);
|
|
204
|
-
const rows = await db.any(
|
|
205
|
-
`SELECT id FROM auth.users WHERE id = $1 AND email IS NULL`,
|
|
206
|
-
[user.id]
|
|
207
|
-
);
|
|
208
|
-
expect(rows.length).toBe(1);
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
it('should verify rls policies work with coalesce functions', async () => {
|
|
212
|
-
db.setContext({ role: 'service_role' });
|
|
213
|
-
|
|
214
|
-
// insert into auth.users using admin connection
|
|
215
|
-
const user = await pg.one(
|
|
216
|
-
`INSERT INTO auth.users (id, email)
|
|
217
|
-
VALUES (gen_random_uuid(), $1)
|
|
218
|
-
RETURNING id`,
|
|
219
|
-
[null]
|
|
220
|
-
);
|
|
221
|
-
const rows = await db.any(
|
|
222
|
-
`SELECT COALESCE(email, '') AS safe_email FROM auth.users WHERE id = $1`,
|
|
223
|
-
[user.id]
|
|
224
|
-
);
|
|
225
|
-
expect(rows.length).toBe(1);
|
|
226
|
-
expect(rows[0].safe_email).toBe('');
|
|
227
|
-
});
|
|
228
|
-
});
|
|
229
|
-
|