2026-04-02 11:20:20 +08:00
|
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
|
|
|
|
|
|
const getMock = vi.fn()
|
|
|
|
|
const postMock = vi.fn()
|
|
|
|
|
const putMock = vi.fn()
|
|
|
|
|
const delMock = vi.fn()
|
|
|
|
|
|
|
|
|
|
vi.mock('@/lib/http/client', () => ({
|
|
|
|
|
get: getMock,
|
|
|
|
|
post: postMock,
|
|
|
|
|
put: putMock,
|
|
|
|
|
del: delMock,
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
describe('users service', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
getMock.mockReset()
|
|
|
|
|
postMock.mockReset()
|
|
|
|
|
putMock.mockReset()
|
|
|
|
|
delMock.mockReset()
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-28 15:19:13 +08:00
|
|
|
it('normalizes backend user list payloads that use users/limit/offset fields', async () => {
|
|
|
|
|
getMock.mockResolvedValue({
|
|
|
|
|
users: [
|
|
|
|
|
{
|
|
|
|
|
id: 7,
|
|
|
|
|
username: 'e2e_admin',
|
|
|
|
|
email: 'admin@example.com',
|
|
|
|
|
nickname: '管理员',
|
|
|
|
|
status: '1',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
total: 1,
|
|
|
|
|
limit: 20,
|
|
|
|
|
offset: 0,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const { listUsers } = await import('./users')
|
|
|
|
|
const result = await listUsers({ page: 1, page_size: 20 })
|
|
|
|
|
|
|
|
|
|
expect(getMock).toHaveBeenCalledWith('/users', { page: 1, page_size: 20 })
|
|
|
|
|
expect(result).toEqual({
|
|
|
|
|
items: [
|
|
|
|
|
{
|
|
|
|
|
id: 7,
|
|
|
|
|
username: 'e2e_admin',
|
|
|
|
|
email: 'admin@example.com',
|
|
|
|
|
phone: '',
|
|
|
|
|
nickname: '管理员',
|
|
|
|
|
avatar: '',
|
|
|
|
|
gender: 0,
|
|
|
|
|
birthday: '',
|
|
|
|
|
region: '',
|
|
|
|
|
bio: '',
|
|
|
|
|
status: 1,
|
|
|
|
|
last_login_at: '',
|
|
|
|
|
last_login_ip: '',
|
|
|
|
|
created_at: '',
|
|
|
|
|
updated_at: '',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
total: 1,
|
|
|
|
|
page: 1,
|
|
|
|
|
page_size: 20,
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2026-04-02 11:20:20 +08:00
|
|
|
it('creates a user through the protected users endpoint', async () => {
|
|
|
|
|
const payload = {
|
|
|
|
|
username: 'new-user',
|
|
|
|
|
password: 'Pass123!@#',
|
|
|
|
|
role_ids: [2],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { createUser } = await import('./users')
|
|
|
|
|
await createUser(payload)
|
|
|
|
|
|
|
|
|
|
expect(postMock).toHaveBeenCalledWith('/users', payload)
|
|
|
|
|
})
|
|
|
|
|
})
|