70 lines
2.4 KiB
TypeScript
70 lines
2.4 KiB
TypeScript
import { prisma } from '@/lib/prisma'
|
|
import { resolveBrandCenterAccessForUser } from '@/lib/brand-center-access'
|
|
|
|
jest.mock('@/lib/prisma', () => ({
|
|
prisma: {
|
|
user: { findUnique: jest.fn() },
|
|
performanceSurveyEmployeeProfile: { findUnique: jest.fn() },
|
|
organizationUnit: { findMany: jest.fn() },
|
|
},
|
|
}))
|
|
|
|
const mockedPrisma = prisma as unknown as {
|
|
user: { findUnique: jest.Mock }
|
|
performanceSurveyEmployeeProfile: { findUnique: jest.Mock }
|
|
organizationUnit: { findMany: jest.Mock }
|
|
}
|
|
|
|
describe('resolveBrandCenterAccessForUser', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
mockedPrisma.organizationUnit.findMany.mockResolvedValue([])
|
|
mockedPrisma.performanceSurveyEmployeeProfile.findUnique.mockResolvedValue(null)
|
|
})
|
|
|
|
test('系统管理员不依赖人员绑定即可进入', async () => {
|
|
await expect(resolveBrandCenterAccessForUser('admin', { role: 'SYSTEM_ADMIN' })).resolves.toBe(true)
|
|
expect(mockedPrisma.user.findUnique).not.toHaveBeenCalled()
|
|
})
|
|
|
|
test('读取活跃绑定的多部门任职路径', async () => {
|
|
mockedPrisma.user.findUnique.mockResolvedValue({
|
|
dingtalkUserId: 'ding-1',
|
|
employeeBindings: [{
|
|
employeeProfile: {
|
|
departmentPath: '集团/摄影中心',
|
|
employmentState: 'ACTIVE',
|
|
departmentMemberships: [{
|
|
departmentPath: '集团/品牌中心/综合支持部',
|
|
sourceDeptId: '20',
|
|
}],
|
|
},
|
|
}],
|
|
})
|
|
|
|
await expect(resolveBrandCenterAccessForUser('user-1', { role: 'USER' })).resolves.toBe(true)
|
|
})
|
|
|
|
test('部门路径缺失时可按任职部门 ID 解析真实组织路径', async () => {
|
|
mockedPrisma.user.findUnique.mockResolvedValue({
|
|
dingtalkUserId: null,
|
|
employeeBindings: [{
|
|
employeeProfile: {
|
|
departmentPath: null,
|
|
employmentState: 'ACTIVE',
|
|
departmentMemberships: [{ departmentPath: null, sourceDeptId: '20' }],
|
|
},
|
|
}],
|
|
})
|
|
mockedPrisma.organizationUnit.findMany.mockResolvedValue([{ path: '集团/品牌中心' }])
|
|
|
|
await expect(resolveBrandCenterAccessForUser('user-2', { role: 'USER' })).resolves.toBe(true)
|
|
})
|
|
|
|
test('未绑定且不在品牌中心时按无权限处理', async () => {
|
|
mockedPrisma.user.findUnique.mockResolvedValue({ dingtalkUserId: null, employeeBindings: [] })
|
|
|
|
await expect(resolveBrandCenterAccessForUser('user-3', { role: 'USER' })).resolves.toBe(false)
|
|
})
|
|
})
|