102 lines
4.7 KiB
TypeScript
102 lines
4.7 KiB
TypeScript
/** @jest-environment node */
|
||
import { NextRequest } from 'next/server'
|
||
import { getServerSession } from 'next-auth'
|
||
import { canUserViewMaterial } from '@/lib/department-permission'
|
||
import { FinishedWebSsoError, issueFinishedWebLink } from '@/lib/finished-web-sso'
|
||
import { prisma } from '@/lib/prisma'
|
||
import { canAccessMaterialPrivacy } from '@/lib/task-detail-privacy'
|
||
import { POST } from './route'
|
||
|
||
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
|
||
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
|
||
jest.mock('@/lib/prisma', () => ({ prisma: { material: { findFirst: jest.fn() } } }))
|
||
jest.mock('@/lib/department-permission', () => ({ canUserViewMaterial: jest.fn() }))
|
||
jest.mock('@/lib/task-detail-privacy', () => ({ canAccessMaterialPrivacy: jest.fn(), TASK_DETAIL_PRIVACY_SELECT: {} }))
|
||
jest.mock('@/lib/employee-directory', () => ({}))
|
||
jest.mock('@/lib/finished-web-sso', () => ({
|
||
...jest.requireActual('@/lib/finished-web-sso'),
|
||
issueFinishedWebLink: jest.fn(),
|
||
}))
|
||
|
||
const params = { id: 'detail-material' }
|
||
const user = { id: 'viewer', role: 'USER', department: '运营部', managedDepartment: null }
|
||
const material = { userId: 'owner', materialType: '自动上架-详情', accessScope: 'general' }
|
||
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(headers: Record<string, string> = { Origin: 'http://localhost:3000' }) {
|
||
return POST(new NextRequest('http://localhost:3000/api/materials/detail-material/finished-web', {
|
||
method: 'POST', headers, body: JSON.stringify({ nickname: '不应使用的花名', userId: 'other-user' }),
|
||
}), { params })
|
||
}
|
||
|
||
beforeEach(() => {
|
||
jest.resetAllMocks()
|
||
;(getServerSession as jest.Mock).mockResolvedValue({ user })
|
||
jest.mocked(prisma.material.findFirst).mockResolvedValue(material as never)
|
||
jest.mocked(canUserViewMaterial).mockResolvedValue(true)
|
||
jest.mocked(canAccessMaterialPrivacy).mockResolvedValue(true)
|
||
jest.mocked(issueFinishedWebLink).mockResolvedValue(link)
|
||
})
|
||
|
||
test('登录用户可按素材权限换链,签发身份只取自会话', async () => {
|
||
const response = await request()
|
||
expect(response.status).toBe(200)
|
||
expect(response.headers.get('cache-control')).toBe('private, no-store')
|
||
expect(await response.json()).toEqual(link)
|
||
expect(prisma.material.findFirst).toHaveBeenCalledWith(expect.objectContaining({
|
||
where: { id: params.id, deletedAt: null, materialType: '自动上架-详情' },
|
||
}))
|
||
expect(canAccessMaterialPrivacy).toHaveBeenCalledWith(material, user.id)
|
||
expect(canUserViewMaterial).toHaveBeenCalledWith(user.department, user.role, null, material.materialType, user.id)
|
||
expect(issueFinishedWebLink).toHaveBeenCalledWith(user.id)
|
||
})
|
||
|
||
test('成品所有者沿用分享页的查看权限', async () => {
|
||
jest.mocked(prisma.material.findFirst).mockResolvedValue({ ...material, userId: user.id } as never)
|
||
jest.mocked(canUserViewMaterial).mockResolvedValue(false)
|
||
expect((await request()).status).toBe(200)
|
||
expect(canAccessMaterialPrivacy).toHaveBeenCalled()
|
||
expect(canUserViewMaterial).not.toHaveBeenCalled()
|
||
})
|
||
|
||
test('未登录时拒绝换链', async () => {
|
||
;(getServerSession as jest.Mock).mockResolvedValue(null)
|
||
expect((await request()).status).toBe(401)
|
||
expect(prisma.material.findFirst).not.toHaveBeenCalled()
|
||
expect(issueFinishedWebLink).not.toHaveBeenCalled()
|
||
})
|
||
|
||
test.each([
|
||
{},
|
||
{ Origin: 'https://untrusted.example', 'Sec-Fetch-Site': 'cross-site' },
|
||
])('拒绝非本站请求 %j', async headers => {
|
||
expect((await request(headers as Record<string, string>)).status).toBe(403)
|
||
expect(prisma.material.findFirst).not.toHaveBeenCalled()
|
||
expect(issueFinishedWebLink).not.toHaveBeenCalled()
|
||
})
|
||
|
||
test('成品不存在、已删除或不是详情类型时不签发', async () => {
|
||
jest.mocked(prisma.material.findFirst).mockResolvedValue(null)
|
||
expect((await request()).status).toBe(404)
|
||
expect(issueFinishedWebLink).not.toHaveBeenCalled()
|
||
})
|
||
|
||
test.each(['privacy', 'type'])('越权拒绝(%s)', async scope => {
|
||
jest.mocked(canAccessMaterialPrivacy).mockResolvedValue(scope !== 'privacy')
|
||
jest.mocked(canUserViewMaterial).mockResolvedValue(scope !== 'type')
|
||
expect((await request()).status).toBe(403)
|
||
expect(issueFinishedWebLink).not.toHaveBeenCalled()
|
||
})
|
||
|
||
test('传递签发模块提供的中文消息和 HTTP 状态', async () => {
|
||
jest.mocked(issueFinishedWebLink).mockRejectedValue(new FinishedWebSsoError('请稍后再试', 429, 'finished_web_upstream'))
|
||
const response = await request()
|
||
expect(response.status).toBe(429)
|
||
expect(await response.json()).toEqual({ error: '请稍后再试' })
|
||
expect(response.headers.get('cache-control')).toBe('private, no-store')
|
||
})
|