|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest' |
| 2 | +import { setup, $fetch } from '@nuxt/test-utils/e2e' |
| 3 | +import type { Database } from 'db0' |
| 4 | +import { createUser } from '../src/utils/user' |
| 5 | +import { createUsersTable } from '../src/utils/create-users-table' |
| 6 | +import { createPersonalAccessTokensTable } from '../src/utils/create-personal-access-tokens-table' |
| 7 | +import type { DatabaseConfig, DatabaseType, ModuleOptions, User } from '../src/types' |
| 8 | +import { fileURLToPath } from 'node:url' |
| 9 | +import { cleanupTestSetup, createTestSetup } from './test-setup' |
| 10 | + |
| 11 | +describe('Auth Whitelist Middleware', async () => { |
| 12 | + let db: Database |
| 13 | + let testOptions: ModuleOptions |
| 14 | + let dbType: DatabaseType |
| 15 | + let dbConfig: DatabaseConfig |
| 16 | + let _testUser: Omit<User, 'password'> |
| 17 | + |
| 18 | + await setup({ |
| 19 | + rootDir: fileURLToPath(new URL('./fixtures/auth-whitelist', import.meta.url)), |
| 20 | + }) |
| 21 | + |
| 22 | + beforeEach(async () => { |
| 23 | + dbType = process.env.DB_CONNECTOR as DatabaseType || 'sqlite' |
| 24 | + if (dbType === 'sqlite') { |
| 25 | + dbConfig = { |
| 26 | + path: './_auth-whitelist-test', |
| 27 | + } |
| 28 | + } |
| 29 | + if (dbType === 'mysql') { |
| 30 | + dbConfig = { |
| 31 | + host: process.env.DB_HOST, |
| 32 | + port: Number.parseInt(process.env.DB_PORT || '3306'), |
| 33 | + user: process.env.DB_USER, |
| 34 | + password: process.env.DB_PASSWORD, |
| 35 | + database: process.env.DB_NAME |
| 36 | + } |
| 37 | + } |
| 38 | + if (dbType === 'postgresql') { |
| 39 | + dbConfig = { |
| 40 | + host: process.env.DB_HOST, |
| 41 | + port: Number.parseInt(process.env.DB_PORT || '5432'), |
| 42 | + user: process.env.DB_USER, |
| 43 | + password: process.env.DB_PASSWORD, |
| 44 | + database: process.env.DB_NAME |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + const settings = await createTestSetup({ |
| 49 | + dbType, |
| 50 | + dbConfig, |
| 51 | + }) |
| 52 | + |
| 53 | + db = settings.db |
| 54 | + testOptions = settings.testOptions |
| 55 | + |
| 56 | + await createUsersTable(testOptions) |
| 57 | + await createPersonalAccessTokensTable(testOptions) |
| 58 | + |
| 59 | + // Create a test user |
| 60 | + _testUser = await createUser({ |
| 61 | + email: 'test@example.com', |
| 62 | + name: 'Test User', |
| 63 | + password: 'password123', |
| 64 | + }, testOptions) |
| 65 | + }) |
| 66 | + |
| 67 | + afterEach(async () => { |
| 68 | + await cleanupTestSetup(dbType, db, [testOptions.connector!.options.path!], testOptions.tables.users) |
| 69 | + await cleanupTestSetup(dbType, db, [testOptions.connector!.options.path!], testOptions.tables.personalAccessTokens) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should allow access to whitelisted routes without authentication', async () => { |
| 73 | + // Test login page (always accessible) |
| 74 | + const loginResponse = await $fetch('/login') |
| 75 | + expect(loginResponse).toBeDefined() |
| 76 | + |
| 77 | + // Test public page (whitelisted) |
| 78 | + const publicResponse = await $fetch('/public') |
| 79 | + expect(publicResponse).toBeDefined() |
| 80 | + |
| 81 | + // Test register page (whitelisted) |
| 82 | + const registerResponse = await $fetch('/register') |
| 83 | + expect(registerResponse).toBeDefined() |
| 84 | + |
| 85 | + // Test about page (whitelisted) |
| 86 | + const aboutResponse = await $fetch('/about') |
| 87 | + expect(aboutResponse).toBeDefined() |
| 88 | + }) |
| 89 | + |
| 90 | + it('should redirect to login when accessing protected routes without authentication', async () => { |
| 91 | + try { |
| 92 | + await $fetch('/') |
| 93 | + } |
| 94 | + catch (error: unknown) { |
| 95 | + const fetchError = error as { response: { status: number } } |
| 96 | + // Should redirect to login (302 redirect) |
| 97 | + expect(fetchError.response.status).toBe(302) |
| 98 | + } |
| 99 | + |
| 100 | + try { |
| 101 | + await $fetch('/dashboard') |
| 102 | + } |
| 103 | + catch (error: unknown) { |
| 104 | + const fetchError = error as { response: { status: number } } |
| 105 | + // Should redirect to login (302 redirect) |
| 106 | + expect(fetchError.response.status).toBe(302) |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + it('should allow access to protected routes when authenticated', async () => { |
| 111 | + // First login to get authentication |
| 112 | + const loginResponse = await $fetch('/api/login', { |
| 113 | + method: 'POST', |
| 114 | + body: { |
| 115 | + email: 'test@example.com', |
| 116 | + password: 'password123', |
| 117 | + }, |
| 118 | + }) |
| 119 | + |
| 120 | + expect(loginResponse).toBeDefined() |
| 121 | + |
| 122 | + // Now try to access protected routes |
| 123 | + const indexResponse = await $fetch('/') |
| 124 | + expect(indexResponse).toBeDefined() |
| 125 | + |
| 126 | + const dashboardResponse = await $fetch('/dashboard') |
| 127 | + expect(dashboardResponse).toBeDefined() |
| 128 | + }) |
| 129 | + |
| 130 | + it('should handle custom whitelist configuration', async () => { |
| 131 | + // The test fixture is configured with custom whitelist: ['/public', '/register', '/about'] |
| 132 | + // Test that these specific routes are accessible |
| 133 | + const whitelistedRoutes = ['/public', '/register', '/about'] |
| 134 | + |
| 135 | + for (const route of whitelistedRoutes) { |
| 136 | + const response = await $fetch(route) |
| 137 | + expect(response).toBeDefined() |
| 138 | + } |
| 139 | + |
| 140 | + // Test that non-whitelisted routes are protected |
| 141 | + const protectedRoutes = ['/', '/dashboard'] |
| 142 | + |
| 143 | + for (const route of protectedRoutes) { |
| 144 | + try { |
| 145 | + await $fetch(route) |
| 146 | + } |
| 147 | + catch (error: unknown) { |
| 148 | + const fetchError = error as { response: { status: number } } |
| 149 | + expect(fetchError.response.status).toBe(302) // Redirect to login |
| 150 | + } |
| 151 | + } |
| 152 | + }) |
| 153 | + |
| 154 | + it('should always allow access to login page regardless of whitelist', async () => { |
| 155 | + // Login page should always be accessible, even when not in whitelist |
| 156 | + const loginResponse = await $fetch('/login') |
| 157 | + expect(loginResponse).toBeDefined() |
| 158 | + }) |
| 159 | + |
| 160 | + it('should maintain authentication state across requests', async () => { |
| 161 | + // Login first |
| 162 | + await $fetch('/api/login', { |
| 163 | + method: 'POST', |
| 164 | + body: { |
| 165 | + email: 'test@example.com', |
| 166 | + password: 'password123', |
| 167 | + }, |
| 168 | + }) |
| 169 | + |
| 170 | + // Access multiple protected routes |
| 171 | + const routes = ['/', '/dashboard'] |
| 172 | + |
| 173 | + for (const route of routes) { |
| 174 | + const response = await $fetch(route) |
| 175 | + expect(response).toBeDefined() |
| 176 | + } |
| 177 | + }) |
| 178 | + |
| 179 | + it('should handle edge cases in whitelist configuration', async () => { |
| 180 | + // Test with trailing slashes |
| 181 | + const loginWithSlash = await $fetch('/login/') |
| 182 | + expect(loginWithSlash).toBeDefined() |
| 183 | + |
| 184 | + // Test with query parameters |
| 185 | + const loginWithQuery = await $fetch('/login?redirect=/dashboard') |
| 186 | + expect(loginWithQuery).toBeDefined() |
| 187 | + |
| 188 | + // Test with hash fragments |
| 189 | + const loginWithHash = await $fetch('/login#section') |
| 190 | + expect(loginWithHash).toBeDefined() |
| 191 | + }) |
| 192 | +}) |
0 commit comments