feat(douyu): 支持外部协作单撤销并推荐账号

This commit is contained in:
yml
2026-11-20 13:02:40 +08:00
parent 1a49bcd481
commit 55021a683c
8 changed files with 1045 additions and 16 deletions
+235
View File
@@ -9,6 +9,7 @@ from web.backend.models import (
AccountLease,
AuditLog,
DouyuTask,
PlatformCommodityMapping,
PlatformFulfillmentEvent,
PlatformFulfillmentTask,
User,
@@ -16,6 +17,7 @@ from web.backend.models import (
from web.backend.routers import douyu_collab as collab_api
from web.backend.schemas import (
DouyuCollabAccountLookupOut,
DouyuCollabAccountRecommendOut,
DouyuCollabAccountRequest,
DouyuCollabCkExportOut,
DouyuCollabOrderCreate,
@@ -85,6 +87,19 @@ 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()
@@ -172,6 +187,226 @@ class TestDouyuCollabOrders:
assert result["goods"][0]["commodity_id"] == "gift-a"
DouyuCollabAccountLookupOut.model_validate(result)
def test_unlink_account_clears_snapshots_and_keeps_platform_lease(self):
order = collab.create_order(
self.session,
self.admin,
order_no="DY-UNLINK-PLATFORM",
shop="店铺A",
account_username="douyu-account-a",
gift_name="礼包A",
required_points=100,
home_images=["collab/unlink.png"],
client=FakeClient(),
)
collab_lease = self.session.query(AccountLease).one()
collab_lease.status = "released"
collab_lease.release_reason = "test_handoff"
platform_lease = AccountLease(
account_id=self.account.id,
holder_user_id=self.admin.id,
scope="elite",
purpose="platform_fulfillment",
batch_id="pf:unlink-platform",
status="active",
)
self.session.add(platform_lease)
self.session.commit()
collab.unlink_account(
self.session,
self.admin,
order,
reason="账号已用于其它履约订单",
expected_version=order.version,
)
self.session.refresh(order)
self.session.refresh(platform_lease)
assert order.account_id is None
assert order.account_username == ""
assert order.bind_game_role == ""
assert order.fish_fin_balance is None
assert order.points_balance is None
assert order.status == collab.STATUS_INCOMPLETE
assert platform_lease.status == "active"
assert platform_lease.purpose == "platform_fulfillment"
assert (
self.session.query(AuditLog)
.filter_by(action="douyu:collab:account_unlink")
.count()
== 1
)
def test_unlink_account_releases_last_collab_lease(self):
order = collab.create_order(
self.session,
self.admin,
order_no="DY-UNLINK-LEASE",
shop="店铺A",
account_username="douyu-account-a",
gift_name="礼包A",
required_points=100,
home_images=["collab/unlink-lease.png"],
client=FakeClient(),
)
lease = self.session.query(AccountLease).one()
collab.unlink_account(
self.session,
self.admin,
order,
reason="误关联",
expected_version=order.version,
)
self.session.refresh(lease)
assert lease.status == "released"
assert lease.release_reason == "douyu_collab_account_unlinked"
def test_account_replace_requires_super_admin_or_operation(self):
order = collab.create_order(
self.session,
self.admin,
order_no="DY-UNLINK-SUPPORT",
shop="店铺A",
account_username="douyu-account-a",
gift_name="礼包A",
required_points=100,
home_images=["collab/unlink-support.png"],
client=FakeClient(),
)
support = User(
username="douyu-collab-support",
password_hash="hash",
role="support",
custom_permissions=[],
is_active=True,
)
self.session.add(support)
self.session.commit()
with pytest.raises(collab.CollabError) as excinfo:
collab.unlink_account(
self.session,
support,
order,
reason="越权测试",
)
assert excinfo.value.code == "account_replace_forbidden"
def test_operation_can_unlink_account(self):
order = collab.create_order(
self.session,
self.admin,
order_no="DY-UNLINK-OPERATION",
shop="店铺A",
account_username="douyu-account-a",
gift_name="礼包A",
required_points=100,
home_images=["collab/unlink-operation.png"],
client=FakeClient(),
)
operator = User(
username="douyu-collab-operation",
password_hash="hash",
role="operation",
is_active=True,
)
self.session.add(operator)
self.session.commit()
collab.unlink_account(
self.session,
operator,
order,
reason="运营纠正误关联",
expected_version=order.version,
)
self.session.refresh(order)
assert order.account_id is None
assert order.status == collab.STATUS_INCOMPLETE
def test_recommend_accounts_prefers_unbound_and_can_exclude_rebindable(self):
mapping = PlatformCommodityMapping(
target_platform="douyu",
internal_sku_key="推荐礼包",
commodity_id="gift-a",
goods_name="推荐礼包",
required_points=100,
scope="elite",
enabled=True,
)
self.session.add(mapping)
self.session.flush()
order = collab.create_order(
self.session,
self.admin,
order_no="DY-RECOMMEND",
shop="店铺A",
gift_name="推荐礼包",
draft=True,
)
bound = Account(
username="douyu-account-bound",
password="password",
email="bound@example.com",
email_password="mail-password",
cookie="acf_uid=200; acf_nickname=bound",
status="active",
auto_managed=True,
elite_opened=True,
points=1000,
)
empty = Account(
username="douyu-account-empty",
password="password",
email="empty@example.com",
email_password="mail-password",
cookie="acf_uid=300; acf_nickname=empty",
status="active",
auto_managed=True,
elite_opened=True,
points=500,
role_bind_status="unbound",
)
self.session.add_all([bound, empty])
self.session.commit()
clients = {
int(self.account.id): FakeClient(),
int(bound.id): FakeClient(),
int(empty.id): FakeUnboundClient(),
}
result = collab.recommend_accounts(
self.session,
self.admin,
order,
include_rebindable=True,
limit=5,
client_factory=lambda account: clients[int(account.id)],
)
assert order.commodity_id == "gift-a"
assert result["candidates"][0]["account_id"] == empty.id
assert result["candidates"][0]["match_type"] == "empty"
assert any(item["match_type"] == "rebind" for item in result["candidates"])
DouyuCollabAccountRecommendOut.model_validate(result)
unbound_only = collab.recommend_accounts(
self.session,
self.admin,
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
]
def test_create_strict_order_uses_internal_account_and_advances(self):
order = collab.create_order(
self.session,