gongxiangp2/lib/__tests__/performance-salary-capabilities.test.ts

34 lines
1.6 KiB
TypeScript

/** @jest-environment node */
import { getServerSession } from 'next-auth'
import { prisma } from '@/lib/prisma'
import { GET } from '@/app/api/workspaces/capabilities/route'
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
jest.mock('@/lib/prisma', () => ({ prisma: { user: { findUnique: jest.fn() } } }))
const getSession = getServerSession as jest.Mock
const findUser = prisma.user.findUnique as jest.Mock
beforeEach(() => jest.resetAllMocks())
test.each([null, { user: { id: 'outside', canAccessBrandCenter: false } }])('未登录或品牌中心范围外不提供入口', async session => {
getSession.mockResolvedValue(session)
const response = await GET()
expect(await response.json()).toEqual({ canViewPerformanceSalary: false })
expect(response.headers.get('Cache-Control')).toBe('private, no-store')
expect(findUser).not.toHaveBeenCalled()
})
test.each([true, false])('仅返回服务器核实的薪资能力:%s', async isSuperAdmin => {
getSession.mockResolvedValue({ user: { id: 'user', canAccessBrandCenter: true, isSuperAdmin: true } })
findUser.mockResolvedValue({ id: 'user', isActive: true, status: 'active', isSuperAdmin, archivedAt: null })
expect(await (await GET()).json()).toEqual({ canViewPerformanceSalary: isSuperAdmin })
})
test('权限查询失败按无权限处理', async () => {
getSession.mockResolvedValue({ user: { id: 'user', canAccessBrandCenter: true } })
findUser.mockRejectedValue(new Error('database unavailable'))
expect(await (await GET()).json()).toEqual({ canViewPerformanceSalary: false })
})