Files
halo-ops-audit/plugin-operation-audit/ui/src/views/AuditLogList.appearance.spec.ts
T
836061373 fc7984291f fix: 登录页访问不再记成「登录成功」——认证端点只记录 POST,登录成败以响应确认为准
现场反馈:审计列表「没有失败,全是成功,登录失败也显示成功」。在 Halo 2.26.1 上
复现(afda8d0 构建):GET /login、GET /login?authentication_required 只是打开登录页
(HTTP 200),却被记成「未知用户 · 登录 200」,列表里看起来全是登录成功;POST /login
本身的成败判定是对的,但被这些记录淹没。

- 捕获:AuthEndpointAuditWebFilter 只记录 POST /login(登录尝试)与 POST /logout
  (登出);登录/登出页面的 GET 访问(打开登录页、被弹回登录页、失败回显、登出
  确认页)原样透传不记录(CaptureRule.isAuthOperation)
- 判定:LoginOutcome 只有响应明确确认才算成功——302 跳回登录页(含 error 参数或
  路径即 /login)、缺失 Location、4xx/5xx 一律判失败;移除失败回显页判定
- 结果语义:新增 AuditResult(成功 / 失败 / 页面访问),结果筛选、CSV 导出与前端
  resultBadge 共用同一规则;登录成功显示「成功」,与「失败」对称
- 历史数据:升级前落库的登录页访问记录(GET,标注 login/logout)展示与导出为
  「访问登录页/登出页」且不计成败,结果筛选与「登录/登出」语义动作筛选均排除;
  落库标注不改写
- demo 同步同一套规则;README 与设计/技术文档同步

测试:后端 286、UI 74、demo 61 全过;真实 Halo 2.26.1 升级场景联调记录见测试报告
2026-12-05 13:24:26 +08:00

121 lines
6.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
// 结构性回归测试:不渲染组件,仅检查 SFC 源码中的页面布局样式。
// 操作审计页需保持控制台默认灰底(.audit-page 不自行设置背景色),
// 筛选栏、数据列表、分页条合并为同一张白色卡片:区块之间不留灰缝,
// 分页条白底随卡片铺满整行、与上方列表左右对齐(与控制台其他列表页观感一致);
// 数据行上方有列题头,题头列宽与数据行各字段固定宽度一一对应,
// 且题头 start/end 两组各自镜像 entity 行两个 td 的 1rem 左右内边距(end 组紧跟 start 组);
// flex: 1 撑满 main-content 剩余高度,使灰底延伸到页面底部。
// 历史上 .audit-page 曾整体铺白导致页面与其他页面观感不一致,
// 分页条曾左右各留 1rem 导致白底与上方卡片错位,
// 卡片之间曾留 1rem 间距导致白色断开,本测试防止回归。
const sfc = readFileSync(fileURLToPath(new URL('./AuditLogList.vue', import.meta.url)), 'utf-8')
function styleRule(selector: string): string {
const start = sfc.indexOf(`${selector} {`)
expect(start, `缺少 ${selector} 样式规则`).toBeGreaterThan(-1)
const end = sfc.indexOf('}', start)
return sfc.slice(start, end)
}
describe('AuditLogList 页面外观', () => {
it('.audit-page 撑满控制台内容区剩余高度且不铺白(保持默认灰底)', () => {
const rule = styleRule('.audit-page')
expect(rule).toMatch(/flex:\s*1/)
expect(rule).not.toMatch(/background\s*:/)
})
it('.audit-body 不设区块间距(筛选/数据/分页之间白色连成一体)', () => {
const rule = styleRule('.audit-body')
expect(rule).not.toMatch(/gap\s*:/)
})
it('筛选栏与数据列表合并为同一张 VCard', () => {
expect(sfc.match(/<VCard/g)?.length).toBe(1)
})
it('.audit-filter .filter-grid 筛选字段与操作按钮排成一行(8 列 + auto 按钮)', () => {
const rule = styleRule('.filter-grid')
expect(rule).toMatch(/grid-template-columns:\s*repeat\(8,\s*minmax\(0,\s*1fr\)\)\s+auto/)
})
it('.audit-pagination 白底铺满整行(不再左右缩进 1rem),仅以 1px 分隔线与列表相接', () => {
const rule = styleRule('.audit-pagination')
expect(rule).not.toMatch(/padding:\s*0\s+1rem/)
expect(rule).toMatch(/padding:\s*1rem/)
expect(rule).toMatch(/border-top:\s*1px\s+solid\s+#eaecf0/)
})
it('列表数据行上方渲染列题头,六个列标签齐全', () => {
expect(sfc).toContain('<div class="audit-header">')
expect(sfc).toContain('操作者 / 时间')
expect(sfc).toContain('>动作<')
expect(sfc).toContain('资源 / 路径')
expect(sfc).toContain('>描述<')
expect(sfc).toContain('>结果<')
expect(sfc).toContain('>IP<')
})
it('题头列宽与数据行 VEntityField 的 width 一一对应', () => {
const pairs: Array<[string, string]> = [
['col-username', '12rem'],
['col-method', '6rem'],
['col-resource', '20rem'],
['col-desc', '10rem'],
['col-status', '4rem'],
['col-ip', '9rem'],
]
for (const [cls, width] of pairs) {
expect(sfc, `缺少题头列 ${cls}`).toMatch(new RegExp(`\\.${cls} \\{ width: ${width};? \\}`))
expect(sfc, `缺少数据列 width="${width}"`).toContain(`width="${width}"`)
}
})
it('题头分组间距与 entity 行一致(start 间距 1rem、end 间距 1.5rem 且不右对齐)', () => {
// entity 行由两个自带 1rem 左右内边距的 td 组成,end 组紧跟 start 组(行宽随内容收缩);
// 题头两组各自镜像 1rem 内边距才能与数据列对齐,
// 历史上曾用 margin-left: auto 把结果/IP 推到行尾导致与数据错位 19rem,本测试防止回归。
const start = styleRule('.header-start')
expect(start).toMatch(/gap:\s*1rem/)
expect(start).toMatch(/padding:\s*0\s+1rem/)
const end = styleRule('.header-end')
expect(end).toMatch(/gap:\s*1\.5rem/)
expect(end).toMatch(/padding:\s*0\s+1rem/)
expect(end).not.toMatch(/margin-left:\s*auto/)
const header = styleRule('.audit-header')
expect(header).toMatch(/padding:\s*0\.5rem\s+0/)
expect(header).toMatch(/border-bottom:\s*1px\s+solid\s+#eaecf0/)
})
it('动作列渲染彩色徽标,描述独立成列且位于资源/路径与结果之间', () => {
// 组件(@halo-dev/components 2.26.0)在无 title/#title 时不渲染 #extra,
// 原先结果徽标实际不可见;原先方法列 title 与徽标重复渲染同一文案。
// 描述原先与徽标同行挤在动作列,现拆为独立列(describeOperation 推导的
// 中文描述,如 登录/新建用户/发布文章),题头与数据行均置于资源/路径之后、结果之前。
expect(sfc).not.toContain(':title="log.spec.method"')
expect(sfc).not.toContain('action-cell')
expect(sfc).toMatch(/<template #title>\s+<span\s+class="method-badge"/)
expect(sfc).toContain('describeOperation(log.spec)')
expect(sfc).toMatch(/<span class="action-desc">\{\{ describeOperation\(log\.spec\) \}\}<\/span>/)
expect(sfc).toMatch(/<template #title>\s+<span\s+class="status-badge"/)
// 数据行内 resource-text(资源/路径列)在 action-desc(描述列)之前,
// action-desc(描述列)在 status-badge(结果列)之前
const resourceIdx = sfc.indexOf('class="resource-text"')
const descIdx = sfc.indexOf('class="action-desc"')
const statusIdx = sfc.indexOf('class="status-badge"')
expect(resourceIdx).toBeGreaterThan(-1)
expect(descIdx).toBeGreaterThan(resourceIdx)
expect(statusIdx).toBeGreaterThan(descIdx)
})
it('结果徽标有中性灰样式:页面访问(登录页/登出页的 GET)不计成败', () => {
// 升级前落库的登录页访问记录原先显示绿色 200,误读为未知用户登录成功;
// resultBadge 现判为 page-visit,需有既非成功色也非失败色的样式承接
expect(sfc).toContain("resultBadge(log.spec)")
expect(styleRule('.status-na')).toMatch(/color:\s*#6b7280/)
})
})