|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' |
| 2 | +import type { APIRoute } from 'astro' |
| 3 | +import { GET as runAll } from '@pages/api/cron/run-all' |
| 4 | + |
| 5 | +const getCronSecretMock = vi.hoisted(() => vi.fn(() => 'cron-secret')) |
| 6 | +const getSiteUrlMock = vi.hoisted(() => vi.fn(() => 'https://example.com')) |
| 7 | + |
| 8 | +vi.mock('@pages/api/_environment/environmentApi', async () => { |
| 9 | + const actual = await vi.importActual<typeof import('@pages/api/_environment/environmentApi')>( |
| 10 | + '@pages/api/_environment/environmentApi', |
| 11 | + ) |
| 12 | + return { |
| 13 | + ...actual, |
| 14 | + getCronSecret: getCronSecretMock, |
| 15 | + getSiteUrl: getSiteUrlMock, |
| 16 | + } |
| 17 | +}) |
| 18 | + |
| 19 | +const buildContext = (request: Request) => ({ |
| 20 | + request, |
| 21 | + clientAddress: '127.0.0.1', |
| 22 | + cookies: { |
| 23 | + get: () => undefined, |
| 24 | + }, |
| 25 | +}) |
| 26 | + |
| 27 | +describe('cron runner', () => { |
| 28 | + let fetchMock: ReturnType<typeof vi.fn> |
| 29 | + let warnSpy: ReturnType<typeof vi.spyOn> |
| 30 | + |
| 31 | + const run = (request: Request) => runAll(buildContext(request) as unknown as Parameters<APIRoute>[0]) |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + vi.clearAllMocks() |
| 35 | + fetchMock = vi.fn() |
| 36 | + vi.stubGlobal('fetch', fetchMock) |
| 37 | + warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) |
| 38 | + }) |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + vi.unstubAllGlobals() |
| 42 | + warnSpy.mockRestore() |
| 43 | + }) |
| 44 | + |
| 45 | + const createResponse = (path: string, overrides?: Partial<Response>) => |
| 46 | + ({ |
| 47 | + ok: true, |
| 48 | + status: 200, |
| 49 | + statusText: 'OK', |
| 50 | + headers: new Headers({ 'x-vercel-elapsed-time': '123' }), |
| 51 | + json: vi.fn().mockResolvedValue({ path }), |
| 52 | + text: vi.fn().mockResolvedValue(''), |
| 53 | + ...overrides, |
| 54 | + }) as unknown as Response |
| 55 | + |
| 56 | + it('rejects unauthorized requests', async () => { |
| 57 | + const request = new Request('https://example.com/api/cron/run-all') |
| 58 | + const response = await run(request) |
| 59 | + const body = await response.json() |
| 60 | + |
| 61 | + expect(response.status).toBe(401) |
| 62 | + expect(body.error.code).toBe('UNAUTHORIZED') |
| 63 | + expect(fetchMock).not.toHaveBeenCalled() |
| 64 | + expect(warnSpy).toHaveBeenCalled() |
| 65 | + }) |
| 66 | + |
| 67 | + it('calls downstream cron endpoints sequentially', async () => { |
| 68 | + fetchMock |
| 69 | + .mockResolvedValueOnce(createResponse('/api/cron/cleanup-confirmations')) |
| 70 | + .mockResolvedValueOnce(createResponse('/api/cron/cleanup-dsar-requests')) |
| 71 | + .mockResolvedValueOnce(createResponse('/api/cron/ping-integrations')) |
| 72 | + |
| 73 | + const request = new Request('https://example.com/api/cron/run-all', { |
| 74 | + method: 'GET', |
| 75 | + headers: { |
| 76 | + authorization: 'Bearer cron-secret', |
| 77 | + }, |
| 78 | + }) |
| 79 | + |
| 80 | + const response = await run(request) |
| 81 | + const body = await response.json() |
| 82 | + |
| 83 | + expect(response.status).toBe(200) |
| 84 | + expect(Array.isArray(body.results)).toBe(true) |
| 85 | + expect(body.results).toHaveLength(3) |
| 86 | + expect(fetchMock).toHaveBeenCalledTimes(3) |
| 87 | + |
| 88 | + const headersUsed = fetchMock.mock.calls[0]?.[1]?.headers as Record<string, string> |
| 89 | + expect(headersUsed.Authorization ?? headersUsed.authorization).toBe('Bearer cron-secret') |
| 90 | + }) |
| 91 | + |
| 92 | + it('surfaces downstream failure details', async () => { |
| 93 | + fetchMock |
| 94 | + .mockResolvedValueOnce( |
| 95 | + createResponse('/api/cron/cleanup-confirmations', { |
| 96 | + ok: false, |
| 97 | + status: 500, |
| 98 | + statusText: 'Internal Server Error', |
| 99 | + headers: new Headers(), |
| 100 | + }), |
| 101 | + ) |
| 102 | + .mockResolvedValueOnce(createResponse('/api/cron/cleanup-dsar-requests')) |
| 103 | + .mockResolvedValueOnce(createResponse('/api/cron/ping-integrations')) |
| 104 | + |
| 105 | + const request = new Request('https://example.com/api/cron/run-all', { |
| 106 | + method: 'GET', |
| 107 | + headers: { |
| 108 | + authorization: 'Bearer cron-secret', |
| 109 | + }, |
| 110 | + }) |
| 111 | + |
| 112 | + const response = await run(request) |
| 113 | + const body = await response.json() |
| 114 | + |
| 115 | + expect(response.status).toBe(500) |
| 116 | + expect(body.error.code).toBe('CRON_RUNNER_TARGET_FAILED') |
| 117 | + expect(body.error.message).toBe('Cron runner failed to execute downstream jobs') |
| 118 | + }) |
| 119 | +}) |
0 commit comments