|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | + |
| 3 | +// Stub Nuxt/h3 helper used at module import time so defineEventHandler calls in |
| 4 | +// the middleware file don't throw during tests. |
| 5 | +;(globalThis as any).defineEventHandler = (handler: any) => handler |
| 6 | + |
| 7 | +// Mock the authentication module so the middleware uses the mocked function. |
| 8 | +vi.mock('../server/modules/authentication', () => ({ |
| 9 | + authenticateAndGetGitHubHeaders: vi.fn() |
| 10 | +})) |
| 11 | + |
| 12 | +// We'll import the mocked auth module and the middleware dynamically after the |
| 13 | +// defineEventHandler stub is installed (static imports are hoisted and would |
| 14 | +// otherwise attempt to evaluate the middleware file too early). |
| 15 | +let authenticateAndGetGitHubHeaders: any |
| 16 | +let middlewareHandler: any |
| 17 | + |
| 18 | +beforeEach(async () => { |
| 19 | + vi.clearAllMocks() |
| 20 | + |
| 21 | + // Minimal runtime config used by the middleware |
| 22 | + ;(globalThis as any).useRuntimeConfig = (_event?: unknown) => ({ |
| 23 | + public: { |
| 24 | + githubEnt: undefined, |
| 25 | + githubOrg: undefined, |
| 26 | + githubTeam: undefined, |
| 27 | + version: 'test' |
| 28 | + } |
| 29 | + }) |
| 30 | + |
| 31 | + // Dynamically import the mocked authentication module and middleware |
| 32 | + const auth = await import('../server/modules/authentication') |
| 33 | + authenticateAndGetGitHubHeaders = auth.authenticateAndGetGitHubHeaders |
| 34 | + |
| 35 | + const mw = await import('../server/middleware/github') |
| 36 | + middlewareHandler = mw.default |
| 37 | +}) |
| 38 | + |
| 39 | +describe('GitHub middleware authentication guard', () => { |
| 40 | + it('skips authentication for health endpoints', async () => { |
| 41 | + // If the auth function is called during this test, fail the test |
| 42 | + ;(authenticateAndGetGitHubHeaders as any).mockImplementation(() => { throw new Error('authenticate called unexpectedly') }) |
| 43 | + |
| 44 | + const event: any = { |
| 45 | + node: { req: { url: '/api/health' } }, |
| 46 | + context: {} |
| 47 | + } |
| 48 | + |
| 49 | + await expect((middlewareHandler as any)(event)).resolves.not.toThrow() |
| 50 | + expect(authenticateAndGetGitHubHeaders).not.toHaveBeenCalled() |
| 51 | + }) |
| 52 | + |
| 53 | + it('skips authentication for live and ready endpoints', async () => { |
| 54 | + ;(authenticateAndGetGitHubHeaders as any).mockImplementation(() => { throw new Error('authenticate called unexpectedly') }) |
| 55 | + |
| 56 | + const liveEvent: any = { node: { req: { url: '/api/live' } }, context: {} } |
| 57 | + const readyEvent: any = { node: { req: { url: '/api/ready' } }, context: {} } |
| 58 | + |
| 59 | + await expect((middlewareHandler as any)(liveEvent)).resolves.not.toThrow() |
| 60 | + await expect((middlewareHandler as any)(readyEvent)).resolves.not.toThrow() |
| 61 | + expect(authenticateAndGetGitHubHeaders).not.toHaveBeenCalled() |
| 62 | + }) |
| 63 | + |
| 64 | + it('requires authentication for other api routes', async () => { |
| 65 | + // Return a resolved Headers object to simulate successful authentication |
| 66 | + ;(authenticateAndGetGitHubHeaders as any).mockResolvedValue(new Headers({ Authorization: 'token x' })) |
| 67 | + |
| 68 | + const event: any = { |
| 69 | + node: { req: { url: '/api/metrics' } }, |
| 70 | + context: {} |
| 71 | + } |
| 72 | + |
| 73 | + await (middlewareHandler as any)(event) |
| 74 | + |
| 75 | + expect(authenticateAndGetGitHubHeaders).toHaveBeenCalled() |
| 76 | + expect(event.context.headers).toBeInstanceOf(Headers) |
| 77 | + }) |
| 78 | +}) |
0 commit comments