audit.spec.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { describe, it, expect, vi, beforeEach } from 'vitest'
  2. import { getAuditLogs, getAuditLogDetail, getAuditLogStats, getUserAuditLogs, getResourceAuditLogs } from '@/api/audit'
  3. import * as request from '@/utils/request'
  4. // Mock request module
  5. vi.mock('@/utils/request', () => ({
  6. get: vi.fn(),
  7. post: vi.fn(),
  8. put: vi.fn(),
  9. del: vi.fn()
  10. }))
  11. // Mock audit logs data
  12. const mockAuditLogs = [
  13. {
  14. id: '1',
  15. user_id: 'user-1',
  16. username: 'admin',
  17. action: 'create',
  18. resource: 'camera',
  19. resource_id: 'cam-001',
  20. ip_address: '192.168.1.1',
  21. created_at: 1704067200
  22. },
  23. {
  24. id: '2',
  25. user_id: 'user-1',
  26. username: 'admin',
  27. action: 'login',
  28. resource: 'auth',
  29. ip_address: '192.168.1.1',
  30. created_at: 1704063600
  31. }
  32. ]
  33. describe('Audit API', () => {
  34. beforeEach(() => {
  35. vi.clearAllMocks()
  36. })
  37. describe('getAuditLogs', () => {
  38. it('应该调用正确的 API 端点', async () => {
  39. const mockResponse = {
  40. code: 200,
  41. data: { rows: mockAuditLogs, total: 2 }
  42. }
  43. vi.mocked(request.get).mockResolvedValue(mockResponse)
  44. await getAuditLogs()
  45. expect(request.get).toHaveBeenCalledWith('/audit-logs', undefined)
  46. })
  47. it('应该传递查询参数', async () => {
  48. const mockResponse = {
  49. code: 200,
  50. data: { rows: [], total: 0 }
  51. }
  52. vi.mocked(request.get).mockResolvedValue(mockResponse)
  53. const params = { action: 'create', resource: 'camera', page: 1, pageSize: 20 }
  54. await getAuditLogs(params)
  55. expect(request.get).toHaveBeenCalledWith('/audit-logs', params)
  56. })
  57. it('应该返回审计日志列表', async () => {
  58. const mockResponse = {
  59. code: 200,
  60. data: { rows: mockAuditLogs, total: 2 }
  61. }
  62. vi.mocked(request.get).mockResolvedValue(mockResponse)
  63. const result = await getAuditLogs()
  64. expect(result.data.rows).toEqual(mockAuditLogs)
  65. expect(result.data.total).toBe(2)
  66. })
  67. it('应该支持日期范围过滤', async () => {
  68. const mockResponse = {
  69. code: 200,
  70. data: { rows: [], total: 0 }
  71. }
  72. vi.mocked(request.get).mockResolvedValue(mockResponse)
  73. const params = {
  74. start_date: '2024-01-01',
  75. end_date: '2024-01-31'
  76. }
  77. await getAuditLogs(params)
  78. expect(request.get).toHaveBeenCalledWith('/audit-logs', params)
  79. })
  80. })
  81. describe('getAuditLogDetail', () => {
  82. it('应该调用正确的 API 端点', async () => {
  83. const mockResponse = {
  84. code: 200,
  85. data: mockAuditLogs[0]
  86. }
  87. vi.mocked(request.get).mockResolvedValue(mockResponse)
  88. await getAuditLogDetail('1')
  89. expect(request.get).toHaveBeenCalledWith('/audit-logs/1')
  90. })
  91. it('应该返回审计日志详情', async () => {
  92. const mockResponse = {
  93. code: 200,
  94. data: mockAuditLogs[0]
  95. }
  96. vi.mocked(request.get).mockResolvedValue(mockResponse)
  97. const result = await getAuditLogDetail('1')
  98. expect(result.data).toEqual(mockAuditLogs[0])
  99. })
  100. })
  101. describe('getAuditLogStats', () => {
  102. it('应该调用正确的 API 端点', async () => {
  103. const mockStats = {
  104. period_days: 7,
  105. total: 100,
  106. by_action: [{ action: 'create', count: 50 }],
  107. by_resource: [{ resource: 'camera', count: 30 }],
  108. by_user: [{ user_id: 'user-1', username: 'admin', count: 80 }],
  109. daily: [{ date: '2024-01-01', count: 15 }]
  110. }
  111. const mockResponse = {
  112. code: 200,
  113. data: mockStats
  114. }
  115. vi.mocked(request.get).mockResolvedValue(mockResponse)
  116. await getAuditLogStats()
  117. expect(request.get).toHaveBeenCalledWith('/audit-logs/stats/summary', { days: 7 })
  118. })
  119. it('应该支持自定义天数', async () => {
  120. const mockResponse = {
  121. code: 200,
  122. data: { period_days: 30 }
  123. }
  124. vi.mocked(request.get).mockResolvedValue(mockResponse)
  125. await getAuditLogStats(30)
  126. expect(request.get).toHaveBeenCalledWith('/audit-logs/stats/summary', { days: 30 })
  127. })
  128. it('应该返回统计数据', async () => {
  129. const mockStats = {
  130. period_days: 7,
  131. total: 100,
  132. by_action: [{ action: 'create', count: 50 }],
  133. by_resource: [{ resource: 'camera', count: 30 }],
  134. by_user: [{ user_id: 'user-1', username: 'admin', count: 80 }],
  135. daily: [{ date: '2024-01-01', count: 15 }]
  136. }
  137. const mockResponse = {
  138. code: 200,
  139. data: mockStats
  140. }
  141. vi.mocked(request.get).mockResolvedValue(mockResponse)
  142. const result = await getAuditLogStats()
  143. expect(result.data).toEqual(mockStats)
  144. })
  145. })
  146. describe('getUserAuditLogs', () => {
  147. it('应该调用正确的 API 端点', async () => {
  148. const mockResponse = {
  149. code: 200,
  150. data: { rows: mockAuditLogs, total: 2 }
  151. }
  152. vi.mocked(request.get).mockResolvedValue(mockResponse)
  153. await getUserAuditLogs('user-1')
  154. expect(request.get).toHaveBeenCalledWith('/audit-logs/user/user-1', undefined)
  155. })
  156. it('应该传递分页参数', async () => {
  157. const mockResponse = {
  158. code: 200,
  159. data: { rows: [], total: 0 }
  160. }
  161. vi.mocked(request.get).mockResolvedValue(mockResponse)
  162. await getUserAuditLogs('user-1', { page: 2, pageSize: 10 })
  163. expect(request.get).toHaveBeenCalledWith('/audit-logs/user/user-1', { page: 2, pageSize: 10 })
  164. })
  165. it('应该返回用户操作历史', async () => {
  166. const mockResponse = {
  167. code: 200,
  168. data: { rows: mockAuditLogs, total: 2 }
  169. }
  170. vi.mocked(request.get).mockResolvedValue(mockResponse)
  171. const result = await getUserAuditLogs('user-1')
  172. expect(result.data.rows).toEqual(mockAuditLogs)
  173. })
  174. })
  175. describe('getResourceAuditLogs', () => {
  176. it('应该调用正确的 API 端点', async () => {
  177. const mockResponse = {
  178. code: 200,
  179. data: { rows: [mockAuditLogs[0]], total: 1 }
  180. }
  181. vi.mocked(request.get).mockResolvedValue(mockResponse)
  182. await getResourceAuditLogs('camera', 'cam-001')
  183. expect(request.get).toHaveBeenCalledWith('/audit-logs/resource/camera/cam-001', undefined)
  184. })
  185. it('应该传递分页参数', async () => {
  186. const mockResponse = {
  187. code: 200,
  188. data: { rows: [], total: 0 }
  189. }
  190. vi.mocked(request.get).mockResolvedValue(mockResponse)
  191. await getResourceAuditLogs('camera', 'cam-001', { page: 1, pageSize: 20 })
  192. expect(request.get).toHaveBeenCalledWith('/audit-logs/resource/camera/cam-001', { page: 1, pageSize: 20 })
  193. })
  194. it('应该返回资源操作历史', async () => {
  195. const mockResponse = {
  196. code: 200,
  197. data: { rows: [mockAuditLogs[0]], total: 1 }
  198. }
  199. vi.mocked(request.get).mockResolvedValue(mockResponse)
  200. const result = await getResourceAuditLogs('camera', 'cam-001')
  201. expect(result.data.rows).toHaveLength(1)
  202. expect(result.data.rows[0].resource).toBe('camera')
  203. })
  204. })
  205. describe('错误处理', () => {
  206. it('API 错误应该被正确传递', async () => {
  207. const error = new Error('Network Error')
  208. vi.mocked(request.get).mockRejectedValue(error)
  209. await expect(getAuditLogs()).rejects.toThrow('Network Error')
  210. })
  211. it('应该处理 API 返回的错误响应', async () => {
  212. const mockResponse = {
  213. code: 403,
  214. message: '无权限访问',
  215. data: null
  216. }
  217. vi.mocked(request.get).mockResolvedValue(mockResponse)
  218. const result = await getAuditLogs()
  219. expect(result.code).toBe(403)
  220. expect(result.message).toBe('无权限访问')
  221. })
  222. })
  223. })