-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathroute.test.ts
More file actions
84 lines (72 loc) · 2.68 KB
/
Copy pathroute.test.ts
File metadata and controls
84 lines (72 loc) · 2.68 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
import { NextRequest } from 'next/server';
jest.mock('@/lib/config.server', () => ({
CRON_SECRET: 'cron-secret',
}));
jest.mock('@/lib/code-reviews/dispatch/dispatch-pending-code-review-owners', () => ({
dispatchPendingCodeReviewOwners: jest.fn(),
}));
import { dispatchPendingCodeReviewOwners } from '@/lib/code-reviews/dispatch/dispatch-pending-code-review-owners';
import { GET } from './route';
const mockDispatchPendingCodeReviewOwners = jest.mocked(dispatchPendingCodeReviewOwners);
describe('GET /api/cron/dispatch-pending-code-reviews', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('rejects requests without cron authorization', async () => {
const response = await GET(
new NextRequest('http://localhost:3000/api/cron/dispatch-pending-code-reviews', {
method: 'GET',
})
);
expect(response.status).toBe(401);
await expect(response.json()).resolves.toEqual({ error: 'Unauthorized' });
expect(mockDispatchPendingCodeReviewOwners).not.toHaveBeenCalled();
});
it('rejects requests with invalid cron authorization', async () => {
const response = await GET(
new NextRequest('http://localhost:3000/api/cron/dispatch-pending-code-reviews', {
method: 'GET',
headers: { authorization: 'Bearer wrong-secret' },
})
);
expect(response.status).toBe(401);
await expect(response.json()).resolves.toEqual({ error: 'Unauthorized' });
expect(mockDispatchPendingCodeReviewOwners).not.toHaveBeenCalled();
});
it('dispatches pending code-review owners when authorized', async () => {
mockDispatchPendingCodeReviewOwners.mockResolvedValue({
ownersConsidered: 4,
ownersProcessed: 3,
ownersWithNoNewDispatch: 1,
ownersSkippedMissingBotUsers: 1,
coordinatorFailures: 0,
reviewsDispatched: 5,
staleReviewsCancelled: 2,
staleAttemptsCancelled: 1,
hasMoreCandidateOwners: true,
});
const response = await GET(
new NextRequest('http://localhost:3000/api/cron/dispatch-pending-code-reviews', {
method: 'GET',
headers: { authorization: 'Bearer cron-secret' },
})
);
expect(response.status).toBe(200);
expect(mockDispatchPendingCodeReviewOwners).toHaveBeenCalledTimes(1);
await expect(response.json()).resolves.toEqual({
success: true,
summary: {
ownersConsidered: 4,
ownersProcessed: 3,
ownersWithNoNewDispatch: 1,
ownersSkippedMissingBotUsers: 1,
coordinatorFailures: 0,
reviewsDispatched: 5,
staleReviewsCancelled: 2,
staleAttemptsCancelled: 1,
hasMoreCandidateOwners: true,
},
timestamp: expect.any(String),
});
});
});