From cd173c4fd498fad4abfb9e20963cae892157c1bd Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Tue, 22 Jul 2025 10:49:05 -0600 Subject: [PATCH] Create assetReroutes.test.ts --- apps/web/src/middleware/assetReroutes.test.ts | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 apps/web/src/middleware/assetReroutes.test.ts diff --git a/apps/web/src/middleware/assetReroutes.test.ts b/apps/web/src/middleware/assetReroutes.test.ts new file mode 100644 index 000000000..78b2184fe --- /dev/null +++ b/apps/web/src/middleware/assetReroutes.test.ts @@ -0,0 +1,105 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import type { User } from '@supabase/supabase-js'; +import { NextRequest, NextResponse } from 'next/server'; +import { assetReroutes } from './assetReroutes'; + +// Mock the busterRoutes module +vi.mock('@/routes/busterRoutes', () => ({ + BusterRoutes: { + AUTH_LOGIN: '/auth/login' + }, + createBusterRoute: vi.fn(({ route }) => route), + isPublicPage: vi.fn(), + isShareableAssetPage: vi.fn(), + getEmbedAssetRedirect: vi.fn() +})); + +import { + isPublicPage, + isShareableAssetPage, + getEmbedAssetRedirect, + createBusterRoute +} from '@/routes/busterRoutes'; + +describe('assetReroutes', () => { + let mockRequest: NextRequest; + let mockResponse: NextResponse; + let mockUser: User; + + beforeEach(() => { + // Reset all mocks before each test + vi.clearAllMocks(); + + // Create mock request with a URL + mockRequest = new NextRequest('https://example.com/dashboard'); + mockResponse = NextResponse.next(); + + // Create a mock user + mockUser = { + id: 'user-123', + email: 'test@example.com' + } as User; + }); + + it('should return original response when user exists', async () => { + // Test case: Authenticated user should proceed normally + const result = await assetReroutes(mockRequest, mockResponse, mockUser); + + expect(result).toBe(mockResponse); + // Verify that auth-related functions weren't called since user exists + expect(isPublicPage).not.toHaveBeenCalled(); + expect(isShareableAssetPage).not.toHaveBeenCalled(); + }); + + it('should redirect to login when user does not exist and page requires authentication', async () => { + // Test case: Unauthenticated user on protected (non-public) page + vi.mocked(isPublicPage).mockReturnValue(false); + + const result = await assetReroutes(mockRequest, mockResponse, null); + + expect(isPublicPage).toHaveBeenCalledWith(mockRequest); + expect(result).toBeInstanceOf(NextResponse); + + // Verify redirect URL contains login path and next parameter + const redirectUrl = result.headers.get('location'); + expect(redirectUrl).toContain('/auth/login'); + expect(redirectUrl).toContain('next='); + }); + + it('should redirect to embed asset when user does not exist on shareable asset page with embed redirect', async () => { + // Test case: Unauthenticated user on shareable page with available embed redirect + vi.mocked(isPublicPage).mockReturnValue(true); + vi.mocked(isShareableAssetPage).mockReturnValue(true); + vi.mocked(getEmbedAssetRedirect).mockReturnValue('/embed/dashboard/123'); + + const result = await assetReroutes(mockRequest, mockResponse, null); + + expect(isPublicPage).toHaveBeenCalledWith(mockRequest); + expect(isShareableAssetPage).toHaveBeenCalledWith(mockRequest); + expect(getEmbedAssetRedirect).toHaveBeenCalledWith(mockRequest); + expect(result).toBeInstanceOf(NextResponse); + + // Verify redirect to embed URL + const redirectUrl = result.headers.get('location'); + expect(redirectUrl).toContain('/embed/dashboard/123'); + }); + + it('should redirect to login when user does not exist on shareable asset page without embed redirect', async () => { + // Test case: Unauthenticated user on shareable page with no embed redirect available + vi.mocked(isPublicPage).mockReturnValue(true); + vi.mocked(isShareableAssetPage).mockReturnValue(true); + vi.mocked(getEmbedAssetRedirect).mockReturnValue(undefined); + + const result = await assetReroutes(mockRequest, mockResponse, null); + + expect(isPublicPage).toHaveBeenCalledWith(mockRequest); + expect(isShareableAssetPage).toHaveBeenCalledWith(mockRequest); + expect(getEmbedAssetRedirect).toHaveBeenCalledWith(mockRequest); + expect(result).toBeInstanceOf(NextResponse); + + // Verify fallback redirect to login with next parameter + const redirectUrl = result.headers.get('location'); + expect(redirectUrl).toContain('/auth/login'); + expect(redirectUrl).toContain('next='); + }); +});