106 lines
3.0 KiB
TypeScript
106 lines
3.0 KiB
TypeScript
jest.mock('@/lib/auth', () => ({
|
|
authOptions: {},
|
|
}))
|
|
|
|
jest.mock('next-auth', () => ({
|
|
getServerSession: jest.fn(),
|
|
}))
|
|
|
|
jest.mock('@/lib/admin-permissions', () => ({
|
|
isCurrentSuperAdmin: jest.fn(),
|
|
}))
|
|
|
|
jest.mock('@/lib/prisma', () => ({
|
|
prisma: {
|
|
user: {
|
|
findUnique: jest.fn(),
|
|
},
|
|
},
|
|
}))
|
|
|
|
jest.mock('bcryptjs', () => ({
|
|
compare: jest.fn(),
|
|
}))
|
|
|
|
import { NextRequest } from 'next/server'
|
|
import { POST } from '../route'
|
|
|
|
const { getServerSession } = require('next-auth')
|
|
const { isCurrentSuperAdmin } = require('@/lib/admin-permissions')
|
|
const { prisma } = require('@/lib/prisma')
|
|
const bcrypt = require('bcryptjs')
|
|
|
|
function createRequest(password?: string) {
|
|
return new NextRequest('http://localhost/api/auth/verify-admin-password', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ password }),
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
}
|
|
|
|
describe('POST /api/auth/verify-admin-password', () => {
|
|
beforeEach(() => {
|
|
jest.resetAllMocks()
|
|
getServerSession.mockResolvedValue({
|
|
user: { id: 'admin-1', role: 'ADMIN', isSuperAdmin: true },
|
|
})
|
|
isCurrentSuperAdmin.mockResolvedValue(true)
|
|
prisma.user.findUnique.mockResolvedValue({ password: 'hashed-password' })
|
|
bcrypt.compare.mockResolvedValue(true)
|
|
})
|
|
|
|
it('未登录时拒绝验证', async () => {
|
|
getServerSession.mockResolvedValue(null)
|
|
|
|
const response = await POST(createRequest('password'))
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(401)
|
|
expect(data.error).toBe('未登录')
|
|
expect(isCurrentSuperAdmin).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('不是最高权限账号时拒绝验证', async () => {
|
|
isCurrentSuperAdmin.mockResolvedValue(false)
|
|
|
|
const response = await POST(createRequest('password'))
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(403)
|
|
expect(data.error).toBe('需要超级管理员权限')
|
|
expect(prisma.user.findUnique).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('密码为空时返回输入错误', async () => {
|
|
const response = await POST(createRequest(''))
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(400)
|
|
expect(data.error).toBe('请输入密码')
|
|
expect(prisma.user.findUnique).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('密码不匹配时拒绝解锁', async () => {
|
|
bcrypt.compare.mockResolvedValue(false)
|
|
|
|
const response = await POST(createRequest('wrong-password'))
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(400)
|
|
expect(data.error).toBe('密码错误')
|
|
expect(bcrypt.compare).toHaveBeenCalledWith('wrong-password', 'hashed-password')
|
|
})
|
|
|
|
it('最高权限账号密码正确时允许解锁', async () => {
|
|
const response = await POST(createRequest('right-password'))
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(data.success).toBe(true)
|
|
expect(prisma.user.findUnique).toHaveBeenCalledWith({
|
|
where: { id: 'admin-1' },
|
|
select: { password: true },
|
|
})
|
|
})
|
|
})
|