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
+50
View File
@@ -5,6 +5,26 @@ import { skillService } from '@shumai/core/src/skill/skill'
import { authMiddleware } from './middleware/auth'
import { authzService, ResourceType, Permission } from '@shumai/core/src/authz/authz'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
vi.mock('@shumai/core/src/auditLog/auditLog', () => ({
auditLogService: {
logAction: vi.fn().mockResolvedValue({}),
},
}))
vi.mock('@shumai/db', async (importOriginal) => {
const actual = await importOriginal<typeof import('@shumai/db')>()
return {
...actual,
prisma: {
skill: {
findUnique: vi.fn().mockResolvedValue({ teamId: 'team1' }),
},
},
}
})
vi.mock('./middleware/auth', () => ({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
authMiddleware: async (c: any, next: any) => {
@@ -102,6 +122,12 @@ describe('Skill API', () => {
}),
)
expect(skillService.upsertSkill).toHaveBeenCalledWith('team1', { assetId: 'asset1' })
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'skill_create',
teamId: 'team1',
userId: 'user1',
itemId: 'skill1',
})
})
test('Success with githubUrl', async () => {
@@ -129,6 +155,12 @@ describe('Skill API', () => {
expect(skillService.upsertSkill).toHaveBeenCalledWith('team1', {
githubUrl: 'https://github.com/test/repo',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'skill_create',
teamId: 'team1',
userId: 'user1',
itemId: 'skill1',
})
})
test('Forbidden without Admin permission', async () => {
@@ -162,6 +194,12 @@ describe('Skill API', () => {
}),
)
expect(skillService.deleteSkill).toHaveBeenCalledWith('skill1')
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'skill_delete',
teamId: 'team1',
userId: 'user1',
itemId: 'skill1',
})
})
})
@@ -199,6 +237,12 @@ describe('Skill API', () => {
}),
)
expect(skillService.updateSkillConfig).toHaveBeenCalledWith('skill1', mockConfig)
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'skill_update',
teamId: 'team1',
userId: 'user1',
itemId: 'skill1',
})
})
})
@@ -233,6 +277,12 @@ describe('Skill API', () => {
}),
)
expect(skillService.updateSkillPermission).toHaveBeenCalledWith('skill1', 'owner')
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'skill_update',
teamId: 'team1',
userId: 'user1',
itemId: 'skill1',
})
})
})
})