/** * Tests pour le middleware d'authentification */ import { NextRequest, NextResponse } from 'next/server'; // We need to mock the middleware module to test it describe('Auth Middleware', () => { describe('Auth Routes Detection', () => { const authRoutes = ['/login', '/register']; it('should identify login as auth route', () => { const pathname = '/login'; const isAuthRoute = authRoutes.some((route) => pathname.startsWith(route)); expect(isAuthRoute).toBe(true); }); it('should identify register as auth route', () => { const pathname = '/register'; const isAuthRoute = authRoutes.some((route) => pathname.startsWith(route)); expect(isAuthRoute).toBe(true); }); it('should not identify home as auth route', () => { const pathname = '/'; const isAuthRoute = authRoutes.some((route) => pathname.startsWith(route)); expect(isAuthRoute).toBe(false); }); it('should not identify dashboard as auth route', () => { const pathname = '/dashboard'; const isAuthRoute = authRoutes.some((route) => pathname.startsWith(route)); expect(isAuthRoute).toBe(false); }); }); describe('Token Detection', () => { it('should detect token from cookies', () => { const cookies = new Map([['auth_token', 'test-token']]); const token = cookies.get('auth_token'); expect(token).toBe('test-token'); }); it('should detect token from authorization header', () => { const authHeader = 'Bearer test-token'; const token = authHeader?.replace('Bearer ', ''); expect(token).toBe('test-token'); }); it('should handle missing token', () => { const cookies = new Map(); const token = cookies.get('auth_token'); expect(token).toBeUndefined(); }); }); describe('Redirect Logic', () => { it('should redirect authenticated users from login to home', () => { const isAuthRoute = true; const hasToken = true; const shouldRedirect = isAuthRoute && hasToken; expect(shouldRedirect).toBe(true); }); it('should not redirect unauthenticated users from login', () => { const isAuthRoute = true; const hasToken = false; const shouldRedirect = isAuthRoute && hasToken; expect(shouldRedirect).toBe(false); }); it('should not redirect authenticated users from non-auth routes', () => { const isAuthRoute = false; const hasToken = true; const shouldRedirect = isAuthRoute && hasToken; expect(shouldRedirect).toBe(false); }); }); describe('Matcher Config', () => { const matcherPattern = /^\/((?!_next\/static|_next\/image|favicon\.ico|.*\..*|public).*)/; it('should match root path', () => { expect('/'.match(matcherPattern)).toBeTruthy(); }); it('should match login path', () => { expect('/login'.match(matcherPattern)).toBeTruthy(); }); it('should match dashboard path', () => { expect('/dashboard'.match(matcherPattern)).toBeTruthy(); }); it('should not match static files', () => { expect('/_next/static/file.js'.match(matcherPattern)).toBeFalsy(); }); it('should not match image optimization', () => { expect('/_next/image'.match(matcherPattern)).toBeFalsy(); }); it('should not match favicon', () => { expect('/favicon.ico'.match(matcherPattern)).toBeFalsy(); }); it('should not match files with extensions', () => { expect('/image.png'.match(matcherPattern)).toBeFalsy(); expect('/script.js'.match(matcherPattern)).toBeFalsy(); }); }); });