feat: 操作审计语义动作标注落库并支持按语义动作筛选
- OperationDescription 新增 actionOf:捕获时由 method/resourceType/path 推导 语义动作键(login/logout/auth + create/update/delete/view + 子动作段 publish/restore/enable 等,activation 归并为 enable),AuditRecorder 落库 spec.action(AuditLogSpec 新增字段) - OperationAuditPlugin 注册 spec.action 索引,AuditLogFilters 白名单放行 fieldSelector=spec.action=...,查询与导出端点共用 - 前端 audit.ts:describeOperation 优先用落库 action(历史记录无标注仍由 method/path 现场推导,同样适用),导出 deriveAction 与 SEMANTIC_ACTIONS (键→中文标签,与后端同一套映射),展示短语统一由动作键推导 - 列表筛选栏新增「语义动作」下拉(7→8 列),buildQuery/重置/watch 接线 - 测试:后端 +10 例(actionOf 全分支、describeAction、落库断言、索引注册、 查询筛选)共 81 例;前端 +8 例(deriveAction、标注优先、选项完备性)共 39 例; gradle build 全绿 - 文档同步:设计文档 V2 行标记已实现(4.1 字段表/4.2 筛选/5 索引/7 局限)、 技术方案 D3 索引表与 5.2 节、两级 README 功能一览与已知局限
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { AuditLogSpec } from './audit'
|
||||
import { describeOperation } from './audit'
|
||||
import { deriveAction, describeOperation, SEMANTIC_ACTIONS } from './audit'
|
||||
|
||||
// 操作描述推导(这条操作真正干了什么):由 method / resourceType / path 推导中文短语。
|
||||
// 历史记录不落库新字段,描述纯展示层推导,因此必须覆盖所有回退路径,防止出现英文原始值。
|
||||
// 操作描述推导(这条操作真正干了什么):由落库的语义动作标注或
|
||||
// method / resourceType / path 推导中文短语。
|
||||
// 描述必须覆盖所有回退路径,防止出现英文原始值。
|
||||
function spec(overrides: Partial<AuditLogSpec>): AuditLogSpec {
|
||||
return {
|
||||
username: 'admin',
|
||||
@@ -133,3 +134,107 @@ describe('describeOperation', () => {
|
||||
}))).toBe('删除other')
|
||||
})
|
||||
})
|
||||
|
||||
describe('deriveAction 语义动作推导', () => {
|
||||
it('登录/登出/认证操作由路径判定(含查询串剥离)', () => {
|
||||
expect(deriveAction('POST', 'auth', '/login')).toBe('login')
|
||||
expect(deriveAction('GET', 'auth', '/login?redirect_uri=https%3A%2F%2Fwww.guichumo.com'))
|
||||
.toBe('login')
|
||||
expect(deriveAction('POST', 'auth', '/logout')).toBe('logout')
|
||||
expect(deriveAction('POST', 'auth', '/auth/other')).toBe('auth')
|
||||
})
|
||||
|
||||
it('子动作段优先于 HTTP 方法', () => {
|
||||
expect(deriveAction('PUT', 'posts',
|
||||
'/apis/api.console.halo.run/v1alpha1/posts/post-abc/publish')).toBe('publish')
|
||||
expect(deriveAction('POST', 'posts',
|
||||
'/apis/api.console.halo.run/v1alpha1/posts/post-abc/restore')).toBe('restore')
|
||||
// 激活与启用归并为同一动作键
|
||||
expect(deriveAction('PUT', 'themes',
|
||||
'/apis/api.console.halo.run/v1alpha1/themes/theme-abc/activation')).toBe('enable')
|
||||
expect(deriveAction('PUT', 'users',
|
||||
'/apis/api.console.halo.run/v1alpha1/users/admin/password')).toBe('password')
|
||||
})
|
||||
|
||||
it('无子动作段时由 HTTP 方法归为 create/update/delete/view', () => {
|
||||
expect(deriveAction('POST', 'users', '/apis/api.console.halo.run/v1alpha1/users'))
|
||||
.toBe('create')
|
||||
expect(deriveAction('PUT', 'posts', '/apis/content.halo.run/v1alpha1/posts/post-abc'))
|
||||
.toBe('update')
|
||||
expect(deriveAction('DELETE', 'plugins',
|
||||
'/apis/api.console.halo.run/v1alpha1/plugins/plugin-abc')).toBe('delete')
|
||||
expect(deriveAction('GET', 'posts', '/apis/content.halo.run/v1alpha1/posts/post-abc'))
|
||||
.toBe('view')
|
||||
})
|
||||
|
||||
it('未知方法与未识别子动作回退为方法键或 null', () => {
|
||||
expect(deriveAction('TRACE', 'posts', '/apis/content.halo.run/v1alpha1/posts/post-abc'))
|
||||
.toBeNull()
|
||||
expect(deriveAction('PUT', 'posts',
|
||||
'/apis/content.halo.run/v1alpha1/posts/post-abc/content')).toBe('update')
|
||||
})
|
||||
})
|
||||
|
||||
describe('describeOperation 落库标注优先', () => {
|
||||
it('spec.action 存在时直接由动作键推导(历史记录无 action 仍走 method/path 推导)', () => {
|
||||
expect(describeOperation(spec({
|
||||
method: 'PUT',
|
||||
resourceType: 'posts',
|
||||
path: '/apis/content.halo.run/v1alpha1/posts/post-abc',
|
||||
action: 'publish',
|
||||
}))).toBe('发布文章')
|
||||
expect(describeOperation(spec({
|
||||
method: 'POST',
|
||||
resourceType: 'auth',
|
||||
path: '/login',
|
||||
action: 'login',
|
||||
}))).toBe('登录')
|
||||
// 与 method/path 现场推导结果一致(同一套推导规则)
|
||||
expect(describeOperation(spec({
|
||||
method: 'PUT',
|
||||
resourceType: 'posts',
|
||||
path: '/apis/api.console.halo.run/v1alpha1/posts/post-abc/publish',
|
||||
action: 'publish',
|
||||
}))).toBe(describeOperation(spec({
|
||||
method: 'PUT',
|
||||
resourceType: 'posts',
|
||||
path: '/apis/api.console.halo.run/v1alpha1/posts/post-abc/publish',
|
||||
})))
|
||||
})
|
||||
|
||||
it('动作键未知时回退为 method 动词 + 资源名', () => {
|
||||
expect(describeOperation(spec({
|
||||
method: 'PUT',
|
||||
resourceType: 'posts',
|
||||
path: '/somewhere/odd',
|
||||
action: 'bogus',
|
||||
}))).toBe('修改文章')
|
||||
})
|
||||
})
|
||||
|
||||
describe('SEMANTIC_ACTIONS 筛选选项', () => {
|
||||
it('值唯一且标签非空', () => {
|
||||
const values = SEMANTIC_ACTIONS.map((a) => a.value)
|
||||
expect(new Set(values).size).toBe(values.length)
|
||||
for (const a of SEMANTIC_ACTIONS) {
|
||||
expect(a.label).not.toBe('')
|
||||
}
|
||||
})
|
||||
|
||||
it('覆盖全部可落库的语义动作键(view 不会被捕获规则记录,除外)', () => {
|
||||
// 全量枚举:登录/登出/认证 + 方法动作 + 全部子动作段(含 activation 归并)
|
||||
const storable = new Set([
|
||||
'login', 'logout', 'auth',
|
||||
'create', 'update', 'delete',
|
||||
'publish', 'unpublish', 'recycle', 'restore', 'enable', 'disable',
|
||||
'upgrade', 'uninstall', 'reload', 'reply', 'recalculate',
|
||||
'password', 'resetpassword',
|
||||
])
|
||||
expect(new Set(SEMANTIC_ACTIONS.map((a) => a.value))).toEqual(storable)
|
||||
// 每个选项都能由 describeOperation 从动作键推出非空短语(下拉与描述保持一致)
|
||||
for (const a of SEMANTIC_ACTIONS) {
|
||||
expect(describeOperation(spec({ resourceType: 'posts', action: a.value })))
|
||||
.not.toBe('')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user