修复(CK检测): 统一斗鱼CK有效性检测入口并禁止非鉴权接口覆盖状态
This commit is contained in:
@@ -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