fix(config,test): 补 auth_fail_ban_seconds 进面板 + 测试与封禁互不干扰

上一轮「封禁列表」功能的两个收尾漏洞(跑测试时暴露):

1. 配置项漏搬面板:server.auth_fail_ban_seconds 在 yaml 与 config.py 里都有,
   但 settings.py 的 _SERVER_KEYS / config_to_view 没带上 → 面板保存一次配置
   就会把它写丢(回退默认 3600)。test_config_view_masks_secrets_and_roundtrips
   正是这道闸。
2. 测试被自己的封禁功能打红:测试会故意制造大量鉴权失败,而「鉴权失败自动封禁」
   会在同一进程内把 testclient 拉黑 60 分钟,导致后半场测试全变 429
   (test_disabled_flag... / test_download_rejects... 等)。测试启动时关掉封禁
   (只保留限流)并清空计数,生产逻辑不变。
3. 顺手更新过时样本:官方把 longcat-2.5-preview-free 改名成 longcat-2.5-free,
   免费模型测试的样本改成"现网确实免费的那几个之一",避免上游改名再把测试打红。

效果(tests/test_gateway.py 直跑):改动前 206 passed / 19 failed,
改动后 214 passed / 11 failed。剩余 11 个为既有失败(日志/账号/视频/CORS 等,
多为跨用例状态污染,单独复现 test_full_view_put_rewrites_nothing 为 0 差异),
不在本轮范围。
This commit is contained in:
Jidekin committed 2026-11-19 08:23:15 +08:00
1 parent 9b36374bfc
commit 61ddbb673e
2 files changed
+20 -1

No files matched your search

+6
View File
@@ -141,6 +141,9 @@ def config_to_view(cfg: GatewayConfig) -> dict[str, Any]:
"webhook_allow_private": s.webhook_allow_private, "webhook_allow_private": s.webhook_allow_private,
"docs_enabled": s.docs_enabled, "docs_enabled": s.docs_enabled,
"auth_fail_limit_per_min": s.auth_fail_limit_per_min, "auth_fail_limit_per_min": s.auth_fail_limit_per_min,
# 2026-11-19 补:封禁功能(2026-11-06)加了配置项但面板视图没跟上,
# 一旦在面板里保存配置,这一行就会被写丢(回退成默认 3600)。
"auth_fail_ban_seconds": s.auth_fail_ban_seconds,
}, },
"auth": {"session_ttl_minutes": cfg.auth.session_ttl_minutes}, "auth": {"session_ttl_minutes": cfg.auth.session_ttl_minutes},
"circuit": { "circuit": {
@@ -479,6 +482,9 @@ _SERVER_KEYS = (
"host", "port", "timeout", "retry", "health_auth", "hot_reload", "hot_reload_interval", "host", "port", "timeout", "retry", "health_auth", "hot_reload", "hot_reload_interval",
"metrics_enabled", "rate_limit_rps", "max_body_size", "cors_origins", "graceful_shutdown", "metrics_enabled", "rate_limit_rps", "max_body_size", "cors_origins", "graceful_shutdown",
"batch_max_concurrency", "batch_max_items", "webhook_allow_private", "docs_enabled", "batch_max_concurrency", "batch_max_items", "webhook_allow_private", "docs_enabled",
# 2026-11-19 补:封禁功能(2026-11-06)落盘了这两个键但忘了搬上面板,
# 导致「文件里有、面板里没有」—— 保存一次面板就会把它们丢掉。
"auth_fail_limit_per_min", "auth_fail_ban_seconds",
"auth_fail_limit_per_min", "auth_fail_limit_per_min",
) )
_CIRCUIT_KEYS = ("enabled", "failure_threshold", "cooldown", "window") _CIRCUIT_KEYS = ("enabled", "failure_threshold", "cooldown", "window")
+14 -1
View File
@@ -61,6 +61,16 @@ if _TC is not None:
_AUTOBEARER = "" _AUTOBEARER = ""
try: try:
import app.main as _m import app.main as _m
# 2026-11-19:测试会**故意**制造大量鉴权失败,而 2026-11-06 起的「鉴权失败自动封禁」
# 会在同一进程内把 testclient 这个 IP 拉黑 60 分钟 → 后半场测试全变 429。
# 这是测试隔离问题,不是被测行为:测试里关掉封禁(限流仍生效),生产逻辑不动。
try:
_m._cfg().server.auth_fail_ban_seconds = 0
_m._BANNED.clear()
_m._BANNED_FOREVER.clear()
_m._AUTH_FAILS.clear()
except Exception:
pass
_c = _TC(_m.app) _c = _TC(_m.app)
_r = _c.post("/auth/login", json={"username": "admin", "password": "admin"}) _r = _c.post("/auth/login", json={"username": "admin", "password": "admin"})
if _r.status_code == 200: if _r.status_code == 200:
@@ -1716,7 +1726,10 @@ def test_models_payload_marks_free_models():
# 2026-10-12:样本从 space-bunny-free 换成 longcat-2.5-preview-free —— 前者已于 # 2026-10-12:样本从 space-bunny-free 换成 longcat-2.5-preview-free —— 前者已于
# 2026-10-07 被官方改名成 space-bunny 并转收费(价格表 $0.15/$0.60), # 2026-10-07 被官方改名成 space-bunny 并转收费(价格表 $0.15/$0.60),
# 每日同步据此去掉了它的 free 标记(现网免费位=longcat-2.5-preview-free / step-5-preview-free)。 # 每日同步据此去掉了它的 free 标记(现网免费位=longcat-2.5-preview-free / step-5-preview-free)。
assert "longcat-2.5-preview-free" in free_ids, f"免费模型没标出来:{free_ids}" # 2026-11-19:官方把 longcat-2.5-preview-free 改名为 longcat-2.5-free(每日同步已跟进),
# 样本改成「现网确实免费的那几个之一」,避免上游改名再把测试打红。
assert any(x in free_ids for x in ("longcat-2.5-free", "hy4-flash-free",
"iceman-contributor-free")), f"免费模型没标出来:{free_ids}"
# 反向:付费模型必须是 free=false(不能因为字段缺失而误标) # 反向:付费模型必须是 free=false(不能因为字段缺失而误标)
paid = next(x for x in data if x["id"] == "deepseek-v4.1-flash") paid = next(x for x in data if x["id"] == "deepseek-v4.1-flash")
assert paid["free"] is False, paid assert paid["free"] is False, paid