@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,137 @@
|
|
|
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 table exists
|
|
34
|
+
const exists = await pg.any(
|
|
35
|
+
`SELECT EXISTS (
|
|
36
|
+
SELECT FROM information_schema.tables
|
|
37
|
+
WHERE table_schema = 'auth' AND table_name = 'sso_domains'
|
|
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 sso_domains table access', () => {
|
|
56
|
+
|
|
57
|
+
it('should verify sso_domains table exists', async () => {
|
|
58
|
+
db.setContext({ role: 'service_role' });
|
|
59
|
+
|
|
60
|
+
// verify table exists in information schema
|
|
61
|
+
const exists = await db.any(
|
|
62
|
+
`SELECT EXISTS (
|
|
63
|
+
SELECT FROM information_schema.tables
|
|
64
|
+
WHERE table_schema = 'auth' AND table_name = 'sso_domains'
|
|
65
|
+
) as exists`
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
expect(Array.isArray(exists)).toBe(true);
|
|
69
|
+
expect(exists[0].exists).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should verify service_role can query sso_domains', async () => {
|
|
73
|
+
db.setContext({ role: 'service_role' });
|
|
74
|
+
|
|
75
|
+
// service_role should be able to query sso_domains
|
|
76
|
+
const domains = await db.any(
|
|
77
|
+
`SELECT id, sso_provider_id, domain, created_at, updated_at
|
|
78
|
+
FROM auth.sso_domains
|
|
79
|
+
LIMIT 10`
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
expect(Array.isArray(domains)).toBe(true);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should verify table has foreign key to sso_providers', async () => {
|
|
86
|
+
db.setContext({ role: 'service_role' });
|
|
87
|
+
|
|
88
|
+
// check for foreign key constraints to sso_providers
|
|
89
|
+
const fks = await db.any(
|
|
90
|
+
`SELECT tc.constraint_name, ccu.table_name AS foreign_table_name
|
|
91
|
+
FROM information_schema.table_constraints AS tc
|
|
92
|
+
JOIN information_schema.constraint_column_usage AS ccu
|
|
93
|
+
ON ccu.constraint_name = tc.constraint_name
|
|
94
|
+
WHERE tc.constraint_type = 'FOREIGN KEY'
|
|
95
|
+
AND tc.table_schema = 'auth'
|
|
96
|
+
AND tc.table_name = 'sso_domains'
|
|
97
|
+
AND ccu.table_name = 'sso_providers'`
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
expect(Array.isArray(fks)).toBe(true);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should verify indexes exist on sso_provider_id and unique lower(domain)', async () => {
|
|
104
|
+
db.setContext({ role: 'service_role' });
|
|
105
|
+
const idx = await db.any(
|
|
106
|
+
`SELECT indexname, indexdef
|
|
107
|
+
FROM pg_indexes
|
|
108
|
+
WHERE schemaname = 'auth' AND tablename = 'sso_domains'`
|
|
109
|
+
);
|
|
110
|
+
expect(Array.isArray(idx)).toBe(true);
|
|
111
|
+
if (idx.length > 0) {
|
|
112
|
+
const defs = idx.map((r: any) => r.indexdef).join(' ');
|
|
113
|
+
expect(defs.toLowerCase().includes('sso_provider_id')).toBe(true);
|
|
114
|
+
expect(defs.toLowerCase().includes('lower(domain)')).toBe(true);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('should verify anon access to sso_domains based on rls', async () => {
|
|
119
|
+
db.setContext({ role: 'service_role' });
|
|
120
|
+
const rlsStatus = await db.any(
|
|
121
|
+
`SELECT c.relrowsecurity
|
|
122
|
+
FROM pg_class c
|
|
123
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
124
|
+
WHERE n.nspname = 'auth' AND c.relname = 'sso_domains'`
|
|
125
|
+
);
|
|
126
|
+
expect(Array.isArray(rlsStatus)).toBe(true);
|
|
127
|
+
expect(rlsStatus.length).toBeGreaterThan(0);
|
|
128
|
+
|
|
129
|
+
db.clearContext();
|
|
130
|
+
const result = await db.any(`SELECT * FROM auth.sso_domains LIMIT 1`);
|
|
131
|
+
expect(Array.isArray(result)).toBe(true);
|
|
132
|
+
if (rlsStatus[0].relrowsecurity === true) {
|
|
133
|
+
expect(result.length).toBe(0);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
@@ -0,0 +1,136 @@
|
|
|
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 table exists
|
|
34
|
+
const exists = await pg.any(
|
|
35
|
+
`SELECT EXISTS (
|
|
36
|
+
SELECT FROM information_schema.tables
|
|
37
|
+
WHERE table_schema = 'auth' AND table_name = 'sso_providers'
|
|
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 sso_providers table access', () => {
|
|
56
|
+
|
|
57
|
+
it('should verify sso_providers table exists', async () => {
|
|
58
|
+
db.setContext({ role: 'service_role' });
|
|
59
|
+
|
|
60
|
+
// verify table exists in information schema
|
|
61
|
+
const exists = await db.any(
|
|
62
|
+
`SELECT EXISTS (
|
|
63
|
+
SELECT FROM information_schema.tables
|
|
64
|
+
WHERE table_schema = 'auth' AND table_name = 'sso_providers'
|
|
65
|
+
) as exists`
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
expect(Array.isArray(exists)).toBe(true);
|
|
69
|
+
expect(exists[0].exists).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should verify service_role can read sso_providers', async () => {
|
|
73
|
+
db.setContext({ role: 'service_role' });
|
|
74
|
+
|
|
75
|
+
// service_role should be able to query sso_providers
|
|
76
|
+
const providers = await db.any(
|
|
77
|
+
`SELECT id, resource_id, created_at, updated_at
|
|
78
|
+
FROM auth.sso_providers
|
|
79
|
+
LIMIT 10`
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
expect(Array.isArray(providers)).toBe(true);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should verify table has primary key on id', async () => {
|
|
86
|
+
db.setContext({ role: 'service_role' });
|
|
87
|
+
|
|
88
|
+
// check for primary key constraint
|
|
89
|
+
const pk = await db.any(
|
|
90
|
+
`SELECT constraint_name
|
|
91
|
+
FROM information_schema.table_constraints
|
|
92
|
+
WHERE table_schema = 'auth' AND table_name = 'sso_providers'
|
|
93
|
+
AND constraint_type = 'PRIMARY KEY'`
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
expect(Array.isArray(pk)).toBe(true);
|
|
97
|
+
if (pk.length > 0) {
|
|
98
|
+
expect(pk[0].constraint_name).toBeDefined();
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('should verify unique index on lower(resource_id) exists', async () => {
|
|
103
|
+
db.setContext({ role: 'service_role' });
|
|
104
|
+
// check for functional unique index on lower(resource_id)
|
|
105
|
+
const idx = await db.any(
|
|
106
|
+
`SELECT indexname, indexdef
|
|
107
|
+
FROM pg_indexes
|
|
108
|
+
WHERE schemaname = 'auth' AND tablename = 'sso_providers'
|
|
109
|
+
AND indexdef ILIKE '%lower(resource_id)%'`
|
|
110
|
+
);
|
|
111
|
+
expect(Array.isArray(idx)).toBe(true);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should verify anon access to sso_providers based on rls', async () => {
|
|
115
|
+
// check rls status
|
|
116
|
+
db.setContext({ role: 'service_role' });
|
|
117
|
+
const rlsStatus = await db.any(
|
|
118
|
+
`SELECT c.relrowsecurity
|
|
119
|
+
FROM pg_class c
|
|
120
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
121
|
+
WHERE n.nspname = 'auth' AND c.relname = 'sso_providers'`
|
|
122
|
+
);
|
|
123
|
+
expect(Array.isArray(rlsStatus)).toBe(true);
|
|
124
|
+
expect(rlsStatus.length).toBeGreaterThan(0);
|
|
125
|
+
|
|
126
|
+
// clear context to anon role
|
|
127
|
+
db.clearContext();
|
|
128
|
+
|
|
129
|
+
const result = await db.any(`SELECT * FROM auth.sso_providers LIMIT 1`);
|
|
130
|
+
expect(Array.isArray(result)).toBe(true);
|
|
131
|
+
if (rlsStatus[0].relrowsecurity === true) {
|
|
132
|
+
expect(result.length).toBe(0);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
@@ -0,0 +1,314 @@
|
|
|
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
|
+
|