58 lines
3.2 KiB
TypeScript
58 lines
3.2 KiB
TypeScript
/** @jest-environment node */
|
|
import { prisma } from '@/lib/prisma'
|
|
import { getServerSession } from 'next-auth'
|
|
import { isSuperAdmin } from '@/lib/department-permission'
|
|
import { loadPermissionGroupDepartments } from '@/lib/permission-groups'
|
|
|
|
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
|
|
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
|
|
jest.mock('@/lib/department-permission', () => ({ isSuperAdmin: jest.fn() }))
|
|
jest.mock('@/lib/user-permission-inherit', () => ({ ensureDefaultPermissionPacks: jest.fn() }))
|
|
jest.mock('@/lib/permission-groups', () => ({ loadPermissionGroupDepartments: jest.fn() }))
|
|
jest.mock('@/lib/prisma', () => ({ prisma: {
|
|
userEmployeeBinding: { findMany: jest.fn() },
|
|
materialPermissionPack: { findMany: jest.fn() },
|
|
organizationUnit: { findMany: jest.fn() },
|
|
} }))
|
|
const { GET } = require('./route') as typeof import('./route')
|
|
|
|
describe('按人看权限与实时部门组一致', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
;(getServerSession as jest.Mock).mockResolvedValue({ user: { id: 'admin' } })
|
|
;(isSuperAdmin as jest.Mock).mockResolvedValue(true)
|
|
;(prisma.userEmployeeBinding.findMany as jest.Mock).mockResolvedValue([{
|
|
userId: 'person', user: { username: 'sample', permissionInherits: [] },
|
|
employeeProfile: { displayName: '示例人员', departmentPath: '公司/旧部门',
|
|
departmentMemberships: [{ sourceDeptId: '42', departmentPath: '公司/旧部门' }] },
|
|
}])
|
|
;(prisma.materialPermissionPack.findMany as jest.Mock).mockResolvedValue([{ id: 'pack', slug: 'sample', name: '示例组' }])
|
|
;(prisma.organizationUnit.findMany as jest.Mock).mockResolvedValue([{ sourceDeptId: 42, path: '公司/新中心/新部门' }])
|
|
;(loadPermissionGroupDepartments as jest.Mock).mockResolvedValue([{
|
|
packId: 'pack', departmentPath: '公司/旧部门', organizationUnit: { path: '公司/新中心/新部门', isActive: true },
|
|
}])
|
|
})
|
|
|
|
test('员工快照和关联快照都过期时,仍显示相同 ID 的当前部门组', async () => {
|
|
const response = await GET({ url: 'https://local/api/organization/user-permissions?q=sample' } as never)
|
|
expect(response.status).toBe(200)
|
|
expect((await response.json()).people[0]).toMatchObject({
|
|
departmentPath: '公司/新中心/新部门', departmentName: '新部门',
|
|
departmentGroups: [{ slug: 'sample', attachedPath: '公司/新中心/新部门', source: 'own' }],
|
|
})
|
|
})
|
|
|
|
test.each([null, { path: '公司/旧部门', isActive: false }])('未确认或已下线的绑定不显示成有效组', async (unit) => {
|
|
;(loadPermissionGroupDepartments as jest.Mock).mockResolvedValue([{ packId: 'pack', departmentPath: '公司/旧部门', organizationUnit: unit }])
|
|
const response = await GET({ url: 'https://local/api/organization/user-permissions?q=sample' } as never)
|
|
expect((await response.json()).people[0].departmentGroups).toEqual([])
|
|
})
|
|
|
|
test('无权查看的人不能查询员工或组数据', async () => {
|
|
;(isSuperAdmin as jest.Mock).mockResolvedValue(false)
|
|
const response = await GET({ url: 'https://local/api/organization/user-permissions?q=sample' } as never)
|
|
expect(response.status).toBe(403)
|
|
expect(prisma.userEmployeeBinding.findMany).not.toHaveBeenCalled()
|
|
})
|
|
})
|