177 lines
5.3 KiB
TypeScript
177 lines
5.3 KiB
TypeScript
import { NextRequest } from 'next/server'
|
|
import { POST } from '../route'
|
|
|
|
jest.mock('@/lib/auth', () => ({
|
|
authOptions: {},
|
|
}))
|
|
|
|
jest.mock('next-auth', () => ({
|
|
getServerSession: jest.fn(),
|
|
}))
|
|
|
|
jest.mock('@/lib/prisma', () => ({
|
|
prisma: {
|
|
user: {
|
|
findFirst: jest.fn(),
|
|
},
|
|
},
|
|
}))
|
|
|
|
jest.mock('@/lib/system-integrity', () => ({
|
|
BUILTIN_TEMPLATE_KEYS: ['3dmax-material'],
|
|
}))
|
|
|
|
jest.mock('@/lib/admin-permissions', () => ({
|
|
isCurrentSuperAdmin: jest.fn(),
|
|
}))
|
|
|
|
jest.mock('@/lib/system-init', () => ({
|
|
getSystemInitToken: jest.fn(() => 'init-token'),
|
|
initializeSystem: jest.fn(),
|
|
}))
|
|
|
|
const { getServerSession } = require('next-auth')
|
|
const { prisma } = require('@/lib/prisma')
|
|
const { isCurrentSuperAdmin } = require('@/lib/admin-permissions')
|
|
const { getSystemInitToken, initializeSystem } = require('@/lib/system-init')
|
|
|
|
describe('POST /api/system/init', () => {
|
|
beforeEach(() => {
|
|
jest.resetAllMocks()
|
|
getServerSession.mockResolvedValue({
|
|
user: {
|
|
id: 'admin-1',
|
|
role: 'ADMIN',
|
|
isSuperAdmin: true,
|
|
},
|
|
})
|
|
prisma.user.findFirst.mockResolvedValue({ id: 'admin-1' })
|
|
isCurrentSuperAdmin.mockResolvedValue(true)
|
|
getSystemInitToken.mockReturnValue('init-token')
|
|
})
|
|
|
|
test('系统已有最高权限账号后,普通管理员不能触发初始化', async () => {
|
|
getServerSession.mockResolvedValue({
|
|
user: {
|
|
id: 'admin-2',
|
|
role: 'ADMIN',
|
|
isSuperAdmin: false,
|
|
},
|
|
})
|
|
prisma.user.findFirst.mockResolvedValue({ id: 'super-1' })
|
|
isCurrentSuperAdmin.mockResolvedValue(false)
|
|
|
|
const response = await POST(new NextRequest('http://localhost/api/system/init', { method: 'POST' }))
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(403)
|
|
expect(data.error).toBe('系统已初始化,后续初始化操作需要最高权限账号登录')
|
|
expect(initializeSystem).not.toHaveBeenCalled()
|
|
})
|
|
|
|
test('系统尚无最高权限账号时,必须携带初始化令牌', async () => {
|
|
getServerSession.mockResolvedValue(null)
|
|
prisma.user.findFirst.mockResolvedValue(null)
|
|
|
|
const response = await POST(new NextRequest('http://localhost/api/system/init', { method: 'POST' }))
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(401)
|
|
expect(data.error).toBe('初始化令牌无效')
|
|
expect(isCurrentSuperAdmin).not.toHaveBeenCalled()
|
|
expect(initializeSystem).not.toHaveBeenCalled()
|
|
})
|
|
|
|
test('系统尚无最高权限账号时,正确初始化令牌可以触发首次初始化', async () => {
|
|
getServerSession.mockResolvedValue(null)
|
|
prisma.user.findFirst.mockResolvedValue(null)
|
|
initializeSystem.mockResolvedValue({
|
|
alreadyInitialized: false,
|
|
initialized: true,
|
|
admin: null,
|
|
defaultDepartment: '默认部门',
|
|
defaultCategory: '默认店铺',
|
|
defaultScene: '默认场景',
|
|
templates: {
|
|
total: 1,
|
|
created: 0,
|
|
createdTemplates: [],
|
|
errors: [],
|
|
},
|
|
})
|
|
|
|
const response = await POST(new NextRequest('http://localhost/api/system/init', {
|
|
method: 'POST',
|
|
headers: { 'x-system-init-token': 'init-token' },
|
|
}))
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(isCurrentSuperAdmin).not.toHaveBeenCalled()
|
|
expect(initializeSystem).toHaveBeenCalled()
|
|
})
|
|
|
|
test('环境变量来源的初始管理员密码不应在初始化响应中回显', async () => {
|
|
initializeSystem.mockResolvedValue({
|
|
alreadyInitialized: false,
|
|
initialized: true,
|
|
admin: {
|
|
email: 'admin@local.flnxi',
|
|
username: 'admin',
|
|
created: true,
|
|
upgraded: false,
|
|
mustChangePassword: true,
|
|
initialPassword: 'Env-Password-Should-Not-Return',
|
|
passwordSource: 'environment',
|
|
},
|
|
defaultDepartment: '默认部门',
|
|
defaultCategory: '默认店铺',
|
|
defaultScene: '默认场景',
|
|
templates: {
|
|
total: 1,
|
|
created: 0,
|
|
createdTemplates: [],
|
|
errors: [],
|
|
},
|
|
})
|
|
|
|
const response = await POST(new NextRequest('http://localhost/api/system/init', { method: 'POST' }))
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(data.data.admin.initialPassword).toBeNull()
|
|
expect(data.data.admin.passwordSource).toBe('environment')
|
|
})
|
|
|
|
test('系统生成的一次性管理员密码仍可在首次初始化响应中返回', async () => {
|
|
initializeSystem.mockResolvedValue({
|
|
alreadyInitialized: false,
|
|
initialized: true,
|
|
admin: {
|
|
email: 'admin@local.flnxi',
|
|
username: 'admin',
|
|
created: true,
|
|
upgraded: false,
|
|
mustChangePassword: true,
|
|
initialPassword: 'Generated-Password-123',
|
|
passwordSource: 'generated',
|
|
},
|
|
defaultDepartment: '默认部门',
|
|
defaultCategory: '默认店铺',
|
|
defaultScene: '默认场景',
|
|
templates: {
|
|
total: 1,
|
|
created: 0,
|
|
createdTemplates: [],
|
|
errors: [],
|
|
},
|
|
})
|
|
|
|
const response = await POST(new NextRequest('http://localhost/api/system/init', { method: 'POST' }))
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(data.data.admin.initialPassword).toBe('Generated-Password-123')
|
|
expect(data.data.admin.passwordSource).toBe('generated')
|
|
})
|
|
})
|