gongxiangp2/lib/__tests__/department-permission-roster.test.ts

328 lines
10 KiB
TypeScript

/** @jest-environment node */
import { canUserViewMaterial, getUserViewableMaterialTypes } from '@/lib/department-permission'
import { prisma } from '@/lib/prisma'
import { canUserAccessPrivateTaskDetail, canUserEditTaskDetailAssets } from '@/lib/user-permission-inherit'
jest.mock('@/lib/prisma', () => ({
prisma: {
user: {
findUnique: jest.fn(),
},
userEmployeeBinding: {
findFirst: jest.fn(),
},
departmentMaterialPermission: {
findMany: jest.fn(),
},
userPermissionInherit: {
findMany: jest.fn(),
},
materialPermissionPackDepartment: {
findMany: jest.fn(),
},
organizationUnit: {
findMany: jest.fn(),
},
},
}))
describe('roster material visibility', () => {
beforeEach(() => {
jest.clearAllMocks()
;(prisma.user.findUnique as jest.Mock).mockResolvedValue({ isSuperAdmin: false })
;(prisma.userPermissionInherit.findMany as jest.Mock).mockResolvedValue([])
;(prisma.materialPermissionPackDepartment.findMany as jest.Mock).mockResolvedValue([])
;(prisma.organizationUnit.findMany as jest.Mock).mockResolvedValue([])
;(prisma.departmentMaterialPermission.findMany as jest.Mock).mockResolvedValue([
{
department: '运营中心',
materialType: '3D场景',
canView: true,
canDownload: true,
canDownloadSource: false,
canShare: true,
},
{
department: '运营中心',
materialType: '任务-详情',
canView: true,
canDownload: true,
canDownloadSource: true,
canShare: true,
},
])
})
test('芷兮这种空账号部门、花名册在品类十九部的人能看见运营中心素材', async () => {
;(prisma.userEmployeeBinding.findFirst as jest.Mock).mockResolvedValue({
employeeProfile: {
departmentPath: '江苏简尚品牌管理有限公司/运营中心A/事业一部/品类十九部',
},
})
await expect(getUserViewableMaterialTypes('', 'USER', null, 'zhixi')).resolves.toEqual([
'3D场景',
'任务-详情',
])
await expect(canUserViewMaterial('', 'USER', null, '3D场景', 'zhixi')).resolves.toBe(true)
})
test('用户继承视觉套餐后,运营能看的还在,视觉多出来的也能看', async () => {
;(prisma.userEmployeeBinding.findFirst as jest.Mock).mockResolvedValue({
employeeProfile: {
departmentPath: '江苏简尚品牌管理有限公司/运营中心A/事业一部/品类十九部',
},
})
;(prisma.userPermissionInherit.findMany as jest.Mock).mockResolvedValue([
{
pack: {
items: [
{
materialType: '详情模板',
canView: true,
canDownload: true,
canDownloadSource: true,
canShare: true,
},
],
},
},
])
await expect(getUserViewableMaterialTypes('', 'USER', null, 'jiuqianqi')).resolves.toEqual([
'3D场景',
'详情模板',
'任务-详情',
])
})
test('部门进了权限组后,下级跟着最近的组走,不再只靠旧部门名', async () => {
;(prisma.userEmployeeBinding.findFirst as jest.Mock).mockResolvedValue({
employeeProfile: {
departmentPath: '江苏简尚品牌管理有限公司/运营中心A/事业一部/品类十九部',
},
})
;(prisma.materialPermissionPackDepartment.findMany as jest.Mock).mockResolvedValue([
{
organizationUnit: { path: '江苏简尚品牌管理有限公司/运营中心A', isActive: true },
pack: {
items: [
{
materialType: '3D场景',
canView: true,
canDownload: true,
canDownloadSource: false,
canShare: true,
},
{
materialType: '详情模板',
canView: true,
canDownload: true,
canDownloadSource: true,
canShare: true,
},
],
},
},
])
await expect(getUserViewableMaterialTypes('', 'USER', null, 'zhixi')).resolves.toEqual([
'3D场景',
'详情模板',
])
})
test('部门权限组勾选任务详情编辑图片后,运行时读取该授权', async () => {
;(prisma.userEmployeeBinding.findFirst as jest.Mock).mockResolvedValue({
employeeProfile: {
departmentPath: '江苏简尚品牌管理有限公司/品牌中心/空间视觉部',
},
})
;(prisma.materialPermissionPackDepartment.findMany as jest.Mock).mockResolvedValue([
{
organizationUnit: { path: '江苏简尚品牌管理有限公司/品牌中心', isActive: true },
pack: {
items: [
{
materialType: '任务-详情',
canView: true,
canDownload: true,
canDownloadSource: true,
canShare: true,
canEditAssets: true,
},
],
},
},
])
await expect(canUserEditTaskDetailAssets('visual-department-member')).resolves.toBe(true)
expect(prisma.materialPermissionPackDepartment.findMany).toHaveBeenCalledWith(
expect.objectContaining({
select: expect.objectContaining({
pack: {
select: {
items: {
select: expect.objectContaining({ canEditAssets: true }),
},
},
},
}),
}),
)
})
test('部门权限组勾选任务详情私密查看后,运行时只认任务详情这条授权', async () => {
;(prisma.userEmployeeBinding.findFirst as jest.Mock).mockResolvedValue({
employeeProfile: {
departmentPath: '江苏简尚品牌管理有限公司/品牌中心/空间视觉部',
},
})
;(prisma.materialPermissionPackDepartment.findMany as jest.Mock).mockResolvedValue([
{
organizationUnit: { path: '江苏简尚品牌管理有限公司/品牌中心', isActive: true },
pack: {
items: [
{
materialType: '任务-详情',
canView: true,
canDownload: true,
canDownloadSource: true,
canShare: true,
canAccessPrivate: true,
},
{
materialType: '3D场景',
canView: true,
canDownload: true,
canDownloadSource: false,
canShare: true,
canAccessPrivate: true,
},
],
},
},
])
await expect(canUserAccessPrivateTaskDetail('visual-department-member')).resolves.toBe(true)
expect(prisma.materialPermissionPackDepartment.findMany).toHaveBeenCalledWith(
expect.objectContaining({
select: expect.objectContaining({
pack: {
select: {
items: {
select: expect.objectContaining({ canAccessPrivate: true }),
},
},
},
}),
}),
)
})
test('人进多组时,各组能看的取并集', async () => {
;(prisma.userEmployeeBinding.findFirst as jest.Mock).mockResolvedValue({
employeeProfile: {
departmentPath: '江苏简尚品牌管理有限公司/运营中心A/事业一部/品类十九部',
},
})
;(prisma.userPermissionInherit.findMany as jest.Mock).mockResolvedValue([
{
pack: {
items: [
{
materialType: '详情模板',
canView: true,
canDownload: true,
canDownloadSource: true,
canShare: true,
},
],
},
},
{
pack: {
items: [
{
materialType: '淘系-主图视频',
canView: true,
canDownload: true,
canDownloadSource: false,
canShare: true,
},
],
},
},
])
await expect(getUserViewableMaterialTypes('', 'USER', null, 'jiuqianqi')).resolves.toEqual([
'3D场景',
'详情模板',
'淘系-主图视频',
'任务-详情',
])
})
test('一人多部门时,各任职路径最近权限组的素材权限取并集', async () => {
;(prisma.userEmployeeBinding.findFirst as jest.Mock).mockResolvedValue({
employeeProfile: {
departmentPath: '江苏简尚品牌管理有限公司/运营中心A/事业一部/品类十九部',
sourcePayload: { syncedFields: { departmentIds: '[19,20]' } },
departmentMemberships: [{ sourceDeptId: '20', departmentPath: null }],
},
})
;(prisma.organizationUnit.findMany as jest.Mock).mockResolvedValue([
{ sourceDeptId: 19, path: '江苏简尚品牌管理有限公司/运营中心A/事业一部/品类十九部' },
{
sourceDeptId: 20,
path: '江苏简尚品牌管理有限公司/品牌中心/空间视觉部',
},
])
;(prisma.materialPermissionPackDepartment.findMany as jest.Mock).mockResolvedValue([
{
organizationUnit: { path: '江苏简尚品牌管理有限公司/运营中心A', isActive: true },
pack: {
items: [
{
materialType: '3D场景',
canView: true,
canDownload: true,
canDownloadSource: false,
canShare: true,
},
],
},
},
{
organizationUnit: { path: '江苏简尚品牌管理有限公司/品牌中心', isActive: true },
pack: {
items: [
{
materialType: '详情模板',
canView: true,
canDownload: true,
canDownloadSource: true,
canShare: true,
},
],
},
},
])
await expect(getUserViewableMaterialTypes('', 'USER', null, 'multi-department')).resolves.toEqual([
'3D场景',
'详情模板',
])
expect(prisma.materialPermissionPackDepartment.findMany).toHaveBeenCalledTimes(1)
})
test('花名册没有部门路径时看不见素材', async () => {
;(prisma.userEmployeeBinding.findFirst as jest.Mock).mockResolvedValue(null)
;(prisma.user.findUnique as jest.Mock).mockResolvedValue({ isSuperAdmin: false, dingtalkUserId: null })
await expect(getUserViewableMaterialTypes('', 'USER', null, 'no-roster')).resolves.toEqual([])
await expect(canUserViewMaterial('', 'USER', null, '3D场景', 'no-roster')).resolves.toBe(false)
})
})