|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | + |
| 3 | +// Mock a minimal module options structure for testing the whitelist logic |
| 4 | +interface TestModuleOptions { |
| 5 | + auth: { |
| 6 | + whitelist: string[] |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +// Extract the whitelist logic for testing |
| 11 | +const getWhitelistWithAutoConfirmEmail = (options: TestModuleOptions) => { |
| 12 | + const combinedWhitelist = [...options.auth.whitelist] |
| 13 | + // Auto-whitelist /confirm-email if /register is whitelisted |
| 14 | + if (combinedWhitelist.includes('/register') && !combinedWhitelist.includes('/confirm-email')) { |
| 15 | + combinedWhitelist.push('/confirm-email') |
| 16 | + } |
| 17 | + return combinedWhitelist |
| 18 | +} |
| 19 | + |
| 20 | +describe('Auto-whitelist functionality', () => { |
| 21 | + it('should auto-add /confirm-email when /register is whitelisted', () => { |
| 22 | + const options: TestModuleOptions = { |
| 23 | + auth: { |
| 24 | + whitelist: ['/noauth', '/register'] |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + const result = getWhitelistWithAutoConfirmEmail(options) |
| 29 | + |
| 30 | + expect(result).toContain('/register') |
| 31 | + expect(result).toContain('/confirm-email') |
| 32 | + expect(result).toContain('/noauth') |
| 33 | + }) |
| 34 | + |
| 35 | + it('should not duplicate /confirm-email if already present', () => { |
| 36 | + const options: TestModuleOptions = { |
| 37 | + auth: { |
| 38 | + whitelist: ['/noauth', '/register', '/confirm-email'] |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + const result = getWhitelistWithAutoConfirmEmail(options) |
| 43 | + |
| 44 | + expect(result.filter(route => route === '/confirm-email')).toHaveLength(1) |
| 45 | + }) |
| 46 | + |
| 47 | + it('should not add /confirm-email if /register is not whitelisted', () => { |
| 48 | + const options: TestModuleOptions = { |
| 49 | + auth: { |
| 50 | + whitelist: ['/noauth', '/login'] |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + const result = getWhitelistWithAutoConfirmEmail(options) |
| 55 | + |
| 56 | + expect(result).not.toContain('/confirm-email') |
| 57 | + expect(result).toContain('/noauth') |
| 58 | + expect(result).toContain('/login') |
| 59 | + }) |
| 60 | + |
| 61 | + it('should handle empty whitelist', () => { |
| 62 | + const options: TestModuleOptions = { |
| 63 | + auth: { |
| 64 | + whitelist: [] |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + const result = getWhitelistWithAutoConfirmEmail(options) |
| 69 | + |
| 70 | + expect(result).not.toContain('/confirm-email') |
| 71 | + expect(result).toHaveLength(0) |
| 72 | + }) |
| 73 | +}) |
0 commit comments