fix(fulfillment): 修复历史账号绑定覆盖当前待确认角色

This commit is contained in:
yml
2026-11-23 19:42:38 +08:00
parent 17ec07b63a
commit 0a32f24fa8
7 changed files with 347 additions and 9 deletions
+119 -1
View File
@@ -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)}"