@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.
Files changed (57) hide show
  1. package/LICENSE +21 -0
  2. package/Makefile +7 -0
  3. package/README.md +37 -0
  4. package/__tests__/_realtime/_realtime-extensions.test.ts +129 -0
  5. package/__tests__/_realtime/_realtime-schema-migrations.test.ts +160 -0
  6. package/__tests__/_realtime/_realtime-tenants.test.ts +163 -0
  7. package/__tests__/auth/auth.audit-log-entries.test.ts +148 -0
  8. package/__tests__/auth/auth.flow-state.test.ts +114 -0
  9. package/__tests__/auth/auth.identities.test.ts +149 -0
  10. package/__tests__/auth/auth.instances.test.ts +134 -0
  11. package/__tests__/auth/auth.mfa-amr-claims.test.ts +142 -0
  12. package/__tests__/auth/auth.mfa-challenges.test.ts +117 -0
  13. package/__tests__/auth/auth.mfa-factors.test.ts +118 -0
  14. package/__tests__/auth/auth.oauth-authorizations.test.ts +123 -0
  15. package/__tests__/auth/auth.oauth-clients.test.ts +120 -0
  16. package/__tests__/auth/auth.oauth-consents.test.ts +122 -0
  17. package/__tests__/auth/auth.one-time-tokens.test.ts +194 -0
  18. package/__tests__/auth/auth.refresh-tokens.test.ts +134 -0
  19. package/__tests__/auth/auth.saml-providers.test.ts +154 -0
  20. package/__tests__/auth/auth.saml-relay-states.test.ts +145 -0
  21. package/__tests__/auth/auth.schema-migrations.test.ts +131 -0
  22. package/__tests__/auth/auth.sessions.test.ts +140 -0
  23. package/__tests__/auth/auth.sso-domains.test.ts +137 -0
  24. package/__tests__/auth/auth.sso-providers.test.ts +136 -0
  25. package/__tests__/auth/auth.users.test.ts +314 -0
  26. package/__tests__/conditional.test.ts +229 -0
  27. package/__tests__/functions.test.ts +252 -0
  28. package/__tests__/jsonb.test.ts +237 -0
  29. package/__tests__/net/net.http-request-queue.test.ts +159 -0
  30. package/__tests__/net/net.http-response.test.ts +163 -0
  31. package/__tests__/realtime/realtime.messages-2025-10-31.test.ts +168 -0
  32. package/__tests__/realtime/realtime.messages.test.ts +162 -0
  33. package/__tests__/realtime/realtime.schema-migrations.test.ts +139 -0
  34. package/__tests__/realtime/realtime.subscription.test.ts +159 -0
  35. package/__tests__/storage/storage.buckets-analytics.test.ts +134 -0
  36. package/__tests__/storage/storage.buckets.test.ts +146 -0
  37. package/__tests__/storage/storage.iceberg-namespaces.test.ts +132 -0
  38. package/__tests__/storage/storage.iceberg-tables.test.ts +142 -0
  39. package/__tests__/storage/storage.migrations.test.ts +133 -0
  40. package/__tests__/storage/storage.objects.test.ts +160 -0
  41. package/__tests__/storage/storage.prefixes.test.ts +138 -0
  42. package/__tests__/storage/storage.s3-multipart-uploads-parts.test.ts +133 -0
  43. package/__tests__/storage/storage.s3-multipart-uploads.test.ts +128 -0
  44. package/__tests__/supabase-functions/supabase.functions-hooks.test.ts +125 -0
  45. package/__tests__/supabase-functions/supabase.functions-migrations.test.ts +213 -0
  46. package/__tests__/temporal.test.ts +225 -0
  47. package/__tests__/vault-secrets.test.ts +145 -0
  48. package/deploy/LICENSE +9 -0
  49. package/deploy/supabase.sql +2271 -0
  50. package/jest.config.js +15 -0
  51. package/launchql.plan +4 -0
  52. package/package.json +23 -0
  53. package/revert/supabase.sql +3 -0
  54. package/sql/supabase--0.0.1.sql +1368 -0
  55. package/src/index.ts +3 -0
  56. package/supabase.control +7 -0
  57. package/verify/supabase.sql +3 -0
@@ -0,0 +1,114 @@
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 auth.flow_state table exists (fail fast, no hiding)
32
+ const flowStateExists = await pg.any(
33
+ `SELECT EXISTS (
34
+ SELECT FROM information_schema.tables
35
+ WHERE table_schema = 'auth' AND table_name = 'flow_state'
36
+ ) as exists`
37
+ );
38
+ expect(flowStateExists[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 flow_state table access', () => {
54
+
55
+ it('should verify flow_state table exists', async () => {
56
+ db.setContext({ role: 'service_role' });
57
+ const exists = await db.any(
58
+ `SELECT EXISTS (
59
+ SELECT FROM information_schema.tables
60
+ WHERE table_schema = 'auth' AND table_name = 'flow_state'
61
+ ) as exists`
62
+ );
63
+ expect(Array.isArray(exists)).toBe(true);
64
+ expect(exists[0].exists).toBe(true);
65
+ });
66
+
67
+ it('should verify service_role can query flow_state structure', async () => {
68
+ db.setContext({ role: 'service_role' });
69
+ const columns = await db.any(
70
+ `SELECT column_name, data_type
71
+ FROM information_schema.columns
72
+ WHERE table_schema = 'auth' AND table_name = 'flow_state'
73
+ ORDER BY ordinal_position`
74
+ );
75
+ expect(Array.isArray(columns)).toBe(true);
76
+ expect(columns.length).toBeGreaterThan(0);
77
+ });
78
+
79
+ it('should verify table presence via pg_class', async () => {
80
+ db.setContext({ role: 'service_role' });
81
+ const tableInfo = await db.any(
82
+ `SELECT c.relname, n.nspname
83
+ FROM pg_class c
84
+ JOIN pg_namespace n ON n.oid = c.relnamespace
85
+ WHERE n.nspname = 'auth' AND c.relname = 'flow_state'`
86
+ );
87
+ expect(Array.isArray(tableInfo)).toBe(true);
88
+ expect(tableInfo.length).toBeGreaterThan(0);
89
+ expect(tableInfo[0].relname).toBe('flow_state');
90
+ expect(tableInfo[0].nspname).toBe('auth');
91
+ });
92
+
93
+ it('should verify anon access to flow_state based on rls', async () => {
94
+ // find rls status
95
+ db.setContext({ role: 'service_role' });
96
+ const rlsStatus = await db.any(
97
+ `SELECT c.relrowsecurity
98
+ FROM pg_class c
99
+ JOIN pg_namespace n ON n.oid = c.relnamespace
100
+ WHERE n.nspname = 'auth' AND c.relname = 'flow_state'`
101
+ );
102
+ expect(Array.isArray(rlsStatus)).toBe(true);
103
+ expect(rlsStatus.length).toBeGreaterThan(0);
104
+
105
+ // query as anon
106
+ db.clearContext();
107
+ const result = await db.any(`SELECT * FROM auth.flow_state LIMIT 1`);
108
+ expect(Array.isArray(result)).toBe(true);
109
+ if (rlsStatus[0].relrowsecurity === true) {
110
+ expect(result.length).toBe(0);
111
+ }
112
+ });
113
+ });
114
+
@@ -0,0 +1,149 @@
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 identities table exists now (fail fast)
32
+ const exists = await pg.any(
33
+ `SELECT EXISTS (
34
+ SELECT FROM information_schema.tables
35
+ WHERE table_schema = 'auth' AND table_name = 'identities'
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 identities table access', () => {
54
+
55
+ it('should verify identities table exists', async () => {
56
+ db.setContext({ role: 'service_role' });
57
+ const exists = await db.any(
58
+ `SELECT EXISTS (
59
+ SELECT FROM information_schema.tables
60
+ WHERE table_schema = 'auth' AND table_name = 'identities'
61
+ ) as exists`
62
+ );
63
+ expect(Array.isArray(exists)).toBe(true);
64
+ expect(exists[0].exists).toBe(true);
65
+ });
66
+
67
+ it('should verify service_role can query identities table structure', async () => {
68
+ db.setContext({ role: 'service_role' });
69
+ const columns = await db.any(
70
+ `SELECT column_name, data_type
71
+ FROM information_schema.columns
72
+ WHERE table_schema = 'auth' AND table_name = 'identities'
73
+ ORDER BY ordinal_position`
74
+ );
75
+ expect(Array.isArray(columns)).toBe(true);
76
+ expect(columns.length).toBeGreaterThan(0);
77
+ const names = columns.map((c: any) => c.column_name);
78
+ expect(names).toContain('id');
79
+ expect(names).toContain('user_id');
80
+ expect(names).toContain('identity_data');
81
+ expect(names).toContain('provider');
82
+ });
83
+
84
+ it('should verify service_role can read identities', async () => {
85
+ db.setContext({ role: 'service_role' });
86
+ const identities = await db.any(`SELECT * FROM auth.identities LIMIT 10`);
87
+ expect(Array.isArray(identities)).toBe(true);
88
+ });
89
+
90
+ it('should verify foreign key to auth.users', async () => {
91
+ db.setContext({ role: 'service_role' });
92
+ const fks = await db.any(
93
+ `SELECT tc.constraint_name, ccu.table_name AS foreign_table_name
94
+ FROM information_schema.table_constraints AS tc
95
+ JOIN information_schema.constraint_column_usage AS ccu
96
+ ON ccu.constraint_name = tc.constraint_name AND ccu.table_schema = tc.table_schema
97
+ WHERE tc.constraint_type = 'FOREIGN KEY'
98
+ AND tc.table_schema = 'auth'
99
+ AND tc.table_name = 'identities'`
100
+ );
101
+ expect(Array.isArray(fks)).toBe(true);
102
+ if (fks.length > 0) {
103
+ const hasUsersFk = fks.some((r: any) => r.foreign_table_name === 'users');
104
+ expect(hasUsersFk).toBe(true);
105
+ }
106
+ });
107
+
108
+ it('should verify composite primary key (provider, id)', async () => {
109
+ db.setContext({ role: 'service_role' });
110
+ const pkCols = await db.any(
111
+ `SELECT kcu.column_name
112
+ FROM information_schema.table_constraints tc
113
+ JOIN information_schema.key_column_usage kcu
114
+ ON tc.constraint_name = kcu.constraint_name
115
+ AND tc.table_schema = kcu.table_schema
116
+ WHERE tc.table_schema = 'auth'
117
+ AND tc.table_name = 'identities'
118
+ AND tc.constraint_type = 'PRIMARY KEY'
119
+ ORDER BY kcu.ordinal_position`
120
+ );
121
+ expect(Array.isArray(pkCols)).toBe(true);
122
+ if (pkCols.length > 0) {
123
+ const cols = pkCols.map((r: any) => r.column_name);
124
+ expect(cols).toEqual(['provider', 'id']);
125
+ }
126
+ });
127
+
128
+ it('should verify anon access to identities based on rls', async () => {
129
+ // get 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 = 'identities'`
136
+ );
137
+ expect(Array.isArray(rlsStatus)).toBe(true);
138
+ expect(rlsStatus.length).toBeGreaterThan(0);
139
+
140
+ // query as anon
141
+ db.clearContext();
142
+ const result = await db.any(`SELECT * FROM auth.identities LIMIT 1`);
143
+ expect(Array.isArray(result)).toBe(true);
144
+ if (rlsStatus[0].relrowsecurity === true) {
145
+ expect(result.length).toBe(0);
146
+ }
147
+ });
148
+ });
149
+
@@ -0,0 +1,134 @@
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 auth.instances exists (fail fast)
32
+ const exists = await pg.any(
33
+ `SELECT EXISTS (
34
+ SELECT FROM information_schema.tables
35
+ WHERE table_schema = 'auth' AND table_name = 'instances'
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 instances table access', () => {
54
+
55
+ it('should verify instances 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 = 'instances'
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 instances', async () => {
70
+ db.setContext({ role: 'service_role' });
71
+
72
+ const instances = await db.any(
73
+ `SELECT id, uuid, created_at, updated_at
74
+ FROM auth.instances
75
+ LIMIT 10`
76
+ );
77
+
78
+ expect(Array.isArray(instances)).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 = 'instances'
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 columns match schema', async () => {
98
+ db.setContext({ role: 'service_role' });
99
+
100
+ const columns = await db.any(
101
+ `SELECT column_name
102
+ FROM information_schema.columns
103
+ WHERE table_schema = 'auth' AND table_name = 'instances'
104
+ AND column_name IN ('id', 'uuid', 'raw_base_config', 'created_at', 'updated_at')
105
+ ORDER BY column_name`
106
+ );
107
+
108
+ expect(Array.isArray(columns)).toBe(true);
109
+ const names = columns.map((c: any) => c.column_name);
110
+ expect(names).toEqual(['created_at','id','raw_base_config','updated_at','uuid']);
111
+ });
112
+
113
+ it('should verify anon access to instances based on rls', async () => {
114
+ // check rls status for instances
115
+ db.setContext({ role: 'service_role' });
116
+ const rlsStatus = await db.any(
117
+ `SELECT c.relrowsecurity
118
+ FROM pg_class c
119
+ JOIN pg_namespace n ON n.oid = c.relnamespace
120
+ WHERE n.nspname = 'auth' AND c.relname = 'instances'`
121
+ );
122
+ expect(Array.isArray(rlsStatus)).toBe(true);
123
+ expect(rlsStatus.length).toBeGreaterThan(0);
124
+
125
+ // query as anon
126
+ db.clearContext();
127
+ const result = await db.any(`SELECT * FROM auth.instances 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
+
@@ -0,0 +1,142 @@
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
+ // grants
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 exists
32
+ const exists = await pg.any(
33
+ `SELECT EXISTS (
34
+ SELECT FROM information_schema.tables
35
+ WHERE table_schema = 'auth' AND table_name = 'mfa_amr_claims'
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 mfa_amr_claims table access', () => {
54
+
55
+ it('should verify mfa_amr_claims 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 = 'mfa_amr_claims'
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 mfa_amr_claims structure', async () => {
71
+ db.setContext({ role: 'service_role' });
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 = 'mfa_amr_claims'
76
+ ORDER BY ordinal_position`
77
+ );
78
+ expect(Array.isArray(columns)).toBe(true);
79
+ expect(columns.length).toBeGreaterThan(0);
80
+ const names = columns.map((c: any) => c.column_name);
81
+ expect(names).toContain('session_id');
82
+ expect(names).toContain('authentication_method');
83
+ });
84
+
85
+ it('should verify foreign key to auth.sessions', async () => {
86
+ db.setContext({ role: 'service_role' });
87
+ const fks = await db.any(
88
+ `SELECT tc.constraint_name, ccu.table_name AS foreign_table_name
89
+ FROM information_schema.table_constraints AS tc
90
+ JOIN information_schema.constraint_column_usage AS ccu
91
+ ON ccu.constraint_name = tc.constraint_name AND ccu.table_schema = tc.table_schema
92
+ WHERE tc.constraint_type = 'FOREIGN KEY'
93
+ AND tc.table_schema = 'auth'
94
+ AND tc.table_name = 'mfa_amr_claims'`
95
+ );
96
+ expect(Array.isArray(fks)).toBe(true);
97
+ if (fks.length > 0) {
98
+ const hasSessionsFk = fks.some((r: any) => r.foreign_table_name === 'sessions');
99
+ expect(hasSessionsFk).toBe(true);
100
+ }
101
+ });
102
+
103
+ it('should verify unique composite key (session_id, authentication_method)', async () => {
104
+ db.setContext({ role: 'service_role' });
105
+ const uniqueCols = await db.any(
106
+ `SELECT kcu.column_name
107
+ FROM information_schema.table_constraints tc
108
+ JOIN information_schema.key_column_usage kcu
109
+ ON tc.constraint_name = kcu.constraint_name
110
+ AND tc.table_schema = kcu.table_schema
111
+ WHERE tc.table_schema = 'auth'
112
+ AND tc.table_name = 'mfa_amr_claims'
113
+ AND tc.constraint_type = 'UNIQUE'
114
+ ORDER BY kcu.ordinal_position`
115
+ );
116
+ expect(Array.isArray(uniqueCols)).toBe(true);
117
+ if (uniqueCols.length > 0) {
118
+ const cols = uniqueCols.map((r: any) => r.column_name);
119
+ expect(cols).toEqual(['session_id','authentication_method']);
120
+ }
121
+ });
122
+
123
+ it('should verify anon access to mfa_amr_claims based on rls', async () => {
124
+ db.setContext({ role: 'service_role' });
125
+ const rlsStatus = await db.any(
126
+ `SELECT c.relrowsecurity
127
+ FROM pg_class c
128
+ JOIN pg_namespace n ON n.oid = c.relnamespace
129
+ WHERE n.nspname = 'auth' AND c.relname = 'mfa_amr_claims'`
130
+ );
131
+ expect(Array.isArray(rlsStatus)).toBe(true);
132
+ expect(rlsStatus.length).toBeGreaterThan(0);
133
+
134
+ db.clearContext();
135
+ const result = await db.any(`SELECT * FROM auth.mfa_amr_claims LIMIT 1`);
136
+ expect(Array.isArray(result)).toBe(true);
137
+ if (rlsStatus[0].relrowsecurity === true) {
138
+ expect(result.length).toBe(0);
139
+ }
140
+ });
141
+ });
142
+
@@ -0,0 +1,117 @@
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
+ // grants
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 exists
32
+ const exists = await pg.any(
33
+ `SELECT EXISTS (
34
+ SELECT FROM information_schema.tables
35
+ WHERE table_schema = 'auth' AND table_name = 'mfa_challenges'
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 mfa_challenges table access', () => {
54
+
55
+ it('should verify mfa_challenges 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 = 'mfa_challenges'
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 mfa_challenges', async () => {
71
+ db.setContext({ role: 'service_role' });
72
+
73
+ // service_role should be able to query mfa_challenges
74
+ const challenges = await db.any(
75
+ `SELECT id, factor_id, ip_address, verified_at, created_at
76
+ FROM auth.mfa_challenges
77
+ LIMIT 10`
78
+ );
79
+
80
+ expect(Array.isArray(challenges)).toBe(true);
81
+ });
82
+
83
+ it('should verify table has index on factor_id', async () => {
84
+ db.setContext({ role: 'service_role' });
85
+
86
+ // check for indexes on factor_id column
87
+ const indexes = await db.any(
88
+ `SELECT indexname
89
+ FROM pg_indexes
90
+ WHERE schemaname = 'auth' AND tablename = 'mfa_challenges'
91
+ AND indexname LIKE '%factor_id%'`
92
+ );
93
+
94
+ expect(Array.isArray(indexes)).toBe(true);
95
+ });
96
+
97
+ it('should verify anon access to mfa_challenges based on rls', async () => {
98
+ // rls status
99
+ db.setContext({ role: 'service_role' });
100
+ const rlsStatus = await db.any(
101
+ `SELECT c.relrowsecurity
102
+ FROM pg_class c
103
+ JOIN pg_namespace n ON n.oid = c.relnamespace
104
+ WHERE n.nspname = 'auth' AND c.relname = 'mfa_challenges'`
105
+ );
106
+ expect(Array.isArray(rlsStatus)).toBe(true);
107
+ expect(rlsStatus.length).toBeGreaterThan(0);
108
+
109
+ db.clearContext();
110
+ const result = await db.any(`SELECT * FROM auth.mfa_challenges LIMIT 1`);
111
+ expect(Array.isArray(result)).toBe(true);
112
+ if (rlsStatus[0].relrowsecurity === true) {
113
+ expect(result.length).toBe(0);
114
+ }
115
+ });
116
+ });
117
+