36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
/** @jest-environment node */
|
|
|
|
import { NextRequest } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { POST } from './route'
|
|
|
|
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
|
|
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
|
|
jest.mock('@/lib/department-permission', () => ({ isSystemAdmin: (role: string) => role === 'SYSTEM_ADMIN' }))
|
|
jest.mock('@/lib/prisma', () => ({ prisma: { user: { update: jest.fn() } } }))
|
|
|
|
test.each([
|
|
[{ isActive: false }, 'suspended'],
|
|
[{ status: 'pending' }, 'suspended'],
|
|
[{ status: 'archived' }, 'archived'],
|
|
[{ isActive: true }, 'active'],
|
|
])('管理员设置用户状态 %j → %s', async (input, state) => {
|
|
jest.mocked(getServerSession).mockResolvedValue({ user: { id: 'operator', role: 'SYSTEM_ADMIN' } })
|
|
const response = await POST(new NextRequest('http://example.test/api/admin/users/toggle-active', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ userId: 'target', ...input }),
|
|
}))
|
|
expect(response.status).toBe(200)
|
|
expect(prisma.user.update).toHaveBeenLastCalledWith({
|
|
where: { id: 'target' },
|
|
data: {
|
|
status: state,
|
|
isActive: state === 'active',
|
|
archivedAt: state === 'archived' ? expect.any(Date) : null,
|
|
statusChangedAt: expect.any(Date),
|
|
statusChangedBy: 'operator',
|
|
},
|
|
})
|
|
})
|