feat(login): 增加登录任务账号信息与筛选
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
"""登录任务列表的账号信息与统一筛选口径。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from datetime import UTC, datetime
|
||||
|
||||
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,
|
||||
export_tasks,
|
||||
list_tasks,
|
||||
task_filter_options,
|
||||
tasks_summary,
|
||||
)
|
||||
|
||||
NOW = datetime(2026, 11, 20, 12, 0, 0, tzinfo=UTC)
|
||||
|
||||
|
||||
async def _read_body(response) -> str:
|
||||
chunks: list[bytes] = []
|
||||
async for chunk in response.body_iterator:
|
||||
chunks.append(chunk if isinstance(chunk, bytes) else str(chunk).encode())
|
||||
return b"".join(chunks).decode("utf-8")
|
||||
|
||||
|
||||
class TestLoginTaskFilters:
|
||||
def setup_method(self):
|
||||
self.engine = get_test_engine()
|
||||
self.db = make_test_session(self.engine)
|
||||
self.admin = User(
|
||||
username="login-filter-admin",
|
||||
password_hash="h",
|
||||
role="super_admin",
|
||||
is_active=True,
|
||||
)
|
||||
self.support = User(
|
||||
username="login-filter-kf",
|
||||
password_hash="h",
|
||||
role="support",
|
||||
is_active=True,
|
||||
)
|
||||
self.other = User(
|
||||
username="login-filter-kf2",
|
||||
password_hash="h",
|
||||
role="support",
|
||||
is_active=True,
|
||||
)
|
||||
self.db.add_all([self.admin, self.support, self.other])
|
||||
self.db.flush()
|
||||
self.account_a = self._account(
|
||||
"task-account-a",
|
||||
tag="A组",
|
||||
assigned_to=self.support.id,
|
||||
status="active",
|
||||
ck_check_status="valid",
|
||||
)
|
||||
self.account_b = self._account(
|
||||
"task-account-b",
|
||||
tag="A组",
|
||||
assigned_to=self.other.id,
|
||||
status="invalid",
|
||||
ck_check_status="invalid",
|
||||
)
|
||||
self.account_c = self._account(
|
||||
"task-account-c",
|
||||
tag="B组",
|
||||
assigned_to=None,
|
||||
status="imported",
|
||||
ck_check_status="",
|
||||
)
|
||||
self.task_a = self._task(self.account_a, "success", "登录成功")
|
||||
self.task_b = self._task(self.account_b, "failed", "账号密码错误")
|
||||
self.task_c = self._task(self.account_c, "success", "登录成功")
|
||||
self.db.commit()
|
||||
|
||||
def teardown_method(self):
|
||||
self.db.close()
|
||||
|
||||
def _account(self, username: str, **kwargs) -> Account:
|
||||
account = Account(
|
||||
username=username,
|
||||
password="p",
|
||||
email=f"{username}@example.com",
|
||||
email_password="ep",
|
||||
cookie=kwargs.pop("cookie", "acf_uid=1"),
|
||||
ck_checked_at=kwargs.pop("ck_checked_at", NOW),
|
||||
cookie_updated_at=kwargs.pop(
|
||||
"cookie_updated_at", datetime(2026, 11, 20, 11, 59, tzinfo=UTC)
|
||||
),
|
||||
**kwargs,
|
||||
)
|
||||
self.db.add(account)
|
||||
self.db.flush()
|
||||
return account
|
||||
|
||||
def _task(self, account: Account, status: str, message: str) -> LoginTask:
|
||||
task = LoginTask(
|
||||
batch_id="login-filter-batch",
|
||||
attempt_batch_id="login-filter-batch",
|
||||
account_id=account.id,
|
||||
status=status,
|
||||
attempt_status=status,
|
||||
attempt_message=message,
|
||||
message=message,
|
||||
created_by=self.admin.id,
|
||||
)
|
||||
self.db.add(task)
|
||||
self.db.flush()
|
||||
return task
|
||||
|
||||
def _names(self, **kwargs) -> list[str]:
|
||||
result = list_tasks(
|
||||
page=1,
|
||||
page_size=50,
|
||||
db=self.db,
|
||||
current=self.admin,
|
||||
**kwargs,
|
||||
)
|
||||
return [item.account_username for item in result["items"]]
|
||||
|
||||
def test_list_returns_current_account_metadata(self):
|
||||
result = list_tasks(
|
||||
page=1,
|
||||
page_size=10,
|
||||
search="task-account-a",
|
||||
db=self.db,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
item = result["items"][0]
|
||||
assert item.account_tag == "A组"
|
||||
assert item.assigned_to == self.support.id
|
||||
assert item.assigned_username == "login-filter-kf"
|
||||
assert item.account_status == "active"
|
||||
assert item.ck_check_status == "valid"
|
||||
assert item.ck_checked_at is not None
|
||||
assert item.cookie_updated_at is not None
|
||||
|
||||
def test_account_filters_are_combined(self):
|
||||
assert self._names(tag="A组") == ["task-account-b", "task-account-a"]
|
||||
assert self._names(tag="A组", assigned_user_id=self.support.id) == [
|
||||
"task-account-a"
|
||||
]
|
||||
assert self._names(assign_status="unassigned") == ["task-account-c"]
|
||||
assert self._names(ck_status="invalid") == ["task-account-b"]
|
||||
assert self._names(ck_status="unchecked") == ["task-account-c"]
|
||||
assert self._names(account_status="invalid") == ["task-account-b"]
|
||||
|
||||
def test_summary_uses_the_same_filters(self):
|
||||
summary = tasks_summary(tag="A组", db=self.db, current=self.admin)
|
||||
|
||||
assert summary["total"] == 2
|
||||
assert summary["success"] == 1
|
||||
assert summary["failed"] == 1
|
||||
|
||||
def test_filter_options_only_include_visible_task_accounts(self):
|
||||
options = task_filter_options(db=self.db, current=self.admin)
|
||||
|
||||
assert options["tags"] == ["A组", "B组"]
|
||||
assert {item["username"] for item in options["assigned_users"]} == {
|
||||
"login-filter-kf",
|
||||
"login-filter-kf2",
|
||||
}
|
||||
|
||||
def test_export_and_bulk_delete_share_filters(self):
|
||||
response = export_tasks(
|
||||
tag="A组",
|
||||
status="failed",
|
||||
db=self.db,
|
||||
current=self.admin,
|
||||
)
|
||||
body = asyncio.run(_read_body(response))
|
||||
assert "task-account-b" in body
|
||||
assert "task-account-a" not in body
|
||||
assert "login-filter-kf2" in body
|
||||
|
||||
task_b_id = self.task_b.id
|
||||
task_a_id = self.task_a.id
|
||||
result = delete_tasks(
|
||||
all_matching=True,
|
||||
tag="A组",
|
||||
status="failed",
|
||||
db=self.db,
|
||||
current=self.admin,
|
||||
)
|
||||
assert result["deleted"] == 1
|
||||
assert self.db.get(LoginTask, task_b_id) is None
|
||||
assert self.db.get(LoginTask, task_a_id) is not None
|
||||
Reference in New Issue
Block a user