2026-05-28 15:11:36 +08:00

89 lines
2.9 KiB
TypeScript

import { NextRequest } from 'next/server'
jest.mock('@/lib/license/license-service', () => ({
getLocalLicenseReadOnly: jest.fn(),
isLicenseConfigExpired: jest.fn(),
}))
const { getLocalLicenseReadOnly, isLicenseConfigExpired } = require('@/lib/license/license-service')
import { GET } from '../route'
describe('GET /api/license/check', () => {
beforeEach(() => {
jest.clearAllMocks()
isLicenseConfigExpired.mockReturnValue(false)
})
test('本地许可证通过验签时应恢复激活状态并写入 cookie', async () => {
getLocalLicenseReadOnly.mockResolvedValue({
instanceId: 'test-instance-id',
licenseKey: 'license-key',
licenseData: 'encrypted-license',
lastVerified: new Date().toISOString(),
cachedConfig: {
type: 'FREE',
limits: { maxUsers: 10, maxMaterials: 100 },
features: {
customTemplates: false,
systemSettings: false,
departmentIsolation: false,
apiAccess: false,
intranetPenetration: false,
},
expiresAt: null,
leaseExpiresAt: new Date(Date.now() + 60_000).toISOString(),
},
})
const response = await GET(new NextRequest('https://localhost/api/license/check'))
const data = await response.json()
expect(response.status).toBe(200)
expect(data.activated).toBe(true)
expect(response.cookies.get('license_activated')?.value).toBe('true')
expect(isLicenseConfigExpired).toHaveBeenCalled()
})
test('本地许可证为空或验签失败时应返回未激活', async () => {
getLocalLicenseReadOnly.mockResolvedValue(null)
const response = await GET(new NextRequest('http://localhost/api/license/check'))
const data = await response.json()
expect(response.status).toBe(200)
expect(data.activated).toBe(false)
expect(response.cookies.get('license_activated')).toBeUndefined()
})
test('本地许可证在线租约过期时不应恢复激活 cookie', async () => {
isLicenseConfigExpired.mockReturnValue(true)
getLocalLicenseReadOnly.mockResolvedValue({
instanceId: 'test-instance-id',
licenseKey: 'license-key',
licenseData: 'encrypted-license',
lastVerified: new Date().toISOString(),
cachedConfig: {
type: 'FREE',
limits: { maxUsers: 10, maxMaterials: 100 },
features: {
customTemplates: false,
systemSettings: false,
departmentIsolation: false,
apiAccess: false,
intranetPenetration: false,
},
expiresAt: null,
leaseExpiresAt: new Date(Date.now() - 60_000).toISOString(),
},
})
const response = await GET(new NextRequest('http://localhost/api/license/check'))
const data = await response.json()
expect(response.status).toBe(200)
expect(data.activated).toBe(false)
expect(response.cookies.get('license_activated')).toBeUndefined()
})
})