119 lines
4.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** @jest-environment node */
import { getServerSession } from 'next-auth'
import { prisma } from '@/lib/prisma'
import { isSuperAdmin } from '@/lib/department-permission'
import { savePermissionPackItems } from '@/lib/user-permission-inherit'
jest.mock('next-auth', () => ({
getServerSession: jest.fn(),
}))
jest.mock('@/lib/auth', () => ({
authOptions: {},
}))
jest.mock('@/lib/prisma', () => ({
prisma: {
materialPermissionPack: {
findUnique: jest.fn(),
},
},
}))
jest.mock('@/lib/department-permission', () => ({
getAllMaterialTypes: jest.fn(),
isSuperAdmin: jest.fn(),
}))
jest.mock('@/lib/user-permission-inherit', () => ({
ensureDefaultPermissionPacks: jest.fn(),
packItemsToMap: jest.fn((items: Array<{ materialType: string }>) => Object.fromEntries(items.map(({ materialType, ...flags }) => [materialType, flags]))),
savePermissionPackItems: jest.fn(),
}))
jest.mock('@/lib/permission-groups', () => ({
findConflictingDepartmentGroup: jest.fn(),
makePermissionGroupSlug: jest.fn(),
seedDefaultGroupDepartments: jest.fn(),
}))
const { PUT } = require('@/app/api/organization/permission-groups/route') as typeof import('@/app/api/organization/permission-groups/route')
describe('/api/organization/permission-groups 保存权限', () => {
beforeEach(() => {
jest.clearAllMocks()
;(getServerSession as jest.Mock).mockResolvedValue({ user: { id: 'super-admin' } })
;(isSuperAdmin as jest.Mock).mockResolvedValue(true)
;(prisma.materialPermissionPack.findUnique as jest.Mock).mockResolvedValue({
id: 'visual-pack',
slug: 'visual',
name: '视觉',
})
})
test('任务详情私密权限仅任务详情可保存 true,其他类型会被后端压回 false', async () => {
const response = await PUT({
json: async () => ({
action: 'save-pack',
slug: 'visual',
permissions: [
{
materialType: '任务-详情',
canView: true,
canDownload: true,
canDownloadSource: true,
canEditAssets: true,
canAccessPrivate: true,
},
{
materialType: '3D场景',
canView: true,
canDownload: true,
canDownloadSource: false,
canAccessPrivate: true,
},
],
}),
} as never)
expect(response.status).toBe(200)
await expect(response.json()).resolves.toMatchObject({
ok: true,
permissions: {
'任务-详情': { canEditAssets: true, canAccessPrivate: true, canShare: true },
'3D场景': { canEditAssets: false, canAccessPrivate: false, canShare: true },
},
})
expect(savePermissionPackItems).toHaveBeenCalledWith('visual', [
expect.objectContaining({
materialType: '任务-详情',
canEditAssets: true,
canAccessPrivate: true,
}),
expect.objectContaining({
materialType: '3D场景',
canEditAssets: false,
canAccessPrivate: false,
}),
])
})
test('保存事务失败不返回已保存权限', async () => {
const errorLog = jest.spyOn(console, 'error').mockImplementation(() => undefined)
;(savePermissionPackItems as jest.Mock).mockRejectedValueOnce(new Error('transaction aborted'))
try {
const response = await PUT({ json: async () => ({ action: 'save-pack', slug: 'visual', permissions: [{ materialType: '任务-详情', canAccessPrivate: true }] }) } as never)
expect(response.status).toBe(500)
await expect(response.json()).resolves.toEqual({ error: '保存权限组失败' })
} finally { errorLog.mockRestore() }
})
test('普通用户不能保存权限,也不能获得保存结果', async () => {
;(isSuperAdmin as jest.Mock).mockResolvedValue(false)
const response = await PUT({ json: async () => ({ action: 'save-pack', slug: 'visual', permissions: [{ materialType: '任务-详情' }] }) } as never)
expect(response.status).toBe(403)
expect(savePermissionPackItems).not.toHaveBeenCalled()
})
})