feat(audit): add audit log support for APIs and WebUI dashboard

This commit is contained in:
yiling
2026-08-01 10:39:31 +08:00
parent 7a83f7d406
commit 118107ebc9
58 changed files with 4298 additions and 23 deletions
+44
View File
@@ -1,5 +1,6 @@
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
import { Hono } from 'hono'
import { AuditAction } from '@shumai/db'
import teamRoute from './team'
import { authMiddleware } from './middleware/auth'
import { teamService } from '@shumai/core/src/team/team'
@@ -44,6 +45,13 @@ vi.mock('@shumai/core/src/ai-usage/ai-usage', () => ({
},
}))
vi.mock('@shumai/core/src/auditLog/auditLog', () => ({
auditLogService: {
logAction: vi.fn(),
listAuditLogs: vi.fn(),
},
}))
describe('team api', () => {
const app = new Hono().use('*', authMiddleware).route('/', teamRoute)
let mockCreateTeam: any // eslint-disable-line @typescript-eslint/no-explicit-any
@@ -471,5 +479,41 @@ describe('team api', () => {
userId: 'user1',
})
})
it('GET /teams/:teamId/audit-logs returns list of audit logs for team owner', async () => {
const { auditLogService } = await import('@shumai/core/src/auditLog/auditLog')
vi.mocked(auditLogService.listAuditLogs).mockResolvedValue({
nodes: [
{
id: 'log1',
action: AuditAction.project_create,
teamId: 't1',
userId: 'user1',
projectId: 'p1',
itemId: 'p1',
createdAt: new Date().toISOString(),
},
],
pageInfo: {
hasNextPage: false,
endCursor: null,
},
total: 1,
})
const res = await app.request('/teams/t1/audit-logs')
expect(res.status).toBe(200)
const data = await res.json()
expect(data.nodes).toHaveLength(1)
expect(data.nodes[0].id).toBe('log1')
expect(authzService.hasPermission).toHaveBeenCalledWith(
expect.objectContaining({
permission: Permission.Admin,
type: ResourceType.Team,
id: 't1',
}),
)
})
})
})