auth-provisioning-self-heal.test.ts70 lines · main
| 1 | import { describe, expect, test } from 'bun:test'; |
| 2 | |
| 3 | import { |
| 4 | AUTH_SELF_HEAL_TABLE_SQL, |
| 5 | ensureTenantAuthSchema, |
| 6 | type AuthSchemaQueryClient, |
| 7 | } from './auth-provisioning.js'; |
| 8 | |
| 9 | describe('ensureTenantAuthSchema', () => { |
| 10 | test('AUTH_SELF_HEAL_TABLE_SQL includes email templates, passkeys, two_factors, jwks', () => { |
| 11 | const blob = AUTH_SELF_HEAL_TABLE_SQL.join('\n'); |
| 12 | expect(blob).toContain('_briven_auth_email_templates'); |
| 13 | expect(blob).toContain('_briven_auth_passkeys'); |
| 14 | expect(blob).toContain('_briven_auth_two_factors'); |
| 15 | expect(blob).toContain('_briven_auth_jwks'); |
| 16 | expect(blob).toContain('CREATE TABLE IF NOT EXISTS'); |
| 17 | }); |
| 18 | |
| 19 | test('adds two_factor_enabled when column probe returns empty', async () => { |
| 20 | const executed: string[] = []; |
| 21 | const client: AuthSchemaQueryClient = { |
| 22 | async query(sql: string) { |
| 23 | executed.push(sql.replace(/\s+/g, ' ').trim()); |
| 24 | if (sql.includes('information_schema.columns')) { |
| 25 | return { rows: [] }; |
| 26 | } |
| 27 | return { rows: [] }; |
| 28 | }, |
| 29 | }; |
| 30 | const result = await ensureTenantAuthSchema(client); |
| 31 | expect(result.columnAdded).toBe(true); |
| 32 | expect(result.tablesOk).toBe(AUTH_SELF_HEAL_TABLE_SQL.length); |
| 33 | expect(executed.some((s) => s.includes('ADD COLUMN two_factor_enabled'))).toBe(true); |
| 34 | }); |
| 35 | |
| 36 | test('skips ADD COLUMN when two_factor_enabled already exists', async () => { |
| 37 | const executed: string[] = []; |
| 38 | const client: AuthSchemaQueryClient = { |
| 39 | async query(sql: string) { |
| 40 | executed.push(sql.replace(/\s+/g, ' ').trim()); |
| 41 | if (sql.includes('information_schema.columns')) { |
| 42 | return { rows: [{ ok: 1 }] }; |
| 43 | } |
| 44 | return { rows: [] }; |
| 45 | }, |
| 46 | }; |
| 47 | const result = await ensureTenantAuthSchema(client); |
| 48 | expect(result.columnAdded).toBe(false); |
| 49 | expect(executed.some((s) => s.includes('ADD COLUMN two_factor_enabled'))).toBe(false); |
| 50 | }); |
| 51 | |
| 52 | test('heals passkey device_type when missing', async () => { |
| 53 | const executed: string[] = []; |
| 54 | const client: AuthSchemaQueryClient = { |
| 55 | async query(sql: string, params?: unknown[]) { |
| 56 | executed.push(sql.replace(/\s+/g, ' ').trim()); |
| 57 | if (sql.includes('information_schema.columns') && params?.[0] === 'device_type') { |
| 58 | return { rows: [] }; |
| 59 | } |
| 60 | if (sql.includes('information_schema.columns')) { |
| 61 | return { rows: [{ ok: 1 }] }; |
| 62 | } |
| 63 | return { rows: [] }; |
| 64 | }, |
| 65 | }; |
| 66 | const result = await ensureTenantAuthSchema(client); |
| 67 | expect(result.columnAdded).toBe(true); |
| 68 | expect(executed.some((s) => s.includes('ADD COLUMN device_type'))).toBe(true); |
| 69 | }); |
| 70 | }); |