feat: add Vitest testing framework, fix proxy auth stripping and 401 handling
- Set up Vitest with jsdom for client tests, node for server tests - Add tests for auth service, proxy handler, API client, and profiles store - Strip Authorization header in proxy to prevent web-ui token leaking to gateway - Distinguish local BFF vs proxied gateway 401s to avoid false logouts - Remove unused hero.png asset Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterAll } from 'vitest'
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
readFile: vi.fn(),
|
||||
writeFile: vi.fn(),
|
||||
}))
|
||||
|
||||
// Mock config
|
||||
vi.mock('../../packages/server/src/config', () => ({
|
||||
config: { dataDir: '/tmp/hermes-test-data' },
|
||||
}))
|
||||
|
||||
import { readFile, writeFile } from 'fs/promises'
|
||||
import { getToken, authMiddleware } from '../../packages/server/src/services/auth'
|
||||
|
||||
const mockedReadFile = vi.mocked(readFile)
|
||||
const mockedWriteFile = vi.mocked(writeFile)
|
||||
|
||||
describe('Auth Service', () => {
|
||||
const originalEnv = process.env
|
||||
|
||||
beforeEach(() => {
|
||||
process.env = { ...originalEnv }
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
process.env = originalEnv
|
||||
})
|
||||
|
||||
describe('getToken', () => {
|
||||
it('returns null when AUTH_DISABLED=1', async () => {
|
||||
process.env.AUTH_DISABLED = '1'
|
||||
|
||||
const token = await getToken()
|
||||
|
||||
expect(token).toBeNull()
|
||||
expect(mockedReadFile).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('returns null when AUTH_DISABLED=true', async () => {
|
||||
process.env.AUTH_DISABLED = 'true'
|
||||
|
||||
const token = await getToken()
|
||||
|
||||
expect(token).toBeNull()
|
||||
})
|
||||
|
||||
it('returns AUTH_TOKEN env var if set', async () => {
|
||||
process.env.AUTH_TOKEN = 'my-custom-token'
|
||||
|
||||
const token = await getToken()
|
||||
|
||||
expect(token).toBe('my-custom-token')
|
||||
expect(mockedReadFile).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('reads token from file if exists', async () => {
|
||||
mockedReadFile.mockResolvedValue('file-token\n')
|
||||
|
||||
const token = await getToken()
|
||||
|
||||
expect(token).toBe('file-token')
|
||||
expect(mockedReadFile).toHaveBeenCalledWith('/tmp/hermes-test-data/.token', 'utf-8')
|
||||
})
|
||||
|
||||
it('generates and saves new token if file missing', async () => {
|
||||
mockedReadFile.mockRejectedValue(new Error('ENOENT'))
|
||||
|
||||
const token = await getToken()
|
||||
|
||||
expect(token).toBeTruthy()
|
||||
expect(token).toHaveLength(64) // 32 bytes hex
|
||||
expect(mockedWriteFile).toHaveBeenCalledWith(
|
||||
'/tmp/hermes-test-data/.token',
|
||||
expect.stringMatching(/^[a-f0-9]{64}\n$/),
|
||||
{ mode: 0o600 },
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('authMiddleware', () => {
|
||||
function createMockCtx(path: string, headers: Record<string, string> = {}, query: Record<string, string> = {}) {
|
||||
return {
|
||||
path,
|
||||
headers,
|
||||
query,
|
||||
status: 200,
|
||||
body: null,
|
||||
set: vi.fn(),
|
||||
}
|
||||
}
|
||||
|
||||
const next = vi.fn()
|
||||
|
||||
it('allows all requests when auth is disabled (null token)', async () => {
|
||||
const middleware = await authMiddleware(null)
|
||||
const ctx = createMockCtx('/api/hermes/sessions')
|
||||
|
||||
await middleware(ctx, next)
|
||||
|
||||
expect(next).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('skips /health path', async () => {
|
||||
const middleware = await authMiddleware('secret')
|
||||
const ctx = createMockCtx('/health')
|
||||
|
||||
await middleware(ctx, next)
|
||||
|
||||
expect(next).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('skips non-API paths', async () => {
|
||||
const middleware = await authMiddleware('secret')
|
||||
const ctx = createMockCtx('/index.html')
|
||||
|
||||
await middleware(ctx, next)
|
||||
|
||||
expect(next).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('requires auth for /webhook path (it is an API-like endpoint)', async () => {
|
||||
const middleware = await authMiddleware('secret')
|
||||
const ctx = createMockCtx('/webhook', {})
|
||||
|
||||
await middleware(ctx, next)
|
||||
|
||||
expect(ctx.status).toBe(401)
|
||||
expect(next).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects request without auth header', async () => {
|
||||
const middleware = await authMiddleware('secret')
|
||||
const ctx = createMockCtx('/api/hermes/sessions', {})
|
||||
|
||||
await middleware(ctx, next)
|
||||
|
||||
expect(ctx.status).toBe(401)
|
||||
expect(next).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects request with wrong token', async () => {
|
||||
const middleware = await authMiddleware('secret')
|
||||
const ctx = createMockCtx('/api/hermes/sessions', { authorization: 'Bearer wrong' })
|
||||
|
||||
await middleware(ctx, next)
|
||||
|
||||
expect(ctx.status).toBe(401)
|
||||
expect(next).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('allows request with correct Bearer token', async () => {
|
||||
const middleware = await authMiddleware('secret')
|
||||
const ctx = createMockCtx('/api/hermes/sessions', { authorization: 'Bearer secret' })
|
||||
|
||||
await middleware(ctx, next)
|
||||
|
||||
expect(next).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('allows request with correct query token', async () => {
|
||||
const middleware = await authMiddleware('secret')
|
||||
const ctx = createMockCtx('/api/hermes/sessions', {}, { token: 'secret' })
|
||||
|
||||
await middleware(ctx, next)
|
||||
|
||||
expect(next).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('returns 401 JSON on auth failure', async () => {
|
||||
const middleware = await authMiddleware('secret')
|
||||
const ctx = createMockCtx('/api/hermes/sessions', { authorization: 'Bearer wrong' })
|
||||
|
||||
await middleware(ctx, next)
|
||||
|
||||
expect(ctx.status).toBe(401)
|
||||
expect(ctx.set).toHaveBeenCalledWith('Content-Type', 'application/json')
|
||||
expect(ctx.body).toEqual({ error: 'Unauthorized' })
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,87 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
// Mock hermes-cli
|
||||
vi.mock('../../packages/server/src/services/hermes-cli', () => ({
|
||||
listProfiles: vi.fn(),
|
||||
getProfile: vi.fn(),
|
||||
createProfile: vi.fn(),
|
||||
deleteProfile: vi.fn(),
|
||||
renameProfile: vi.fn(),
|
||||
useProfile: vi.fn(),
|
||||
stopGateway: vi.fn(),
|
||||
startGateway: vi.fn(),
|
||||
startGatewayBackground: vi.fn(),
|
||||
setupReset: vi.fn(),
|
||||
exportProfile: vi.fn(),
|
||||
importProfile: vi.fn(),
|
||||
}))
|
||||
|
||||
import * as hermesCli from '../../packages/server/src/services/hermes-cli'
|
||||
|
||||
describe('Profile Routes', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('ensureApiServerConfig (via active profile switch)', () => {
|
||||
it('should inject api_server config when missing', async () => {
|
||||
// This tests the logic that profiles.ts ensures api_server config exists
|
||||
// We test the ensureApiServerConfig behavior indirectly through the module
|
||||
const { existsSync, readFileSync, writeFileSync } = await import('fs')
|
||||
vi.mock('fs', () => ({
|
||||
existsSync: vi.fn().mockReturnValue(true),
|
||||
readFileSync: vi.fn().mockReturnValue('platforms: {}'),
|
||||
writeFileSync: vi.fn(),
|
||||
createReadStream: vi.fn(),
|
||||
unlinkSync: vi.fn(),
|
||||
mkdirSync: vi.fn(),
|
||||
copyFileSync: vi.fn(),
|
||||
mkdir: vi.fn(),
|
||||
writeFile: vi.fn(),
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
||||
describe('hermes-cli wrapper', () => {
|
||||
it('listProfiles returns array', async () => {
|
||||
const mockProfiles = [{ name: 'default', active: true }]
|
||||
vi.mocked(hermesCli.listProfiles).mockResolvedValue(mockProfiles as any)
|
||||
|
||||
const result = await hermesCli.listProfiles()
|
||||
expect(result).toEqual(mockProfiles)
|
||||
})
|
||||
|
||||
it('getProfile returns profile detail', async () => {
|
||||
const mockDetail = { name: 'default', path: '/tmp/default' }
|
||||
vi.mocked(hermesCli.getProfile).mockResolvedValue(mockDetail as any)
|
||||
|
||||
const result = await hermesCli.getProfile('default')
|
||||
expect(result).toEqual(mockDetail)
|
||||
expect(hermesCli.getProfile).toHaveBeenCalledWith('default')
|
||||
})
|
||||
|
||||
it('createProfile calls CLI with name and clone flag', async () => {
|
||||
vi.mocked(hermesCli.createProfile).mockResolvedValue('Profile created')
|
||||
|
||||
await hermesCli.createProfile('test', true)
|
||||
|
||||
expect(hermesCli.createProfile).toHaveBeenCalledWith('test', true)
|
||||
})
|
||||
|
||||
it('deleteProfile calls CLI with name', async () => {
|
||||
vi.mocked(hermesCli.deleteProfile).mockResolvedValue(true)
|
||||
|
||||
await hermesCli.deleteProfile('test')
|
||||
|
||||
expect(hermesCli.deleteProfile).toHaveBeenCalledWith('test')
|
||||
})
|
||||
|
||||
it('renameProfile calls CLI with old and new name', async () => {
|
||||
vi.mocked(hermesCli.renameProfile).mockResolvedValue(true)
|
||||
|
||||
await hermesCli.renameProfile('old', 'new')
|
||||
|
||||
expect(hermesCli.renameProfile).toHaveBeenCalledWith('old', 'new')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,145 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
// Mock config
|
||||
vi.mock('../../packages/server/src/config', () => ({
|
||||
config: { upstream: 'http://127.0.0.1:8642' },
|
||||
}))
|
||||
|
||||
const mockFetch = vi.fn()
|
||||
vi.stubGlobal('fetch', mockFetch)
|
||||
|
||||
import { proxy } from '../../packages/server/src/routes/hermes/proxy-handler'
|
||||
|
||||
function createMockCtx(overrides: Record<string, any> = {}) {
|
||||
let headersSent = false
|
||||
const ctx: any = {
|
||||
path: '/api/hermes/jobs',
|
||||
method: 'GET',
|
||||
headers: { host: 'localhost:8648', 'content-type': 'application/json' },
|
||||
query: {},
|
||||
search: '',
|
||||
req: { method: 'GET' },
|
||||
res: {
|
||||
write: vi.fn(),
|
||||
end: vi.fn(),
|
||||
headersSent: false,
|
||||
writableEnded: false,
|
||||
},
|
||||
request: { rawBody: undefined },
|
||||
status: 200,
|
||||
set: vi.fn(),
|
||||
body: null,
|
||||
...overrides,
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
describe('Proxy Handler', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('rewrites /api/hermes/v1/* to /v1/*', async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
status: 200,
|
||||
headers: new Headers({ 'content-type': 'application/json' }),
|
||||
body: null,
|
||||
json: () => Promise.resolve({ ok: true }),
|
||||
})
|
||||
|
||||
const ctx = createMockCtx({ path: '/api/hermes/v1/runs', search: '' })
|
||||
await proxy(ctx)
|
||||
|
||||
expect(mockFetch).toHaveBeenCalledOnce()
|
||||
const url = mockFetch.mock.calls[0][0]
|
||||
expect(url).toContain('/v1/runs')
|
||||
expect(url).not.toContain('/api/hermes')
|
||||
})
|
||||
|
||||
it('rewrites /api/hermes/* to /api/*', async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
status: 200,
|
||||
headers: new Headers({ 'content-type': 'application/json' }),
|
||||
body: null,
|
||||
json: () => Promise.resolve({ ok: true }),
|
||||
})
|
||||
|
||||
const ctx = createMockCtx({ path: '/api/hermes/jobs', search: '' })
|
||||
await proxy(ctx)
|
||||
|
||||
const url = mockFetch.mock.calls[0][0]
|
||||
expect(url).toContain('/api/jobs')
|
||||
expect(url).not.toContain('/api/hermes')
|
||||
})
|
||||
|
||||
it('strips authorization header', async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
status: 200,
|
||||
headers: new Headers({ 'content-type': 'application/json' }),
|
||||
body: null,
|
||||
json: () => Promise.resolve({}),
|
||||
})
|
||||
|
||||
const ctx = createMockCtx({
|
||||
headers: { host: 'localhost:8648', authorization: 'Bearer web-ui-token' },
|
||||
})
|
||||
await proxy(ctx)
|
||||
|
||||
const [, options] = mockFetch.mock.calls[0]
|
||||
expect(options.headers.authorization).toBeUndefined()
|
||||
})
|
||||
|
||||
it('replaces host header with upstream host', async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
status: 200,
|
||||
headers: new Headers({ 'content-type': 'application/json' }),
|
||||
body: null,
|
||||
json: () => Promise.resolve({}),
|
||||
})
|
||||
|
||||
const ctx = createMockCtx()
|
||||
await proxy(ctx)
|
||||
|
||||
const [, options] = mockFetch.mock.calls[0]
|
||||
expect(options.headers.host).toBe('127.0.0.1:8642')
|
||||
})
|
||||
|
||||
it('forwards query string', async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
status: 200,
|
||||
headers: new Headers({ 'content-type': 'text/event-stream' }),
|
||||
body: null,
|
||||
json: () => Promise.resolve({}),
|
||||
})
|
||||
|
||||
const ctx = createMockCtx({ search: '?include_disabled=true' })
|
||||
await proxy(ctx)
|
||||
|
||||
const url = mockFetch.mock.calls[0][0]
|
||||
expect(url).toContain('?include_disabled=true')
|
||||
})
|
||||
|
||||
it('returns 502 on connection failure', async () => {
|
||||
mockFetch.mockRejectedValue(new Error('ECONNREFUSED'))
|
||||
|
||||
const ctx = createMockCtx()
|
||||
await proxy(ctx)
|
||||
|
||||
expect(ctx.status).toBe(502)
|
||||
expect(ctx.body).toEqual({ error: { message: 'Proxy error: ECONNREFUSED' } })
|
||||
})
|
||||
|
||||
it('passes through non-200 status codes', async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
status: 404,
|
||||
headers: new Headers({ 'content-type': 'application/json' }),
|
||||
body: null,
|
||||
json: () => Promise.resolve({ error: 'Not found' }),
|
||||
})
|
||||
|
||||
const ctx = createMockCtx()
|
||||
await proxy(ctx)
|
||||
|
||||
expect(ctx.status).toBe(404)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user