Files
cmshoppe/tests/test_appconfig.py
T
chengma 01e319cad8 feat: 完成T-503敏感信息提示与脱敏
- 保存或变更账号密码、AI API Key 前弹出本地明文保存提示

- 新增敏感值打码、结构化日志脱敏和自由文本替换工具

- 补充 GUI/appconfig 单测,覆盖明文提示和脱敏边界

- 同步任务看板、当前状态、架构、API、路由和编码规则文档
2026-06-29 10:05:16 +08:00

143 lines
5.3 KiB
Python

import os
import sys
import unittest
sys.path.insert(0, os.path.dirname(__file__))
from _helpers import TempDirMixin
from app import appconfig
class AppConfigTests(TempDirMixin, unittest.TestCase):
def test_config_load_update_and_response_timeout(self):
with self.make_temp_dir() as temp_dir:
config_path = os.path.join(temp_dir, "config.json")
config = appconfig.load_config(config_path)
self.assertTrue(os.path.exists(config_path))
self.assertEqual("images", appconfig.image_dir(config))
self.assertEqual(240, appconfig.response_timeout(config))
updated = appconfig.update_config(
{"ai": {"resolution": "2k"}},
path=config_path,
)
self.assertEqual(360, appconfig.response_timeout(updated))
self.assertEqual((9222, 9260), appconfig.debug_port_range(updated))
self.assert_removed(temp_dir)
def test_config_rejects_sensitive_fields(self):
with self.make_temp_dir() as temp_dir:
config_path = os.path.join(temp_dir, "config.json")
with self.assertRaises(appconfig.ConfigError):
appconfig.save_config({"api_key": "secret"}, path=config_path)
with self.assertRaises(appconfig.ConfigError):
appconfig.save_config(
{"ai": {"provider_token": "secret"}},
path=config_path,
)
self.assert_removed(temp_dir)
def test_ai_models_crud_filter_mask_and_get_model(self):
with self.make_temp_dir() as temp_dir:
models_path = os.path.join(temp_dir, "ai_models.json")
models = appconfig.list_ai_models(path=models_path)
self.assertEqual({"text", "image"}, {model["category"] for model in models})
self.assertTrue(all("api_key_set" in model for model in models))
appconfig.add_ai_model(
{
"name": "Text 2",
"category": "text",
"enabled": True,
"url": "https://example.invalid/v1/chat/completions",
"model": "demo-model",
"api_key": "sk-1234567890",
"api_type": "chat",
"connect_timeout_seconds": 1,
"extra_body": {"temperature": 0},
},
path=models_path,
)
text_models = appconfig.list_ai_models("text", path=models_path)
self.assertEqual(2, len(text_models))
self.assertEqual("sk-1***7890", text_models[-1]["api_key"])
self.assertTrue(text_models[-1]["api_key_set"])
private_model = appconfig.get_model("Text 2", path=models_path)
self.assertEqual("sk-1234567890", private_model["api_key"])
self.assertEqual({"temperature": 0}, private_model["extra_body"])
appconfig.update_ai_model(
"Text 2",
path=models_path,
name="Text 3",
enabled=False,
)
self.assertFalse(appconfig.get_model("Text 3", path=models_path)["enabled"])
self.assert_removed(temp_dir)
def test_ai_model_constraints_and_connection_validation(self):
with self.make_temp_dir() as temp_dir:
models_path = os.path.join(temp_dir, "ai_models.json")
appconfig.list_ai_models(path=models_path)
with self.assertRaises(appconfig.ConfigError):
appconfig.add_ai_model(
{
"name": "GPT-5.5 文本",
"category": "text",
"enabled": True,
"api_type": "chat",
"connect_timeout_seconds": 30,
},
path=models_path,
)
with self.assertRaises(appconfig.ConfigError):
appconfig.delete_ai_model("Nano Banana 2", path=models_path)
result = appconfig.test_ai_model("GPT-5.5 文本", path=models_path)
self.assertFalse(result["ok"])
self.assertIn("url", result["error"])
self.assertIn("model", result["error"])
self.assertIn("api_key", result["error"])
self.assert_removed(temp_dir)
def test_sanitize_for_log_masks_secret_fields(self):
payload = {
"name": "demo",
"api_key": "sk-1234567890",
"nested": {
"password": "account-secret",
"items": [
{"provider_token": "token-secret"},
{"value": "safe"},
{"api_key": {"value": "nested-secret"}},
],
},
}
sanitized = appconfig.sanitize_for_log(payload)
self.assertEqual("demo", sanitized["name"])
self.assertEqual("sk-1***7890", sanitized["api_key"])
self.assertEqual("acco***cret", sanitized["nested"]["password"])
self.assertEqual("toke***cret", sanitized["nested"]["items"][0]["provider_token"])
self.assertEqual("safe", sanitized["nested"]["items"][1]["value"])
self.assertEqual("***", sanitized["nested"]["items"][2]["api_key"])
self.assertEqual("sk-1234567890", payload["api_key"])
if __name__ == "__main__":
unittest.main()