@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.
Files changed (69) hide show
  1. package/LICENSE +2 -1
  2. package/Makefile +2 -3
  3. package/README.md +45 -59
  4. package/deploy/roles/anon.sql +15 -0
  5. package/deploy/schemas/supabase_integration/procedures/pre_request.sql +26 -0
  6. package/deploy/schemas/supabase_integration/schema.sql +9 -0
  7. package/package.json +27 -12
  8. package/pgpm-supabase.control +7 -0
  9. package/pgpm.plan +6 -3
  10. package/revert/roles/anon.sql +7 -0
  11. package/revert/schemas/supabase_integration/procedures/pre_request.sql +7 -0
  12. package/revert/schemas/supabase_integration/schema.sql +7 -0
  13. package/sql/pgpm-supabase--1.0.0.bundle.tar.gz +0 -0
  14. package/sql/pgpm-supabase--1.0.0.sql +32 -0
  15. package/verify/roles/anon.sql +12 -0
  16. package/verify/schemas/supabase_integration/procedures/pre_request.sql +19 -0
  17. package/verify/schemas/supabase_integration/schema.sql +18 -0
  18. package/__tests__/_realtime/_realtime-extensions.test.ts +0 -129
  19. package/__tests__/_realtime/_realtime-schema-migrations.test.ts +0 -160
  20. package/__tests__/_realtime/_realtime-tenants.test.ts +0 -163
  21. package/__tests__/auth/auth.audit-log-entries.test.ts +0 -148
  22. package/__tests__/auth/auth.flow-state.test.ts +0 -114
  23. package/__tests__/auth/auth.identities.test.ts +0 -149
  24. package/__tests__/auth/auth.instances.test.ts +0 -134
  25. package/__tests__/auth/auth.mfa-amr-claims.test.ts +0 -142
  26. package/__tests__/auth/auth.mfa-challenges.test.ts +0 -117
  27. package/__tests__/auth/auth.mfa-factors.test.ts +0 -118
  28. package/__tests__/auth/auth.oauth-authorizations.test.ts +0 -123
  29. package/__tests__/auth/auth.oauth-clients.test.ts +0 -120
  30. package/__tests__/auth/auth.oauth-consents.test.ts +0 -122
  31. package/__tests__/auth/auth.one-time-tokens.test.ts +0 -194
  32. package/__tests__/auth/auth.refresh-tokens.test.ts +0 -134
  33. package/__tests__/auth/auth.saml-providers.test.ts +0 -154
  34. package/__tests__/auth/auth.saml-relay-states.test.ts +0 -145
  35. package/__tests__/auth/auth.schema-migrations.test.ts +0 -131
  36. package/__tests__/auth/auth.sessions.test.ts +0 -140
  37. package/__tests__/auth/auth.sso-domains.test.ts +0 -137
  38. package/__tests__/auth/auth.sso-providers.test.ts +0 -136
  39. package/__tests__/auth/auth.users.test.ts +0 -314
  40. package/__tests__/conditional.test.ts +0 -229
  41. package/__tests__/functions.test.ts +0 -252
  42. package/__tests__/jsonb.test.ts +0 -237
  43. package/__tests__/net/net.http-request-queue.test.ts +0 -159
  44. package/__tests__/net/net.http-response.test.ts +0 -163
  45. package/__tests__/realtime/realtime.messages-2025-10-31.test.ts +0 -168
  46. package/__tests__/realtime/realtime.messages.test.ts +0 -162
  47. package/__tests__/realtime/realtime.schema-migrations.test.ts +0 -139
  48. package/__tests__/realtime/realtime.subscription.test.ts +0 -159
  49. package/__tests__/storage/storage.buckets-analytics.test.ts +0 -134
  50. package/__tests__/storage/storage.buckets.test.ts +0 -146
  51. package/__tests__/storage/storage.iceberg-namespaces.test.ts +0 -132
  52. package/__tests__/storage/storage.iceberg-tables.test.ts +0 -142
  53. package/__tests__/storage/storage.migrations.test.ts +0 -133
  54. package/__tests__/storage/storage.objects.test.ts +0 -160
  55. package/__tests__/storage/storage.prefixes.test.ts +0 -138
  56. package/__tests__/storage/storage.s3-multipart-uploads-parts.test.ts +0 -133
  57. package/__tests__/storage/storage.s3-multipart-uploads.test.ts +0 -128
  58. package/__tests__/supabase-functions/supabase.functions-hooks.test.ts +0 -125
  59. package/__tests__/supabase-functions/supabase.functions-migrations.test.ts +0 -213
  60. package/__tests__/temporal.test.ts +0 -225
  61. package/__tests__/vault-secrets.test.ts +0 -145
  62. package/deploy/LICENSE +0 -9
  63. package/deploy/supabase.sql +0 -2271
  64. package/jest.config.js +0 -15
  65. package/revert/supabase.sql +0 -3
  66. package/sql/supabase--0.0.1.sql +0 -1368
  67. package/src/index.ts +0 -3
  68. package/supabase.control +0 -7
  69. package/verify/supabase.sql +0 -3
@@ -1,122 +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
- // 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
32
- const exists = await pg.any(
33
- `SELECT EXISTS (
34
- SELECT FROM information_schema.tables
35
- WHERE table_schema = 'auth' AND table_name = 'oauth_consents'
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 oauth_consents table access', () => {
54
-
55
- it('should verify oauth_consents table exists', async () => {
56
- db.setContext({ role: 'service_role' });
57
-
58
- // verify table exists in information schema
59
- const exists = await db.any(
60
- `SELECT EXISTS (
61
- SELECT FROM information_schema.tables
62
- WHERE table_schema = 'auth' AND table_name = 'oauth_consents'
63
- ) as exists`
64
- );
65
-
66
- expect(Array.isArray(exists)).toBe(true);
67
- expect(exists[0].exists).toBe(true);
68
- });
69
-
70
- it('should verify service_role can query oauth_consents', async () => {
71
- db.setContext({ role: 'service_role' });
72
-
73
- // service_role should be able to query oauth_consents
74
- const consents = await db.any(
75
- `SELECT id, user_id, client_id, scopes
76
- FROM auth.oauth_consents
77
- LIMIT 10`
78
- );
79
-
80
- expect(Array.isArray(consents)).toBe(true);
81
- });
82
-
83
- it('should verify foreign keys to users and oauth_clients', async () => {
84
- db.setContext({ role: 'service_role' });
85
-
86
- const fks = await db.any(
87
- `SELECT tc.constraint_name, ccu.table_name AS foreign_table_name
88
- FROM information_schema.table_constraints AS tc
89
- JOIN information_schema.constraint_column_usage AS ccu
90
- ON ccu.constraint_name = tc.constraint_name AND ccu.table_schema = tc.table_schema
91
- WHERE tc.constraint_type = 'FOREIGN KEY'
92
- AND tc.table_schema = 'auth'
93
- AND tc.table_name = 'oauth_consents'`
94
- );
95
- expect(Array.isArray(fks)).toBe(true);
96
- if (fks.length > 0) {
97
- const tables = new Set(fks.map((r: any) => r.foreign_table_name));
98
- expect(tables.has('users')).toBe(true);
99
- expect(tables.has('oauth_clients')).toBe(true);
100
- }
101
- });
102
-
103
- it('should verify anon access to oauth_consents based on rls', async () => {
104
- db.setContext({ role: 'service_role' });
105
- const rlsStatus = await db.any(
106
- `SELECT c.relrowsecurity
107
- FROM pg_class c
108
- JOIN pg_namespace n ON n.oid = c.relnamespace
109
- WHERE n.nspname = 'auth' AND c.relname = 'oauth_consents'`
110
- );
111
- expect(Array.isArray(rlsStatus)).toBe(true);
112
- expect(rlsStatus.length).toBeGreaterThan(0);
113
-
114
- db.clearContext();
115
- const result = await db.any(`SELECT * FROM auth.oauth_consents LIMIT 1`);
116
- expect(Array.isArray(result)).toBe(true);
117
- if (rlsStatus[0].relrowsecurity === true) {
118
- expect(result.length).toBe(0);
119
- }
120
- });
121
- });
122
-
@@ -1,194 +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.one_time_tokens exists
34
- const exists = await pg.any(
35
- `SELECT EXISTS (
36
- SELECT FROM information_schema.tables
37
- WHERE table_schema = 'auth' AND table_name = 'one_time_tokens'
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 one_time_tokens table access', () => {
56
-
57
- it('should verify one_time_tokens 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 = 'one_time_tokens'
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 one_time_tokens', async () => {
73
- db.setContext({ role: 'service_role' });
74
-
75
- const rows = await db.any(
76
- `SELECT id, user_id, token_type, token_hash, relates_to, created_at, updated_at
77
- FROM auth.one_time_tokens
78
- LIMIT 10`
79
- );
80
- expect(Array.isArray(rows)).toBe(true);
81
- });
82
-
83
- it('should verify table has indexes on token_hash and relates_to', async () => {
84
- db.setContext({ role: 'service_role' });
85
-
86
- // check for indexes on the table
87
- const indexes = await db.any(
88
- `SELECT indexname, indexdef
89
- FROM pg_indexes
90
- WHERE schemaname = 'auth' AND tablename = 'one_time_tokens'`
91
- );
92
-
93
- expect(Array.isArray(indexes)).toBe(true);
94
- if (indexes.length > 0) {
95
- const defs = indexes.map((r: any) => r.indexdef.toLowerCase()).join(' ');
96
- expect(defs.includes('token_hash')).toBe(true);
97
- expect(defs.includes('relates_to')).toBe(true);
98
- }
99
- });
100
-
101
- it('should verify table has primary key on id', async () => {
102
- db.setContext({ role: 'service_role' });
103
- const pk = await db.any(
104
- `SELECT constraint_name
105
- FROM information_schema.table_constraints
106
- WHERE table_schema = 'auth' AND table_name = 'one_time_tokens'
107
- AND constraint_type = 'PRIMARY KEY'`
108
- );
109
- expect(Array.isArray(pk)).toBe(true);
110
- if (pk.length > 0) {
111
- expect(pk[0].constraint_name).toBeDefined();
112
- }
113
- });
114
-
115
- it('should verify foreign key to auth.users on user_id', async () => {
116
- db.setContext({ role: 'service_role' });
117
- const fks = await db.any(
118
- `SELECT tc.constraint_name, ccu.table_name AS foreign_table_name, ccu.column_name
119
- FROM information_schema.table_constraints AS tc
120
- JOIN information_schema.constraint_column_usage AS ccu
121
- ON ccu.constraint_name = tc.constraint_name
122
- WHERE tc.constraint_type = 'FOREIGN KEY'
123
- AND tc.table_schema = 'auth'
124
- AND tc.table_name = 'one_time_tokens'`
125
- );
126
- expect(Array.isArray(fks)).toBe(true);
127
- if (fks.length > 0) {
128
- const foreignTables = fks.map((r: any) => r.foreign_table_name);
129
- expect(foreignTables.includes('users')).toBe(true);
130
- }
131
- });
132
-
133
- it('should verify unique constraint on (user_id, token_type)', async () => {
134
- db.setContext({ role: 'service_role' });
135
- const uniques = await db.any(
136
- `SELECT indexname, indexdef
137
- FROM pg_indexes
138
- WHERE schemaname = 'auth' AND tablename = 'one_time_tokens'`
139
- );
140
- expect(Array.isArray(uniques)).toBe(true);
141
- if (uniques.length > 0) {
142
- const defs = uniques.map((r: any) => r.indexdef.toLowerCase()).join(' ');
143
- expect(defs.includes('(user_id, token_type)')).toBe(true);
144
- }
145
- });
146
-
147
- it('should verify enum type one_time_token_type exists with values', async () => {
148
- db.setContext({ role: 'service_role' });
149
- const typeExists = await db.any(
150
- `SELECT EXISTS (
151
- SELECT 1 FROM pg_type WHERE typname = 'one_time_token_type'
152
- ) AS exists`
153
- );
154
- expect(typeExists[0].exists).toBe(true);
155
- const labels = await db.any(
156
- `SELECT e.enumlabel
157
- FROM pg_type t
158
- JOIN pg_enum e ON t.oid = e.enumtypid
159
- WHERE t.typname = 'one_time_token_type'`
160
- );
161
- const vals = labels.map((r: any) => r.enumlabel);
162
- expect(vals).toEqual(expect.arrayContaining([
163
- 'confirmation_token',
164
- 'reauthentication_token',
165
- 'recovery_token',
166
- 'email_change_token_new',
167
- 'email_change_token_current',
168
- 'phone_change_token'
169
- ]));
170
- });
171
-
172
- it('should verify anon access to one_time_tokens based on rls', async () => {
173
- // check rls status
174
- db.setContext({ role: 'service_role' });
175
- const rlsStatus = await db.any(
176
- `SELECT c.relrowsecurity
177
- FROM pg_class c
178
- JOIN pg_namespace n ON n.oid = c.relnamespace
179
- WHERE n.nspname = 'auth' AND c.relname = 'one_time_tokens'`
180
- );
181
- expect(Array.isArray(rlsStatus)).toBe(true);
182
- expect(rlsStatus.length).toBeGreaterThan(0);
183
-
184
- // clear context to anon role
185
- db.clearContext();
186
-
187
- const result = await db.any(`SELECT * FROM auth.one_time_tokens LIMIT 1`);
188
- expect(Array.isArray(result)).toBe(true);
189
- if (rlsStatus[0].relrowsecurity === true) {
190
- expect(result.length).toBe(0);
191
- }
192
- });
193
- });
194
-
@@ -1,134 +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
- // 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 refresh_tokens exists
32
- const exists = await pg.any(
33
- `SELECT EXISTS (
34
- SELECT FROM information_schema.tables
35
- WHERE table_schema = 'auth' AND table_name = 'refresh_tokens'
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 refresh_tokens table access', () => {
54
-
55
- it('should verify refresh_tokens 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 = 'refresh_tokens'
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 refresh_tokens', async () => {
70
- db.setContext({ role: 'service_role' });
71
-
72
- const tokens = await db.any(
73
- `SELECT id, token, user_id, revoked, created_at, updated_at, instance_id, session_id
74
- FROM auth.refresh_tokens
75
- LIMIT 10`
76
- );
77
-
78
- expect(Array.isArray(tokens)).toBe(true);
79
- });
80
-
81
- it('should verify table has primary key on id', async () => {
82
- db.setContext({ role: 'service_role' });
83
-
84
- const pk = await db.any(
85
- `SELECT constraint_name
86
- FROM information_schema.table_constraints
87
- WHERE table_schema = 'auth' AND table_name = 'refresh_tokens'
88
- AND constraint_type = 'PRIMARY KEY'`
89
- );
90
-
91
- expect(Array.isArray(pk)).toBe(true);
92
- if (pk.length > 0) {
93
- expect(pk[0].constraint_name).toBeDefined();
94
- }
95
- });
96
-
97
- it('should verify table has indexes on instance_id and token', async () => {
98
- db.setContext({ role: 'service_role' });
99
-
100
- const indexes = await db.any(
101
- `SELECT indexname
102
- FROM pg_indexes
103
- WHERE schemaname = 'auth' AND tablename = 'refresh_tokens'
104
- AND (indexname LIKE '%instance_id%' OR indexname LIKE '%token%')`
105
- );
106
-
107
- expect(Array.isArray(indexes)).toBe(true);
108
- if (indexes.length > 0) {
109
- const names = indexes.map((r: any) => r.indexname).join(' ');
110
- expect(names.includes('instance_id') || names.includes('token')).toBe(true);
111
- }
112
- });
113
-
114
- it('should verify anon access to refresh_tokens 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 = 'refresh_tokens'`
122
- );
123
- expect(Array.isArray(rlsStatus)).toBe(true);
124
- expect(rlsStatus.length).toBeGreaterThan(0);
125
-
126
- db.clearContext();
127
- const result = await db.any(`SELECT * FROM auth.refresh_tokens LIMIT 1`);
128
- expect(Array.isArray(result)).toBe(true);
129
- if (rlsStatus[0].relrowsecurity === true) {
130
- expect(result.length).toBe(0);
131
- }
132
- });
133
- });
134
-
@@ -1,154 +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 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 = 'saml_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 saml_providers table access', () => {
56
-
57
- it('should verify saml_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 = 'saml_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 query saml_providers', async () => {
73
- db.setContext({ role: 'service_role' });
74
-
75
- // service_role should be able to query saml_providers
76
- const providers = await db.any(
77
- `SELECT id, sso_provider_id, entity_id, metadata_xml, metadata_url, attribute_mapping, created_at, updated_at
78
- FROM auth.saml_providers
79
- LIMIT 10`
80
- );
81
-
82
- expect(Array.isArray(providers)).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 = 'saml_providers'
97
- AND ccu.table_name = 'sso_providers'`
98
- );
99
-
100
- expect(Array.isArray(fks)).toBe(true);
101
- });
102
-
103
- it('should verify unique constraint on entity_id', async () => {
104
- db.setContext({ role: 'service_role' });
105
- const uniques = await db.any(
106
- `SELECT tc.constraint_name, ccu.column_name
107
- FROM information_schema.table_constraints tc
108
- JOIN information_schema.constraint_column_usage ccu
109
- ON ccu.constraint_name = tc.constraint_name
110
- WHERE tc.table_schema = 'auth'
111
- AND tc.table_name = 'saml_providers'
112
- AND tc.constraint_type = 'UNIQUE'`
113
- );
114
- expect(Array.isArray(uniques)).toBe(true);
115
- if (uniques.length > 0) {
116
- const cols = uniques.map((r: any) => r.column_name);
117
- expect(cols.includes('entity_id')).toBe(true);
118
- }
119
- });
120
-
121
- it('should verify index exists on sso_provider_id', async () => {
122
- db.setContext({ role: 'service_role' });
123
- const idx = await db.any(
124
- `SELECT indexname, indexdef
125
- FROM pg_indexes
126
- WHERE schemaname = 'auth' AND tablename = 'saml_providers'`
127
- );
128
- expect(Array.isArray(idx)).toBe(true);
129
- if (idx.length > 0) {
130
- const defs = idx.map((r: any) => r.indexdef).join(' ');
131
- expect(defs.toLowerCase().includes('sso_provider_id')).toBe(true);
132
- }
133
- });
134
-
135
- it('should verify anon access to saml_providers based on rls', async () => {
136
- db.setContext({ role: 'service_role' });
137
- const rlsStatus = await db.any(
138
- `SELECT c.relrowsecurity
139
- FROM pg_class c
140
- JOIN pg_namespace n ON n.oid = c.relnamespace
141
- WHERE n.nspname = 'auth' AND c.relname = 'saml_providers'`
142
- );
143
- expect(Array.isArray(rlsStatus)).toBe(true);
144
- expect(rlsStatus.length).toBeGreaterThan(0);
145
-
146
- db.clearContext();
147
- const result = await db.any(`SELECT * FROM auth.saml_providers LIMIT 1`);
148
- expect(Array.isArray(result)).toBe(true);
149
- if (rlsStatus[0].relrowsecurity === true) {
150
- expect(result.length).toBe(0);
151
- }
152
- });
153
- });
154
-