修复(CK检测): 统一斗鱼CK有效性检测入口并禁止非鉴权接口覆盖状态
This commit is contained in:
@@ -109,6 +109,11 @@ def test_execute_work_item_syncs_all_goods_and_completes(db):
|
||||
session_factory=make_test_session,
|
||||
client_factory=lambda _cookie: object(),
|
||||
snapshot_fetcher=lambda *_args, **_kwargs: _snapshot(),
|
||||
session_checker=lambda _cookie: {
|
||||
"valid": True,
|
||||
"message": "有效",
|
||||
"checked_at": datetime.now(UTC),
|
||||
},
|
||||
)
|
||||
|
||||
with make_test_session() as verify:
|
||||
@@ -158,6 +163,11 @@ def test_work_item_failure_returns_to_pending_for_retry(db):
|
||||
session_factory=make_test_session,
|
||||
client_factory=lambda _cookie: object(),
|
||||
snapshot_fetcher=fail,
|
||||
session_checker=lambda _cookie: {
|
||||
"valid": True,
|
||||
"message": "有效",
|
||||
"checked_at": datetime.now(UTC),
|
||||
},
|
||||
)
|
||||
|
||||
with make_test_session() as verify:
|
||||
@@ -212,6 +222,11 @@ def test_esports_profile_sync_persists_data_without_rewriting_ck(db):
|
||||
session_factory=make_test_session,
|
||||
client_factory=lambda _cookie: object(),
|
||||
probe_fetcher=probe,
|
||||
session_checker=lambda _cookie: {
|
||||
"valid": True,
|
||||
"message": "有效",
|
||||
"checked_at": datetime.now(UTC),
|
||||
},
|
||||
)
|
||||
|
||||
with make_test_session() as verify:
|
||||
@@ -262,6 +277,11 @@ def test_exchange_record_work_persists_events_and_raw_projection(db):
|
||||
work.id,
|
||||
session_factory=make_test_session,
|
||||
client_factory=lambda _cookie: _Client(),
|
||||
session_checker=lambda _cookie: {
|
||||
"valid": True,
|
||||
"message": "有效",
|
||||
"checked_at": datetime.now(UTC),
|
||||
},
|
||||
)
|
||||
|
||||
with make_test_session() as verify:
|
||||
@@ -303,6 +323,11 @@ def test_run_account_sync_queue_claims_and_executes(db):
|
||||
session_factory=make_test_session,
|
||||
client_factory=lambda _cookie: object(),
|
||||
snapshot_fetcher=lambda *_args, **_kwargs: _snapshot(),
|
||||
session_checker=lambda _cookie: {
|
||||
"valid": True,
|
||||
"message": "有效",
|
||||
"checked_at": datetime.now(UTC),
|
||||
},
|
||||
)
|
||||
|
||||
with make_test_session() as verify:
|
||||
@@ -403,3 +428,53 @@ def test_expire_stale_login_work(db):
|
||||
assert expired == 1
|
||||
assert item.status == "failed"
|
||||
assert "已转人工" in item.last_error
|
||||
|
||||
|
||||
def test_storehome_sync_rejects_invalid_cookie_without_writing_capability(db):
|
||||
_config(db)
|
||||
account = _account("sync-invalid-ck")
|
||||
db.add(account)
|
||||
db.commit()
|
||||
work = enqueue_account_work(
|
||||
db,
|
||||
account_id=account.id,
|
||||
work_type=WORK_STOREHOME_SYNC,
|
||||
)
|
||||
db.commit()
|
||||
claim_account_work(
|
||||
db,
|
||||
work_types=(WORK_STOREHOME_SYNC,),
|
||||
limit=1,
|
||||
)
|
||||
db.commit()
|
||||
|
||||
called = {"snapshot": 0}
|
||||
|
||||
def _fail_if_called(*_args, **_kwargs):
|
||||
called["snapshot"] += 1
|
||||
return _snapshot()
|
||||
|
||||
result = execute_account_work_item(
|
||||
work.id,
|
||||
session_factory=make_test_session,
|
||||
client_factory=lambda _cookie: object(),
|
||||
snapshot_fetcher=_fail_if_called,
|
||||
session_checker=lambda _cookie: {
|
||||
"valid": False,
|
||||
"message": "鱼丸接口: 账户登陆异常,请重新登陆;等级接口: 用户未登录",
|
||||
"checked_at": datetime.now(UTC),
|
||||
},
|
||||
)
|
||||
|
||||
with make_test_session() as verify:
|
||||
acc = verify.get(Account, account.id)
|
||||
states = (
|
||||
verify.query(AccountCommodityState)
|
||||
.filter(AccountCommodityState.account_id == account.id)
|
||||
.all()
|
||||
)
|
||||
assert result["ok"] is False
|
||||
assert called["snapshot"] == 0
|
||||
assert states == []
|
||||
assert acc.ck_check_status == "invalid"
|
||||
assert acc.status == "invalid"
|
||||
|
||||
@@ -345,6 +345,43 @@ class TestCookieOperation:
|
||||
assert result["nickname"] == "new-name"
|
||||
assert result["level"] == 12
|
||||
|
||||
@patch("web.backend.services.cookie_check_service.requests.get")
|
||||
def test_cookie_check_retries_timeout_and_allows_single_endpoint_fallback(self, mock_get):
|
||||
import requests as req_lib
|
||||
from web.backend.services.cookie_check_service import classify_douyu_cookie_result
|
||||
|
||||
# fishBall 两次均网络超时,但等级接口明确返回有效 -> 降级判有效
|
||||
mock_get.side_effect = [
|
||||
req_lib.ReadTimeout("Read timed out."),
|
||||
req_lib.ReadTimeout("Read timed out."),
|
||||
SimpleNamespace(json=lambda: {"error": 0, "data": {"nn": "ok-user", "lv": 10}}),
|
||||
]
|
||||
res_degraded = check_douyu_cookie("valid-cookie")
|
||||
assert res_degraded["valid"] is True
|
||||
assert res_degraded["nickname"] == "ok-user"
|
||||
assert res_degraded["level"] == 10
|
||||
|
||||
# 若鱼丸明确返回账户登陆异常,即使等级接口 ok 仍判失效(invalid)
|
||||
mock_get.side_effect = [
|
||||
SimpleNamespace(json=lambda: {"error": 1, "msg": "账户登陆异常,请重新登陆"}),
|
||||
SimpleNamespace(json=lambda: {"error": 0, "data": {"nn": "ok-user", "lv": 10}}),
|
||||
]
|
||||
res_auth_fail = check_douyu_cookie("half-expired")
|
||||
assert res_auth_fail["valid"] is False
|
||||
assert classify_douyu_cookie_result(res_auth_fail) == "invalid"
|
||||
|
||||
# 两次全部超时(无鉴权失败)应判 error 而非 invalid
|
||||
mock_get.side_effect = [
|
||||
req_lib.ReadTimeout("Read timed out."),
|
||||
req_lib.ReadTimeout("Read timed out."),
|
||||
req_lib.ReadTimeout("Read timed out."),
|
||||
req_lib.ReadTimeout("Read timed out."),
|
||||
]
|
||||
res_timeout = check_douyu_cookie("timeout-cookie")
|
||||
assert res_timeout["valid"] is False
|
||||
assert classify_douyu_cookie_result(res_timeout) == "error"
|
||||
assert "网络读取超时" in res_timeout["message"]
|
||||
|
||||
@patch("web.backend.routers.cookies._start_relogin_accounts")
|
||||
def test_relogin_invalid_only_targets_support_visible_accounts(self, start_relogin):
|
||||
self.owned_account.ck_check_status = "invalid"
|
||||
|
||||
@@ -300,3 +300,105 @@ def test_persist_probe_error_preserves_last_known_role(db):
|
||||
event = db.query(AccountEvent).filter_by(event_type="account_probe").one()
|
||||
assert event.detail["probe_id"] == "probe-error"
|
||||
assert event.detail["role"] == ROLE_ERROR
|
||||
|
||||
|
||||
def test_probe_account_uses_unified_ck_check_even_when_client_supplied(monkeypatch):
|
||||
import web.backend.services.douyu_account_probe as probe_module
|
||||
|
||||
checked = []
|
||||
|
||||
def _fake_checker(cookie: str):
|
||||
checked.append(cookie)
|
||||
return {
|
||||
"valid": False,
|
||||
"message": "鱼丸接口: 账户登陆异常,请重新登陆;等级接口: 用户未登录",
|
||||
"checked_at": datetime.now(UTC),
|
||||
}
|
||||
|
||||
monkeypatch.setattr(probe_module, "check_douyu_cookie", _fake_checker)
|
||||
account = Account(
|
||||
id=99,
|
||||
username="client-with-ck",
|
||||
password="p",
|
||||
email="ck@example.com",
|
||||
email_password="ep",
|
||||
cookie="acf_uid=99",
|
||||
)
|
||||
client = ProbeClient()
|
||||
result = probe_account(account, CONFIG, client=client)
|
||||
|
||||
assert checked == ["acf_uid=99"]
|
||||
assert result["session"]["status"] == "invalid"
|
||||
assert client.calls == 0
|
||||
|
||||
|
||||
def test_persist_probe_ignores_unauthoritative_activity_session_and_verify_revalidates(db):
|
||||
from web.backend.services.douyu_account_probe import verify_account_session
|
||||
|
||||
account = Account(
|
||||
username="legacy-activity-ck",
|
||||
password="p",
|
||||
email="legacy@example.com",
|
||||
email_password="ep",
|
||||
status="invalid",
|
||||
ck_check_status="invalid",
|
||||
ck_checked_at=datetime.now(UTC),
|
||||
ck_check_result={"message": "活动接口可用"},
|
||||
cookie="acf_uid=88",
|
||||
)
|
||||
db.add(account)
|
||||
db.commit()
|
||||
|
||||
persist_probe_result(
|
||||
db,
|
||||
account,
|
||||
{
|
||||
"probe_id": "non-auth",
|
||||
"identity": {"uid": "88", "username": account.username},
|
||||
"session": {
|
||||
"status": "valid",
|
||||
"message": "活动接口可用",
|
||||
"authoritative": False,
|
||||
"detail": {},
|
||||
},
|
||||
"elite": {},
|
||||
"eligibility": {},
|
||||
"roles": {},
|
||||
"gold": {},
|
||||
"goods": {},
|
||||
"errors": {},
|
||||
},
|
||||
source="test.non_auth",
|
||||
record_event=False,
|
||||
commit=True,
|
||||
)
|
||||
db.refresh(account)
|
||||
assert account.ck_check_status == "invalid"
|
||||
|
||||
# 即使历史误记为 valid + "活动接口可用",verify_account_session 也必须忽略缓存重检
|
||||
account.ck_check_status = "valid"
|
||||
account.ck_check_result = {"message": "活动接口可用"}
|
||||
account.ck_checked_at = datetime.now(UTC)
|
||||
db.commit()
|
||||
|
||||
calls = {"n": 0}
|
||||
|
||||
def _real_fail(_cookie: str):
|
||||
calls["n"] += 1
|
||||
return {
|
||||
"valid": False,
|
||||
"message": "鱼丸接口: 账户登陆异常,请重新登陆;等级接口: 用户未登录",
|
||||
"checked_at": datetime.now(UTC),
|
||||
}
|
||||
|
||||
session = verify_account_session(
|
||||
db,
|
||||
account,
|
||||
source="test.recheck",
|
||||
checker=_real_fail,
|
||||
max_age_seconds=3600,
|
||||
commit=True,
|
||||
)
|
||||
assert calls["n"] == 1
|
||||
assert session["status"] == "invalid"
|
||||
assert account.ck_check_status == "invalid"
|
||||
|
||||
Reference in New Issue
Block a user