from server.services.dramabox.app_api import account_from_bootstrap from server.services.dramabox.login import _credential_record def test_credential_file_is_one_json_document() -> None: document = { "kind": "dramabox-login", "version": 1, "app": { "token": "raw-token", "device_id": "ad-1", "android_id": "android-1", "user_id": "88", "vip": "y", "name": "示例", }, } record = _credential_record(document) assert record["token"] == "Bearer raw-token" assert record["device_id"] == "ad-1" assert record["android_id"] == "android-1" assert record["user_id"] == "88" assert record["vip"] is True assert record["name"] == "示例" for broken in ( {}, {"kind": "other", "version": 1, "app": document["app"]}, {"kind": "dramabox-login", "version": 2, "app": document["app"]}, {"kind": "dramabox-login", "version": 1, "app": {"token": "Bearer x"}}, ): try: _credential_record(broken) except RuntimeError: continue raise AssertionError("不完整的凭据文件应被拒绝") def test_account_profile_must_match_the_credential() -> None: app = {"user_id": "88", "token": "Bearer old"} fresh = account_from_bootstrap(app, {"user": {"uid": "88", "token": "new", "nickname": "阿花", "isVip": 1}}) assert fresh["token"] == "Bearer new" assert fresh["name"] == "阿花" assert fresh["vip"] is True try: account_from_bootstrap(app, {"user": {"uid": "99", "token": "new", "nickname": "别人"}}) except RuntimeError as exc: assert "失效" in str(exc) else: raise AssertionError("用户编号不一致应视为未登录") try: account_from_bootstrap(app, {}) except RuntimeError as exc: assert "失效" in str(exc) else: raise AssertionError("没有个人信息应视为未登录") if __name__ == "__main__": test_credential_file_is_one_json_document() test_account_profile_must_match_the_credential() print("ok")