57 lines
2.0 KiB
TypeScript
57 lines
2.0 KiB
TypeScript
import { NextRequest } from 'next/server'
|
|
import { POST } from '../route'
|
|
import { runLicenseVerification } from '@/lib/license/license-service'
|
|
import { requireSuperAdminAfterInitialSetup } from '@/lib/license/license-route-guards'
|
|
|
|
jest.mock('@/lib/license/license-service', () => ({
|
|
runLicenseVerification: jest.fn(),
|
|
}))
|
|
|
|
jest.mock('@/lib/license/license-route-guards', () => ({
|
|
requireSuperAdminAfterInitialSetup: jest.fn(),
|
|
}))
|
|
|
|
const mockRunLicenseVerification = runLicenseVerification as jest.MockedFunction<typeof runLicenseVerification>
|
|
const mockRequireSuperAdminAfterInitialSetup = requireSuperAdminAfterInitialSetup as jest.MockedFunction<typeof requireSuperAdminAfterInitialSetup>
|
|
|
|
function createRequest() {
|
|
return new NextRequest('http://localhost:3000/api/license/verify', {
|
|
method: 'POST',
|
|
})
|
|
}
|
|
|
|
describe('POST /api/license/verify', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
mockRequireSuperAdminAfterInitialSetup.mockResolvedValue({ allowed: true })
|
|
mockRunLicenseVerification.mockResolvedValue({ valid: true })
|
|
})
|
|
|
|
it('已有最高权限账号后,未授权请求不能触发许可证校验写入', async () => {
|
|
mockRequireSuperAdminAfterInitialSetup.mockResolvedValueOnce({
|
|
allowed: false,
|
|
status: 403,
|
|
error: '系统已初始化,许可证变更需要最高权限账号登录',
|
|
})
|
|
|
|
const response = await POST(createRequest())
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(403)
|
|
expect(data).toEqual({
|
|
valid: false,
|
|
error: '系统已初始化,许可证变更需要最高权限账号登录',
|
|
})
|
|
expect(mockRunLicenseVerification).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('最高权限账号可触发服务端许可证校验', async () => {
|
|
const response = await POST(createRequest())
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(data).toEqual({ valid: true })
|
|
expect(mockRunLicenseVerification).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|