发布(0.4.6): 修复预售兑换记录判定与详情展示并新增工作台强制发放
This commit is contained in:
@@ -1830,7 +1830,7 @@ class TestDouyuCollabOrders:
|
||||
old_record = exchange_record(occurred_at=datetime.now(UTC) - timedelta(days=1))
|
||||
wrong_commodity = exchange_record(
|
||||
commodity_id="gift-other",
|
||||
name="礼包A",
|
||||
name="礼包B",
|
||||
)
|
||||
|
||||
result = collab.query_activity(
|
||||
@@ -2465,6 +2465,8 @@ class TestDouyuCollabOrders:
|
||||
assert "/api/douyu/external-orders/{order_id}/balances/query" in paths
|
||||
assert "/api/douyu/external-orders/activity/query" in paths
|
||||
assert "/api/douyu/external-orders/{order_id}/activity/query" in paths
|
||||
assert "/api/douyu/external-orders/{order_id}/exchange-records" in paths
|
||||
assert "/api/douyu/external-orders/{order_id}/exchange-records/query" in paths
|
||||
assert "/api/douyu/collab-shops/{shop_id}" in paths
|
||||
assert "put" in paths["/api/douyu/collab-shops/{shop_id}"]
|
||||
assert "delete" in paths["/api/douyu/collab-shops/{shop_id}"]
|
||||
@@ -2640,3 +2642,94 @@ class TestDouyuCollabOrders:
|
||||
reason="角色不一致时也不能强行完成",
|
||||
)
|
||||
assert rollback_exc.value.code == "role_mismatch"
|
||||
|
||||
def test_record_query_matches_rotated_commodity_id_by_name_and_lists_records(self):
|
||||
"""活动商品改过 ID(如 bag346 -> bag413)时,同名且同角色/时间命中仍应判成功并可在详情展示。"""
|
||||
from web.backend.models import DouyuGoodsSnapshot
|
||||
|
||||
now = datetime.now(UTC)
|
||||
self.session.add_all(
|
||||
[
|
||||
DouyuGoodsSnapshot(
|
||||
commodity_id="20260120QYOOB_bag346",
|
||||
name="冥河战士大礼包",
|
||||
score=8000,
|
||||
updated_at=now - timedelta(days=10),
|
||||
),
|
||||
DouyuGoodsSnapshot(
|
||||
commodity_id="20260120QYOOB_bag413",
|
||||
name="冥河战士大礼包",
|
||||
score=8000,
|
||||
updated_at=now - timedelta(hours=1),
|
||||
),
|
||||
]
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
# 解析礼包名应优先选用最新快照 bag413
|
||||
_, resolved_cid, resolved_points = collab._resolve_gift(
|
||||
self.session,
|
||||
gift_name="冥河战士大礼包",
|
||||
commodity_id="",
|
||||
required_points=None,
|
||||
)
|
||||
assert resolved_cid == "20260120QYOOB_bag413"
|
||||
assert resolved_points == 8000
|
||||
|
||||
# 模拟历史已保存旧 ID bag346 的订单,遇上官方返回的 bag413 兑换记录
|
||||
order = collab.create_order(
|
||||
self.session,
|
||||
self.admin,
|
||||
order_no="2631901603397968",
|
||||
shop="店铺A",
|
||||
account_username="douyu-account-a",
|
||||
gift_name="冥河战士大礼包",
|
||||
commodity_id="20260120QYOOB_bag346",
|
||||
required_points=8000,
|
||||
home_images=["collab/home-rotated-cid.png"],
|
||||
client=FakeClient(),
|
||||
)
|
||||
for status in (collab.STATUS_RECHARGED, collab.STATUS_SCRIPTED):
|
||||
collab.set_status(
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
status=status,
|
||||
reason="准备测试改 ID 判定",
|
||||
)
|
||||
|
||||
wrong_role_record = {
|
||||
**exchange_record(name="冥河战士大礼包", commodity_id="20260120QYOOB_bag413"),
|
||||
"showName": "其他角色(安卓(android)-微信)",
|
||||
}
|
||||
matched_record = {
|
||||
**exchange_record(name="冥河战士大礼包", commodity_id="20260120QYOOB_bag413"),
|
||||
"showName": "角色A(苹果(iOS)-微信)",
|
||||
"score": "8000",
|
||||
}
|
||||
|
||||
detail_records = collab.list_order_exchange_records(
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
refresh=True,
|
||||
client=FakeClient(records=[wrong_role_record, matched_record]),
|
||||
)
|
||||
assert detail_records["total"] == 2
|
||||
assert detail_records["matched_count"] == 1
|
||||
assert [item["match_reason"] for item in detail_records["records"]] == [
|
||||
"角色不一致",
|
||||
"命中本单",
|
||||
]
|
||||
# 详情只读查询不应改动订单状态
|
||||
assert order.status == collab.STATUS_SCRIPTED
|
||||
|
||||
# 正式自动判定应命中 rotated_cid 记录并转为 done
|
||||
result = collab.query_activity(
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
client=FakeClient(records=[matched_record]),
|
||||
)
|
||||
assert result["verdict"] == collab.VERDICT_SUCCESS
|
||||
assert result["order"].status == collab.STATUS_DONE
|
||||
|
||||
@@ -170,3 +170,74 @@ class TestDouyuWritePathContracts:
|
||||
self.session.refresh(exchange_task)
|
||||
assert exchange_task.status == "failed"
|
||||
exchange_client.create_exchange_order.assert_not_called()
|
||||
|
||||
def test_runner_force_goods_allows_matching_role_and_blocks_drift(self):
|
||||
self.account.cookie = "acf_uid=100"
|
||||
self.account.ck_check_status = "valid"
|
||||
self.account.game_name = "客户角色"
|
||||
self.account.role_bind_status = "bound"
|
||||
self.session.add(
|
||||
AccountLease(
|
||||
account_id=self.account.id,
|
||||
holder_user_id=self.user.id,
|
||||
scope="elite",
|
||||
purpose="platform_fulfillment",
|
||||
batch_id="pf:9184",
|
||||
status="active",
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
lock_task = self._task(
|
||||
"lock_goods",
|
||||
{
|
||||
"commodity_id": "bag10",
|
||||
"num": 1,
|
||||
"force_occupied": True,
|
||||
"force_expected_role": "客户角色",
|
||||
},
|
||||
)
|
||||
runner = self._runner("lock_goods")
|
||||
client = Mock()
|
||||
client.create_exchange_order.return_value = {
|
||||
"order_id": "7788",
|
||||
"expire_seconds": "300",
|
||||
}
|
||||
runner._client = Mock(return_value=client)
|
||||
runner._execute_lock_goods(
|
||||
self.session,
|
||||
lock_task,
|
||||
self.account,
|
||||
"acf_uid=100",
|
||||
{"manual_id": "G4KA4Qnz4LDp7", "rid": "9263298"},
|
||||
)
|
||||
self.session.refresh(lock_task)
|
||||
assert lock_task.status == "success"
|
||||
client.create_exchange_order.assert_called_once()
|
||||
|
||||
# 若排队期间角色被改成其他角色,执行器必须拒绝
|
||||
self.account.game_name = "漂移角色"
|
||||
self.session.commit()
|
||||
drift_task = self._task(
|
||||
"lock_goods",
|
||||
{
|
||||
"commodity_id": "bag10",
|
||||
"num": 1,
|
||||
"force_occupied": True,
|
||||
"force_expected_role": "客户角色",
|
||||
},
|
||||
)
|
||||
drift_runner = self._runner("lock_goods")
|
||||
drift_client = Mock()
|
||||
drift_runner._client = Mock(return_value=drift_client)
|
||||
drift_runner._execute_lock_goods(
|
||||
self.session,
|
||||
drift_task,
|
||||
self.account,
|
||||
"acf_uid=100",
|
||||
{"manual_id": "G4KA4Qnz4LDp7", "rid": "9263298"},
|
||||
)
|
||||
self.session.refresh(drift_task)
|
||||
assert drift_task.status == "failed"
|
||||
assert "不一致" in drift_task.message
|
||||
drift_client.create_exchange_order.assert_not_called()
|
||||
|
||||
@@ -6403,3 +6403,132 @@ class TestBindingAndCompletion(_Base):
|
||||
enqueue_execution=False,
|
||||
)
|
||||
assert collab_query_count == 1
|
||||
|
||||
def test_workbench_force_exchange_requires_permission_and_matching_role(self):
|
||||
"""超管/运营可在核对账号与非空角色一致后强制锁单/兑换,并只在履约时间线留痕。"""
|
||||
from fastapi import HTTPException
|
||||
|
||||
from web.backend.models import DouyuGoodsSnapshot
|
||||
from web.backend.routers.douyu import _prepare_task_batch
|
||||
from web.backend.schemas import DouyuTaskBatchRequest
|
||||
|
||||
task = self._allocated()
|
||||
task.binding_status = "bound"
|
||||
task.execution_status = "bound"
|
||||
task.customer_role_name = "客户目标角色"
|
||||
account = self.session.get(Account, task.account_id)
|
||||
account.game_name = ""
|
||||
account.role_bind_status = "bound"
|
||||
self.session.add(
|
||||
DouyuGoodsSnapshot(
|
||||
commodity_id="bag-force-1",
|
||||
name="套装-花椰椰",
|
||||
score=988,
|
||||
raw={"name": "套装-花椰椰"},
|
||||
)
|
||||
)
|
||||
support_user = User(
|
||||
username="pf-support",
|
||||
password_hash="h",
|
||||
role="support",
|
||||
is_active=True,
|
||||
)
|
||||
op_user = User(
|
||||
username="pf-op",
|
||||
password_hash="h",
|
||||
role="operation",
|
||||
is_active=True,
|
||||
)
|
||||
self.session.add_all([support_user, op_user])
|
||||
self.session.commit()
|
||||
|
||||
req_exchange = DouyuTaskBatchRequest(
|
||||
account_ids=[account.id],
|
||||
task_type="exchange_goods",
|
||||
handbook_scope="elite",
|
||||
payload={"commodity_id": "bag-force-1"},
|
||||
)
|
||||
req_force = DouyuTaskBatchRequest(
|
||||
account_ids=[account.id],
|
||||
task_type="exchange_goods",
|
||||
handbook_scope="elite",
|
||||
payload={"commodity_id": "bag-force-1"},
|
||||
force_occupied=True,
|
||||
)
|
||||
|
||||
# 客服即使有自己名下账号并带 douyu:task,也无权在占用期间强制发放
|
||||
account.assigned_to = support_user.id
|
||||
self.session.commit()
|
||||
with pytest.raises(HTTPException) as exc_sup_409:
|
||||
_prepare_task_batch(req_exchange, self.session, support_user)
|
||||
assert exc_sup_409.value.status_code == 409
|
||||
|
||||
with pytest.raises(HTTPException) as exc_sup_403:
|
||||
_prepare_task_batch(req_force, self.session, support_user)
|
||||
assert exc_sup_403.value.status_code == 403
|
||||
account.assigned_to = None
|
||||
self.session.commit()
|
||||
|
||||
# 运营但账号尚未获取到游戏角色名 -> 严格拒绝并提示先一键获取游戏名
|
||||
with pytest.raises(HTTPException) as exc_empty_role:
|
||||
_prepare_task_batch(req_exchange, self.session, op_user)
|
||||
assert exc_empty_role.value.status_code == 409
|
||||
assert "一键获取游戏名" in exc_empty_role.value.detail
|
||||
|
||||
with pytest.raises(HTTPException) as exc_empty_force:
|
||||
_prepare_task_batch(req_force, self.session, op_user)
|
||||
assert exc_empty_force.value.status_code == 409
|
||||
assert "一键获取游戏名" in exc_empty_force.value.detail
|
||||
|
||||
# 角色与履约目标角色不一致 -> 拒绝
|
||||
account.game_name = "别的错误角色"
|
||||
self.session.commit()
|
||||
with pytest.raises(HTTPException) as exc_mismatch:
|
||||
_prepare_task_batch(req_force, self.session, op_user)
|
||||
assert exc_mismatch.value.status_code == 409
|
||||
assert "不一致" in exc_mismatch.value.detail
|
||||
|
||||
# 禁止强制绑码/换绑
|
||||
req_bind_force = DouyuTaskBatchRequest(
|
||||
account_ids=[account.id],
|
||||
task_type="get_bind_qr",
|
||||
handbook_scope="elite",
|
||||
force_occupied=True,
|
||||
)
|
||||
with pytest.raises(HTTPException) as exc_bind:
|
||||
_prepare_task_batch(req_bind_force, self.session, op_user)
|
||||
assert exc_bind.value.status_code == 400
|
||||
|
||||
# 角色一致后:首次返回 requires_force_confirmation,二次确认提交成功且保持履约状态
|
||||
account.game_name = "客户目标角色"
|
||||
self.session.commit()
|
||||
|
||||
preview = _prepare_task_batch(req_exchange, self.session, op_user)
|
||||
assert preview["requires_force_confirmation"] is True
|
||||
assert preview["occupied_accounts"][0]["account_role"] == "客户目标角色"
|
||||
assert preview["occupied_accounts"][0]["customer_role_name"] == "客户目标角色"
|
||||
|
||||
created = _prepare_task_batch(req_force, self.session, op_user)
|
||||
assert created["success"] is True
|
||||
assert created["count"] == 1
|
||||
|
||||
douyu_task = (
|
||||
self.session.query(DouyuTask)
|
||||
.filter(DouyuTask.batch_id == created["batch_id"])
|
||||
.one()
|
||||
)
|
||||
assert douyu_task.result["payload"]["force_occupied"] is True
|
||||
assert douyu_task.result["payload"]["force_expected_role"] == "客户目标角色"
|
||||
|
||||
self.session.refresh(task)
|
||||
assert task.execution_status == "bound"
|
||||
assert task.binding_status == "bound"
|
||||
|
||||
evt = (
|
||||
self.session.query(PlatformFulfillmentEvent)
|
||||
.filter_by(task_id=task.id, event_type="workbench_force_exchange")
|
||||
.one()
|
||||
)
|
||||
assert evt.detail_json["operatorUsername"] == "pf-op"
|
||||
assert evt.detail_json["accountRole"] == "客户目标角色"
|
||||
assert evt.detail_json["goodsName"] == "套装-花椰椰"
|
||||
|
||||
Reference in New Issue
Block a user