feat(api): add missing audit log calls to API endpoints

This commit is contained in:
yiling
2026-08-01 11:03:50 +08:00
parent 7dfb2c7f2f
commit 0324b95c7e
18 changed files with 695 additions and 2 deletions
+30
View File
@@ -6,6 +6,14 @@ import { searchService } from '@shumai/core/src/search/search'
import { authzService, ResourceType, Permission } from '@shumai/core/src/authz/authz'
import { authMiddleware } from './middleware/auth'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
vi.mock('@shumai/core/src/auditLog/auditLog', () => ({
auditLogService: {
logAction: vi.fn().mockResolvedValue({}),
},
}))
vi.mock('@shumai/core/src/authz/authz', () => ({
authzService: {
hasPermission: vi.fn().mockResolvedValue(undefined),
@@ -33,6 +41,7 @@ describe('folder api', () => {
vi.mocked(authzService.hasPermission).mockClear()
vi.spyOn(assetService, 'getAsset').mockImplementation(vi.fn())
vi.spyOn(assetService, 'getAssetContext').mockResolvedValue({ teamId: 'test-team' })
vi.spyOn(assetService, 'createAsset').mockImplementation(vi.fn())
vi.spyOn(assetService, 'updateAssetName').mockImplementation(vi.fn())
vi.spyOn(assetService, 'deleteAssets').mockImplementation(vi.fn())
@@ -101,6 +110,13 @@ describe('folder api', () => {
type: ResourceType.Asset,
id: 'test-id',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'asset_update',
teamId: 'test-team',
userId: 'user1',
projectId: undefined,
itemId: 'test-id',
})
})
it('POST /folders', async () => {
@@ -132,6 +148,13 @@ describe('folder api', () => {
type: ResourceType.Asset,
id: 'parent-id',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'asset_create',
teamId: 'test-team',
userId: 'user1',
projectId: undefined,
itemId: 'new-id',
})
})
it('GET /folders/:folderId/children', async () => {
@@ -186,6 +209,13 @@ describe('folder api', () => {
id: 'test-id',
})
expect(assetService.deleteAssets).toHaveBeenCalledWith(['test-id'])
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'asset_delete',
teamId: 'test-team',
userId: 'user1',
projectId: undefined,
itemId: 'test-id',
})
})
it('POST /folders/restore', async () => {