142 lines
4.1 KiB
TypeScript
142 lines
4.1 KiB
TypeScript
import { NextRequest } from 'next/server'
|
|
|
|
jest.mock('@/lib/auth', () => ({
|
|
authOptions: {},
|
|
}))
|
|
|
|
jest.mock('next-auth', () => ({
|
|
getServerSession: jest.fn(),
|
|
}))
|
|
|
|
jest.mock('@/lib/prisma', () => ({
|
|
prisma: {
|
|
user: {
|
|
aggregate: jest.fn(),
|
|
count: jest.fn(),
|
|
groupBy: jest.fn(),
|
|
},
|
|
material: {
|
|
aggregate: jest.fn(),
|
|
count: jest.fn(),
|
|
findMany: jest.fn(),
|
|
},
|
|
systemSettings: {
|
|
findFirst: jest.fn(),
|
|
},
|
|
$queryRaw: jest.fn(),
|
|
},
|
|
}))
|
|
|
|
jest.mock('@/lib/admin-permissions', () => ({
|
|
isCurrentSuperAdmin: jest.fn(),
|
|
}))
|
|
|
|
const { getServerSession } = require('next-auth')
|
|
const { prisma } = require('@/lib/prisma')
|
|
const { isCurrentSuperAdmin } = require('@/lib/admin-permissions')
|
|
|
|
import { GET } from '../system-status/route'
|
|
|
|
function createRequest() {
|
|
return new NextRequest('http://localhost/api/admin/system-status')
|
|
}
|
|
|
|
describe('admin system status auth', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
})
|
|
|
|
it('未登录不能读取系统监控数据', async () => {
|
|
getServerSession.mockResolvedValue(null)
|
|
|
|
const response = await GET(createRequest())
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(401)
|
|
expect(data.error).toContain('未登录')
|
|
expect(isCurrentSuperAdmin).not.toHaveBeenCalled()
|
|
expect(prisma.user.count).not.toHaveBeenCalled()
|
|
expect(prisma.material.count).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('普通用户不能读取系统监控数据', async () => {
|
|
getServerSession.mockResolvedValue({
|
|
user: { id: 'user-1', role: 'USER' },
|
|
})
|
|
isCurrentSuperAdmin.mockResolvedValue(false)
|
|
|
|
const response = await GET(createRequest())
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(403)
|
|
expect(data.error).toContain('最高权限')
|
|
expect(prisma.user.count).not.toHaveBeenCalled()
|
|
expect(prisma.material.count).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('普通管理员不能读取系统监控数据', async () => {
|
|
getServerSession.mockResolvedValue({
|
|
user: { id: 'admin-1', role: 'ADMIN' },
|
|
})
|
|
isCurrentSuperAdmin.mockResolvedValue(false)
|
|
|
|
const response = await GET(createRequest())
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(403)
|
|
expect(data.error).toContain('最高权限')
|
|
expect(prisma.user.count).not.toHaveBeenCalled()
|
|
expect(prisma.material.count).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('最高权限账号可以读取系统监控数据', async () => {
|
|
getServerSession.mockResolvedValue({
|
|
user: { id: 'super-1', role: 'ADMIN', isSuperAdmin: true },
|
|
})
|
|
isCurrentSuperAdmin.mockResolvedValue(true)
|
|
|
|
prisma.user.aggregate.mockResolvedValue({ _count: { id: 1 } })
|
|
prisma.material.aggregate.mockResolvedValue({ _count: { id: 0 } })
|
|
prisma.systemSettings.findFirst.mockResolvedValue({
|
|
enableRegistration: true,
|
|
enableUserActivation: false,
|
|
siteName: 'FLNXI素材库',
|
|
})
|
|
prisma.material.findMany.mockResolvedValue([])
|
|
prisma.user.count
|
|
.mockResolvedValueOnce(1)
|
|
.mockResolvedValueOnce(1)
|
|
prisma.material.count
|
|
.mockResolvedValueOnce(0)
|
|
.mockResolvedValueOnce(0)
|
|
prisma.user.groupBy
|
|
.mockResolvedValueOnce([])
|
|
.mockResolvedValueOnce([])
|
|
prisma.$queryRaw.mockResolvedValue([])
|
|
|
|
const response = await GET(createRequest())
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(data.overview.totalUsers).toBe(1)
|
|
expect(data.status.database).toBe('healthy')
|
|
})
|
|
|
|
it('最高权限账号读取时数据库查询失败会标记为 unhealthy', async () => {
|
|
getServerSession.mockResolvedValue({
|
|
user: { id: 'super-1', role: 'ADMIN', isSuperAdmin: true },
|
|
})
|
|
isCurrentSuperAdmin.mockResolvedValue(true)
|
|
|
|
prisma.user.aggregate.mockRejectedValueOnce(new Error('database connection failed'))
|
|
prisma.$queryRaw.mockRejectedValueOnce(new Error('trend query failed'))
|
|
|
|
const response = await GET(createRequest())
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(data.overview.totalUsers).toBe(0)
|
|
expect(data.status.database).toBe('unhealthy')
|
|
})
|
|
})
|