package notify import ( "strings" "testing" ) func TestMaskedValueHidesBodyButKeepsTailHint(t *testing.T) { const secret = "https://oapi.dingtalk.com/robot/send?access_token=abcdef123456" got := MaskedValue(secret) if strings.Contains(got, "abcdef123456") { t.Fatalf("掩码值泄露了完整凭据: %q", got) } if strings.Contains(got, "oapi.dingtalk.com") { t.Fatalf("掩码值不应暴露地址主体: %q", got) } // 末 6 位要保留,用户才能认出是哪个机器人。 if !strings.HasSuffix(got, "123456") { t.Fatalf("应保留末 6 位作为辨识提示: %q", got) } if !IsMasked(got) { t.Fatalf("掩码值必须能被 IsMasked 识别: %q", got) } } func TestMaskedValueShortSecretGivesNoHint(t *testing.T) { // 短凭据如果也暴露末 6 位,等于把整个凭据暴露出去。 for _, s := range []string{"abc", "abcdef", ""} { got := MaskedValue(s) if got != MaskedPrefix { t.Fatalf("长度 %d 的凭据不应给出尾部提示,得到 %q", len(s), got) } if s != "" && strings.Contains(got, s) { t.Fatalf("掩码值包含了原值: %q", got) } } } func TestMaskConfigMasksOnlySecrets(t *testing.T) { cfg := map[string]any{ "webhook": "https://example.com/hook?token=SECRETVALUE", "secret": "SECtest123456", "port": float64(587), "host": "smtp.example.com", } masked := MaskConfig(KindDingTalk, cfg) for _, k := range []string{"webhook", "secret"} { s, _ := masked[k].(string) if !IsMasked(s) { t.Errorf("%s 应被掩码,得到 %q", k, s) } } // 非凭据字段必须原样保留,否则 UI 无法展示。 if masked["port"] != float64(587) { t.Errorf("非凭据字段 port 不应改动: %v", masked["port"]) } } func TestMaskConfigUnknownKindReturnsEmpty(t *testing.T) { // 渠道类型无法识别时,宁可让 UI 显示空配置,也不要把可能含凭据的原始内容吐回去。 got := MaskConfig("nope", map[string]any{"webhook": "https://x/y?token=LEAK"}) if len(got) != 0 { t.Fatalf("未知渠道类型应返回空配置,得到 %v", got) } } func TestMaskConfigDoesNotMutateInput(t *testing.T) { // 掩码是展示层行为,不能反过来把库里的真值改掉。 cfg := map[string]any{"webhook": "https://example.com/hook", "secret": "SECtest123456"} _ = MaskConfig(KindDingTalk, cfg) if IsMasked(cfg["secret"].(string)) { t.Fatal("MaskConfig 修改了入参,会导致真实凭据被掩码值覆盖") } } func TestMergeConfigKeepsStoredOnMaskedIncoming(t *testing.T) { stored := map[string]any{"webhook": "https://real/hook", "secret": "REALSECRET", "method": "POST"} // 用户只改了 method,浏览器提交的是掩码值+新 method。 incoming := map[string]any{ "webhook": MaskedValue("https://real/hook"), "secret": MaskedValue("REALSECRET"), "method": "PUT", } got := MergeConfig(stored, incoming) if got["webhook"] != "https://real/hook" || got["secret"] != "REALSECRET" { t.Fatalf("掩码字段应保留库中原值,得到 %v", got) } if got["method"] != "PUT" { t.Fatalf("被修改的字段应生效,得到 %v", got["method"]) } } func TestMergeConfigEmptyStringClears(t *testing.T) { stored := map[string]any{"webhook": "https://real/hook", "secret": "REALSECRET"} got := MergeConfig(stored, map[string]any{"secret": ""}) if _, ok := got["secret"]; ok { t.Fatalf("空串应清空该字段,得到 %v", got) } // 没提到的字段保留(局部更新语义)。 if got["webhook"] != "https://real/hook" { t.Fatalf("未提及的字段应保留,得到 %v", got) } } func TestMergeConfigKeepsUnmentionedStoredKeys(t *testing.T) { stored := map[string]any{"host": "smtp.example.com", "port": float64(587), "password": "pw"} got := MergeConfig(stored, map[string]any{"port": float64(465)}) if got["host"] != "smtp.example.com" || got["password"] != "pw" { t.Fatalf("未提及的字段应保留,得到 %v", got) } if got["port"] != float64(465) { t.Fatalf("已提及的字段应更新,得到 %v", got["port"]) } } func TestSecretKeysDeclaredForEveryKind(t *testing.T) { // 编译器已经强制每个渠道实现 SecretKeys,这里再确认一遍「没有渠道在掩码上 // 交白卷」——返回空切片的渠道意味着它的凭据会明文回显到浏览器。 expect := map[string]bool{ KindDingTalk: true, KindFeishu: true, KindWeCom: true, KindWebhook: true, KindTelegram: true, KindEmail: true, } for kind, ch := range registry { if !expect[kind] { t.Errorf("渠道 %s 未在测试中登记掩码预期", kind) continue } if len(ch.SecretKeys()) == 0 { t.Errorf("渠道 %s 未声明任何凭据字段,其配置会明文回显", kind) } } }