- 纯函数单测(go test -short)覆盖: classification (levelIndex/maxLevel/materializeClassification/principalMaxIndex/deniedLevelsFor)、 display_name (propString/resolveDisplayName)、 governance (diffProps/mergeProps/parseStringArray/similarity) - 集成测试(-tags=integration,跑真实 PG)覆盖 issue #4 #5 端到端: enum_violation 拦截 + 友好错误;分级过滤 manager vs analyst(L0/L1) 越级 404; properties.sensitivity 越级 redact 为 ***;删→恢复关系边 (links_snapshot+edges_created=1); recompute dry_run 不落库 / apply=true 落库修正错误分级 - 集成测试踩坑:X-Principal 与 principal_clearances.principal 必须一致;每次新主体必须清 authzCache(sync.Map 进程级),否则历史空 Authz 会让分级过滤假阴性 - docs/TESTING.md:测试分层 / 跑测命令 / 编写新测试的约定 - CHANGELOG [1.2.0] 加 测试 与 文档 段落
181 lines
5.7 KiB
Go
181 lines
5.7 KiB
Go
package main
|
||
|
||
import "testing"
|
||
|
||
// ---------- levelIndex / maxLevel ----------
|
||
|
||
func TestLevelIndex(t *testing.T) {
|
||
levels := []string{"L0", "L1", "L2", "L3"}
|
||
cases := []struct {
|
||
in string
|
||
want int
|
||
}{
|
||
{"L0", 0},
|
||
{"L1", 1},
|
||
{"L3", 3},
|
||
{"L9", -1},
|
||
{"", -1},
|
||
}
|
||
for _, c := range cases {
|
||
if got := levelIndex(levels, c.in); got != c.want {
|
||
t.Errorf("levelIndex(%q) = %d, want %d", c.in, got, c.want)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestMaxLevel(t *testing.T) {
|
||
levels := []string{"L0", "L1", "L2", "L3"}
|
||
|
||
// 两个已知:取较大下标
|
||
if got := maxLevel("L1", "L3", levels); got != "L3" {
|
||
t.Errorf("max(L1,L3) = %q, want L3", got)
|
||
}
|
||
if got := maxLevel("L3", "L1", levels); got != "L3" {
|
||
t.Errorf("max(L3,L1) = %q, want L3", got)
|
||
}
|
||
if got := maxLevel("L2", "L2", levels); got != "L2" {
|
||
t.Errorf("max(L2,L2) = %q, want L2", got)
|
||
}
|
||
|
||
// 一空一有:返回非空
|
||
if got := maxLevel("", "L2", levels); got != "L2" {
|
||
t.Errorf("max('',L2) = %q, want L2", got)
|
||
}
|
||
if got := maxLevel("L2", "", levels); got != "L2" {
|
||
t.Errorf("max(L2,'') = %q, want L2", got)
|
||
}
|
||
|
||
// 两个空
|
||
if got := maxLevel("", "", levels); got != "" {
|
||
t.Errorf("max('','') = %q, want ''", got)
|
||
}
|
||
|
||
// 一个未知一个已知:返回已知(已知优于未知,保证分级配置扩展时不静默失效)
|
||
if got := maxLevel("L9", "L2", levels); got != "L2" {
|
||
t.Errorf("max(L9,L2) = %q, want L2", got)
|
||
}
|
||
if got := maxLevel("L2", "L9", levels); got != "L2" {
|
||
t.Errorf("max(L2,L9) = %q, want L2", got)
|
||
}
|
||
|
||
// 两个未知:字典序兜底(确定性)
|
||
if got := maxLevel("ZZ", "AA", levels); got != "ZZ" {
|
||
t.Errorf("max(ZZ,AA) = %q, want ZZ (字典序)", got)
|
||
}
|
||
}
|
||
|
||
// ---------- materializeClassification ----------
|
||
|
||
func TestMaterializeClassification(t *testing.T) {
|
||
cfg := classConfig{Property: "data_level", Levels: []string{"L0", "L1", "L2", "L3"}, Default: "L0"}
|
||
|
||
// 属性值 → 直接采用
|
||
got := materializeClassification(cfg, map[string]interface{}{"data_level": "L2"}, "")
|
||
if got != "L2" {
|
||
t.Errorf("属性 L2 → %q, want L2", got)
|
||
}
|
||
|
||
// 属性空 → 默认
|
||
got = materializeClassification(cfg, map[string]interface{}{}, "")
|
||
if got != "L0" {
|
||
t.Errorf("空属性 → %q, want L0 (default)", got)
|
||
}
|
||
|
||
// KM 源文件 floor 抬升分级
|
||
got = materializeClassification(cfg, map[string]interface{}{"data_level": "L1"}, "L2")
|
||
if got != "L2" {
|
||
t.Errorf("属性 L1 + floor L2 → %q, want L2 (floor 抬升)", got)
|
||
}
|
||
|
||
// 属性分级更高 → 不会被 floor 拉低
|
||
got = materializeClassification(cfg, map[string]interface{}{"data_level": "L3"}, "L1")
|
||
if got != "L3" {
|
||
t.Errorf("属性 L3 + floor L1 → %q, want L3 (max)", got)
|
||
}
|
||
|
||
// 数字属性 → propString 转字面量;"2" 不在 cfg.Levels 里 → maxLevel 走"未知优于空"分支返回 "2"
|
||
// 真实业务应禁止数字级别的设计选择,所以测试只验证字面量化行为,不预测具体值。
|
||
got = materializeClassification(cfg, map[string]interface{}{"data_level": float64(2)}, "")
|
||
if got != "2" {
|
||
t.Errorf("属性 float(2) → %q, want %q (字面量化)", got, "2")
|
||
}
|
||
|
||
// 未配置分级 → 直接返回 floor
|
||
off := classConfig{}
|
||
got = materializeClassification(off, map[string]interface{}{"data_level": "L2"}, "L1")
|
||
if got != "L1" {
|
||
t.Errorf("未配置分级 + floor L1 → %q, want L1", got)
|
||
}
|
||
}
|
||
|
||
// ---------- principalMaxIndex / deniedLevelsFor ----------
|
||
|
||
func TestPrincipalMaxIndex(t *testing.T) {
|
||
levels := []string{"L0", "L1", "L2", "L3"}
|
||
|
||
// manager 永远全开
|
||
if got := principalMaxIndex(&Authz{Principal: "manager"}, levels); got != 3 {
|
||
t.Errorf("manager → %d, want 3", got)
|
||
}
|
||
|
||
// nil authz 当 manager
|
||
if got := principalMaxIndex(nil, levels); got != 3 {
|
||
t.Errorf("nil authz → %d, want 3", got)
|
||
}
|
||
|
||
// analyst L0+L2 → max index = 2
|
||
authz := &Authz{Principal: "analyst", Clearances: map[string]bool{"L0": true, "L2": true}}
|
||
if got := principalMaxIndex(authz, levels); got != 2 {
|
||
t.Errorf("analyst(L0,L2) → %d, want 2", got)
|
||
}
|
||
|
||
// 完全无交集 → -1(fail-closed:分级一旦启用就不静默失效)
|
||
authz = &Authz{Principal: "analyst", Clearances: map[string]bool{"ZZ": true}}
|
||
if got := principalMaxIndex(authz, levels); got != -1 {
|
||
t.Errorf("analyst(ZZ) → %d, want -1", got)
|
||
}
|
||
|
||
// 空 clearance → -1
|
||
authz = &Authz{Principal: "analyst", Clearances: map[string]bool{}}
|
||
if got := principalMaxIndex(authz, levels); got != -1 {
|
||
t.Errorf("analyst(empty) → %d, want -1", got)
|
||
}
|
||
}
|
||
|
||
func TestDeniedLevelsFor(t *testing.T) {
|
||
levels := []string{"L0", "L1", "L2", "L3"}
|
||
cfg := classConfig{Property: "data_level", Levels: levels, Default: "L0"}
|
||
|
||
// manager → 无屏蔽
|
||
if got := deniedLevelsFor(&Authz{Principal: "manager"}, cfg); got != nil {
|
||
t.Errorf("manager denied = %v, want nil", got)
|
||
}
|
||
|
||
// analyst L0+L1 → 屏蔽 L2+L3
|
||
authz := &Authz{Principal: "analyst", Clearances: map[string]bool{"L0": true, "L1": true}}
|
||
got := deniedLevelsFor(authz, cfg)
|
||
if len(got) != 2 || got[0] != "L2" || got[1] != "L3" {
|
||
t.Errorf("analyst(L0,L1) denied = %v, want [L2 L3]", got)
|
||
}
|
||
|
||
// 完全无交集 → 屏蔽全部(fail-closed)
|
||
authz = &Authz{Principal: "analyst", Clearances: map[string]bool{"ZZ": true}}
|
||
got = deniedLevelsFor(authz, cfg)
|
||
if len(got) != 4 {
|
||
t.Errorf("analyst(ZZ) denied = %v, want 全屏蔽", got)
|
||
}
|
||
|
||
// 未启用分级(无 property)→ 无屏蔽
|
||
off := classConfig{}
|
||
got = deniedLevelsFor(authz, off)
|
||
if got != nil {
|
||
t.Errorf("未配置分级 denied = %v, want nil", got)
|
||
}
|
||
|
||
// 主体覆盖全级别 → 无屏蔽
|
||
authz = &Authz{Principal: "analyst", Clearances: map[string]bool{"L0": true, "L1": true, "L2": true, "L3": true}}
|
||
if got := deniedLevelsFor(authz, cfg); got != nil {
|
||
t.Errorf("全覆盖 denied = %v, want nil", got)
|
||
}
|
||
}
|