feat(weibo): 支持管理员强制转入工作台且不影响原使用人
This commit is contained in:
+115
-1
@@ -17,16 +17,18 @@ from alembic import command
|
||||
from alembic.config import Config
|
||||
from alembic.script import ScriptDirectory
|
||||
from sqlalchemy import create_engine, inspect, text
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
import web.backend.database as database_module
|
||||
from tests.support.db import get_mysql_credentials, get_test_engine
|
||||
from web.backend.database import Base
|
||||
from web.backend.models import User, WeiboAccount
|
||||
|
||||
TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261111_0076"
|
||||
HEAD_REVISION = "20261112_0077"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
@@ -154,6 +156,118 @@ class TestMigrationSmoke:
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(text(f"DROP DATABASE IF EXISTS `{mig_db}`;"))
|
||||
|
||||
def test_0077_converts_super_admin_membership_to_overlay(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()
|
||||
with admin_engine.begin() as conn:
|
||||
conn.execute(
|
||||
text(
|
||||
f"CREATE DATABASE `{mig_db}` "
|
||||
"CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;"
|
||||
)
|
||||
)
|
||||
|
||||
original_url = database_module.DATABASE_URL
|
||||
mig_engine = create_engine(mig_url)
|
||||
try:
|
||||
database_module.DATABASE_URL = mig_url
|
||||
cfg = _migration_config()
|
||||
cfg.set_main_option("sqlalchemy.url", mig_url)
|
||||
command.upgrade(cfg, "20261111_0076")
|
||||
|
||||
with Session(mig_engine) as session:
|
||||
admin = User(
|
||||
username="migration-admin",
|
||||
password_hash="hash",
|
||||
role="super_admin",
|
||||
is_active=True,
|
||||
)
|
||||
support = User(
|
||||
username="migration-support",
|
||||
password_hash="hash",
|
||||
role="support",
|
||||
is_active=True,
|
||||
)
|
||||
admin_account = WeiboAccount(
|
||||
uid="4014000771",
|
||||
nickname="管理员旧映射",
|
||||
cookie="SCF=a; SUB=admin",
|
||||
)
|
||||
support_account = WeiboAccount(
|
||||
uid="4014000772",
|
||||
nickname="客服旧映射",
|
||||
cookie="SCF=b; SUB=support",
|
||||
)
|
||||
session.add_all([admin, support, admin_account, support_account])
|
||||
session.commit()
|
||||
admin_id = admin.id
|
||||
support_id = support.id
|
||||
admin_account_id = admin_account.id
|
||||
support_account_id = support_account.id
|
||||
|
||||
with mig_engine.begin() as conn:
|
||||
for user_id, account_id in (
|
||||
(admin_id, admin_account_id),
|
||||
(support_id, support_account_id),
|
||||
):
|
||||
conn.execute(
|
||||
text(
|
||||
"INSERT INTO weibo_workbench_accounts "
|
||||
"(user_id, account_id, created_at) "
|
||||
"VALUES (:user_id, :account_id, UTC_TIMESTAMP())"
|
||||
),
|
||||
{"user_id": user_id, "account_id": account_id},
|
||||
)
|
||||
|
||||
command.upgrade(cfg, "20261112_0077")
|
||||
|
||||
with mig_engine.begin() as conn:
|
||||
rows = conn.execute(
|
||||
text(
|
||||
"SELECT user_id, account_id, exclusive_slot "
|
||||
"FROM weibo_workbench_accounts ORDER BY user_id"
|
||||
)
|
||||
).all()
|
||||
assert {
|
||||
(user_id, account_id, exclusive_slot)
|
||||
for user_id, account_id, exclusive_slot in rows
|
||||
} == {
|
||||
(admin_id, admin_account_id, None),
|
||||
(support_id, support_account_id, 1),
|
||||
}
|
||||
|
||||
# 管理员可以给同一个账号再挂一条并行映射,普通唯一槽位保持不变。
|
||||
conn.execute(
|
||||
text(
|
||||
"INSERT INTO weibo_workbench_accounts "
|
||||
"(user_id, account_id, exclusive_slot, created_at) "
|
||||
"VALUES (:user_id, :account_id, NULL, UTC_TIMESTAMP())"
|
||||
),
|
||||
{"user_id": admin_id, "account_id": support_account_id},
|
||||
)
|
||||
assert (
|
||||
conn.execute(
|
||||
text(
|
||||
"SELECT COUNT(*) FROM weibo_workbench_accounts "
|
||||
"WHERE account_id = :account_id"
|
||||
),
|
||||
{"account_id": support_account_id},
|
||||
).scalar_one()
|
||||
== 2
|
||||
)
|
||||
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}`;"))
|
||||
|
||||
|
||||
class TestRollbackBackfillScript:
|
||||
"""回滚前置脚本:把账号侧 CK 搬回 `login_tasks`。
|
||||
|
||||
Reference in New Issue
Block a user