gongxiangp2/lib/__tests__/ai-detail-access.test.ts

21 lines
1.5 KiB
TypeScript

/** @jest-environment node */
import { assertDetailGeneratorUser } from '@/lib/detail-generator/access'
import { isSuperAdmin } from '@/lib/department-permission'
import { prisma } from '@/lib/prisma'
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
jest.mock('@/lib/department-permission', () => ({ isSuperAdmin: jest.fn() }))
jest.mock('@/lib/prisma', () => ({ prisma: { user: { findUnique: jest.fn() } } }))
const originalFlag = process.env.DETAIL_GENERATOR_ENABLED
beforeEach(() => { jest.clearAllMocks(); process.env.DETAIL_GENERATOR_ENABLED = 'true'; (isSuperAdmin as jest.Mock).mockResolvedValue(true) })
afterAll(() => { if (originalFlag === undefined) delete process.env.DETAIL_GENERATOR_ENABLED; else process.env.DETAIL_GENERATOR_ENABLED = originalFlag })
test('an old login cannot run generation when the database account is inactive even if status says active', async () => {
;(prisma.user.findUnique as jest.Mock).mockResolvedValue({ isActive: false, status: 'active', archivedAt: null })
await expect(assertDetailGeneratorUser('owner')).rejects.toMatchObject({ status: 403, code: 'account_inactive' })
})
test('revoked super administrator fails before opening a model execution account', async () => {
;(isSuperAdmin as jest.Mock).mockResolvedValue(false)
await expect(assertDetailGeneratorUser('owner')).rejects.toMatchObject({ status: 403, code: 'forbidden' })
expect(prisma.user.findUnique).not.toHaveBeenCalled()
})