feat(auth): separate read and write bilibili accounts
This commit is contained in:
1 parent
5c2ab7b187
commit
178ab64b18
17 files changed
+671
-707
No files matched your search
+65
-140
@@ -1,5 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""配置加载 / 校验 / 热加载测试(不联网): uv run python tests/test_config.py"""
|
||||
"""双账号配置加载 / 校验 / 热加载测试(不联网): uv run python tests/test_config.py"""
|
||||
|
||||
import json
|
||||
import os
|
||||
@@ -13,24 +13,22 @@ sys.stdout.reconfigure(encoding="utf-8")
|
||||
|
||||
from reaction_board.config import ConfigError, ConfigStore, load_config # noqa: E402
|
||||
|
||||
COOKIE = "SESSDATA=secret%2C1800000000%2Cabc; bili_jct=csrf-secret; buvid3=dev"
|
||||
|
||||
|
||||
def write(path: Path, data) -> None:
|
||||
path.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
|
||||
|
||||
def bump_mtime(path: Path) -> None:
|
||||
"""保证 mtime 严格大于上一次,避免 Windows 时间精度导致热加载测试偶发失败。"""
|
||||
target = max(path.stat().st_mtime_ns, time.time_ns()) + 1_000_000
|
||||
os.utime(path, ns=(target, target))
|
||||
|
||||
|
||||
def base_legacy() -> dict:
|
||||
def base_config() -> dict:
|
||||
return {
|
||||
"mid": 28802351,
|
||||
"csrf": "csrf-secret",
|
||||
"cookie": COOKIE,
|
||||
"bilibili": {
|
||||
"read": {"mid": 11, "csrf": "read-csrf", "cookie": "read-cookie"},
|
||||
"write": {"mid": 22, "csrf": "write-csrf", "cookie": "write-cookie"},
|
||||
},
|
||||
"overrides": {"1": {"mid": 2, "name": "x"}},
|
||||
"sleep": {"min": 1},
|
||||
"folders": [
|
||||
@@ -42,168 +40,95 @@ def base_legacy() -> dict:
|
||||
}
|
||||
|
||||
|
||||
def test_legacy_flat_mapping():
|
||||
def test_dual_accounts_and_env_override():
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "config.json"
|
||||
write(path, base_legacy())
|
||||
cfg = load_config(path, env={})
|
||||
assert cfg.bilibili.mid == 28802351
|
||||
assert cfg.bilibili.csrf == "csrf-secret"
|
||||
assert cfg.bilibili.cookie == COOKIE
|
||||
assert cfg.storage.database_path == Path(tmp) / "data" / "app.db"
|
||||
assert cfg.storage.backup_dir == Path(tmp) / "data" / "backup"
|
||||
assert cfg.schedule.snapshot_at == "00:01"
|
||||
assert [f.media_id for f in cfg.folders] == [4451053851, 3700023351]
|
||||
assert cfg.track_fans == (37754047,)
|
||||
assert cfg.up_blacklist_seed == (1, 2) # 去重
|
||||
assert cfg.raw["overrides"] # 旧字段仍保留给 favsort
|
||||
assert set(cfg.heat_weights) == {
|
||||
"view", "like", "coin", "favorite", "share", "reply", "danmaku"
|
||||
}
|
||||
|
||||
|
||||
def test_nested_and_env_override():
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "config.json"
|
||||
write(path, {
|
||||
"bilibili": {"mid": 1, "csrf": "old", "cookie": "old-cookie"},
|
||||
"storage": {"database_path": "var/app.db", "backup_dir": "var/bak"},
|
||||
"folders": [],
|
||||
})
|
||||
write(path, base_config())
|
||||
env = {
|
||||
"BILI_MID": "999",
|
||||
"BILI_CSRF": "env-csrf",
|
||||
"BILI_COOKIE": "env-cookie",
|
||||
"TG_BOT_TOKEN": "123456:abcdefghijklmnopqrstuvwxyzABCDE",
|
||||
"TG_CHAT_ID": "42",
|
||||
"DASHBOARD_AUTH_TOKEN": "web-token",
|
||||
"BILI_READ_MID": "101", "BILI_READ_CSRF": "env-read", "BILI_READ_COOKIE": "env-read-cookie",
|
||||
"BILI_WRITE_MID": "202", "BILI_WRITE_CSRF": "env-write", "BILI_WRITE_COOKIE": "env-write-cookie",
|
||||
"DATABASE_PATH": "custom.db",
|
||||
}
|
||||
cfg = load_config(path, env=env)
|
||||
assert cfg.bilibili.mid == 999
|
||||
assert cfg.bilibili.csrf == "env-csrf"
|
||||
assert cfg.bilibili.cookie == "env-cookie"
|
||||
assert cfg.telegram.chat_id == 42
|
||||
assert cfg.telegram.bot_token.startswith("123456:")
|
||||
assert cfg.web.auth_token == "web-token"
|
||||
assert (cfg.bilibili.read.mid, cfg.bilibili.read.csrf, cfg.bilibili.read.cookie) == (101, "env-read", "env-read-cookie")
|
||||
assert (cfg.bilibili.write.mid, cfg.bilibili.write.csrf, cfg.bilibili.write.cookie) == (202, "env-write", "env-write-cookie")
|
||||
assert cfg.storage.database_path == Path(tmp) / "custom.db"
|
||||
# 备份目录仍按配置文件目录解析
|
||||
assert cfg.storage.backup_dir == Path(tmp) / "var" / "bak"
|
||||
assert cfg.up_blacklist_seed == (1, 2)
|
||||
|
||||
|
||||
def test_absolute_paths_kept():
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
db = str(Path(tmp) / "abs" / "app.db")
|
||||
path = Path(tmp) / "config.json"
|
||||
write(path, {
|
||||
"bilibili": {"mid": 1, "csrf": "c", "cookie": "k"},
|
||||
"storage": {"database_path": db, "backup_dir": str(Path(tmp) / "b")},
|
||||
"folders": [],
|
||||
})
|
||||
cfg = load_config(path, env={})
|
||||
assert cfg.storage.database_path == Path(db)
|
||||
|
||||
|
||||
def test_invalid_configs():
|
||||
def test_flat_and_missing_account_rejected():
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "config.json"
|
||||
cases = [
|
||||
({"folders": [{"year": 2025, "media_id": 1},
|
||||
{"year": 2026, "media_id": 1}]}, "media_id 重复"),
|
||||
({"folders": [{"year": 2025, "media_id": 1},
|
||||
{"year": 2025, "media_id": 2}]}, "year 重复"),
|
||||
({"folders": [{"year": 2025, "media_id": 0}]}, "不能小于 1"),
|
||||
({"schedule": {"snapshot_at": "24:00"}}, "snapshot_at"),
|
||||
({"schedule": {"snapshot_at": "1:01"}}, "snapshot_at"),
|
||||
({"notify": {"max_items_per_message": 0}}, "max_items_per_message"),
|
||||
({"heat_weights": {"view": -1}}, "heat_weights.view"),
|
||||
({"classify": {"require_title": ""}}, "require_title"),
|
||||
]
|
||||
base = {
|
||||
"bilibili": {"mid": 1, "csrf": "c", "cookie": "k"},
|
||||
"folders": [],
|
||||
}
|
||||
for patch, needle in cases:
|
||||
data = json.loads(json.dumps(base))
|
||||
data.update(patch)
|
||||
write(path, data)
|
||||
try:
|
||||
load_config(path, env={})
|
||||
except ConfigError as exc:
|
||||
assert needle in str(exc), (needle, str(exc))
|
||||
else:
|
||||
raise AssertionError(f"应当失败: {patch}")
|
||||
write(path, {"mid": 1, "csrf": "x", "cookie": "y", "folders": []})
|
||||
try:
|
||||
load_config(path, env={})
|
||||
except ConfigError as exc:
|
||||
assert "bilibili.read" in str(exc)
|
||||
else:
|
||||
raise AssertionError("flat 格式必须拒绝")
|
||||
write(path, {"bilibili": {"read": {}}, "folders": []})
|
||||
try:
|
||||
load_config(path, env={})
|
||||
except ConfigError as exc:
|
||||
assert "bilibili.write" in str(exc)
|
||||
else:
|
||||
raise AssertionError("缺少 write 必须拒绝")
|
||||
|
||||
|
||||
def test_require_auth():
|
||||
def test_validation_require_read_auth_and_hot_reload():
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "config.json"
|
||||
write(path, {"bilibili": {"mid": 1, "csrf": "", "cookie": ""}, "folders": []})
|
||||
data = base_config()
|
||||
data["bilibili"]["read"].update(cookie="", csrf="")
|
||||
write(path, data)
|
||||
load_config(path, env={}, require_auth=False)
|
||||
try:
|
||||
load_config(path, env={}, require_auth=True)
|
||||
except ConfigError as exc:
|
||||
assert "cookie" in str(exc)
|
||||
assert "bilibili.read.cookie" in str(exc)
|
||||
else:
|
||||
raise AssertionError("require_auth 应拒绝空凭据")
|
||||
raise AssertionError("read 空凭据必须拒绝")
|
||||
|
||||
|
||||
def test_env_int_invalid():
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "config.json"
|
||||
write(path, base_legacy())
|
||||
try:
|
||||
load_config(path, env={"BILI_MID": "abc"})
|
||||
except ConfigError as exc:
|
||||
assert "BILI_MID" in str(exc)
|
||||
else:
|
||||
raise AssertionError("非法整数环境变量应失败")
|
||||
|
||||
|
||||
def test_hot_reload_keeps_last_good_config():
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "config.json"
|
||||
write(path, base_legacy())
|
||||
write(path, base_config())
|
||||
store = ConfigStore(path, env={})
|
||||
assert store.get().folders[0].title == "2025"
|
||||
|
||||
data = base_legacy()
|
||||
data["folders"][0]["title"] = "2025-新"
|
||||
data = base_config()
|
||||
data["folders"][0]["title"] = "2025-new"
|
||||
write(path, data)
|
||||
bump_mtime(path)
|
||||
assert store.get().folders[0].title == "2025-新"
|
||||
assert store.last_error is None
|
||||
|
||||
path.write_text("{not json", encoding="utf-8")
|
||||
assert store.get().folders[0].title == "2025-new"
|
||||
path.write_text("{bad", encoding="utf-8")
|
||||
bump_mtime(path)
|
||||
assert store.get().folders[0].title == "2025-新"
|
||||
assert store.last_error is not None
|
||||
assert store.get().folders[0].title == "2025-new"
|
||||
assert store.last_error
|
||||
|
||||
|
||||
def test_invalid_values():
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "config.json"
|
||||
data = base_config()
|
||||
data["bilibili"]["write"]["mid"] = "bad"
|
||||
write(path, data)
|
||||
bump_mtime(path)
|
||||
assert store.get().folders[0].title == "2025-新"
|
||||
assert store.last_error is None
|
||||
|
||||
|
||||
def test_missing_file():
|
||||
try:
|
||||
load_config(Path(tempfile.gettempdir()) / "no-such-bili-fav-config.json", env={})
|
||||
except ConfigError as exc:
|
||||
assert "缺少配置文件" in str(exc)
|
||||
else:
|
||||
raise AssertionError("缺失配置应失败")
|
||||
try:
|
||||
load_config(path, env={})
|
||||
except ConfigError as exc:
|
||||
assert "bilibili.write.mid" in str(exc)
|
||||
else:
|
||||
raise AssertionError("非法 mid 必须拒绝")
|
||||
write(path, base_config())
|
||||
try:
|
||||
load_config(path, env={"BILI_READ_MID": "bad"})
|
||||
except ConfigError as exc:
|
||||
assert "BILI_READ_MID" in str(exc)
|
||||
else:
|
||||
raise AssertionError("非法环境变量必须拒绝")
|
||||
|
||||
|
||||
def main():
|
||||
test_legacy_flat_mapping()
|
||||
test_nested_and_env_override()
|
||||
test_absolute_paths_kept()
|
||||
test_invalid_configs()
|
||||
test_require_auth()
|
||||
test_env_int_invalid()
|
||||
test_hot_reload_keeps_last_good_config()
|
||||
test_missing_file()
|
||||
print("OK: 配置加载、校验与热加载测试全部通过")
|
||||
test_dual_accounts_and_env_override()
|
||||
test_flat_and_missing_account_rejected()
|
||||
test_validation_require_read_auth_and_hot_reload()
|
||||
test_invalid_values()
|
||||
print("OK: 双账号配置加载、拒绝 flat 格式、ENV 覆盖与热加载测试全部通过")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user