Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/llms-txt-hub / apps/web/app/api/debug/__tests__/access-control.test.ts |
| Date | Not specified in source |
| Repository | Janady13/llms-txt-hub |
| Revision | e0f4bf978d4df168369c7601d9c5f5ea89e84377 |
| Branch | main |
| Path or archive member | apps/web/app/api/debug/__tests__/access-control.test.ts |
| Original SHA-256 | 9e8f7f50b321929a73ca7bfef76e22cae34bd0e975dc798d22dbd0fd5a7ccc5d |
| Git blob SHA | 7424472e1d4d018e78a0d5ef13d02b1fd25f0ec9 |
| Reading-copy SHA-256 | 9e8f7f50b321929a73ca7bfef76e22cae34bd0e975dc798d22dbd0fd5a7ccc5d |
Recovered source transcript
Download complete source text ↓121 lines · 3.7 KB
import { POST as clearGithubCache } from '@/app/api/debug/clear-github-cache/route'
import { GET as getContentPaths } from '@/app/api/debug/content-paths/route'
jest.mock('@thedaviddias/auth', () => ({
auth: jest.fn()
}))
jest.mock('@thedaviddias/logging', () => ({
logger: {
error: jest.fn(),
info: jest.fn(),
warn: jest.fn()
}
}))
jest.mock('@/lib/github-security-utils', () => ({
GitHubAPIClient: {
getInstance: jest.fn(() => ({
clearRateLimitCache: jest.fn()
}))
}
}))
const { auth } = require('@thedaviddias/auth')
const mockAuth = auth as jest.MockedFunction<any>
describe('/api/debug route access control', () => {
const env = process.env as Record<string, string | undefined>
const originalNodeEnv = env.NODE_ENV
const originalDebugToken = env.DEBUG_API_TOKEN
afterEach(() => {
env.NODE_ENV = originalNodeEnv
env.DEBUG_API_TOKEN = originalDebugToken
jest.clearAllMocks()
})
it('returns 404 in production for debug content-paths endpoint', async () => {
env.NODE_ENV = 'production'
mockAuth.mockResolvedValue({ user: { id: 'user_123' } })
const response = await getContentPaths(
new Request('http://localhost/api/debug/content-paths') as any
)
const data = await response.json()
expect(response.status).toBe(404)
expect(data.error).toBe('Not found')
})
it('returns 401 for debug content-paths endpoint when unauthenticated in development', async () => {
env.NODE_ENV = 'development'
mockAuth.mockResolvedValue(null)
const response = await getContentPaths(
new Request('http://localhost/api/debug/content-paths') as any
)
const data = await response.json()
expect(response.status).toBe(401)
expect(data.error).toBe('Unauthorized')
})
it('returns 404 in production for debug cache clear endpoint', async () => {
env.NODE_ENV = 'production'
mockAuth.mockResolvedValue({ user: { id: 'user_123' } })
const response = await clearGithubCache(
new Request('http://localhost/api/debug/clear-github-cache', { method: 'POST' }) as any
)
const data = await response.json()
expect(response.status).toBe(404)
expect(data.error).toBe('Not found')
})
it('returns 401 for debug cache clear endpoint when unauthenticated in development', async () => {
env.NODE_ENV = 'development'
mockAuth.mockResolvedValue(null)
const response = await clearGithubCache(
new Request('http://localhost/api/debug/clear-github-cache', { method: 'POST' }) as any
)
const data = await response.json()
expect(response.status).toBe(401)
expect(data.error).toBe('Unauthorized')
})
it('returns 403 when DEBUG_API_TOKEN is configured but missing from request', async () => {
env.NODE_ENV = 'development'
env.DEBUG_API_TOKEN = 'secret-token'
mockAuth.mockResolvedValue({ user: { id: 'user_123' } })
const response = await clearGithubCache(
new Request('http://localhost/api/debug/clear-github-cache', { method: 'POST' }) as any
)
const data = await response.json()
expect(response.status).toBe(403)
expect(data.error).toBe('Forbidden')
})
it('allows request when DEBUG_API_TOKEN matches', async () => {
env.NODE_ENV = 'development'
env.DEBUG_API_TOKEN = 'secret-token'
mockAuth.mockResolvedValue({ user: { id: 'user_123' } })
const response = await clearGithubCache(
new Request('http://localhost/api/debug/clear-github-cache', {
method: 'POST',
headers: { 'x-debug-token': 'secret-token' }
}) as any
)
const data = await response.json()
expect(response.status).toBe(200)
expect(data.message).toBe('GitHub API cache cleared successfully')
})
})