90 lines
3.3 KiB
Python
90 lines
3.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""双账号 auth 纯函数与写回隔离测试(不联网): uv run python tests/test_auth.py"""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
sys.stdout.reconfigure(encoding="utf-8")
|
|
|
|
from reaction_board.bilibili.auth import ( # noqa: E402
|
|
cookie_value,
|
|
cookies_from_set_cookie,
|
|
merge_cookie,
|
|
parse_cookie,
|
|
sessdata_expiry,
|
|
)
|
|
from reaction_board.bilibili.auth_flow import AuthService, write_qr_png # noqa: E402
|
|
|
|
READ_OLD = "buvid3=READ-B3; {{SECRET_i5bghhpt}}readcsrf; DedeUserID=11; SESSDATA=old%2C1700000000%2Ca"
|
|
WRITE_OLD = "buvid3=WRITE-B3; bili_jct=writecsrf; DedeUserID=22; SESSDATA=old%2C1700000000%2Cb"
|
|
NEW = {"SESSDATA": "new%2C1800000000%2Cxyz", "bili_jct": "newcsrf", "DedeUserID": "99"}
|
|
|
|
|
|
def raw_config():
|
|
return {
|
|
"bilibili": {
|
|
"read": {"mid": 11, "csrf": "readcsrf", "cookie": READ_OLD},
|
|
"write": {"mid": 22, "csrf": "writecsrf", "cookie": WRITE_OLD},
|
|
},
|
|
"folders": [],
|
|
}
|
|
|
|
|
|
def test_cookie_helpers():
|
|
assert parse_cookie(READ_OLD)[0] == ("buvid3", "READ-B3")
|
|
merged = merge_cookie(WRITE_OLD, NEW)
|
|
assert cookie_value(merged, "SESSDATA") == "new%2C1800000000%2Cxyz"
|
|
assert cookie_value(merged, "bili_jct") == "newcsrf"
|
|
assert cookie_value(merged, "buvid3") == "WRITE-B3"
|
|
assert sessdata_expiry(merged) == 1800000000
|
|
headers = ["SESSDATA=x; Path=/", "bili_jct=y; Domain=.bilibili.com"]
|
|
assert cookies_from_set_cookie(headers) == {"SESSDATA": "x", "bili_jct": "y"}
|
|
|
|
|
|
def test_finish_updates_only_selected_account_and_rolls_back():
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / "config.json"
|
|
original = raw_config()
|
|
path.write_text(json.dumps(original, ensure_ascii=False), encoding="utf-8")
|
|
service = AuthService(path, account="write")
|
|
|
|
service._get_json = lambda credentials, url, auth: { # type: ignore[method-assign]
|
|
"code": 0, "data": {"isLogin": True, "mid": 99, "uname": "writer"}
|
|
}
|
|
assert service.finish(service._load_raw(), NEW) == 0
|
|
saved = json.loads(path.read_text(encoding="utf-8"))
|
|
assert saved["bilibili"]["read"] == original["bilibili"]["read"]
|
|
assert saved["bilibili"]["write"]["mid"] == 99
|
|
assert saved["bilibili"]["write"]["csrf"] == "newcsrf"
|
|
assert cookie_value(saved["bilibili"]["write"]["cookie"], "buvid3") == "WRITE-B3"
|
|
assert path.with_name("config.json.bak").exists()
|
|
|
|
before = path.read_text(encoding="utf-8")
|
|
service._get_json = lambda credentials, url, auth: { # type: ignore[method-assign]
|
|
"code": -101, "data": {"isLogin": False}
|
|
}
|
|
assert service.finish(service._load_raw(), NEW) == 4
|
|
assert path.read_text(encoding="utf-8") == before
|
|
|
|
|
|
def test_qr_png():
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / "qr.png"
|
|
write_qr_png("https://example.com/login?key=abc", path)
|
|
assert path.read_bytes().startswith(b"\x89PNG\r\n\x1a\n")
|
|
|
|
|
|
def main():
|
|
test_cookie_helpers()
|
|
test_finish_updates_only_selected_account_and_rolls_back()
|
|
test_qr_png()
|
|
print("OK: 双账号 auth Cookie 合并、写回隔离与回滚测试全部通过")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|