refactor(douyu): 统一账号准入与本地查询链路
This commit is contained in:
@@ -7,8 +7,10 @@ import pytest
|
||||
from tests.support.db import get_test_engine, make_test_session
|
||||
from web.backend.models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountEvent,
|
||||
AccountLease,
|
||||
AccountSyncState,
|
||||
AuditLog,
|
||||
DouyuTask,
|
||||
PlatformCommodityMapping,
|
||||
@@ -116,19 +118,6 @@ class FakeNoBindClient(FakeClient):
|
||||
raise RuntimeError("未绑定角色")
|
||||
|
||||
|
||||
class FakeUnboundClient(FakeClient):
|
||||
def bind_info(self, **_):
|
||||
return {
|
||||
"is_bound_account": False,
|
||||
"is_bound_role": False,
|
||||
"is_bound_act": False,
|
||||
"role_name": "",
|
||||
"area_name": "",
|
||||
"plat_name": "",
|
||||
"nickname": "",
|
||||
}
|
||||
|
||||
|
||||
class TestDouyuCollabOrders:
|
||||
def setup_method(self):
|
||||
self.engine = get_test_engine()
|
||||
@@ -378,7 +367,17 @@ class TestDouyuCollabOrders:
|
||||
assert order.account_id is None
|
||||
assert order.status == collab.STATUS_INCOMPLETE
|
||||
|
||||
def test_recommend_accounts_prefers_unbound_and_can_exclude_rebindable(self):
|
||||
def test_recommend_accounts_prefers_unbound_and_can_exclude_rebindable(
|
||||
self,
|
||||
monkeypatch,
|
||||
):
|
||||
monkeypatch.setattr(
|
||||
collab,
|
||||
"probe_account",
|
||||
lambda *_args, **_kwargs: (_ for _ in ()).throw(
|
||||
AssertionError("本地推荐不应访问斗鱼官网")
|
||||
),
|
||||
)
|
||||
mapping = PlatformCommodityMapping(
|
||||
target_platform="douyu",
|
||||
internal_sku_key="推荐礼包",
|
||||
@@ -405,9 +404,12 @@ class TestDouyuCollabOrders:
|
||||
email_password="mail-password",
|
||||
cookie="acf_uid=200; acf_nickname=bound",
|
||||
status="active",
|
||||
ck_check_status="valid",
|
||||
auto_managed=True,
|
||||
elite_opened=True,
|
||||
points=1000,
|
||||
role_bind_status="bound",
|
||||
game_name="角色B",
|
||||
)
|
||||
empty = Account(
|
||||
username="douyu-account-empty",
|
||||
@@ -416,18 +418,37 @@ class TestDouyuCollabOrders:
|
||||
email_password="mail-password",
|
||||
cookie="acf_uid=300; acf_nickname=empty",
|
||||
status="active",
|
||||
ck_check_status="valid",
|
||||
auto_managed=True,
|
||||
elite_opened=True,
|
||||
points=500,
|
||||
role_bind_status="unbound",
|
||||
)
|
||||
self.session.add_all([bound, empty])
|
||||
self.session.flush()
|
||||
self.session.add_all(
|
||||
[
|
||||
AccountCommodityState(
|
||||
account_id=bound.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id="gift-a",
|
||||
availability="available",
|
||||
observed_at=datetime.now(UTC),
|
||||
valid_until=datetime.now(UTC) + timedelta(hours=1),
|
||||
),
|
||||
AccountCommodityState(
|
||||
account_id=empty.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id="gift-a",
|
||||
availability="available",
|
||||
observed_at=datetime.now(UTC),
|
||||
valid_until=datetime.now(UTC) + timedelta(hours=1),
|
||||
),
|
||||
]
|
||||
)
|
||||
self.session.commit()
|
||||
clients = {
|
||||
int(self.account.id): FakeClient(),
|
||||
int(bound.id): FakeClient(),
|
||||
int(empty.id): FakeUnboundClient(),
|
||||
}
|
||||
|
||||
result = collab.recommend_accounts(
|
||||
self.session,
|
||||
@@ -435,7 +456,6 @@ class TestDouyuCollabOrders:
|
||||
order,
|
||||
include_rebindable=True,
|
||||
limit=5,
|
||||
client_factory=lambda account: clients[int(account.id)],
|
||||
)
|
||||
|
||||
assert order.commodity_id == "gift-a"
|
||||
@@ -450,7 +470,6 @@ class TestDouyuCollabOrders:
|
||||
order,
|
||||
include_rebindable=False,
|
||||
limit=5,
|
||||
client_factory=lambda account: clients[int(account.id)],
|
||||
)
|
||||
assert [item["account_id"] for item in unbound_only["candidates"]] == [
|
||||
empty.id
|
||||
@@ -1611,6 +1630,16 @@ class TestDouyuCollabOrders:
|
||||
|
||||
assert client.offsets == [0, 100]
|
||||
assert len(loaded) == 101
|
||||
sync_state = (
|
||||
self.session.query(AccountSyncState)
|
||||
.filter_by(
|
||||
account_id=order.account_id,
|
||||
platform="douyu",
|
||||
domain="exchange_records",
|
||||
)
|
||||
.one()
|
||||
)
|
||||
assert len(sync_state.raw_payload_json["elite"]) == 101
|
||||
|
||||
def test_record_query_rejects_old_record_and_wrong_commodity(self):
|
||||
order = collab.create_order(
|
||||
|
||||
Reference in New Issue
Block a user