Files
opencode-manager/backend/test/routes/ssh.test.ts
T
Chris Scott 414f39b809 feat: SSH Authentication Keys v2 (#113)
* feat: add SSH authentication keys v2 for Git operations

Add comprehensive SSH key authentication support for Git operations including:
- SSH host key verification with user approval flow
- Encrypted SSH key storage with passphrase support
- SSH connection testing endpoint
- Real-time host key change warnings

Backend changes:
- New SSHHostKeyHandler for managing host key verification via IPC
- Add passphrase handler for locked SSH keys
- Add SSH key manager utilities (key writing, validation, cleanup)
- Add crypto utilities for secure key/secret encryption
- Add SSH validation utilities for key format checking
- Add SSH routes for host key response handling
- Enhance GitAuthService with SSH setup and environment methods
- Update GitService to handle SSH authentication for push/fetch/pull
- Update OpenCode server to support SSH keys with SSH config generation
- Add database migration for trusted_ssh_hosts table
- Add settings endpoint for SSH connection testing

Frontend changes:
- Add SSHHostKeyDialog for host key verification UI
- Add PassphraseModal for SSH key passphrase input
- Update GitCredentialDialog with SSH key type and testing
- Update GitSettings to manage SSH keys
- Add SSH API client and hooks (useSSH, usePassphraseHandler)

Shared changes:
- Add SSH schemas and types

Test coverage:
- Integration tests for SSH flow
- IPC handler tests for host key and passphrase
- SSH route tests
- SSH utility validation tests
- Updated existing tests for SSH-aware changes
2026-02-08 00:06:44 -05:00

269 lines
8.7 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { Hono } from 'hono'
import type { GitAuthService } from '../../src/services/git-auth'
vi.mock('bun:sqlite', () => ({
Database: vi.fn(),
}))
vi.mock('@opencode-manager/shared/config/env', () => ({
ENV: {
AUTH: {
SECRET: 'test-secret-for-encryption'
},
OPENCODE: {
PORT: 5551
},
SERVER: {
PORT: 5003
}
},
getReposPath: vi.fn(() => '/tmp/test-repos'),
getWorkspacePath: vi.fn(() => '/tmp/test-workspace')
}))
vi.mock('@opencode-manager/shared/config', () => ({
DEFAULTS: {
SSE: {
RECONNECT_DELAY_MS: 1000,
MAX_RECONNECT_DELAY_MS: 30000,
IDLE_GRACE_PERIOD_MS: 120000,
},
},
}))
vi.mock('eventsource', () => ({
EventSource: vi.fn(),
}))
vi.mock('../../src/utils/logger', () => ({
logger: {
info: vi.fn(),
error: vi.fn(),
warn: vi.fn()
}
}))
import { createSSHRoutes } from '../../src/routes/ssh'
interface MockSSHHostKeyHandler {
respond: ReturnType<typeof vi.fn>
getPendingCount?: ReturnType<typeof vi.fn>
}
interface MockGitAuthService {
sshHostKeyHandler: MockSSHHostKeyHandler | null
}
describe('SSH Routes', () => {
let app: Hono
let mockGitAuthService: MockGitAuthService
beforeEach(() => {
vi.clearAllMocks()
mockGitAuthService = {
sshHostKeyHandler: {
respond: vi.fn().mockReturnValue({ success: true }),
getPendingCount: vi.fn().mockReturnValue(0)
}
}
app = createSSHRoutes(mockGitAuthService as unknown as GitAuthService)
})
describe('POST /host-key/respond', () => {
it('should accept valid response with accept', async () => {
const response = await app.request('/host-key/respond', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ requestId: 'test-123', response: 'accept' })
})
const body = await response.json()
expect(response.status).toBe(200)
expect(body).toHaveProperty('success', true)
expect(mockGitAuthService.sshHostKeyHandler?.respond).toHaveBeenCalledWith({
requestId: 'test-123',
response: 'accept'
})
})
it('should accept valid response with reject', async () => {
const response = await app.request('/host-key/respond', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ requestId: 'test-456', response: 'reject' })
})
const body = await response.json()
expect(response.status).toBe(200)
expect(body).toHaveProperty('success', true)
expect(mockGitAuthService.sshHostKeyHandler?.respond).toHaveBeenCalledWith({
requestId: 'test-456',
response: 'reject'
})
})
it('should return 400 for invalid request body missing requestId', async () => {
const response = await app.request('/host-key/respond', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ response: 'accept' })
})
const body = await response.json()
expect(response.status).toBe(400)
expect(body).toHaveProperty('success', false)
expect(body).toHaveProperty('error', 'Invalid request body')
})
it('should return 400 for invalid request body missing response', async () => {
const response = await app.request('/host-key/respond', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ requestId: 'test-123' })
})
const body = await response.json()
expect(response.status).toBe(400)
expect(body).toHaveProperty('success', false)
expect(body).toHaveProperty('error', 'Invalid request body')
})
it('should return 400 for invalid response value', async () => {
const response = await app.request('/host-key/respond', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ requestId: 'test-123', response: 'invalid' })
})
const body = await response.json()
expect(response.status).toBe(400)
expect(body).toHaveProperty('success', false)
expect(body).toHaveProperty('error', 'Invalid request body')
})
it('should return 404 when handler not found', async () => {
mockGitAuthService.sshHostKeyHandler = null
app = createSSHRoutes(mockGitAuthService as unknown as GitAuthService)
const response = await app.request('/host-key/respond', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ requestId: 'test-123', response: 'accept' })
})
const body = await response.json()
expect(response.status).toBe(404)
expect(body).toHaveProperty('success', false)
expect(body).toHaveProperty('error', 'SSH host key handler not found')
})
it('should handle request ID matching correctly', async () => {
const requestId = 'unique-request-id-12345'
await app.request('/host-key/respond', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ requestId, response: 'accept' })
})
expect(mockGitAuthService.sshHostKeyHandler?.respond).toHaveBeenCalledWith({
requestId: 'unique-request-id-12345',
response: 'accept'
})
})
it('should return handler result on success', async () => {
mockGitAuthService.sshHostKeyHandler = {
respond: vi.fn().mockReturnValue({ success: true }),
getPendingCount: vi.fn()
}
app = createSSHRoutes(mockGitAuthService as unknown as GitAuthService)
const response = await app.request('/host-key/respond', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ requestId: 'test-123', response: 'accept' })
})
const body = await response.json()
expect(body).toEqual({ success: true })
})
it('should return handler error when request not found', async () => {
mockGitAuthService.sshHostKeyHandler = {
respond: vi.fn().mockReturnValue({ success: false, error: 'Request not found or expired' }),
getPendingCount: vi.fn()
}
app = createSSHRoutes(mockGitAuthService as unknown as GitAuthService)
const response = await app.request('/host-key/respond', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ requestId: 'expired-123', response: 'accept' })
})
const body = await response.json()
expect(body).toEqual({ success: false, error: 'Request not found or expired' })
})
})
describe('GET /host-key/status', () => {
it('should return pending count correctly when zero', async () => {
mockGitAuthService.sshHostKeyHandler = {
respond: vi.fn(),
getPendingCount: vi.fn().mockReturnValue(0)
}
app = createSSHRoutes(mockGitAuthService as unknown as GitAuthService)
const response = await app.request('/host-key/status')
const body = await response.json()
expect(response.status).toBe(200)
expect(body).toHaveProperty('success', true)
expect(body).toHaveProperty('pendingCount', 0)
})
it('should return pending count correctly when non-zero', async () => {
mockGitAuthService.sshHostKeyHandler = {
respond: vi.fn(),
getPendingCount: vi.fn().mockReturnValue(5)
}
app = createSSHRoutes(mockGitAuthService as unknown as GitAuthService)
const response = await app.request('/host-key/status')
const body = await response.json()
expect(response.status).toBe(200)
expect(body).toHaveProperty('success', true)
expect(body).toHaveProperty('pendingCount', 5)
})
it('should return 404 when handler not found', async () => {
mockGitAuthService.sshHostKeyHandler = null
app = createSSHRoutes(mockGitAuthService as unknown as GitAuthService)
const response = await app.request('/host-key/status')
const body = await response.json()
expect(response.status).toBe(404)
expect(body).toHaveProperty('success', false)
expect(body).toHaveProperty('error', 'SSH host key handler not found')
})
it('should return 500 when getPendingCount is undefined', async () => {
mockGitAuthService.sshHostKeyHandler = {
respond: vi.fn(),
getPendingCount: undefined as unknown as ReturnType<typeof vi.fn>
}
app = createSSHRoutes(mockGitAuthService as unknown as GitAuthService)
const response = await app.request('/host-key/status')
const body = await response.json()
expect(response.status).toBe(500)
expect(body).toHaveProperty('success', false)
})
})
})