统一推送前Python代码格式

This commit is contained in:
yml2213
2026-09-05 19:25:46 +08:00
parent 90a0065918
commit 8309072120
17 changed files with 492 additions and 189 deletions
+8 -4
View File
@@ -17,8 +17,10 @@ def _fake_response(status=200, headers=None, text="", json_body=None):
resp.status_code = status
resp.headers = headers or {}
resp.text = text
resp.json = Mock(return_value=json_body) if json_body is not None else Mock(
side_effect=ValueError("bad json")
resp.json = (
Mock(return_value=json_body)
if json_body is not None
else Mock(side_effect=ValueError("bad json"))
)
return resp
@@ -36,7 +38,9 @@ def test_fetches_four_fields_from_middle_and_anon(monkeypatch):
session = Mock(spec=requests.Session)
deviceid = "w_1147246591246680065"
session.get.return_value = _fake_response(
headers={"Set-Cookie": f"udb_deviceid={deviceid}; Max-Age=315360000; Domain=huya.com"}
headers={
"Set-Cookie": f"udb_deviceid={deviceid}; Max-Age=315360000; Domain=huya.com"
}
)
session.post.return_value = _fake_response(json_body=_fake_anon_body())
monkeypatch.setattr("core.huya.anon_device._new_session", lambda **kw: session)
@@ -129,4 +133,4 @@ def test_non_json_anon_body_raises(monkeypatch):
monkeypatch.setattr("core.huya.anon_device._new_session", lambda **kw: session)
with pytest.raises(AnonymousDeviceError, match="非 JSON"):
fetch_anonymous_device_state(session=session)
fetch_anonymous_device_state(session=session)
+26 -5
View File
@@ -98,10 +98,24 @@ def test_app_login_cookie_does_not_fabricate_browser_state():
)
values = dict(item.split("=", 1) for item in cookie.split("; "))
assert set(values) == {
"udb_cred", "udb_uid", "yyuid", "udb_version", "udb_biztoken",
"udb_origin", "udb_status", "sdid", "hdid", "udb_passport", "username",
"udb_cred",
"udb_uid",
"yyuid",
"udb_version",
"udb_biztoken",
"udb_origin",
"udb_status",
"sdid",
"hdid",
"udb_passport",
"username",
}
assert not {"_qimei_uuid42", "_qimei_fingerprint", "_qimei_h38", "udb_anobiztoken"} & set(values)
assert not {
"_qimei_uuid42",
"_qimei_fingerprint",
"_qimei_h38",
"udb_anobiztoken",
} & set(values)
def test_app_login_cookie_overwrites_auth_but_keeps_distinct_explicit_device_values():
@@ -137,12 +151,19 @@ def test_app_login_cookie_contains_web_session_shape():
session = MagicMock()
session.cookies = {"udb_uid": "123"}
cookie = HuyaAppPasswordLogin._merge_web_cookie(
session, "web-token", "s" * 193, "h" * 40,
session,
"web-token",
"s" * 193,
"h" * 40,
{"guid": "a" * 32, "udb_guiddata": "b" * 32},
)
keys = {item.split("=", 1)[0] for item in cookie.split("; ")}
expected = {
"udb_biztoken", "sdid", "hdid", "guid", "udb_guiddata",
"udb_biztoken",
"sdid",
"hdid",
"guid",
"udb_guiddata",
}
assert expected <= keys
+4 -2
View File
@@ -10,7 +10,9 @@ def test_minimal_app_cookie_reports_only_missing_web_guid():
)
assert HuyaBatchRunnerCore._web_wss_cookie_missing(cookie) == (
"udb_deviceid", "udb_guiddata", "udb_anobiztoken",
"udb_deviceid",
"udb_guiddata",
"udb_anobiztoken",
)
assert not HuyaBatchRunnerCore._has_web_wss_cookie(cookie)
@@ -37,4 +39,4 @@ def test_script_only_fields_do_not_block_wss():
)
assert HuyaBatchRunnerCore._web_wss_cookie_missing(cookie) == ()
assert HuyaBatchRunnerCore._has_web_wss_cookie(cookie)
assert HuyaBatchRunnerCore._has_web_wss_cookie(cookie)
+24 -8
View File
@@ -26,7 +26,9 @@ class TestHuyaWorkbenchScope:
Base.metadata.create_all(self.engine)
self.session = sessionmaker(bind=self.engine)()
self.user = User(username="operator", password_hash="hash", role="super_admin")
self.account = HuyaAccount(uid="100", yyuid="100", cookie="yyuid=100; udb_uid=100")
self.account = HuyaAccount(
uid="100", yyuid="100", cookie="yyuid=100; udb_uid=100"
)
self.session.add_all([self.user, self.account])
self.session.commit()
@@ -37,11 +39,15 @@ class TestHuyaWorkbenchScope:
def test_accounts_and_tasks_are_scoped(self):
update_huya_workbench_accounts(
HuyaWorkbenchAccountsUpdate(handbook_scope="elite", account_ids=[self.account.id]),
HuyaWorkbenchAccountsUpdate(
handbook_scope="elite", account_ids=[self.account.id]
),
db=self.session,
current=self.user,
)
result = list_huya_workbench_accounts("elite", db=self.session, current=self.user)
result = list_huya_workbench_accounts(
"elite", db=self.session, current=self.user
)
assert result == {"account_ids": [self.account.id], "configured": True}
assert self.session.query(HuyaWorkbench).count() == 1
@@ -77,29 +83,39 @@ class TestHuyaWorkbenchScope:
self.session.commit()
update_huya_workbench_accounts(
HuyaWorkbenchAccountsUpdate(handbook_scope="browser_exchange", account_ids=[self.account.id]),
HuyaWorkbenchAccountsUpdate(
handbook_scope="browser_exchange", account_ids=[self.account.id]
),
db=self.session,
current=self.user,
)
assert list_huya_workbench_accounts("browser_exchange", db=self.session, current=self.user) == {
assert list_huya_workbench_accounts(
"browser_exchange", db=self.session, current=self.user
) == {
"account_ids": [self.account.id],
"configured": True,
}
# browser_exchange 与 elite 两个 scope 的账号集合互不影响
assert list_huya_workbench_accounts("elite", db=self.session, current=self.user) == {
assert list_huya_workbench_accounts(
"elite", db=self.session, current=self.user
) == {
"account_ids": [],
"configured": False,
}
# ids 接口:has_cookie=True 只返回有 Cookie 的账号
result = list_huya_account_ids(has_cookie=True, db=self.session, current=self.user)
result = list_huya_account_ids(
has_cookie=True, db=self.session, current=self.user
)
assert result == {"account_ids": [self.account.id], "total": 1}
all_ids = list_huya_account_ids(db=self.session, current=self.user)
assert sorted(all_ids["account_ids"]) == sorted([self.account.id, no_cookie.id])
assert all_ids["total"] == 2
# ids 接口:search 匹配 uid
searched = list_huya_account_ids(search="200", db=self.session, current=self.user)
searched = list_huya_account_ids(
search="200", db=self.session, current=self.user
)
assert searched["account_ids"] == [no_cookie.id]
def test_account_delete_cleans_workbench_membership(self):