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