Files
2026-09-10 18:04:19 +08:00

52 lines
2.0 KiB
Go

// 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 censor
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/tamakyi/TamaBox/internal/conf"
)
// sourceNames 把通道列表映射成名称序列,便于断言「选了几条、哪条、顺序如何」。
func sourceNames(chain []TextCensor) []string {
names := make([]string, 0, len(chain))
for _, censor := range chain {
names = append(names, censor.String())
}
return names
}
// TestCensorChainSelection 覆盖 [security] text_censor_source 对审核通道链的装配结果。
//
// 顺序即优先级:靠前通道「通过」即返回;未通过则交给后面的通道复核。
// 最要紧的是默认(留空 / auto)必须与引入该配置前完全一致,避免升级改变线上判定。
func TestCensorChainSelection(t *testing.T) {
original := conf.Security.TextCensorSource
t.Cleanup(func() { conf.Security.TextCensorSource = original })
for _, tc := range []struct {
name string
source string
want []string
}{
{name: "留空:七牛优先 + 阿里云兜底(历史行为)", source: "", want: []string{"qiniu", "aliyun"}},
{name: "auto:同留空", source: "auto", want: []string{"qiniu", "aliyun"}},
{name: "qiniu:只走七牛", source: "qiniu", want: []string{"qiniu"}},
{name: "aliyun:只走阿里云", source: "aliyun", want: []string{"aliyun"}},
{name: "大小写与空白不敏感", source: " AlIyUn \t", want: []string{"aliyun"}},
// 未知取值在启动期已被 conf.Init 直接拒绝(见 internal/conf validateTextCensorSource);
// 这里断言 censor 侧仍保守回退到历史行为,避免绕过 Init 的调用路径(如 CLI/tests)行为突变。
{name: "未知取值:保守回退 auto", source: "bogus", want: []string{"qiniu", "aliyun"}},
} {
t.Run(tc.name, func(t *testing.T) {
conf.Security.TextCensorSource = tc.source
assert.Equal(t, tc.want, sourceNames(censorChain()))
})
}
}