2026-05-25 14:52:24 +08:00

130 lines
3.2 KiB
TypeScript

jest.mock('@/lib/auth', () => ({
authOptions: {},
}))
jest.mock('next-auth', () => ({
getServerSession: jest.fn(),
}))
jest.mock('@/lib/admin-permissions', () => ({
isCurrentSuperAdmin: jest.fn(),
}))
jest.mock('@/lib/prisma', () => ({
prisma: {
user: {
count: jest.fn(),
},
material: {
count: jest.fn(),
},
systemConfig: {
findUnique: jest.fn(),
},
},
}))
jest.mock('@/lib/license', () => ({
getLicense: jest.fn(),
}))
import { GET } from '../route'
const { getServerSession } = require('next-auth')
const { isCurrentSuperAdmin } = require('@/lib/admin-permissions')
const { prisma } = require('@/lib/prisma')
const { getLicense } = require('@/lib/license')
const activeProLicense = {
type: 'PRO',
expiresAt: null,
limits: {
maxUsers: 100,
maxMaterials: 1000,
},
features: {
customTemplates: true,
systemSettings: true,
departmentIsolation: true,
apiAccess: true,
intranetPenetration: true,
},
}
describe('GET /api/license/info', () => {
beforeEach(() => {
jest.resetAllMocks()
getServerSession.mockResolvedValue({
user: { id: 'admin-1', role: 'ADMIN', isSuperAdmin: false },
})
isCurrentSuperAdmin.mockResolvedValue(false)
prisma.user.count.mockResolvedValue(12)
prisma.material.count.mockResolvedValue(345)
prisma.systemConfig.findUnique.mockResolvedValue({
value: JSON.stringify({
licenseKey: 'FLNXI-PRO-SECRET-0001',
lastVerified: '2026-05-24T00:00:00.000Z',
}),
})
getLicense.mockReturnValue(activeProLicense)
})
it('未登录不能读取许可证详情和使用规模', async () => {
getServerSession.mockResolvedValue(null)
const response = await GET()
const data = await response.json()
expect(response.status).toBe(401)
expect(data.error).toBe('未登录')
expect(prisma.user.count).not.toHaveBeenCalled()
expect(prisma.material.count).not.toHaveBeenCalled()
})
it('普通用户不能读取许可证详情', async () => {
getServerSession.mockResolvedValue({
user: { id: 'user-1', role: 'USER' },
})
const response = await GET()
const data = await response.json()
expect(response.status).toBe(403)
expect(data.error).toBe('权限不足')
expect(prisma.user.count).not.toHaveBeenCalled()
})
it('普通管理员只能看到脱敏激活码', async () => {
const response = await GET()
const data = await response.json()
expect(response.status).toBe(200)
expect(data.licenseKey).toBe('FLNX****0001')
expect(data.usage.users.current).toBe(12)
expect(data.usage.materials.current).toBe(345)
})
it('最高权限账号可以看到完整激活码', async () => {
isCurrentSuperAdmin.mockResolvedValue(true)
const response = await GET()
const data = await response.json()
expect(response.status).toBe(200)
expect(data.licenseKey).toBe('FLNXI-PRO-SECRET-0001')
})
it('管理员在未激活时可读取未激活状态', async () => {
getLicense.mockReturnValue(null)
const response = await GET()
const data = await response.json()
expect(response.status).toBe(200)
expect(data.activated).toBe(false)
expect(data.licenseKey).toBeNull()
expect(data.usage.users.current).toBe(12)
})
})