|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | + |
| 3 | +describe('Auth redirect URL construction', () => { |
| 4 | + it('should construct correct login URL with redirectTo param from pathname', () => { |
| 5 | + const request = new Request('http://localhost:3000/dashboard/courses') |
| 6 | + const redirectTo = new URL(request.url).pathname |
| 7 | + |
| 8 | + const searchParams = new URLSearchParams([['redirectTo', redirectTo]]) |
| 9 | + const loginUrl = `/login?${searchParams}` |
| 10 | + |
| 11 | + expect(loginUrl).toBe('/login?redirectTo=%2Fdashboard%2Fcourses') |
| 12 | + }) |
| 13 | + |
| 14 | + it('should use custom redirectTo when provided', () => { |
| 15 | + const redirectTo = '/custom/path' |
| 16 | + |
| 17 | + const searchParams = new URLSearchParams([['redirectTo', redirectTo]]) |
| 18 | + const loginUrl = `/login?${searchParams}` |
| 19 | + |
| 20 | + expect(loginUrl).toBe('/login?redirectTo=%2Fcustom%2Fpath') |
| 21 | + }) |
| 22 | + |
| 23 | + it('should handle root path', () => { |
| 24 | + const request = new Request('http://localhost:3000/') |
| 25 | + const redirectTo = new URL(request.url).pathname |
| 26 | + |
| 27 | + const searchParams = new URLSearchParams([['redirectTo', redirectTo]]) |
| 28 | + const loginUrl = `/login?${searchParams}` |
| 29 | + |
| 30 | + expect(loginUrl).toBe('/login?redirectTo=%2F') |
| 31 | + }) |
| 32 | + |
| 33 | + it('should handle paths with query parameters', () => { |
| 34 | + const redirectTo = '/dashboard/courses?filter=active' |
| 35 | + |
| 36 | + const searchParams = new URLSearchParams([['redirectTo', redirectTo]]) |
| 37 | + const loginUrl = `/login?${searchParams}` |
| 38 | + |
| 39 | + expect(loginUrl).toContain('redirectTo=') |
| 40 | + expect(decodeURIComponent(loginUrl)).toContain( |
| 41 | + '/dashboard/courses?filter=active' |
| 42 | + ) |
| 43 | + }) |
| 44 | +}) |
| 45 | + |
| 46 | +describe('Login redirect URL preservation', () => { |
| 47 | + it('should extract redirectTo from URL search params', () => { |
| 48 | + const url = new URL( |
| 49 | + 'http://localhost:3000/login?redirectTo=/dashboard/profile' |
| 50 | + ) |
| 51 | + const redirectTo = url.searchParams.get('redirectTo') ?? '/dashboard' |
| 52 | + |
| 53 | + expect(redirectTo).toBe('/dashboard/profile') |
| 54 | + }) |
| 55 | + |
| 56 | + it('should default to /dashboard when redirectTo is not provided', () => { |
| 57 | + const url = new URL('http://localhost:3000/login') |
| 58 | + const redirectTo = url.searchParams.get('redirectTo') ?? '/dashboard' |
| 59 | + |
| 60 | + expect(redirectTo).toBe('/dashboard') |
| 61 | + }) |
| 62 | + |
| 63 | + it('should preserve redirectTo in success/failure redirects', () => { |
| 64 | + const redirectTo = '/dashboard/transactions' |
| 65 | + const successRedirect = `/login?redirectTo=${encodeURIComponent( |
| 66 | + redirectTo |
| 67 | + )}` |
| 68 | + const failureRedirect = `/login?redirectTo=${encodeURIComponent( |
| 69 | + redirectTo |
| 70 | + )}` |
| 71 | + |
| 72 | + expect(successRedirect).toBe( |
| 73 | + '/login?redirectTo=%2Fdashboard%2Ftransactions' |
| 74 | + ) |
| 75 | + expect(failureRedirect).toBe( |
| 76 | + '/login?redirectTo=%2Fdashboard%2Ftransactions' |
| 77 | + ) |
| 78 | + }) |
| 79 | +}) |
0 commit comments