S1 加好了列,本提交把**读与写一起切**过去。这是设计里「同一份数据的读与写必须
在同一个提交里切」的要求:先切读或先切写都会出现「写账号、读记录」的中间态,
跑起来不报错,只是默默用错数据。
## 核心改动
**判定「谁有 CK」不再借道登录记录**
- `cookie_holding_filter()` / `cookie_operable_filter()` / `cookie_holding_account_ids_query()`
/ `visible_douyu_task_accounts()` 全部改为作用于 `Account`;
- `cookie_operable_filter()` 的「在途排除」仍查 `login_tasks`(那是**某次尝试**的属性),
用 `NOT EXISTS` 子查询 + 迁移 0042 新增的 `(account_id, attempt_status)` 索引;
- 删除死代码 `latest_cookie_login_task()`;`latest_cookie()` 改为直接读账号。
**CK 写入**
- `_write_login_success()` 写 `Account.cookie / cookie_updated_at / cookie_source`;
- `_mark_attempt_failed()` 改看**账号**有没有 CK(决定是否沿用旧的失败语义);
- 检测模式的 `task.cookie = ""` **不再清空账号的 CK**(见下「行为变更」)。
**自动化作业按账号调度**
- `ck_health`:`_select_tasks` → `_select_accounts`,`db.query(Account)`;新增
`count_remaining()`(刻意不带 LIMIT,否则积压数恒等于批次上限);
- `ck_relogin`:同样按账号选行;结算改为「账号 + 本轮尝试记录」,
退避进度写账号;`settle_fresh_results(batch_id)` 按 `attempt_batch_id` 配对,
`run_ck_relogin` 把 **account_ids** 交给 runner;
- 顺带让**没有任何登录历史的账号**也能被巡检/重登(以前它们永远进不了队列)。
**登录批次**
- `LoginBatchRunner` 去掉 `relogin_task_ids`,重登与登录统一走 `account_ids`;
- 批次行复用逻辑重写:只保证「同一账号不并发两行」,不再关心「哪一行持有 CK」。
失败行复用同时看 `attempt_status`(账号有 CK 时 `_mark_attempt_failed` 刻意不动
`status`,只看 status 会永远命不中 → 每次失败刷一行)。
**删除护栏语义变更(用户可感知)**
- 去掉「持有 CK 的记录不可删」——CK 在账号上,删历史不再影响登录态,历史终于可清理;
- **保留**「在途尝试不可删」(批次线程正拿着那条记录)。
## 行为变更(需评审)
1. **账号检测不再清空 CK**。旧代码在 check 模式写 `task.cookie = ""`,而那时记录的
cookie 就是账号唯一凭证 —— 一次「批量检测」会把账号登录态删掉。解耦后
「检测账号凭据」与「CK 是否有效」本就是两件事(后者归 `ck_health` 判),
继续清空等于凭空制造不可逆的数据丢失(重登当前成功率极低)。
2. **Cookie 页的记录单位从「登录记录」变成「账号」**,所有接口的 id 入参/出参
变成 `account_id`;`DELETE /cookies/{id}` 清的是该账号的 CK,**必写审计**。
3. 重复检测从「同一账号多行持 CK」改为「**同一个 CK 出现在多个账号上**」——
后者才是真风险(两个账号共用一个登录态)。用 sha256 指纹分组:
CK 是 AES-GCM + 随机 nonce 加密,**同一明文每次密文都不同**,比密文永远比不出重复。
## 修掉一个真实并发缺陷
`_write_login_success` 原先被传入 `self.db`(runner 主会话),而它是在**工作线程**里
用 `worker_db` 执行的。`emit()` 会往 `account_events` 插一行,其 `account_id` 外键对
`accounts` 那一行加**共享锁**;这把锁加在另一个会话上,紧接着本会话的
`UPDATE accounts SET cookie=...` 就被自己人挡住 —— 实测 **50 秒后
`Lock wait timeout exceeded`,登录明明成功却写不回 CK**(改造前 UPDATE 打在
`login_tasks` 上所以没撞上)。改为传 `worker_db`:事件本就该和状态变更同事务提交,
顺带修掉跨线程共用一个 Session 的隐患。
## 验证
- 全量 pytest **368 passed / 6 skipped**;迁移链 3 passed;
- 前端 `tsc -b && vite build` 通过;
- `ruff check` / `ruff format --check` 全绿(顺带修掉 main 上遗留的 F821);
- 新增/改写的关键回归:**删掉全部登录历史后账号的 CK 与健康状态仍在**、
**无登录历史的账号也能进巡检队列**、结算不碰 CK、删除护栏改为在途语义。
仍未做(设计里排期的后续阶段):`account_lifecycle` 状态收口(S3)、事件发射点补齐(S4)、
业务事实直发事件(S5)、账号页重构(S6)。注意 `status` 目前只有模型默认值与迁移回填,
所以 `disabled/archived` 还没有任何写入方——S3 落地时要把自动化选行的状态过滤一起补上。
178 lines
6.5 KiB
Python
178 lines
6.5 KiB
Python
"""回归:账号只要自己有 CK 就可见,**与任何登录记录的状态无关**。
|
|
|
|
这一条是 2026-10-04 CK 解耦的核心验收点。
|
|
|
|
解耦前:CK 存在 `login_tasks.cookie` 上,于是「记录被复用置为 pending」「重登失败」
|
|
都会让账号看起来「没有 CK」;线上出过事故(账号「远行者103」被分配后,
|
|
客服与管理员在工作台都看不到它)。
|
|
解耦后: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,
|
|
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()
|
|
|
|
# 有 CK 的账号:即使历史记录是「重登失败」,也必须可见。
|
|
self.relogin_account = self._make_account("远行者103", self.support.id)
|
|
self.relogin_account.cookie = "acf_uid=481802039"
|
|
self.relogin_account.ck_check_status = "valid"
|
|
self.session.add(
|
|
LoginTask(
|
|
batch_id="relogin-failed",
|
|
account_id=self.relogin_account.id,
|
|
created_by=self.support.id,
|
|
status="relogin_failed",
|
|
)
|
|
)
|
|
# 有 CK 的账号:即使历史记录停在 pending(批次排队中),也必须可见。
|
|
self.pending_account = self._make_account("排队中账号", self.support.id)
|
|
self.pending_account.cookie = "acf_uid=2"
|
|
self.session.add(
|
|
LoginTask(
|
|
batch_id="queued",
|
|
account_id=self.pending_account.id,
|
|
created_by=self.admin.id,
|
|
status="pending",
|
|
)
|
|
)
|
|
# 从未登录成功:没有 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",
|
|
)
|
|
)
|
|
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_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_accounts_with_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",
|
|
"排队中账号",
|
|
}
|
|
# 解耦后 id 就是账号 id(以前是「持有 CK 的登录记录 id」)。
|
|
assert {item["id"] for item in items} == {
|
|
self.relogin_account.id,
|
|
self.pending_account.id,
|
|
}
|
|
|
|
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
|
|
|
|
def test_deleting_login_history_does_not_touch_cookie(self):
|
|
"""本次改造的验收核心:删掉全部登录历史,账号的 CK 与健康状态仍在。"""
|
|
self.session.query(LoginTask).delete(synchronize_session=False)
|
|
self.session.commit()
|
|
self.session.expire_all()
|
|
|
|
fresh = (
|
|
self.session.query(Account)
|
|
.filter(Account.id == self.relogin_account.id)
|
|
.one()
|
|
)
|
|
assert fresh.cookie == "acf_uid=481802039"
|
|
assert fresh.ck_check_status == "valid"
|
|
assert self._task_account_names(self.admin) == {"远行者103", "排队中账号"}
|