97 lines
2.5 KiB
TypeScript
97 lines
2.5 KiB
TypeScript
import { NextRequest } from 'next/server'
|
|
|
|
jest.mock('@/lib/auth', () => ({
|
|
authOptions: {},
|
|
}))
|
|
|
|
jest.mock('next-auth', () => ({
|
|
getServerSession: jest.fn(),
|
|
}))
|
|
|
|
jest.mock('@/lib/admin-permissions', () => ({
|
|
isCurrentSuperAdmin: jest.fn(),
|
|
}))
|
|
|
|
jest.mock('@/lib/video-transcode-queue', () => ({
|
|
transcodeQueue: {
|
|
getStatus: jest.fn(() => ({
|
|
queueLength: 0,
|
|
processingCount: 0,
|
|
maxConcurrent: 2,
|
|
processingIds: [],
|
|
queuedIds: [],
|
|
})),
|
|
},
|
|
}))
|
|
|
|
jest.mock('@/lib/prisma', () => ({
|
|
prisma: {
|
|
material: {
|
|
count: jest.fn(),
|
|
findMany: jest.fn(),
|
|
},
|
|
videoTranscodeJob: {
|
|
count: jest.fn(),
|
|
},
|
|
},
|
|
}))
|
|
|
|
const { getServerSession } = require('next-auth')
|
|
const { isCurrentSuperAdmin } = require('@/lib/admin-permissions')
|
|
const { prisma } = require('@/lib/prisma')
|
|
|
|
import { GET } from '../route'
|
|
|
|
function createRequest() {
|
|
return new NextRequest('http://localhost/api/admin/video-transcode-stats')
|
|
}
|
|
|
|
describe('admin video transcode stats 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.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.material.count).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('最高权限账号可以读取转码统计', async () => {
|
|
getServerSession.mockResolvedValue({
|
|
user: { id: 'super-1', role: 'ADMIN', isSuperAdmin: true },
|
|
})
|
|
isCurrentSuperAdmin.mockResolvedValue(true)
|
|
prisma.material.count.mockResolvedValue(0)
|
|
prisma.videoTranscodeJob.count.mockResolvedValue(0)
|
|
prisma.material.findMany.mockResolvedValue([])
|
|
|
|
const response = await GET(createRequest())
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(data.statistics.total).toBe(0)
|
|
expect(data.jobs.failed).toBe(0)
|
|
})
|
|
})
|