68 lines
3.1 KiB
TypeScript
68 lines
3.1 KiB
TypeScript
/** @jest-environment node */
|
|
import { createHmac } from 'node:crypto'
|
|
import { GET } from '@/app/api/desktop/[...path]/route'
|
|
import { requireDesktop } from '@/lib/desktop-client/service'
|
|
import { DesktopClientError } from '@/lib/desktop-client/contract.mjs'
|
|
|
|
jest.mock('@/lib/desktop-client/service', () => ({ requireDesktop: jest.fn() }))
|
|
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
|
|
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
|
|
|
|
const context = { params: { path: ['knowledge', 'session'] } }
|
|
const request = () => new Request('https://platform.test/api/desktop/knowledge/session')
|
|
const originalSecret = process.env.XIAOYI_KNOWLEDGE_SESSION_SECRET
|
|
const secret = 'knowledge-session-test-secret-123456789'
|
|
|
|
describe('desktop knowledge session', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
jest.useFakeTimers().setSystemTime(new Date('2026-12-01T08:00:00Z'))
|
|
process.env.XIAOYI_KNOWLEDGE_SESSION_SECRET = secret
|
|
;(requireDesktop as jest.Mock).mockResolvedValue({
|
|
account: { id: 'test-account' },
|
|
expiresAt: new Date('2026-12-01T09:00:00Z'),
|
|
})
|
|
})
|
|
|
|
afterEach(() => {
|
|
jest.useRealTimers()
|
|
if (originalSecret === undefined) delete process.env.XIAOYI_KNOWLEDGE_SESSION_SECRET
|
|
else process.env.XIAOYI_KNOWLEDGE_SESSION_SECRET = originalSecret
|
|
})
|
|
|
|
test('requires an active desktop session and never exposes a token on failure', async () => {
|
|
for (const status of [401, 403]) {
|
|
;(requireDesktop as jest.Mock).mockRejectedValueOnce(new DesktopClientError('无权访问', status, 'denied'))
|
|
const response = await GET(request(), context)
|
|
expect(response.status).toBe(status)
|
|
expect(response.headers.get('cache-control')).toBe('private, no-store')
|
|
expect(await response.json()).not.toHaveProperty('token')
|
|
}
|
|
})
|
|
|
|
test('signs the account identity with the configured key and limits the lifetime', async () => {
|
|
const response = await GET(request(), context)
|
|
expect(response.status).toBe(200)
|
|
expect(response.headers.get('cache-control')).toBe('private, no-store')
|
|
const result = await response.json()
|
|
const [encoded, signature] = result.token.replace('ekb_xiaoyi_session_', '').split('.')
|
|
expect(signature).toBe(createHmac('sha256', secret).update(encoded).digest('base64url'))
|
|
expect(JSON.parse(Buffer.from(encoded, 'base64url').toString())).toEqual({
|
|
sub: 'test-account', aud: 'xiaoyi-knowledge', iat: 1796112000, exp: 1796112300,
|
|
})
|
|
expect(result.expiresAt).toBe('2026-12-01T08:05:00.000Z')
|
|
|
|
;(requireDesktop as jest.Mock).mockResolvedValueOnce({
|
|
account: { id: 'test-account' }, expiresAt: new Date('2026-12-01T08:00:45Z'),
|
|
})
|
|
expect((await (await GET(request(), context)).json()).expiresAt).toBe('2026-12-01T08:00:45.000Z')
|
|
})
|
|
|
|
test('reports an unavailable signer without issuing a token', async () => {
|
|
delete process.env.XIAOYI_KNOWLEDGE_SESSION_SECRET
|
|
const response = await GET(request(), context)
|
|
expect(response.status).toBe(503)
|
|
expect(await response.json()).toEqual({ error: '小壹知识库自动授权暂不可用', code: 'knowledge_session_unavailable' })
|
|
})
|
|
})
|