// Copyright 2022 E99p1ant. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. package conf import ( "testing" "github.com/stretchr/testify/assert" ) // TestValidateTextCensorSource 覆盖 [security] text_censor_source 与 [app] 凭据的搭配校验。 // // 关注两类真实故障: // 1. 显式选定通道却缺凭据 —— 运行期审核必然失败,而审核失败是 fail-closed, // 会让整站发布/私信不可用且现象隐蔽,必须在启动阶段拦住; // 2. 取值拼写错误 —— 若被静默回退成 auto,运维会误以为「已经只用七牛了」。 func TestValidateTextCensorSource(t *testing.T) { for _, tc := range []struct { name string enabled bool source string qk, qs string ak, as string wantErr bool }{ {name: "审核关闭:不校验任何通道配置", enabled: false, source: "aliyun"}, {name: "审核关闭:非法取值也不报错", enabled: false, source: "qinui"}, {name: "留空:通过(历史默认行为)", enabled: true, source: ""}, {name: "auto:通过", enabled: true, source: "auto"}, {name: "auto:无任何凭据也通过(不阻断开箱启动)", enabled: true, source: "auto"}, {name: "qiniu:凭据齐备通过", enabled: true, source: "qiniu", qk: "k", qs: "s"}, {name: "qiniu:缺 access key 报错", enabled: true, source: "qiniu", qs: "s", wantErr: true}, {name: "qiniu:缺 access secret 报错", enabled: true, source: "qiniu", qk: "k", wantErr: true}, {name: "aliyun:凭据齐备通过", enabled: true, source: "aliyun", ak: "k", as: "s"}, {name: "aliyun:缺凭据报错", enabled: true, source: "aliyun", wantErr: true}, {name: "大小写与空白不敏感", enabled: true, source: " QINIU ", qk: "k", qs: "s"}, {name: "非法取值:报错而非静默回退为 auto", enabled: true, source: "qinui", qk: "k", qs: "s", wantErr: true}, } { t.Run(tc.name, func(t *testing.T) { err := validateTextCensorSource(tc.enabled, tc.source, tc.qk, tc.qs, tc.ak, tc.as) if tc.wantErr { assert.Error(t, err) return } assert.NoError(t, err) }) } }