Skip to content

Commit a099991

Browse files
karpikplCopilot
andauthored
fix #268 disable auth for healtcheck endpoints (#269)
* fix #268 disable auth for healtcheck endpoints * Update server/middleware/github.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * copilot test --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 0849c3f commit a099991

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

server/middleware/github.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ export default defineEventHandler(async (event) => {
2424
// Only apply authentication to API routes
2525
const url = event.node.req.url || '';
2626

27-
// Skip authentication for non-API routes
28-
if (!url.startsWith('/api/')) {
27+
const healthCheckPaths = ['/api/health', '/api/live', '/api/ready'];
28+
// Skip authentication for non-API routes and health check endpoints
29+
if (!url.startsWith('/api/') || healthCheckPaths.includes(url)) {
2930
return;
3031
}
3132

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)