fix(fulfillment): 修复历史账号绑定覆盖当前待确认角色
This commit is contained in:
+119
-1
@@ -31,6 +31,7 @@ from web.backend.models import (
|
||||
AuditLog,
|
||||
AutomationJob,
|
||||
DouyuExternalOrder,
|
||||
DouyuTask,
|
||||
HuyaExternalOrder,
|
||||
PlatformCommodityMapping,
|
||||
PlatformFulfillmentEvent,
|
||||
@@ -43,7 +44,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261123_0125"
|
||||
HEAD_REVISION = "20261123_0126"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
@@ -464,6 +465,123 @@ class TestMigrationSmoke:
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(text(f"DROP DATABASE IF EXISTS `{mig_db}`;"))
|
||||
|
||||
def test_0126_repairs_false_confirmation_from_historical_account_binding(self):
|
||||
creds = get_mysql_credentials()
|
||||
mig_db = f"douyu_login_mig_{secrets.token_hex(4)}"
|
||||
mig_url = (
|
||||
f"mysql+pymysql://{quote_plus(creds['user'])}:"
|
||||
f"{quote_plus(creds['password'])}@{creds['host']}:{creds['port']}/"
|
||||
f"{mig_db}?charset=utf8mb4"
|
||||
)
|
||||
admin_engine = get_test_engine()
|
||||
mig_engine = create_engine(mig_url)
|
||||
original_url = database_module.DATABASE_URL
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(
|
||||
text(
|
||||
f"CREATE DATABASE `{mig_db}` "
|
||||
"CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;"
|
||||
)
|
||||
)
|
||||
try:
|
||||
database_module.DATABASE_URL = mig_url
|
||||
cfg = _migration_config()
|
||||
cfg.set_main_option("sqlalchemy.url", mig_url)
|
||||
command.upgrade(cfg, "20261123_0125")
|
||||
with Session(mig_engine) as session:
|
||||
user = User(
|
||||
username="false-binding-user",
|
||||
password_hash="hash",
|
||||
role="super_admin",
|
||||
is_active=True,
|
||||
)
|
||||
account = Account(
|
||||
username="false-binding-account",
|
||||
password="password",
|
||||
email="false-binding@example.com",
|
||||
email_password="mail-password",
|
||||
cookie="acf_uid=1",
|
||||
role_bind_status="bound",
|
||||
game_name="历史角色",
|
||||
game_channel="微信 · 苹果(iOS)",
|
||||
status="active",
|
||||
)
|
||||
session.add_all([user, account])
|
||||
session.flush()
|
||||
lease = AccountLease(
|
||||
account_id=account.id,
|
||||
holder_user_id=user.id,
|
||||
status="active",
|
||||
purpose="platform_fulfillment",
|
||||
)
|
||||
bind_task = DouyuTask(
|
||||
batch_id="false-binding-qr",
|
||||
account_id=account.id,
|
||||
account_assigned_to=user.id,
|
||||
task_type="get_bind_qr",
|
||||
handbook_scope="elite",
|
||||
status="success",
|
||||
result={
|
||||
"role_name": "本次待确认角色",
|
||||
"game_channel": "手Q / 苹果(iOS)",
|
||||
"bind_ready_for_confirm": True,
|
||||
"bind_confirmed": False,
|
||||
"bind_phase": "role_ready",
|
||||
},
|
||||
created_by=user.id,
|
||||
)
|
||||
session.add_all([lease, bind_task])
|
||||
session.flush()
|
||||
task = PlatformFulfillmentTask(
|
||||
source_system="order_site",
|
||||
source_order_id="FALSE-BIND-ORDER",
|
||||
source_order_item_id="FALSE-BIND-ITEM",
|
||||
platform_order_id="FALSE-BIND-PLATFORM",
|
||||
internal_sku_key="误绑定修复测试",
|
||||
sku_name="误绑定修复测试",
|
||||
scope="elite",
|
||||
account_id=account.id,
|
||||
lease_id=lease.id,
|
||||
bind_task_id=bind_task.id,
|
||||
binding_status="bound",
|
||||
execution_status="bound",
|
||||
)
|
||||
session.add(task)
|
||||
session.flush()
|
||||
session.add(
|
||||
PlatformFulfillmentEvent(
|
||||
task_id=task.id,
|
||||
event_type="binding_synced_from_account",
|
||||
actor_type="system",
|
||||
actor_id="",
|
||||
detail_json={"backfilled": True},
|
||||
)
|
||||
)
|
||||
session.commit()
|
||||
task_id = int(task.id)
|
||||
|
||||
command.upgrade(cfg, "head")
|
||||
|
||||
with Session(mig_engine) as session:
|
||||
task = session.get(PlatformFulfillmentTask, task_id)
|
||||
repair = (
|
||||
session.query(PlatformFulfillmentEvent)
|
||||
.filter_by(
|
||||
task_id=task_id,
|
||||
event_type="binding_false_confirmation_reverted",
|
||||
)
|
||||
.one()
|
||||
)
|
||||
assert task.binding_status == "role_ready"
|
||||
assert task.execution_status == "role_ready"
|
||||
assert repair.detail_json["boundRoleName"] == "历史角色"
|
||||
assert repair.detail_json["pendingRoleName"] == "本次待确认角色"
|
||||
finally:
|
||||
database_module.DATABASE_URL = original_url
|
||||
mig_engine.dispose()
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(text(f"DROP DATABASE IF EXISTS `{mig_db}`;"))
|
||||
|
||||
def test_0125_preserves_shop_names_without_automatic_aliases(self):
|
||||
creds = get_mysql_credentials()
|
||||
mig_db = f"douyu_login_mig_{secrets.token_hex(4)}"
|
||||
|
||||
@@ -1813,13 +1813,13 @@ class TestBindingAndCompletion(_Base):
|
||||
handbook_scope="elite",
|
||||
status="success",
|
||||
result={
|
||||
"role_name": "旧角色",
|
||||
"current_role_name": "旧角色",
|
||||
"baseline_role_name": "旧角色",
|
||||
"role_name": "新角色",
|
||||
"current_role_name": "新角色",
|
||||
"baseline_role_name": "历史角色",
|
||||
"baseline_is_bound_act": True,
|
||||
"before_bind_info": {
|
||||
"act_alias": "20260120QYOOB",
|
||||
"role_name": "旧角色",
|
||||
"role_name": "历史角色",
|
||||
"is_bound_act": True,
|
||||
"need_bind_act": True,
|
||||
},
|
||||
@@ -1838,7 +1838,7 @@ class TestBindingAndCompletion(_Base):
|
||||
task.execution_status = "role_ready"
|
||||
account = self.session.get(Account, task.account_id)
|
||||
account.role_bind_status = "bound"
|
||||
account.game_name = "新角色"
|
||||
account.game_name = "历史角色"
|
||||
account.game_channel = "微信 · 苹果(iOS)"
|
||||
confirm_task = DouyuTask(
|
||||
batch_id="confirm-from-elite-workbench",
|
||||
@@ -1860,8 +1860,11 @@ class TestBindingAndCompletion(_Base):
|
||||
|
||||
pre_sync_detail = service.get_task_detail(self.session, task.id)
|
||||
assert pre_sync_detail["task"]["bindTask"]["roleName"] == "新角色"
|
||||
assert pre_sync_detail["task"]["bindTask"]["bindReadyForConfirm"] is False
|
||||
assert pre_sync_detail["task"]["bindTask"]["bindReadyForConfirm"] is True
|
||||
|
||||
account.game_name = "新角色"
|
||||
account.role_bind_status = "bound"
|
||||
self.session.commit()
|
||||
synced = service.sync_task_from_binding_runner(
|
||||
self.session,
|
||||
douyu_task_id=confirm_task.id,
|
||||
@@ -1883,8 +1886,74 @@ class TestBindingAndCompletion(_Base):
|
||||
assert detail["task"]["bindTask"]["roleName"] == "新角色"
|
||||
assert detail["task"]["bindTask"]["bindReadyForConfirm"] is False
|
||||
assert detail["task"]["bindTask"]["bindConfirmed"] is True
|
||||
assert task.bind_task_id == confirm_task.id
|
||||
assert event.detail_json["roleName"] == "新角色"
|
||||
|
||||
def test_bind_qr_success_does_not_promote_existing_account_binding(self):
|
||||
task = self._allocated()
|
||||
account = self.session.get(Account, task.account_id)
|
||||
account.role_bind_status = "bound"
|
||||
account.game_name = "历史角色"
|
||||
account.game_channel = "微信 · 苹果(iOS)"
|
||||
bind_task = DouyuTask(
|
||||
batch_id="bind-pending-current-order",
|
||||
account_id=task.account_id,
|
||||
account_assigned_to=self.admin_id,
|
||||
task_type="get_bind_qr",
|
||||
handbook_scope="elite",
|
||||
status="success",
|
||||
result={
|
||||
"act_alias": "cjm",
|
||||
"role_name": "本次待确认角色",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "苹果(iOS)",
|
||||
"baseline_role_name": "历史角色",
|
||||
"baseline_is_bound_act": True,
|
||||
"before_bind_info": {
|
||||
"act_alias": "20260120QYOOB",
|
||||
"role_name": "历史角色",
|
||||
"is_bound_act": True,
|
||||
"need_bind_act": True,
|
||||
},
|
||||
"need_bind_act": False,
|
||||
"is_bound_act": False,
|
||||
"bind_ready_for_confirm": True,
|
||||
"bind_confirmed": False,
|
||||
"bind_phase": "role_ready",
|
||||
},
|
||||
created_by=self.admin_id,
|
||||
)
|
||||
self.session.add(bind_task)
|
||||
self.session.flush()
|
||||
task.bind_task_id = bind_task.id
|
||||
task.binding_status = "role_ready"
|
||||
task.execution_status = "role_ready"
|
||||
self.session.commit()
|
||||
|
||||
synced = service.sync_task_from_binding_runner(
|
||||
self.session,
|
||||
douyu_task_id=bind_task.id,
|
||||
)
|
||||
self.session.refresh(task)
|
||||
detail = service.get_task_detail(self.session, task.id)
|
||||
|
||||
assert synced == 1
|
||||
assert task.bind_task_id == bind_task.id
|
||||
assert task.binding_status == "role_ready"
|
||||
assert task.execution_status == "role_ready"
|
||||
assert detail["task"]["bindTask"]["roleName"] == "本次待确认角色"
|
||||
assert detail["task"]["bindTask"]["bindReadyForConfirm"] is True
|
||||
assert detail["task"]["bindTask"]["bindConfirmed"] is False
|
||||
assert (
|
||||
self.session.query(PlatformFulfillmentEvent)
|
||||
.filter_by(
|
||||
task_id=task.id,
|
||||
event_type="binding_synced_from_account",
|
||||
)
|
||||
.count()
|
||||
== 0
|
||||
)
|
||||
|
||||
def test_customer_start_mapping_missing_is_idempotent(self, monkeypatch):
|
||||
task = self.task(mapped=False)
|
||||
task.issue_code = "mapping_missing"
|
||||
|
||||
Reference in New Issue
Block a user