@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.
- package/LICENSE +2 -1
- package/Makefile +2 -3
- package/README.md +45 -59
- 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,163 +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_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
|
-
|
|
@@ -1,168 +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 realtime schema exists (optional schema)
|
|
13
|
-
const realtimeSchemaExists = await pg.any(
|
|
14
|
-
`SELECT EXISTS (
|
|
15
|
-
SELECT FROM information_schema.schemata
|
|
16
|
-
WHERE schema_name = 'realtime'
|
|
17
|
-
) as exists`
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
if (realtimeSchemaExists[0]?.exists === true) {
|
|
21
|
-
// grant access to realtime schema for testing
|
|
22
|
-
await pg.any(
|
|
23
|
-
`GRANT USAGE ON SCHEMA realtime TO public;
|
|
24
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA realtime TO service_role;
|
|
25
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA realtime TO authenticated;
|
|
26
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA realtime TO anon;
|
|
27
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA realtime GRANT SELECT ON TABLES TO service_role;
|
|
28
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA realtime GRANT SELECT ON TABLES TO authenticated;
|
|
29
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA realtime GRANT SELECT ON TABLES TO anon;`,
|
|
30
|
-
[]
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
// check if realtime.messages_2025_10_31 partition 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 = 'realtime' AND table_name = 'messages_2025_10_31'
|
|
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: realtime messages partition table access', () => {
|
|
57
|
-
|
|
58
|
-
it('should verify messages partition table exists', async () => {
|
|
59
|
-
db.setContext({ role: 'service_role' });
|
|
60
|
-
|
|
61
|
-
// verify partition table exists in information schema
|
|
62
|
-
const exists = await db.any(
|
|
63
|
-
`SELECT EXISTS (
|
|
64
|
-
SELECT FROM information_schema.tables
|
|
65
|
-
WHERE table_schema = 'realtime' AND table_name = 'messages_2025_10_31'
|
|
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 partition table', async () => {
|
|
78
|
-
if (!tableExists) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
db.setContext({ role: 'service_role' });
|
|
83
|
-
|
|
84
|
-
// service_role should be able to query partition table directly
|
|
85
|
-
const messages = await db.any(
|
|
86
|
-
`SELECT id, channel, payload
|
|
87
|
-
FROM realtime.messages_2025_10_31
|
|
88
|
-
LIMIT 10`
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
expect(Array.isArray(messages)).toBe(true);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('should verify partition inherits from parent table structure', async () => {
|
|
95
|
-
if (!tableExists) {
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
db.setContext({ role: 'service_role' });
|
|
100
|
-
|
|
101
|
-
// partition tables inherit column structure from parent table
|
|
102
|
-
// verify columns match between parent and partition
|
|
103
|
-
const parentColumns = await db.any(
|
|
104
|
-
`SELECT column_name
|
|
105
|
-
FROM information_schema.columns
|
|
106
|
-
WHERE table_schema = 'realtime' AND table_name = 'messages'
|
|
107
|
-
ORDER BY ordinal_position`
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
const partitionColumns = await db.any(
|
|
111
|
-
`SELECT column_name
|
|
112
|
-
FROM information_schema.columns
|
|
113
|
-
WHERE table_schema = 'realtime' AND table_name = 'messages_2025_10_31'
|
|
114
|
-
ORDER BY ordinal_position`
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
expect(Array.isArray(parentColumns)).toBe(true);
|
|
118
|
-
expect(Array.isArray(partitionColumns)).toBe(true);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it('should prevent authenticated users from accessing partition table without proper permissions', async () => {
|
|
122
|
-
if (!tableExists) {
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// create a test user as admin using db with service_role context
|
|
127
|
-
// using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
|
|
128
|
-
db.setContext({ role: 'service_role' });
|
|
129
|
-
const user = await db.one(
|
|
130
|
-
`INSERT INTO auth.users (id, email)
|
|
131
|
-
VALUES (gen_random_uuid(), $1)
|
|
132
|
-
RETURNING id`,
|
|
133
|
-
['realtime-partition-test@example.com']
|
|
134
|
-
);
|
|
135
|
-
|
|
136
|
-
// set context to simulate authenticated user
|
|
137
|
-
db.setContext({
|
|
138
|
-
role: 'authenticated',
|
|
139
|
-
'request.jwt.claim.sub': user.id
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
// authenticated users should not be able to access partition table (rls blocks)
|
|
143
|
-
const result = await db.any(
|
|
144
|
-
`SELECT * FROM realtime.messages_2025_10_31 LIMIT 1`
|
|
145
|
-
);
|
|
146
|
-
|
|
147
|
-
// rls should block access, result should be empty
|
|
148
|
-
expect(result.length).toBe(0);
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
it('should prevent anon from accessing partition table', async () => {
|
|
152
|
-
if (!tableExists) {
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// clear context to anon role
|
|
157
|
-
db.clearContext();
|
|
158
|
-
|
|
159
|
-
// anon should not be able to access partition table (rls blocks)
|
|
160
|
-
const result = await db.any(
|
|
161
|
-
`SELECT * FROM realtime.messages_2025_10_31 LIMIT 1`
|
|
162
|
-
);
|
|
163
|
-
|
|
164
|
-
// rls should block access, result should be empty
|
|
165
|
-
expect(result.length).toBe(0);
|
|
166
|
-
});
|
|
167
|
-
});
|
|
168
|
-
|
|
@@ -1,162 +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 realtime schema exists (optional schema)
|
|
13
|
-
const realtimeSchemaExists = await pg.any(
|
|
14
|
-
`SELECT EXISTS (
|
|
15
|
-
SELECT FROM information_schema.schemata
|
|
16
|
-
WHERE schema_name = 'realtime'
|
|
17
|
-
) as exists`
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
if (realtimeSchemaExists[0]?.exists === true) {
|
|
21
|
-
// grant access to realtime schema for testing
|
|
22
|
-
await pg.any(
|
|
23
|
-
`GRANT USAGE ON SCHEMA realtime TO public;
|
|
24
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA realtime TO service_role;
|
|
25
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA realtime TO authenticated;
|
|
26
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA realtime TO anon;
|
|
27
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA realtime GRANT SELECT ON TABLES TO service_role;
|
|
28
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA realtime GRANT SELECT ON TABLES TO authenticated;
|
|
29
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA realtime GRANT SELECT ON TABLES TO anon;`,
|
|
30
|
-
[]
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
// check if realtime.messages 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 = 'realtime' AND table_name = 'messages'
|
|
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: realtime messages table access', () => {
|
|
57
|
-
|
|
58
|
-
it('should verify messages 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 = 'realtime' AND table_name = 'messages'
|
|
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 messages', async () => {
|
|
78
|
-
if (!tableExists) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
db.setContext({ role: 'service_role' });
|
|
83
|
-
|
|
84
|
-
// service_role should be able to query messages
|
|
85
|
-
const messages = await db.any(
|
|
86
|
-
`SELECT id, channel, payload, inserted_at
|
|
87
|
-
FROM realtime.messages
|
|
88
|
-
LIMIT 10`
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
expect(Array.isArray(messages)).toBe(true);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('should verify table is partitioned or has partition inheritance', async () => {
|
|
95
|
-
if (!tableExists) {
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
db.setContext({ role: 'service_role' });
|
|
100
|
-
|
|
101
|
-
// realtime.messages uses table partitioning for time-based data management
|
|
102
|
-
// check for partition inheritance in pg_class
|
|
103
|
-
const partitionInfo = await db.any(
|
|
104
|
-
`SELECT c.relname, c.relkind
|
|
105
|
-
FROM pg_class c
|
|
106
|
-
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
107
|
-
WHERE n.nspname = 'realtime'
|
|
108
|
-
AND c.relname LIKE 'messages%'
|
|
109
|
-
AND (c.relkind = 'r' OR c.relkind = 'p')`
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
expect(Array.isArray(partitionInfo)).toBe(true);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it('should prevent authenticated users from accessing messages without proper permissions', async () => {
|
|
116
|
-
if (!tableExists) {
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// create a test user as admin using db with service_role context
|
|
121
|
-
// using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
|
|
122
|
-
db.setContext({ role: 'service_role' });
|
|
123
|
-
const user = await db.one(
|
|
124
|
-
`INSERT INTO auth.users (id, email)
|
|
125
|
-
VALUES (gen_random_uuid(), $1)
|
|
126
|
-
RETURNING id`,
|
|
127
|
-
['realtime-messages-test@example.com']
|
|
128
|
-
);
|
|
129
|
-
|
|
130
|
-
// set context to simulate authenticated user
|
|
131
|
-
db.setContext({
|
|
132
|
-
role: 'authenticated',
|
|
133
|
-
'request.jwt.claim.sub': user.id
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
// authenticated users should not be able to access messages (rls blocks)
|
|
137
|
-
const result = await db.any(
|
|
138
|
-
`SELECT * FROM realtime.messages LIMIT 1`
|
|
139
|
-
);
|
|
140
|
-
|
|
141
|
-
// rls should block access, result should be empty
|
|
142
|
-
expect(result.length).toBe(0);
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
it('should prevent anon from accessing messages', async () => {
|
|
146
|
-
if (!tableExists) {
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// clear context to anon role
|
|
151
|
-
db.clearContext();
|
|
152
|
-
|
|
153
|
-
// anon should not be able to access messages (rls blocks)
|
|
154
|
-
const result = await db.any(
|
|
155
|
-
`SELECT * FROM realtime.messages LIMIT 1`
|
|
156
|
-
);
|
|
157
|
-
|
|
158
|
-
// rls should block access, result should be empty
|
|
159
|
-
expect(result.length).toBe(0);
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
|
|
@@ -1,139 +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 realtime schema exists (optional schema)
|
|
13
|
-
const realtimeSchemaExists = await pg.any(
|
|
14
|
-
`SELECT EXISTS (
|
|
15
|
-
SELECT FROM information_schema.schemata
|
|
16
|
-
WHERE schema_name = 'realtime'
|
|
17
|
-
) as exists`
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
if (realtimeSchemaExists[0]?.exists === true) {
|
|
21
|
-
// grant access to realtime schema for testing
|
|
22
|
-
await pg.any(
|
|
23
|
-
`GRANT USAGE ON SCHEMA realtime TO public;
|
|
24
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA realtime TO service_role;
|
|
25
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA realtime TO authenticated;
|
|
26
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA realtime TO anon;
|
|
27
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA realtime GRANT SELECT ON TABLES TO service_role;
|
|
28
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA realtime GRANT SELECT ON TABLES TO authenticated;
|
|
29
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA realtime GRANT SELECT ON TABLES TO anon;`,
|
|
30
|
-
[]
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
// check if realtime.schema_migrations 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 = 'realtime' AND table_name = 'schema_migrations'
|
|
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: realtime schema_migrations table access', () => {
|
|
57
|
-
|
|
58
|
-
it('should verify schema_migrations 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 = 'realtime' AND table_name = 'schema_migrations'
|
|
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 read migration versions', async () => {
|
|
78
|
-
if (!tableExists) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
db.setContext({ role: 'service_role' });
|
|
83
|
-
|
|
84
|
-
// service_role should be able to query migration versions
|
|
85
|
-
const migrations = await db.any(
|
|
86
|
-
`SELECT version FROM realtime.schema_migrations ORDER BY version`
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
expect(Array.isArray(migrations)).toBe(true);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('should prevent authenticated users from accessing schema_migrations without proper permissions', async () => {
|
|
93
|
-
if (!tableExists) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// create a test user as admin using db with service_role context
|
|
98
|
-
// using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
|
|
99
|
-
db.setContext({ role: 'service_role' });
|
|
100
|
-
const user = await db.one(
|
|
101
|
-
`INSERT INTO auth.users (id, email)
|
|
102
|
-
VALUES (gen_random_uuid(), $1)
|
|
103
|
-
RETURNING id`,
|
|
104
|
-
['realtime-schema-migrations-test@example.com']
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
// set context to simulate authenticated user
|
|
108
|
-
db.setContext({
|
|
109
|
-
role: 'authenticated',
|
|
110
|
-
'request.jwt.claim.sub': user.id
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
// authenticated users should not be able to access schema_migrations (rls blocks)
|
|
114
|
-
const result = await db.any(
|
|
115
|
-
`SELECT * FROM realtime.schema_migrations LIMIT 1`
|
|
116
|
-
);
|
|
117
|
-
|
|
118
|
-
// rls should block access, result should be empty
|
|
119
|
-
expect(result.length).toBe(0);
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it('should prevent anon from accessing schema_migrations', async () => {
|
|
123
|
-
if (!tableExists) {
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// clear context to anon role
|
|
128
|
-
db.clearContext();
|
|
129
|
-
|
|
130
|
-
// anon should not be able to access schema_migrations (rls blocks)
|
|
131
|
-
const result = await db.any(
|
|
132
|
-
`SELECT * FROM realtime.schema_migrations LIMIT 1`
|
|
133
|
-
);
|
|
134
|
-
|
|
135
|
-
// rls should block access, result should be empty
|
|
136
|
-
expect(result.length).toBe(0);
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
|