@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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Interweb, Inc. <developers@interweb.co>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Makefile
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# @pgpm/supabase
|
|
2
|
+
|
|
3
|
+
<p align="center" width="100%">
|
|
4
|
+
<img height="250" src="https://raw.githubusercontent.com/launchql/supabase-test/refs/heads/main/docs/img/logos.svg" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center" width="100%">
|
|
8
|
+
<a href="https://github.com/launchql/supabase-test/actions/workflows/ci.yml">
|
|
9
|
+
<img height="20" src="https://github.com/launchql/supabase-test/actions/workflows/ci.yml/badge.svg" />
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/launchql/supabase-test/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a>
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
Supabase-focused SQL, tests, and helpers for building robust applications with Row-Level Security.
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
|
|
18
|
+
- 🔐 Supabase auth integration helpers
|
|
19
|
+
- 🧪 Test utilities for RLS policies
|
|
20
|
+
- 📦 Reusable SQL patterns and functions
|
|
21
|
+
- ⚡ Optimized for Supabase local development
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
# Run tests
|
|
27
|
+
pnpm test
|
|
28
|
+
|
|
29
|
+
# Watch mode for development
|
|
30
|
+
pnpm test:watch
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Disclaimer
|
|
34
|
+
|
|
35
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
36
|
+
|
|
37
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
|
@@ -0,0 +1,129 @@
|
|
|
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.extensions 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 = 'extensions'
|
|
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 extensions table access', () => {
|
|
57
|
+
|
|
58
|
+
it('should verify extensions table exists in _realtime schema', 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 = 'extensions'
|
|
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 extensions table 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 = '_realtime' AND table_name = 'extensions'
|
|
89
|
+
ORDER BY ordinal_position`
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
expect(Array.isArray(columns)).toBe(true);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should prevent anon from accessing extensions table', async () => {
|
|
96
|
+
if (!tableExists) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// clear context to anon role
|
|
101
|
+
db.clearContext();
|
|
102
|
+
|
|
103
|
+
// anon should not be able to access _realtime.extensions (rls blocks)
|
|
104
|
+
const result = await db.any(
|
|
105
|
+
`SELECT * FROM _realtime.extensions LIMIT 1`
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
// rls should block access, result should be empty
|
|
109
|
+
expect(result.length).toBe(0);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should verify table has proper grants via information_schema', async () => {
|
|
113
|
+
if (!tableExists) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
db.setContext({ role: 'service_role' });
|
|
118
|
+
|
|
119
|
+
// check table privileges/grants
|
|
120
|
+
const grants = await db.any(
|
|
121
|
+
`SELECT grantee, privilege_type
|
|
122
|
+
FROM information_schema.table_privileges
|
|
123
|
+
WHERE table_schema = '_realtime' AND table_name = 'extensions'`
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
expect(Array.isArray(grants)).toBe(true);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { getConnections, PgTestClient } from 'supabase-test';
|
|
2
|
+
|
|
3
|
+
let pg: PgTestClient;
|
|
4
|
+
let db: PgTestClient;
|
|
5
|
+
let teardown: () => Promise<void>;
|
|
6
|
+
|
|
7
|
+
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 records', async () => {
|
|
78
|
+
if (!tableExists) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
db.setContext({ role: 'service_role' });
|
|
83
|
+
|
|
84
|
+
// service_role should be able to query schema_migrations
|
|
85
|
+
const migrations = await db.any(
|
|
86
|
+
`SELECT * FROM _realtime.schema_migrations`
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
expect(Array.isArray(migrations)).toBe(true);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should verify table has version column', async () => {
|
|
93
|
+
if (!tableExists) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
db.setContext({ role: 'service_role' });
|
|
98
|
+
|
|
99
|
+
// check for version column in schema_migrations table
|
|
100
|
+
const columns = await db.any(
|
|
101
|
+
`SELECT column_name, data_type
|
|
102
|
+
FROM information_schema.columns
|
|
103
|
+
WHERE table_schema = '_realtime' AND table_name = 'schema_migrations'
|
|
104
|
+
AND column_name = 'version'`
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
expect(Array.isArray(columns)).toBe(true);
|
|
108
|
+
if (columns.length > 0) {
|
|
109
|
+
expect(columns[0].column_name).toBe('version');
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('should prevent authenticated users from accessing schema_migrations without proper permissions', async () => {
|
|
114
|
+
if (!tableExists) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// create a test user as admin using db with service_role context
|
|
119
|
+
// using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
|
|
120
|
+
db.setContext({ role: 'service_role' });
|
|
121
|
+
const user = await db.one(
|
|
122
|
+
`INSERT INTO auth.users (id, email)
|
|
123
|
+
VALUES (gen_random_uuid(), $1)
|
|
124
|
+
RETURNING id`,
|
|
125
|
+
['realtime-migrations-test@example.com']
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
// set context to simulate authenticated user
|
|
129
|
+
db.setContext({
|
|
130
|
+
role: 'authenticated',
|
|
131
|
+
'request.jwt.claim.sub': user.id
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// authenticated users should not be able to access schema_migrations (rls blocks)
|
|
135
|
+
const result = await db.any(
|
|
136
|
+
`SELECT * FROM _realtime.schema_migrations LIMIT 1`
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
// rls should block access, result should be empty
|
|
140
|
+
expect(result.length).toBe(0);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('should prevent anon from reading migrations', async () => {
|
|
144
|
+
if (!tableExists) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// clear context to anon role
|
|
149
|
+
db.clearContext();
|
|
150
|
+
|
|
151
|
+
// anon should not be able to access schema_migrations (rls blocks)
|
|
152
|
+
const result = await db.any(
|
|
153
|
+
`SELECT * FROM _realtime.schema_migrations LIMIT 1`
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
// rls should block access, result should be empty
|
|
157
|
+
expect(result.length).toBe(0);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
@@ -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 _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.tenants 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 = 'tenants'
|
|
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 tenants table access', () => {
|
|
57
|
+
|
|
58
|
+
it('should verify tenants 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 = 'tenants'
|
|
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 tenants 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 = '_realtime' AND table_name = 'tenants'
|
|
89
|
+
ORDER BY ordinal_position`
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
expect(Array.isArray(columns)).toBe(true);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should verify table has primary key constraint', async () => {
|
|
96
|
+
if (!tableExists) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
db.setContext({ role: 'service_role' });
|
|
101
|
+
|
|
102
|
+
// check for primary key constraint
|
|
103
|
+
const constraints = await db.any(
|
|
104
|
+
`SELECT constraint_name, constraint_type
|
|
105
|
+
FROM information_schema.table_constraints
|
|
106
|
+
WHERE table_schema = '_realtime' AND table_name = 'tenants'
|
|
107
|
+
AND constraint_type = 'PRIMARY KEY'`
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
expect(Array.isArray(constraints)).toBe(true);
|
|
111
|
+
if (constraints.length > 0) {
|
|
112
|
+
expect(constraints[0].constraint_name).toBeDefined();
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should prevent authenticated users from accessing tenants 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
|
+
['realtime-tenants-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 tenants (rls blocks)
|
|
138
|
+
const result = await db.any(
|
|
139
|
+
`SELECT * FROM _realtime.tenants 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 tenants', 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 tenants (rls blocks)
|
|
155
|
+
const result = await db.any(
|
|
156
|
+
`SELECT * FROM _realtime.tenants LIMIT 1`
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
// rls should block access, result should be empty
|
|
160
|
+
expect(result.length).toBe(0);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
@@ -0,0 +1,148 @@
|
|
|
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 schema for testing
|
|
20
|
+
await pg.any(
|
|
21
|
+
`GRANT USAGE ON SCHEMA auth TO public;
|
|
22
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO service_role;
|
|
23
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO authenticated;
|
|
24
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO anon;
|
|
25
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO service_role;
|
|
26
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO authenticated;
|
|
27
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO anon;`,
|
|
28
|
+
[]
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
// assert table exists for this schema
|
|
32
|
+
const exists = await pg.any(
|
|
33
|
+
`SELECT EXISTS (
|
|
34
|
+
SELECT FROM information_schema.tables
|
|
35
|
+
WHERE table_schema = 'auth' AND table_name = 'audit_log_entries'
|
|
36
|
+
) as exists`
|
|
37
|
+
);
|
|
38
|
+
expect(exists[0].exists).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterAll(async () => {
|
|
42
|
+
await teardown();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
beforeEach(async () => {
|
|
46
|
+
await db.beforeEach();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
afterEach(async () => {
|
|
50
|
+
await db.afterEach();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('tutorial: auth audit_log_entries table access', () => {
|
|
54
|
+
|
|
55
|
+
it('should verify audit_log_entries table exists', async () => {
|
|
56
|
+
db.setContext({ role: 'service_role' });
|
|
57
|
+
|
|
58
|
+
const exists = await db.any(
|
|
59
|
+
`SELECT EXISTS (
|
|
60
|
+
SELECT FROM information_schema.tables
|
|
61
|
+
WHERE table_schema = 'auth' AND table_name = 'audit_log_entries'
|
|
62
|
+
) as exists`
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
expect(Array.isArray(exists)).toBe(true);
|
|
66
|
+
expect(exists[0].exists).toBe(true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('should verify service_role can read audit log entries', async () => {
|
|
70
|
+
db.setContext({ role: 'service_role' });
|
|
71
|
+
|
|
72
|
+
const entries = await db.any(
|
|
73
|
+
`SELECT id, instance_id, payload, created_at
|
|
74
|
+
FROM auth.audit_log_entries
|
|
75
|
+
LIMIT 10`
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
expect(Array.isArray(entries)).toBe(true);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('should verify table has expected columns', async () => {
|
|
82
|
+
db.setContext({ role: 'service_role' });
|
|
83
|
+
|
|
84
|
+
const columns = await db.any(
|
|
85
|
+
`SELECT column_name
|
|
86
|
+
FROM information_schema.columns
|
|
87
|
+
WHERE table_schema = 'auth' AND table_name = 'audit_log_entries'`
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
expect(Array.isArray(columns)).toBe(true);
|
|
91
|
+
const names = columns.map((r: any) => r.column_name);
|
|
92
|
+
expect(names).toEqual(expect.arrayContaining(['id', 'instance_id', 'payload', 'created_at']));
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should verify table has primary key on id', async () => {
|
|
96
|
+
db.setContext({ role: 'service_role' });
|
|
97
|
+
|
|
98
|
+
const pk = await db.any(
|
|
99
|
+
`SELECT constraint_name
|
|
100
|
+
FROM information_schema.table_constraints
|
|
101
|
+
WHERE table_schema = 'auth' AND table_name = 'audit_log_entries'
|
|
102
|
+
AND constraint_type = 'PRIMARY KEY'`
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
expect(Array.isArray(pk)).toBe(true);
|
|
106
|
+
if (pk.length > 0) {
|
|
107
|
+
expect(pk[0].constraint_name).toBeDefined();
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('should verify table has index on instance_id', async () => {
|
|
112
|
+
db.setContext({ role: 'service_role' });
|
|
113
|
+
|
|
114
|
+
const indexes = await db.any(
|
|
115
|
+
`SELECT indexname, indexdef
|
|
116
|
+
FROM pg_indexes
|
|
117
|
+
WHERE schemaname = 'auth' AND tablename = 'audit_log_entries'
|
|
118
|
+
AND indexname LIKE '%instance_id%'`
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
expect(Array.isArray(indexes)).toBe(true);
|
|
122
|
+
if (indexes.length > 0) {
|
|
123
|
+
const defs = indexes.map((r: any) => r.indexdef.toLowerCase()).join(' ');
|
|
124
|
+
expect(defs.includes('instance_id')).toBe(true);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should verify anon access to audit logs based on rls', async () => {
|
|
129
|
+
// check rls status
|
|
130
|
+
db.setContext({ role: 'service_role' });
|
|
131
|
+
const rlsStatus = await db.any(
|
|
132
|
+
`SELECT c.relrowsecurity
|
|
133
|
+
FROM pg_class c
|
|
134
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
135
|
+
WHERE n.nspname = 'auth' AND c.relname = 'audit_log_entries'`
|
|
136
|
+
);
|
|
137
|
+
expect(Array.isArray(rlsStatus)).toBe(true);
|
|
138
|
+
expect(rlsStatus.length).toBeGreaterThan(0);
|
|
139
|
+
|
|
140
|
+
db.clearContext();
|
|
141
|
+
const result = await db.any(`SELECT * FROM auth.audit_log_entries LIMIT 1`);
|
|
142
|
+
expect(Array.isArray(result)).toBe(true);
|
|
143
|
+
if (rlsStatus[0].relrowsecurity === true) {
|
|
144
|
+
expect(result.length).toBe(0);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|