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
+44
-4
@@ -22,9 +22,10 @@ import reaction_board.cli as cli # noqa: E402
|
||||
def write_config(tmp, *, cookie="SESSDATA=x; buvid3=y", csrf="csrf"):
|
||||
path = Path(tmp) / "config.json"
|
||||
path.write_text(json.dumps({
|
||||
"mid": 1,
|
||||
"csrf": csrf,
|
||||
"cookie": cookie,
|
||||
"bilibili": {
|
||||
"read": {"mid": 1, "csrf": csrf, "cookie": cookie},
|
||||
"write": {"mid": 2, "csrf": "write-csrf", "cookie": "write-cookie"},
|
||||
},
|
||||
"storage": {
|
||||
"database_path": str(Path(tmp) / "app.db"),
|
||||
"backup_dir": str(Path(tmp) / "backup"),
|
||||
@@ -112,6 +113,37 @@ def test_auth_check_without_cookie():
|
||||
assert "未配置 cookie" in payload["message"]
|
||||
|
||||
|
||||
def test_auth_check_all_and_auth_dispatch():
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = write_config(tmp, cookie="", csrf="")
|
||||
code, output = run_cli(["auth-check", "--account", "all", "--config", str(path)])
|
||||
assert code == 1
|
||||
payload = json.loads(output)
|
||||
assert payload["ok"] is False
|
||||
assert [item["account"] for item in payload["accounts"]] == ["read", "write"]
|
||||
|
||||
original = cli.AuthService
|
||||
calls = []
|
||||
|
||||
class FakeAuthService:
|
||||
def __init__(self, path, *, account, logger):
|
||||
calls.append((str(path), account))
|
||||
|
||||
def run(self, *, timeout, no_browser):
|
||||
assert timeout == 9 and no_browser is True
|
||||
return 7
|
||||
|
||||
cli.AuthService = FakeAuthService
|
||||
try:
|
||||
code, _ = run_cli([
|
||||
"auth", "--account", "write", "--timeout", "9", "--no-browser", "--config", str(path),
|
||||
])
|
||||
finally:
|
||||
cli.AuthService = original
|
||||
assert code == 7
|
||||
assert calls == [(str(path), "write")]
|
||||
|
||||
|
||||
def test_run_once_offline():
|
||||
class FakeClient:
|
||||
def check_login(self):
|
||||
@@ -125,7 +157,13 @@ def test_run_once_offline():
|
||||
path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
|
||||
|
||||
original = cli._client
|
||||
cli._client = lambda *args, **kwargs: FakeClient()
|
||||
selected = []
|
||||
|
||||
def fake_client(credentials, *args, **kwargs):
|
||||
selected.append(credentials)
|
||||
return FakeClient()
|
||||
|
||||
cli._client = fake_client
|
||||
try:
|
||||
code, output = run_cli(["run", "--once", "--config", str(path)])
|
||||
finally:
|
||||
@@ -133,6 +171,7 @@ def test_run_once_offline():
|
||||
assert code == 0
|
||||
assert '"name": "folder_sync"' in output
|
||||
assert '"name": "snapshot"' in output
|
||||
assert selected and all(credentials.mid == 1 for credentials in selected)
|
||||
|
||||
|
||||
def main_test():
|
||||
@@ -141,6 +180,7 @@ def main_test():
|
||||
test_sync_requires_auth_without_network()
|
||||
test_snapshot_offline_empty_db()
|
||||
test_auth_check_without_cookie()
|
||||
test_auth_check_all_and_auth_dispatch()
|
||||
test_run_once_offline()
|
||||
print("OK: CLI init/sync/snapshot/auth-check/run/status/错误路径测试全部通过")
|
||||
|
||||
|
||||
Reference in new issue
Block a user