| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import { describe, it, expect, vi, beforeEach } from 'vitest'
- import { login, getInfo, logout, changePassword } from '@/api/login'
- import * as request from '@/utils/request'
- import { mockLoginResponse, mockAdminInfo, wrapResponse, wrapErrorResponse } from '../../fixtures'
- // Mock request module
- vi.mock('@/utils/request', () => ({
- get: vi.fn(),
- post: vi.fn()
- }))
- describe('Login API', () => {
- beforeEach(() => {
- vi.clearAllMocks()
- })
- describe('login', () => {
- it('should call POST /admin/auth/login with credentials', async () => {
- const mockResponse = wrapResponse(mockLoginResponse)
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const result = await login({ username: 'admin', password: 'password123' })
- expect(request.post).toHaveBeenCalledWith('/admin/auth/login', {
- username: 'admin',
- password: 'password123'
- })
- expect(result.code).toBe(200)
- expect(result.data.token).toBe(mockLoginResponse.token)
- })
- it('should handle login failure', async () => {
- const mockResponse = wrapErrorResponse('用户名或密码错误', 401)
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const result = await login({ username: 'wrong', password: 'wrong' })
- expect(result.code).toBe(401)
- expect(result.message).toBe('用户名或密码错误')
- })
- })
- describe('getInfo', () => {
- it('should call GET /admin/auth/info', async () => {
- const mockResponse = wrapResponse(mockAdminInfo)
- vi.mocked(request.get).mockResolvedValue(mockResponse)
- const result = await getInfo()
- expect(request.get).toHaveBeenCalledWith('/admin/auth/info')
- expect(result.code).toBe(200)
- expect(result.data.username).toBe(mockAdminInfo.username)
- })
- })
- describe('logout', () => {
- it('should call POST /admin/auth/logout', async () => {
- const mockResponse = wrapResponse(null)
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const result = await logout()
- expect(request.post).toHaveBeenCalledWith('/admin/auth/logout')
- expect(result.code).toBe(200)
- })
- })
- describe('changePassword', () => {
- it('should call POST /admin/auth/password with password data', async () => {
- const mockResponse = wrapResponse(null, 200, '密码修改成功')
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const result = await changePassword({
- oldPassword: 'oldpass',
- newPassword: 'newpass'
- })
- expect(request.post).toHaveBeenCalledWith('/admin/auth/password', {
- oldPassword: 'oldpass',
- newPassword: 'newpass'
- })
- expect(result.code).toBe(200)
- })
- it('should handle wrong old password', async () => {
- const mockResponse = wrapErrorResponse('原密码错误', 400)
- vi.mocked(request.post).mockResolvedValue(mockResponse)
- const result = await changePassword({
- oldPassword: 'wrongpass',
- newPassword: 'newpass'
- })
- expect(result.code).toBe(400)
- expect(result.message).toBe('原密码错误')
- })
- })
- })
|