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
+38
View File
@@ -4,6 +4,8 @@ import metadataRoute from './metadata'
import { metadataService } from '@shumai/core/src/metadata/metadata'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
vi.mock('./middleware/auth', () => ({
authMiddleware: async (
c: Context<{ Variables: { user: { id: string; name: string } } }>,
@@ -16,6 +18,22 @@ vi.mock('./middleware/auth', () => ({
vi.mock('@shumai/core/src/authz/authz')
vi.mock('@shumai/core/src/metadata/metadata')
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: {
project: {
findUnique: vi.fn().mockResolvedValue({ teamId: 't1' }),
},
},
}
})
describe('metadata api', () => {
const app = new Hono<{ Variables: { user: { id: string; name: string } } }>()
@@ -91,6 +109,12 @@ describe('metadata api', () => {
type: ResourceType.Team,
id: 't1',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'metadata_field_create',
teamId: 't1',
userId: 'user1',
itemId: 'newfield',
})
})
it('GET /projects/:projectId/fields', async () => {
@@ -167,6 +191,13 @@ describe('metadata api', () => {
type: ResourceType.MetadataField,
id: 'f1',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'metadata_field_delete',
teamId: 't1',
userId: 'user1',
projectId: undefined,
itemId: 'f1',
})
})
it('PUT /fields/:fieldId', async () => {
@@ -204,5 +235,12 @@ describe('metadata api', () => {
type: ResourceType.MetadataField,
id: 'f1',
})
expect(auditLogService.logAction).toHaveBeenCalledWith({
action: 'metadata_field_update',
teamId: 't1',
userId: 'user1',
projectId: undefined,
itemId: 'f1',
})
})
})