生产是 MySQL 8、测试是 SQLite,两者的方言差异在本地完全测不出来。今天为此 付出了两次代价: 1. `ORDER BY ck_checked_at ASC NULLS FIRST` 在 SQLite 上完全正常,本地 258 条 全绿,一部署到生产第一次巡检就 1064 语法错误(MySQL 不支持 NULLS FIRST); 2. P1b 自动重登的结算复用调度器事务读取,在 SQLite 下看不出问题,但在 MySQL 的 REPEATABLE READ 下读到的是批次开始前的快照 —— 10 条实际成功的重登全部被记成 失败。SQLite 的读一致性与 MySQL 不同,这类问题跑一万遍也复现不出来。 改动: - 新增 tests/support/db.py:测试专用引擎/会话工具,连接串从 .env 读取, 默认指向独立的 douyu_login_test 库; - 新增 tests/conftest.py:会话级建好表结构,每个用例前后清空所有表做隔离; - 根 conftest.py 接管连接串设置:它在 pytest 收集阶段最先执行,在这里把 DATABASE_URL 指向测试库,应用模块首次 import 拿到就是 MySQL,不依赖任何 import 顺序(原先这里是 sqlite:// 兜底,改造后会变成「先建 SQLite engine 再打补丁换成 MySQL」,顺序一变就静默跑错库); - 21 个测试文件的 setup 样板统一改用共享测试引擎; - 两个迁移测试改为在临时库 douyu_login_mig_<随机> 上跑完整 alembic upgrade head, 跑完 drop —— 迁移脚本本身也终于在 MySQL 上被验证; - dev.sh 与 README 的过时说明一并更新(原先写着「不需要 MySQL / .env 配置」; 「测试各自建 SQLite 内存库」)。 安全与易用: - 测试库名与开发库同名时**直接拒绝运行**(clear_tables 会 DELETE 所有表, 一旦 TEST_MYSQL_DATABASE 被指到 douyu_login 就是数据灾难); - 连不上 MySQL 时给出可操作提示(先 ./dev.sh 起库 / 用 TEST_MYSQL_DATABASE 覆盖), 而不是抛一段 SQLAlchemy 原始堆栈。 验收: - 全量 301 条通过,连跑两次结果一致(隔离有效),耗时约 12.5 秒(原 SQLite 约 3.6 秒); - 单文件独立运行通过(不依赖全量执行顺序); - `grep -rn sqlite tests/ conftest.py` 已无实际用法; - **方言确实生效**:临时把一条查询改回 `NULLS FIRST`,13 条测试立刻变红(含运行期 失败),还原即恢复;这条由 test_generated_sql_is_mysql_compatible 长期守着。 代价(已在 README 与规划附录 C.2 记明):跑测试前必须先有 MySQL,pre-push 钩子 同理;CI 需要 service container。
162 lines
5.8 KiB
Python
162 lines
5.8 KiB
Python
"""回归:只要账号还留着 CK,就不得因为登录批次状态而从工作台消失。
|
||
|
||
线上事故(2026-10-01):账号「远行者103」被分配给客服后,客服与管理员在
|
||
斗鱼工作台都看不到它,只有账号管理里还能搜到。原因是可见性判定写成了
|
||
``LoginTask.status == "success"``,而普通登录批次启动时会把复用的成功记录
|
||
置为 ``pending``,CK 重登失败会置为 ``relogin_failed`` —— 两种状态都保留旧
|
||
Cookie,却让账号从工作台与 CK 列表里凭空消失。
|
||
"""
|
||
|
||
import pytest
|
||
|
||
from tests.support.db import get_test_engine, make_test_session
|
||
from web.backend.models import Account, LoginTask, User
|
||
from web.backend.routers.cookies import list_cookies
|
||
from web.backend.routers.douyu import list_task_account_ids, list_task_accounts
|
||
from web.backend.services.account_service import cookie_account_ids_query
|
||
from web.backend.services.douyu_service import (
|
||
create_douyu_planned_tasks,
|
||
latest_cookie_login_task,
|
||
visible_douyu_task_accounts,
|
||
)
|
||
|
||
|
||
class TestDouyuCookieVisibility:
|
||
def setup_method(self):
|
||
self.engine = get_test_engine()
|
||
self.session = make_test_session(self.engine)
|
||
|
||
self.admin = User(username="admin", password_hash="hash", role="super_admin")
|
||
self.support = User(username="kf_7", password_hash="hash", role="support")
|
||
self.session.add_all([self.admin, self.support])
|
||
self.session.commit()
|
||
|
||
# relogin_failed:重登失败但旧 CK 已按设计保留
|
||
self.relogin_account = self._make_account("远行者103", self.support.id)
|
||
self.session.add(
|
||
LoginTask(
|
||
batch_id="relogin-failed",
|
||
account_id=self.relogin_account.id,
|
||
created_by=self.support.id,
|
||
status="relogin_failed",
|
||
cookie="acf_uid=481802039",
|
||
ck_check_status="valid",
|
||
)
|
||
)
|
||
# pending:普通登录批次排队中,复用了成功记录,旧 CK 仍在
|
||
self.pending_account = self._make_account("排队中账号", self.support.id)
|
||
self.session.add(
|
||
LoginTask(
|
||
batch_id="queued",
|
||
account_id=self.pending_account.id,
|
||
created_by=self.admin.id,
|
||
status="pending",
|
||
cookie="acf_uid=2",
|
||
)
|
||
)
|
||
# 从未登录成功:没有 CK,必须继续隐藏
|
||
self.empty_account = self._make_account("无CK账号", self.support.id)
|
||
self.session.add(
|
||
LoginTask(
|
||
batch_id="failed",
|
||
account_id=self.empty_account.id,
|
||
created_by=self.admin.id,
|
||
status="failed",
|
||
cookie="",
|
||
)
|
||
)
|
||
self.session.commit()
|
||
|
||
def _make_account(self, username: str, assigned_to: int) -> Account:
|
||
account = Account(
|
||
username=username,
|
||
password="password",
|
||
email=f"{username}@example.com",
|
||
email_password="mail-password",
|
||
assigned_to=assigned_to,
|
||
)
|
||
self.session.add(account)
|
||
self.session.flush()
|
||
return account
|
||
|
||
def teardown_method(self):
|
||
self.session.close()
|
||
|
||
def _task_account_names(self, current: User) -> set[str]:
|
||
result = list_task_accounts(
|
||
search="",
|
||
tag="",
|
||
ids="",
|
||
account_names="",
|
||
page=None,
|
||
page_size=50,
|
||
db=self.session,
|
||
current=current,
|
||
)
|
||
return {item.username for item in result}
|
||
|
||
@pytest.mark.parametrize("current_attr", ["admin", "support"])
|
||
def test_workbench_lists_accounts_that_still_hold_cookie(self, current_attr):
|
||
current = getattr(self, current_attr)
|
||
names = self._task_account_names(current)
|
||
assert {"远行者103", "排队中账号"} <= names
|
||
assert "无CK账号" not in names
|
||
|
||
def test_workbench_account_ids_keep_cookie_holders(self):
|
||
result = list_task_account_ids(
|
||
search="",
|
||
tag="",
|
||
account_names="",
|
||
db=self.session,
|
||
current=self.admin,
|
||
)
|
||
assert set(result["account_ids"]) == {
|
||
self.relogin_account.id,
|
||
self.pending_account.id,
|
||
}
|
||
|
||
def test_latest_cookie_login_task_ignores_batch_status(self):
|
||
task = latest_cookie_login_task(self.session, self.relogin_account.id)
|
||
assert task is not None
|
||
assert task.cookie == "acf_uid=481802039"
|
||
|
||
def test_visible_task_accounts_include_retained_cookie(self):
|
||
accounts = visible_douyu_task_accounts(
|
||
self.session,
|
||
[self.relogin_account.id, self.empty_account.id],
|
||
)
|
||
assert [account.id for account in accounts] == [self.relogin_account.id]
|
||
|
||
def test_account_has_cookie_filter_matches_workbench(self):
|
||
ids = {
|
||
account_id for (account_id,) in cookie_account_ids_query(self.session).all()
|
||
}
|
||
assert ids == {self.relogin_account.id, self.pending_account.id}
|
||
|
||
def test_cookie_management_page_lists_retained_cookie(self):
|
||
items = list_cookies(
|
||
search="",
|
||
tag="",
|
||
account_names="",
|
||
page=None,
|
||
page_size=50,
|
||
include_cookie=True,
|
||
db=self.session,
|
||
current=self.admin,
|
||
)
|
||
assert {item["account_username"] for item in items} == {
|
||
"远行者103",
|
||
"排队中账号",
|
||
}
|
||
|
||
def test_retained_cookie_account_can_still_create_task(self):
|
||
batch_id, created = create_douyu_planned_tasks(
|
||
self.session,
|
||
[self.relogin_account.id],
|
||
"create_gold_qr",
|
||
"elite",
|
||
self.admin.id,
|
||
)
|
||
assert batch_id
|
||
assert created == 1
|