90 lines
4.1 KiB
TypeScript
90 lines
4.1 KiB
TypeScript
/** @jest-environment node */
|
|
import { DesktopClientError } from '@/lib/desktop-client/contract.mjs'
|
|
import { getDesktopDelivery } from '@/lib/desktop-client/deliveries'
|
|
import { requireDesktop } from '@/lib/desktop-client/service'
|
|
import { FinishedWebSsoError, issueFinishedWebLink } from '@/lib/finished-web-sso'
|
|
import { GET, POST } from './route'
|
|
|
|
jest.mock('next-auth', () => ({}))
|
|
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
|
|
jest.mock('@/lib/desktop-client/service', () => ({ requireDesktop: jest.fn() }))
|
|
jest.mock('@/lib/desktop-client/deliveries', () => ({ getDesktopDelivery: jest.fn() }))
|
|
jest.mock('@/lib/employee-directory', () => ({}))
|
|
jest.mock('@/lib/finished-web-sso', () => ({
|
|
...jest.requireActual('@/lib/finished-web-sso'),
|
|
issueFinishedWebLink: jest.fn(),
|
|
}))
|
|
|
|
const path = ['deliveries', 'owned-project', 'detail', 'owned-material', 'finished-web']
|
|
const receipt = {
|
|
requestId: 'owned-request', materialId: 'owned-material', materialType: '自动上架-详情',
|
|
state: 'completed' as const, listingStatus: null,
|
|
}
|
|
const link = {
|
|
url: 'https://pddg.jiansuntuzhi.cn/finished-web/#token=simulated-link',
|
|
expiresAt: '2027-01-02T07:04:20.518Z',
|
|
user: { id: 123, username: 'operator', name: '测试账号', role: 'operator' },
|
|
}
|
|
|
|
function request(method: 'POST' | 'GET' = 'POST', segments = path) {
|
|
const input = new Request('http://localhost:3000/api/desktop/' + segments.join('/'), {
|
|
method, headers: { Authorization: 'Bearer desktop-session-placeholder' },
|
|
})
|
|
return (method === 'POST' ? POST : GET)(input, { params: { path: segments } })
|
|
}
|
|
|
|
beforeEach(() => {
|
|
jest.resetAllMocks()
|
|
jest.mocked(requireDesktop).mockResolvedValue({
|
|
id: 'session', account: { id: 'owner', name: '实名' }, expiresAt: new Date(), isSuperAdmin: false,
|
|
})
|
|
jest.mocked(getDesktopDelivery).mockResolvedValue(receipt)
|
|
jest.mocked(issueFinishedWebLink).mockResolvedValue(link)
|
|
})
|
|
|
|
test('桌面会话只能为当前账户的指定详情交付换链', async () => {
|
|
const response = await request()
|
|
expect(jest.mocked(requireDesktop).mock.calls[0][0].headers.get('authorization')).toBe('Bearer desktop-session-placeholder')
|
|
expect(getDesktopDelivery).toHaveBeenCalledWith('owner', 'owned-project', 'detail')
|
|
expect(issueFinishedWebLink).toHaveBeenCalledWith('owner')
|
|
expect(response.status).toBe(200)
|
|
expect(response.headers.get('cache-control')).toBe('private, no-store')
|
|
expect(await response.json()).toEqual(link)
|
|
})
|
|
|
|
test('无效或已撤销的 Bearer 会话无法进入交付查询', async () => {
|
|
jest.mocked(requireDesktop).mockRejectedValue(new DesktopClientError('请重新登录', 401, 'session_expired'))
|
|
expect((await request()).status).toBe(401)
|
|
expect(getDesktopDelivery).not.toHaveBeenCalled()
|
|
expect(issueFinishedWebLink).not.toHaveBeenCalled()
|
|
})
|
|
|
|
test('无权访问项目时不签发', async () => {
|
|
jest.mocked(getDesktopDelivery).mockRejectedValue(new DesktopClientError('项目不存在或无权访问', 404, 'not_found'))
|
|
expect((await request()).status).toBe(404)
|
|
expect(issueFinishedWebLink).not.toHaveBeenCalled()
|
|
})
|
|
|
|
test.each([
|
|
['无交付', null],
|
|
['已删除', { ...receipt, state: 'removed' as const }],
|
|
['成品编号不一致', { ...receipt, materialId: 'other-material' }],
|
|
])('%s时不签发', async (_label, value) => {
|
|
jest.mocked(getDesktopDelivery).mockResolvedValue(value)
|
|
expect((await request()).status).toBe(404)
|
|
expect(issueFinishedWebLink).not.toHaveBeenCalled()
|
|
})
|
|
|
|
test('GET 和非详情类型均不能触发签发', async () => {
|
|
expect((await request('GET')).status).toBe(404)
|
|
expect((await request('POST', [path[0], path[1], 'video', path[3], path[4]])).status).toBe(404)
|
|
expect(issueFinishedWebLink).not.toHaveBeenCalled()
|
|
})
|
|
|
|
test('上游 401 与客户端会话失效有不同错误码', async () => {
|
|
jest.mocked(issueFinishedWebLink).mockRejectedValue(new FinishedWebSsoError('工作台拒绝了服务授权', 401, 'finished_web_upstream'))
|
|
const response = await request()
|
|
expect(response.status).toBe(401)
|
|
expect(await response.json()).toEqual({ error: '工作台拒绝了服务授权', code: 'finished_web_upstream' })
|
|
})
|