login.spec.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { describe, it, expect, vi, beforeEach } from 'vitest'
  2. import { login, getInfo, logout, changePassword } from '@/api/login'
  3. import * as request from '@/utils/request'
  4. import { mockLoginResponse, mockAdminInfo, wrapResponse, wrapErrorResponse } from '../../fixtures'
  5. // Mock request module
  6. vi.mock('@/utils/request', () => ({
  7. get: vi.fn(),
  8. post: vi.fn()
  9. }))
  10. describe('Login API', () => {
  11. beforeEach(() => {
  12. vi.clearAllMocks()
  13. })
  14. describe('login', () => {
  15. it('should call POST /admin/auth/login with credentials', async () => {
  16. const mockResponse = wrapResponse(mockLoginResponse)
  17. vi.mocked(request.post).mockResolvedValue(mockResponse)
  18. const result = await login({ username: 'admin', password: 'password123' })
  19. expect(request.post).toHaveBeenCalledWith('/admin/auth/login', {
  20. username: 'admin',
  21. password: 'password123'
  22. })
  23. expect(result.code).toBe(200)
  24. expect(result.data.token).toBe(mockLoginResponse.token)
  25. })
  26. it('should handle login failure', async () => {
  27. const mockResponse = wrapErrorResponse('用户名或密码错误', 401)
  28. vi.mocked(request.post).mockResolvedValue(mockResponse)
  29. const result = await login({ username: 'wrong', password: 'wrong' })
  30. expect(result.code).toBe(401)
  31. expect(result.message).toBe('用户名或密码错误')
  32. })
  33. })
  34. describe('getInfo', () => {
  35. it('should call GET /admin/auth/info', async () => {
  36. const mockResponse = wrapResponse(mockAdminInfo)
  37. vi.mocked(request.get).mockResolvedValue(mockResponse)
  38. const result = await getInfo()
  39. expect(request.get).toHaveBeenCalledWith('/admin/auth/info')
  40. expect(result.code).toBe(200)
  41. expect(result.data.username).toBe(mockAdminInfo.username)
  42. })
  43. })
  44. describe('logout', () => {
  45. it('should call POST /admin/auth/logout', async () => {
  46. const mockResponse = wrapResponse(null)
  47. vi.mocked(request.post).mockResolvedValue(mockResponse)
  48. const result = await logout()
  49. expect(request.post).toHaveBeenCalledWith('/admin/auth/logout')
  50. expect(result.code).toBe(200)
  51. })
  52. })
  53. describe('changePassword', () => {
  54. it('should call POST /admin/auth/password with password data', async () => {
  55. const mockResponse = wrapResponse(null, 200, '密码修改成功')
  56. vi.mocked(request.post).mockResolvedValue(mockResponse)
  57. const result = await changePassword({
  58. oldPassword: 'oldpass',
  59. newPassword: 'newpass'
  60. })
  61. expect(request.post).toHaveBeenCalledWith('/admin/auth/password', {
  62. oldPassword: 'oldpass',
  63. newPassword: 'newpass'
  64. })
  65. expect(result.code).toBe(200)
  66. })
  67. it('should handle wrong old password', async () => {
  68. const mockResponse = wrapErrorResponse('原密码错误', 400)
  69. vi.mocked(request.post).mockResolvedValue(mockResponse)
  70. const result = await changePassword({
  71. oldPassword: 'wrongpass',
  72. newPassword: 'newpass'
  73. })
  74. expect(result.code).toBe(400)
  75. expect(result.message).toBe('原密码错误')
  76. })
  77. })
  78. })