95 lines
2.6 KiB
TypeScript
95 lines
2.6 KiB
TypeScript
import { NextRequest } from 'next/server'
|
|
import { POST } from '../route'
|
|
|
|
jest.mock('@/lib/prisma', () => ({
|
|
prisma: {
|
|
user: {
|
|
update: jest.fn(),
|
|
},
|
|
dingtalkLoginToken: {
|
|
findUnique: jest.fn(),
|
|
updateMany: jest.fn(),
|
|
create: jest.fn(),
|
|
},
|
|
$transaction: jest.fn(),
|
|
},
|
|
}))
|
|
|
|
const { prisma } = require('@/lib/prisma')
|
|
|
|
describe('POST /api/dingtalk/complete-profile', () => {
|
|
beforeEach(() => {
|
|
jest.resetAllMocks()
|
|
})
|
|
|
|
it('不再接受裸 userId 完善资料', async () => {
|
|
const response = await POST(
|
|
new NextRequest('http://localhost/api/dingtalk/complete-profile', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ userId: 'user-1', department: '设计部' }),
|
|
})
|
|
)
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(400)
|
|
expect(data.success).toBe(false)
|
|
expect(prisma.user.update).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('凭有效 profileToken 更新部门,并为已激活用户签发登录票据', async () => {
|
|
const mockTx = {
|
|
dingtalkLoginToken: {
|
|
findUnique: jest.fn().mockResolvedValue({
|
|
id: 'profile-token-row',
|
|
purpose: 'complete_profile',
|
|
usedAt: null,
|
|
expiresAt: new Date(Date.now() + 60_000),
|
|
user: {
|
|
id: 'user-1',
|
|
email: 'user@example.com',
|
|
username: '张三',
|
|
role: 'USER',
|
|
isSuperAdmin: false,
|
|
avatar: null,
|
|
isActive: false,
|
|
},
|
|
}),
|
|
updateMany: jest.fn().mockResolvedValue({ count: 1 }),
|
|
},
|
|
}
|
|
prisma.$transaction.mockImplementation((callback: any) => callback(mockTx))
|
|
prisma.user.update.mockResolvedValue({
|
|
id: 'user-1',
|
|
email: 'user@example.com',
|
|
username: '张三',
|
|
realName: '张三',
|
|
avatar: null,
|
|
role: 'USER',
|
|
department: '设计部',
|
|
isActive: true,
|
|
})
|
|
prisma.dingtalkLoginToken.create.mockResolvedValue({ id: 'login-token-row' })
|
|
|
|
const response = await POST(
|
|
new NextRequest('http://localhost/api/dingtalk/complete-profile', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ profileToken: 'profile-token', department: '设计部' }),
|
|
})
|
|
)
|
|
const data = await response.json()
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(data.loginToken).toEqual(expect.any(String))
|
|
expect(prisma.user.update).toHaveBeenCalledWith({
|
|
where: { id: 'user-1' },
|
|
data: { department: '设计部' },
|
|
})
|
|
expect(prisma.dingtalkLoginToken.create).toHaveBeenCalledWith({
|
|
data: expect.objectContaining({
|
|
userId: 'user-1',
|
|
purpose: 'login',
|
|
}),
|
|
})
|
|
})
|
|
})
|