|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { NextRequest, NextResponse } from 'next/server'; |
| 3 | +import { middleware } from './middleware'; |
| 4 | +import { auth } from './auth'; |
| 5 | +import type { Session } from 'next-auth'; |
| 6 | + |
| 7 | +vi.mock('./lib/rate-limit', () => ({ |
| 8 | + rateLimit: vi.fn().mockResolvedValue({ |
| 9 | + success: true, |
| 10 | + limit: 60, |
| 11 | + remaining: 59, |
| 12 | + reset: 123456789, |
| 13 | + }), |
| 14 | + getRateLimitHeaders: vi.fn(() => ({})), |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock('./auth', () => ({ |
| 18 | + auth: vi.fn(), |
| 19 | +})); |
| 20 | + |
| 21 | +describe('middleware auth and authorization', () => { |
| 22 | + beforeEach(() => { |
| 23 | + vi.clearAllMocks(); |
| 24 | + vi.stubEnv('ENTERPRISE_ADMIN_GITHUB_IDS', ''); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + vi.unstubAllGlobals(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('unauthenticated request to /api/enterprise/teams returns 401', async () => { |
| 32 | + vi.mocked(auth).mockResolvedValue(null); |
| 33 | + |
| 34 | + const request = new NextRequest('http://localhost:3000/api/enterprise/teams'); |
| 35 | + const response = await middleware(request); |
| 36 | + |
| 37 | + expect(response.status).toBe(401); |
| 38 | + const body = await response.json(); |
| 39 | + expect(body).toEqual({ error: 'Authentication required' }); |
| 40 | + expect(response.headers.get('X-Frame-Options')).toBe('DENY'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('authenticated request to /api/enterprise/teams returns 503 if ENTERPRISE_ADMIN_GITHUB_IDS is empty', async () => { |
| 44 | + vi.mocked(auth).mockResolvedValue({ user: { id: 'user123' } } as unknown as Session); |
| 45 | + vi.stubEnv('ENTERPRISE_ADMIN_GITHUB_IDS', ''); |
| 46 | + |
| 47 | + const request = new NextRequest('http://localhost:3000/api/enterprise/teams'); |
| 48 | + const response = await middleware(request); |
| 49 | + |
| 50 | + expect(response.status).toBe(503); |
| 51 | + const body = await response.json(); |
| 52 | + expect(body).toEqual({ error: 'Enterprise admin access not configured' }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('authenticated request to /api/enterprise/teams returns 403 if user is not an enterprise admin', async () => { |
| 56 | + vi.mocked(auth).mockResolvedValue({ user: { id: 'user123' } } as unknown as Session); |
| 57 | + vi.stubEnv('ENTERPRISE_ADMIN_GITHUB_IDS', 'admin1,admin2'); |
| 58 | + |
| 59 | + const request = new NextRequest('http://localhost:3000/api/enterprise/teams'); |
| 60 | + const response = await middleware(request); |
| 61 | + |
| 62 | + expect(response.status).toBe(403); |
| 63 | + const body = await response.json(); |
| 64 | + expect(body).toEqual({ error: 'Forbidden: enterprise admin access required' }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('authenticated request to /api/enterprise/teams passes if user is an enterprise admin', async () => { |
| 68 | + vi.mocked(auth).mockResolvedValue({ user: { id: 'admin1' } } as unknown as Session); |
| 69 | + vi.stubEnv('ENTERPRISE_ADMIN_GITHUB_IDS', 'admin1,admin2'); |
| 70 | + const nextSpy = vi.spyOn(NextResponse, 'next'); |
| 71 | + |
| 72 | + const request = new NextRequest('http://localhost:3000/api/enterprise/teams'); |
| 73 | + const response = await middleware(request); |
| 74 | + |
| 75 | + expect(nextSpy).toHaveBeenCalled(); |
| 76 | + expect(response.headers.get('X-Frame-Options')).toBe('DENY'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('unauthenticated request to /api/architecture returns 401', async () => { |
| 80 | + vi.mocked(auth).mockResolvedValue(null); |
| 81 | + |
| 82 | + const request = new NextRequest('http://localhost:3000/api/architecture'); |
| 83 | + const response = await middleware(request); |
| 84 | + |
| 85 | + expect(response.status).toBe(401); |
| 86 | + }); |
| 87 | + |
| 88 | + it('authenticated request to /api/architecture passes', async () => { |
| 89 | + vi.mocked(auth).mockResolvedValue({ user: { id: 'user123' } } as unknown as Session); |
| 90 | + const nextSpy = vi.spyOn(NextResponse, 'next'); |
| 91 | + |
| 92 | + const request = new NextRequest('http://localhost:3000/api/architecture'); |
| 93 | + await middleware(request); |
| 94 | + |
| 95 | + expect(nextSpy).toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('unauthenticated request to /api/streak passes', async () => { |
| 99 | + vi.mocked(auth).mockResolvedValue(null); |
| 100 | + const nextSpy = vi.spyOn(NextResponse, 'next'); |
| 101 | + |
| 102 | + const request = new NextRequest('http://localhost:3000/api/streak?user=octocat'); |
| 103 | + await middleware(request); |
| 104 | + |
| 105 | + expect(nextSpy).toHaveBeenCalled(); |
| 106 | + expect(auth).not.toHaveBeenCalled(); |
| 107 | + }); |
| 108 | +}); |
0 commit comments