fix(login): 删除任务不再丢 CK —— 只删不持有 CK 的记录【数据安全】
事故:斗鱼账号的 CK **只存在 `login_tasks.cookie`**(`accounts` 表没有 cookie 列),删掉记录即永久丢失登录态。而列表显示的「状态」是最近一次尝试 (`attempt_status`),于是一条**显示「失败」但实际仍持有有效 CK** 的记录 (status=success + cookie + 上次尝试失败)会被「清空失败」当成垃圾删掉。 修复: - 批量删除只删空 cookie 的记录,持有 CK 的一律跳过,并在响应里报告跳过数; - 单条删除遇到持有 CK 的记录直接 409,提示先处理该账号的 CK; - 前端确认弹窗写明「持有 CK 的记录会跳过」。 顺带修统计口径:分页后 success/failed 只统计当前页,却和「共 N 个任务」 并排显示(曾出现「共 8508,成功 48」)。改走 /tasks/summary 的全局统计。 回归测试补 5 条,专门盯「显示失败但持有 CK」这个事故场景。
This commit is contained in:
@@ -18,7 +18,7 @@ from sqlalchemy.orm import sessionmaker
|
||||
import web.backend.database as database_module
|
||||
from tests.support.db import get_test_engine, make_test_session
|
||||
from web.backend.models import Account, LoginTask, User
|
||||
from web.backend.routers.login import delete_tasks, list_tasks
|
||||
from web.backend.routers.login import delete_task, delete_tasks, list_tasks
|
||||
|
||||
|
||||
class TestTaskPagination:
|
||||
@@ -229,3 +229,154 @@ class TestBulkDelete:
|
||||
finally:
|
||||
db.close()
|
||||
assert "无效的任务ID" in str(exc.value)
|
||||
|
||||
|
||||
class TestCkProtection:
|
||||
"""⚠️ 最重要的护栏:删除不能丢 CK。
|
||||
|
||||
事故背景(2026-10-03):斗鱼账号的 CK **只存在 `login_tasks.cookie`**,
|
||||
`accounts` 表没有 cookie 列。列表显示的「状态」是最近一次尝试
|
||||
(``attempt_status``),于是一条**显示「失败」但实际持有有效 CK** 的记录
|
||||
(``status='success'`` + cookie + 上次尝试失败)会被「清空失败」当成垃圾删掉,
|
||||
账号的登录态随之永久丢失。
|
||||
|
||||
所以判定必须看 ``cookie``,不能看显示状态。
|
||||
"""
|
||||
|
||||
def setup_method(self):
|
||||
self.engine = get_test_engine()
|
||||
database_module.SessionLocal.configure(bind=self.engine)
|
||||
self.session = make_test_session(self.engine)
|
||||
|
||||
self.user = User(username="ck-admin", password_hash="h", role="super_admin")
|
||||
self.session.add(self.user)
|
||||
self.session.flush()
|
||||
self.account = Account(
|
||||
username="CK保护账号",
|
||||
password="p",
|
||||
email="ck@example.com",
|
||||
email_password="ep",
|
||||
)
|
||||
self.session.add(self.account)
|
||||
self.session.flush()
|
||||
|
||||
# ① 显示失败、但持有有效 CK —— 生产事故就是删到了这种
|
||||
self.holds_ck_shows_failed = self._add(
|
||||
status="success", attempt_status="failed", cookie="valid-ck-1"
|
||||
)
|
||||
# ② 成功且持有 CK
|
||||
self.holds_ck_shows_success = self._add(
|
||||
status="success", attempt_status="success", cookie="valid-ck-2"
|
||||
)
|
||||
# ③ 真垃圾:没有任何 CK
|
||||
self.no_ck = self._add(status="failed", attempt_status="failed", cookie="")
|
||||
|
||||
# 用两个账号,避免同一账号多条记录干扰「账号是否还有 CK」的判断
|
||||
self.other = Account(
|
||||
username="CK保护账号2",
|
||||
password="p",
|
||||
email="ck2@example.com",
|
||||
email_password="ep",
|
||||
)
|
||||
self.session.add(self.other)
|
||||
self.session.flush()
|
||||
self.holds_ck_other = self._add(
|
||||
status="success",
|
||||
attempt_status="failed",
|
||||
cookie="valid-ck-3",
|
||||
account_id=self.other.id,
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
def _add(self, *, status, attempt_status, cookie, account_id=None):
|
||||
task = LoginTask(
|
||||
batch_id="ck-batch",
|
||||
account_id=account_id or self.account.id,
|
||||
created_by=self.user.id,
|
||||
status=status,
|
||||
cookie=cookie,
|
||||
attempt_status=attempt_status,
|
||||
attempt_batch_id="ck-batch",
|
||||
message="x",
|
||||
)
|
||||
self.session.add(task)
|
||||
self.session.flush()
|
||||
return task.id
|
||||
|
||||
def teardown_method(self):
|
||||
self.session.close()
|
||||
|
||||
def _fresh(self):
|
||||
return sessionmaker(bind=self.engine)()
|
||||
|
||||
def _exists(self, task_id):
|
||||
db = self._fresh()
|
||||
try:
|
||||
return (
|
||||
db.query(LoginTask).filter(LoginTask.id == task_id).first() is not None
|
||||
)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
def test_bulk_delete_skips_ck_holding_records(self):
|
||||
"""「清空失败」绝不能删掉持有 CK 的记录,哪怕它显示为失败。"""
|
||||
db = self._fresh()
|
||||
try:
|
||||
res = delete_tasks(
|
||||
status="failed", all_matching=True, db=db, current=self.user
|
||||
)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
assert self._exists(self.holds_ck_shows_failed), (
|
||||
"显示失败但持有 CK 的记录被删了!"
|
||||
)
|
||||
assert self._exists(self.holds_ck_other), "另一个账号的 CK 记录被删了!"
|
||||
assert not self._exists(self.no_ck), "无 CK 的历史记录应被删除"
|
||||
assert res["deleted"] == 1
|
||||
assert res["skipped"] >= 2
|
||||
|
||||
def test_bulk_delete_reports_skipped_count(self):
|
||||
db = self._fresh()
|
||||
try:
|
||||
res = delete_tasks(all_matching=True, db=db, current=self.user)
|
||||
finally:
|
||||
db.close()
|
||||
# 3 条持有 CK(含另一个账号)、1 条无 CK
|
||||
assert res["deleted"] == 1
|
||||
assert res["skipped"] == 3
|
||||
assert "持有 CK" in res["message"]
|
||||
|
||||
def test_explicit_ids_cannot_delete_ck_holder(self):
|
||||
"""即使用户手动勾选,持有 CK 的也不能删。"""
|
||||
db = self._fresh()
|
||||
try:
|
||||
res = delete_tasks(
|
||||
task_ids=f"{self.holds_ck_shows_failed},{self.no_ck}",
|
||||
db=db,
|
||||
current=self.user,
|
||||
)
|
||||
finally:
|
||||
db.close()
|
||||
assert self._exists(self.holds_ck_shows_failed)
|
||||
assert not self._exists(self.no_ck)
|
||||
assert res["deleted"] == 1
|
||||
|
||||
def test_single_delete_refuses_ck_holder(self):
|
||||
db = self._fresh()
|
||||
try:
|
||||
with pytest.raises(Exception) as exc:
|
||||
delete_task(self.holds_ck_shows_success, db=db, current=self.user)
|
||||
finally:
|
||||
db.close()
|
||||
assert "持有 CK" in str(exc.value)
|
||||
assert self._exists(self.holds_ck_shows_success)
|
||||
|
||||
def test_single_delete_allows_empty_cookie(self):
|
||||
db = self._fresh()
|
||||
try:
|
||||
res = delete_task(self.no_ck, db=db, current=self.user)
|
||||
finally:
|
||||
db.close()
|
||||
assert res["success"] is True
|
||||
assert not self._exists(self.no_ck)
|
||||
|
||||
Reference in New Issue
Block a user