gongxiangp2/scripts/audit-permission-pack-parity.test.ts

112 lines
4.5 KiB
TypeScript

/** @jest-environment node */
import { Prisma, type PrismaClient } from '@prisma/client'
import { auditPermissionPackParity } from './audit-permission-pack-parity'
jest.mock('@/lib/prisma', () => ({ prisma: {} }))
const allow = (materialType: string) => ({
materialType,
canView: true,
canDownload: true,
canDownloadSource: true,
canShare: true,
canEditAssets: false,
canAccessPrivate: false,
})
const operatingPath = '公司/运营中心A'
const brandPath = '公司/品牌中心'
function createFixture() {
const users = [
{ id: 'dingtalk', username: '钉钉匹配', isSuperAdmin: false },
{ id: 'empty-org-pack', username: '空组覆盖', isSuperAdmin: false },
{ id: 'no-path', username: '未匹配', isSuperAdmin: false },
{ id: 'super', username: '管理员', isSuperAdmin: true },
]
const tx = {
$executeRaw: jest.fn(),
user: {
findMany: jest.fn().mockResolvedValueOnce(users).mockResolvedValueOnce([]),
findUnique: jest.fn(async ({ where }) => where.id === 'dingtalk' ? { dingtalkUserId: 'dd-id' } : null),
},
userEmployeeBinding: {
findFirst: jest.fn(async ({ where }) => where.userId === 'empty-org-pack'
? { employeeProfile: { departmentPath: brandPath } } : null),
},
performanceSurveyEmployeeProfile: {
findUnique: jest.fn().mockResolvedValue({ departmentPath: operatingPath }),
},
materialPermissionPackDepartment: {
findMany: jest.fn().mockResolvedValue([
{ organizationUnit: { path: brandPath, isActive: true }, pack: { items: [] } },
]),
},
departmentMaterialPermission: {
findMany: jest.fn().mockResolvedValue([
{ department: '运营中心', ...allow('主图视频') },
{ department: '视觉中心', ...allow('任务-详情') },
]),
},
userPermissionInherit: {
findMany: jest.fn().mockResolvedValue([
{ pack: { items: [{ ...allow('主图视频'), canView: false, canDownloadSource: false, canShare: false }] } },
]),
},
}
const transaction = jest.fn(async (work: (db: typeof tx) => unknown) => work(tx))
const client = { $transaction: transaction } as unknown as PrismaClient
return { client, tx, transaction }
}
test('在只读快照中找出钉钉匹配用户的实际差异,空组覆盖旧表且个人组可补齐权限', async () => {
const { client, tx, transaction } = createFixture()
const result = await auditPermissionPackParity(client)
expect(transaction).toHaveBeenCalledWith(expect.any(Function), {
isolationLevel: Prisma.TransactionIsolationLevel.RepeatableRead,
timeout: 300_000,
})
expect(tx.$executeRaw.mock.calls[0][0][0]).toBe('SET TRANSACTION READ ONLY')
expect(tx.$executeRaw.mock.invocationCallOrder[0]).toBeLessThan(tx.departmentMaterialPermission.findMany.mock.invocationCallOrder[0])
expect(tx.departmentMaterialPermission.findMany).toHaveBeenCalledTimes(1)
expect(tx.user.findMany).toHaveBeenNthCalledWith(2, expect.objectContaining({
where: { OR: [{ isActive: true }, { status: 'active' }], id: { gt: 'super' } },
}))
expect(tx.userPermissionInherit.findMany).toHaveBeenCalledTimes(1)
expect(result).toMatchObject({
activeUsers: 4,
superAdmins: 1,
usersWithoutRosterPath: 1,
differenceUsers: 1,
differences: [{
userId: 'dingtalk',
username: '钉钉匹配',
rosterPaths: [operatingPath],
legacySources: [{ path: operatingPath, key: '运营中心', source: 'legacy' }],
}],
})
for (const materialType of ['主图视频', '淘系-主图视频']) {
expect(result.differences[0].changes.filter((change) => change.materialType === materialType)).toEqual([
{ materialType, action: 'canView', entryPoint: 'permission', current: true, organizationOnly: false },
{ materialType, action: 'canDownloadSource', entryPoint: 'permission', current: true, organizationOnly: false },
{ materialType, action: 'canShare', entryPoint: 'permission', current: true, organizationOnly: false },
{ materialType, action: 'canView', entryPoint: 'listing', current: true, organizationOnly: false },
])
}
})
test('个人组覆盖全部旧授权时,全员对账返回零差异', async () => {
const { client, tx } = createFixture()
tx.userPermissionInherit.findMany.mockResolvedValue([{ pack: { items: [allow('主图视频')] } }])
await expect(auditPermissionPackParity(client)).resolves.toEqual({
activeUsers: 4,
superAdmins: 1,
usersWithoutRosterPath: 1,
differenceUsers: 0,
differences: [],
})
})