132 lines
3.7 KiB
Python
132 lines
3.7 KiB
Python
"""斗鱼账号实时状态刷新与生命周期写回测试。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
from typing import cast
|
|
|
|
from core.douyu import DouyuActivityClient
|
|
from web.backend.models import Account
|
|
from web.backend.services.douyu_account_state import refresh_douyu_account_state
|
|
|
|
|
|
class FakeClient:
|
|
def acf_ccn(self, *, refresh_subscribe: bool = False):
|
|
assert refresh_subscribe is False
|
|
return "csrf"
|
|
|
|
def elite_user_info(self, **_kwargs):
|
|
return {
|
|
"manual_type": 1,
|
|
"manual_score": 5200,
|
|
"expire_time": 1799999999,
|
|
}
|
|
|
|
def query_points(self, **_kwargs):
|
|
return {
|
|
"points": 5200,
|
|
"willExpireTime": 1799999999,
|
|
}
|
|
|
|
def query_limited_goods(self, **_kwargs):
|
|
return {"limited_goods": [{"commodityId": "limited-goods"}]}
|
|
|
|
def bind_info(self, **_kwargs):
|
|
return {
|
|
"is_bound_role": False,
|
|
"role_name": "",
|
|
"change_role_wait_time": 3600,
|
|
"area_name": "安卓",
|
|
"plat_name": "QQ",
|
|
}
|
|
|
|
|
|
def test_refresh_writes_ck_elite_points_limited_and_bind_lifecycle(db):
|
|
account = Account(
|
|
username="refresh-account",
|
|
password="p",
|
|
email="refresh@example.com",
|
|
email_password="ep",
|
|
status="imported",
|
|
ck_check_status="",
|
|
cookie="acf_uid=100; acf_nickname=test",
|
|
elite_opened=None,
|
|
points=None,
|
|
auto_managed=True,
|
|
)
|
|
db.add(account)
|
|
db.commit()
|
|
|
|
state = refresh_douyu_account_state(
|
|
db,
|
|
account,
|
|
manual_id="manual",
|
|
rid="room",
|
|
act_alias="alias",
|
|
client=cast(DouyuActivityClient, FakeClient()),
|
|
cookie_checker=lambda _cookie: {
|
|
"valid": True,
|
|
"message": "有效",
|
|
"fish_ball": 10,
|
|
"nickname": "测试",
|
|
"level": 5,
|
|
"checked_at": datetime.now(UTC),
|
|
},
|
|
)
|
|
|
|
db.refresh(account)
|
|
assert state.ck_verdict == "valid"
|
|
assert state.limited_ids == frozenset({"limited-goods"})
|
|
assert account.status == "active"
|
|
assert account.ck_check_status == "valid"
|
|
assert account.elite_opened is True
|
|
assert account.points == 5200
|
|
assert account.role_bind_status == "unbound"
|
|
assert account.change_role_wait_time == 3600
|
|
assert account.role_change_available_at is None
|
|
assert state.role_change_available_at is None
|
|
assert state.role_change_blocked is False
|
|
assert account.overview_queried_at is not None
|
|
assert account.overview_snapshot is not None
|
|
assert (
|
|
account.overview_snapshot["limited_goods"][0]["commodityId"] == "limited-goods"
|
|
)
|
|
|
|
|
|
def test_invalid_ck_writes_lifecycle_without_querying_activity(db):
|
|
account = Account(
|
|
username="invalid-account",
|
|
password="p",
|
|
email="invalid@example.com",
|
|
email_password="ep",
|
|
status="active",
|
|
ck_check_status="",
|
|
cookie="acf_uid=100",
|
|
auto_managed=True,
|
|
)
|
|
db.add(account)
|
|
db.commit()
|
|
|
|
state = refresh_douyu_account_state(
|
|
db,
|
|
account,
|
|
manual_id="manual",
|
|
rid="room",
|
|
act_alias="alias",
|
|
client=cast(DouyuActivityClient, FakeClient()),
|
|
cookie_checker=lambda _cookie: {
|
|
"valid": False,
|
|
"message": "未登录",
|
|
"fish_ball": None,
|
|
"nickname": None,
|
|
"level": None,
|
|
"checked_at": datetime.now(UTC),
|
|
},
|
|
)
|
|
|
|
db.refresh(account)
|
|
assert state.ck_verdict == "invalid"
|
|
assert account.ck_check_status == "invalid"
|
|
assert account.status == "invalid"
|
|
assert state.elite_opened is None
|