75 lines
2.1 KiB
TypeScript
75 lines
2.1 KiB
TypeScript
import { NextRequest } from 'next/server'
|
|
|
|
jest.mock('next-auth', () => ({
|
|
getServerSession: jest.fn(),
|
|
}))
|
|
|
|
jest.mock('@/lib/auth', () => ({
|
|
authOptions: {},
|
|
}))
|
|
|
|
jest.mock('@/lib/admin-permissions', () => ({
|
|
isCurrentSuperAdmin: jest.fn(),
|
|
}))
|
|
|
|
jest.mock('@/lib/prisma', () => ({
|
|
prisma: {
|
|
$queryRaw: jest.fn(),
|
|
},
|
|
}))
|
|
|
|
const { getServerSession } = require('next-auth')
|
|
const { isCurrentSuperAdmin } = require('@/lib/admin-permissions')
|
|
const { prisma } = require('@/lib/prisma')
|
|
|
|
import { GET } from '../route'
|
|
|
|
describe('GET /api/admin/material-logs', () => {
|
|
beforeEach(() => {
|
|
jest.resetAllMocks()
|
|
})
|
|
|
|
it('非最高权限账号不能查看素材日志', async () => {
|
|
getServerSession.mockResolvedValue({ user: { id: 'admin-1', role: 'ADMIN' } })
|
|
isCurrentSuperAdmin.mockResolvedValue(false)
|
|
|
|
const response = await GET(new NextRequest('http://localhost/api/admin/material-logs'))
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(403)
|
|
expect(data.error).toContain('最高权限')
|
|
expect(prisma.$queryRaw).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('最高权限账号可以查看素材日志且不执行运行期 DDL', async () => {
|
|
getServerSession.mockResolvedValue({ user: { id: 'super-1', role: 'ADMIN' } })
|
|
isCurrentSuperAdmin.mockResolvedValue(true)
|
|
prisma.$queryRaw.mockResolvedValue([
|
|
{
|
|
id: 'log-1',
|
|
action: 'UPLOAD',
|
|
materialId: 'material-1',
|
|
userId: 'user-1',
|
|
timestamp: new Date('2026-05-28T10:00:00.000Z'),
|
|
changes: null,
|
|
materialSnapshot: { productModel: '旧素材' },
|
|
username: '张三',
|
|
email: 'user@example.com',
|
|
productModel: '当前素材',
|
|
},
|
|
])
|
|
|
|
const response = await GET(new NextRequest('http://localhost/api/admin/material-logs'))
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(data.logs).toHaveLength(1)
|
|
expect(data.logs[0]).toMatchObject({
|
|
action: '上传素材',
|
|
materialName: '当前素材',
|
|
username: '张三',
|
|
})
|
|
expect(prisma.$queryRaw).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|