修复(履约与分配): 封堵工作台换绑、预售共号与分配中心越权占用

This commit is contained in:
yml
2026-11-27 13:42:28 +08:00
parent 9100991cb1
commit 5006367217
13 changed files with 1031 additions and 34 deletions
+103
View File
@@ -21,8 +21,10 @@ from tests.support.db import get_test_engine, make_test_session
from web.backend.models import (
Account,
AccountCommodityState,
AccountEvent,
AccountLease,
DouyuExternalOrder,
PlatformFulfillmentTask,
User,
)
from web.backend.routers.accounts import (
@@ -476,6 +478,107 @@ class TestAssignmentPools(_Base):
assert exc_info.value.status_code == 400
assert "已停用" in exc_info.value.detail
def test_assign_and_dispatch_block_fulfillment_and_collab_accounts(self):
free = self.make_account("assign-guard-free")
leased = self.make_account("assign-guard-leased")
collab = self.make_account("assign-guard-collab")
pf_task_acc = self.make_account("assign-guard-pf-task")
self.session.add_all(
[
AccountLease(
account_id=leased.id,
holder_user_id=self.admin.id,
purpose="platform_fulfillment",
status="active",
),
DouyuExternalOrder(
order_no="ASSIGN-GUARD-COLLAB-1",
account_id=collab.id,
account_username=collab.username,
status="to_recharge",
),
PlatformFulfillmentTask(
source_order_id="PF-ORDER-ASSIGN-GUARD-1",
source_order_item_id="PF-ITEM-ASSIGN-GUARD-1",
platform_order_id="PF-NO-ASSIGN-GUARD-1",
shop_id="shop1",
account_id=pf_task_acc.id,
execution_status="allocated",
binding_status="not_started",
),
]
)
self.session.commit()
for blocked in (leased, collab, pf_task_acc):
with pytest.raises(HTTPException) as single_exc:
assign_account(
blocked.id,
AccountAssign(assigned_to=self.support.id),
db=self.session,
current=self.admin,
)
assert single_exc.value.status_code == 409
with pytest.raises(HTTPException) as batch_exc:
batch_assign_accounts(
BatchAssign(account_ids=[blocked.id], assigned_to=self.support.id),
db=self.session,
current=self.admin,
)
assert batch_exc.value.status_code == 409
# 即使前端/调用方传 occupancy="" 或 "no_lease",dispatch 也必须兜底跳过被占用账号
dispatched = dispatch_assignments(
AssignmentDispatch(
assigned_to=self.support.id,
quantity=10,
occupancy="",
),
db=self.session,
current=self.admin,
)
assert dispatched["account_ids"] == [free.id]
self.session.refresh(leased)
self.session.refresh(collab)
self.session.refresh(pf_task_acc)
assert leased.assigned_to is None
assert collab.assigned_to is None
assert pf_task_acc.assigned_to is None
# 退回/取消分配仍允许在账号被占用后执行
free.assigned_to = self.support.id
self.session.commit()
unassigned = batch_assign_accounts(
BatchAssign(account_ids=[free.id], assigned_to=None),
db=self.session,
current=self.admin,
)
assert unassigned["count"] == 1
def test_bind_change_available_respects_recent_7_day_bind_ok_lock(self):
locked = self.make_account(
"pool-recent-bind-ok",
role_bind_status="bound",
role_change_available_at=self.now - timedelta(hours=1),
ck_expected_expires_at=self.now + timedelta(days=3),
)
self.session.add(
AccountEvent(
account_id=locked.id,
event_type="bind_ok",
category="business",
title="最近绑定",
subject_type="elite_role",
subject_key="近期角色",
occurred_at=self.now - timedelta(days=2),
)
)
self.session.commit()
assert self.names(bind_change_status="available") == []
assert self.names(bind_change_status="cooldown") == [locked.username]
class TestAllMatchingAssign(_Base):
def test_assigns_every_matching_unassigned(self):
+166
View File
@@ -2474,3 +2474,169 @@ class TestDouyuCollabOrders:
assert "/api/douyu/collab-gifts/{gift_id}" in paths
assert "put" in paths["/api/douyu/collab-gifts/{gift_id}"]
assert "delete" in paths["/api/douyu/collab-gifts/{gift_id}"]
def test_same_account_collab_orders_reject_conflicting_roles(self):
first = collab.create_order(
self.session,
self.admin,
order_no="DY-ROLE-CONFLICT-1",
shop="店铺A",
account_username="douyu-account-a",
gift_name="礼包A",
required_points=100,
home_images=["collab/home-conflict-1.png"],
client=FakeClient(),
)
assert first.bind_game_role == "角色A"
class RoleBClient(FakeClient):
def bind_info(self, **_):
return {
"is_bound_account": True,
"is_bound_role": True,
"is_bound_act": True,
"role_name": "角色B",
"area_name": "微信",
"plat_name": "苹果(iOS)",
"nickname": "角色B昵称",
}
with pytest.raises(collab.CollabError) as create_exc:
collab.create_order(
self.session,
self.admin,
order_no="DY-ROLE-CONFLICT-2",
shop="店铺A",
account_username="douyu-account-a",
gift_name="礼包A",
required_points=100,
home_images=["collab/home-conflict-2.png"],
client=RoleBClient(),
)
assert create_exc.value.code == "collab_role_conflict"
assert create_exc.value.status_code == 409
draft = collab.create_order(
self.session,
self.admin,
order_no="DY-ROLE-CONFLICT-DRAFT",
shop="店铺A",
gift_name="礼包A",
required_points=100,
draft=True,
)
with pytest.raises(collab.CollabError) as switch_exc:
collab.set_account(
self.session,
self.admin,
draft,
"douyu-account-a",
client=RoleBClient(),
)
assert switch_exc.value.code == "collab_role_conflict"
other_account = Account(
username="douyu-account-b",
password="password",
email="b@example.com",
email_password="mail-password",
cookie="acf_uid=101; acf_nickname=testb",
)
self.session.add(other_account)
self.session.commit()
voided = collab.create_order(
self.session,
self.admin,
order_no="DY-ROLE-CONFLICT-VOID",
shop="店铺A",
account_username="douyu-account-b",
gift_name="礼包A",
required_points=100,
home_images=["collab/home-conflict-void.png"],
client=RoleBClient(),
)
collab.set_status(
self.session,
self.admin,
voided,
status=collab.STATUS_VOID,
reason="作废再测试串号恢复",
)
voided.account_id = self.account.id
voided.account_username = self.account.username
self.session.commit()
with pytest.raises(collab.CollabError) as restore_exc:
collab.set_status(
self.session,
self.admin,
voided,
status=collab.STATUS_INCOMPLETE,
reason="尝试跨角色恢复",
)
assert restore_exc.value.code == "collab_role_conflict"
def test_query_activity_and_set_status_done_reject_role_mismatch(self):
order = collab.create_order(
self.session,
self.admin,
order_no="DY-ROLE-MISMATCH-DONE",
shop="店铺A",
account_username="douyu-account-a",
gift_name="礼包A",
required_points=100,
home_images=["collab/home-role-mismatch.png"],
client=FakeClient(),
)
for status in (collab.STATUS_RECHARGED, collab.STATUS_SCRIPTED):
collab.set_status(
self.session,
self.admin,
order,
status=status,
reason="准备测试角色校验",
)
self.account.game_name = "被别人绑走的角色"
self.session.commit()
with pytest.raises(collab.CollabError) as manual_exc:
collab.set_status(
self.session,
self.admin,
order,
status=collab.STATUS_DONE,
reason="误标成功",
)
assert manual_exc.value.code == "role_mismatch"
assert manual_exc.value.status_code == 409
result = collab.query_activity(
self.session,
self.admin,
order,
client=FakeClient(records=[exchange_record()]),
)
assert result["verdict"] == collab.VERDICT_FAILED
assert "禁止自动标记成功" in result["message"]
assert result["order"].status == collab.STATUS_ROLE_ERROR
assert result["order"].done_at is None
lease = (
self.session.query(AccountLease)
.filter(
AccountLease.account_id == self.account.id,
AccountLease.status == "active",
)
.one()
)
assert lease.purpose == collab.LEASE_PURPOSE_DOUYU_COLLAB
with pytest.raises(collab.CollabError) as rollback_exc:
collab.set_status(
self.session,
self.admin,
order,
status=collab.STATUS_DONE,
rollback=True,
reason="角色不一致时也不能强行完成",
)
assert rollback_exc.value.code == "role_mismatch"
+66 -1
View File
@@ -3,7 +3,13 @@
from unittest.mock import Mock
from tests.support.db import get_test_engine, make_test_session
from web.backend.models import Account, DouyuGoodsSnapshot, DouyuTask, User
from web.backend.models import (
Account,
AccountLease,
DouyuGoodsSnapshot,
DouyuTask,
User,
)
from web.backend.services.douyu_runner import DouyuBatchRunner
@@ -105,3 +111,62 @@ class TestDouyuWritePathContracts:
assert task.result["exchange_id"] == "11098933"
client.pay_exchange_order.assert_called_once()
client.create_exchange_order.assert_not_called()
def test_runner_blocks_unlinked_bind_and_exchange_when_account_leased(self):
self.session.add(
AccountLease(
account_id=self.account.id,
holder_user_id=self.user.id,
scope="elite",
purpose="platform_fulfillment",
batch_id="pf:999",
status="active",
)
)
self.session.commit()
qr_task = self._task("get_bind_qr", {})
qr_runner = self._runner("get_bind_qr")
qr_client = Mock()
qr_runner._client = Mock(return_value=qr_client)
qr_runner._execute_get_bind_qr(
self.session,
qr_task,
self.account,
"acf_uid=100",
{"bind_act_alias": "20260120QYOOB"},
)
self.session.refresh(qr_task)
assert qr_task.status == "failed"
assert "平台履约" in qr_task.message
qr_client.bind_info.assert_not_called()
confirm_task = self._task("confirm_bind", {})
confirm_runner = self._runner("confirm_bind")
confirm_client = Mock()
confirm_runner._client = Mock(return_value=confirm_client)
confirm_runner._execute_confirm_bind(
self.session,
confirm_task,
self.account,
"acf_uid=100",
{"confirm_act_alias": "20260120QYOOB"},
)
self.session.refresh(confirm_task)
assert confirm_task.status == "failed"
confirm_client.confirm_bind.assert_not_called()
exchange_task = self._task("exchange_goods", {"commodity_id": "bag10"})
exchange_runner = self._runner("exchange_goods")
exchange_client = Mock()
exchange_runner._client = Mock(return_value=exchange_client)
exchange_runner._execute_exchange_goods(
self.session,
exchange_task,
self.account,
"acf_uid=100",
{"manual_id": "G4KA4Qnz4LDp7", "rid": "9263298"},
)
self.session.refresh(exchange_task)
assert exchange_task.status == "failed"
exchange_client.create_exchange_order.assert_not_called()
+97
View File
@@ -6306,3 +6306,100 @@ class TestBindingAndCompletion(_Base):
.count()
== 1
)
def test_workbench_blocks_rebind_and_goods_on_bound_or_collab_accounts(self):
"""已绑定履约单与预售台账占用期间,禁止工作台无归属绑码/换绑,且履约期禁兑换。"""
from web.backend.models import DouyuExternalOrder
from web.backend.services.douyu_service import (
DouyuLeaseConflictError,
create_douyu_planned_tasks,
)
task = self._allocated()
task.binding_status = "bound"
task.execution_status = "bound"
self.session.commit()
for blocked_type in (
"get_bind_qr",
"confirm_bind",
"lock_goods",
"pay_locked_order",
"exchange_goods",
):
with pytest.raises(DouyuLeaseConflictError):
create_douyu_planned_tasks(
self.session,
[task.account_id],
blocked_type,
"elite",
self.admin_id,
enqueue_execution=False,
)
# 只读查询仍允许从工作台发起
_, query_count = create_douyu_planned_tasks(
self.session,
[task.account_id],
"query_points",
"elite",
self.admin_id,
enqueue_execution=False,
)
assert query_count == 1
collab_account = Account(
username="collab-occupied-acc",
password="pw",
email="collab@example.com",
email_password="epw",
cookie="acf_uid=9090; acf_nickname=collab",
status="active",
)
self.session.add(collab_account)
self.session.flush()
collab_lease = AccountLease(
account_id=collab_account.id,
holder_user_id=self.admin_id,
scope="elite",
purpose="douyu_collab",
batch_id=f"collab:{collab_account.id}",
status="active",
)
self.session.add(collab_lease)
self.session.add(
DouyuExternalOrder(
order_no="DY-WORKBENCH-BLOCK-1",
shop="店铺A",
owner_id=self.admin_id,
account_id=collab_account.id,
account_username=collab_account.username,
gift_name="礼包A",
bind_game_role="原绑定角色",
status="to_recharge",
created_by=self.admin_id,
)
)
self.session.commit()
for blocked_bind_type in ("get_bind_qr", "confirm_bind"):
with pytest.raises(DouyuLeaseConflictError):
create_douyu_planned_tasks(
self.session,
[collab_account.id],
blocked_bind_type,
"elite",
self.admin_id,
enqueue_execution=False,
)
# 预售台账账号仍可由人工从工作台查积分或做兑换,不被绑定拦截误伤
_, collab_query_count = create_douyu_planned_tasks(
self.session,
[collab_account.id],
"query_points",
"elite",
self.admin_id,
enqueue_execution=False,
)
assert collab_query_count == 1