diff --git a/tests/test_login_task_filters.py b/tests/test_login_task_filters.py index b73240e..e884e3e 100644 --- a/tests/test_login_task_filters.py +++ b/tests/test_login_task_filters.py @@ -5,15 +5,20 @@ from __future__ import annotations import asyncio from datetime import UTC, datetime +import pytest +from fastapi import HTTPException + 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 ( + create_batch, delete_tasks, export_tasks, list_tasks, task_filter_options, tasks_summary, ) +from web.backend.schemas import LOGIN_BATCH_MAX_ACCOUNTS, LoginBatchRequest NOW = datetime(2026, 11, 20, 12, 0, 0, tzinfo=UTC) @@ -188,3 +193,18 @@ class TestLoginTaskFilters: 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 + + def test_batch_login_over_limit_returns_chinese_message(self): + requested = LOGIN_BATCH_MAX_ACCOUNTS + 37 + req = LoginBatchRequest(account_ids=list(range(1, requested + 1))) + + with pytest.raises(HTTPException) as exc_info: + asyncio.run(create_batch(req, db=self.db, current=self.admin)) + + assert exc_info.value.status_code == 400 + assert ( + f"单次最多选择 {LOGIN_BATCH_MAX_ACCOUNTS} 个账号" + in str(exc_info.value.detail) + ) + assert f"当前选择了 {requested} 个" in str(exc_info.value.detail) + assert "请分批操作" in str(exc_info.value.detail) diff --git a/web/backend/routers/login.py b/web/backend/routers/login.py index 005b1fd..67f86f3 100644 --- a/web/backend/routers/login.py +++ b/web/backend/routers/login.py @@ -22,7 +22,7 @@ from ..deps import authenticate_websocket, get_current_user, require_permission from ..models import Account, LoginTask, User from ..models import ProxyConfig as ProxyConfigModel from ..permissions import get_user_permissions, user_has_permission -from ..schemas import LoginBatchRequest, LoginTaskOut +from ..schemas import LOGIN_BATCH_MAX_ACCOUNTS, LoginBatchRequest, LoginTaskOut from ..services.audit_service import record_audit from ..services.login_failure import classify_failure, failure_kind_label from ..services.login_service import LoginBatchRunner, batch_registry @@ -150,6 +150,14 @@ async def create_batch( current: User = Depends(require_permission("login:batch")), ): """创建批量登录或账号检测任务。""" + if len(req.account_ids) > LOGIN_BATCH_MAX_ACCOUNTS: + raise HTTPException( + status_code=400, + detail=( + f"单次最多选择 {LOGIN_BATCH_MAX_ACCOUNTS} 个账号," + f"当前选择了 {len(req.account_ids)} 个,请分批操作" + ), + ) if not req.account_ids: raise HTTPException(status_code=400, detail="请选择账号") diff --git a/web/backend/schemas.py b/web/backend/schemas.py index 8be0b08..88c9a56 100644 --- a/web/backend/schemas.py +++ b/web/backend/schemas.py @@ -363,8 +363,13 @@ class AccountOut(BaseModel): # ---- 登录任务 ---- +LOGIN_BATCH_MAX_ACCOUNTS = 50 + + class LoginBatchRequest(BaseModel): - account_ids: list[int] = Field(..., min_length=1, max_length=50) + # 上限由路由返回中文业务提示;这里保留更大的硬上限,避免直接暴露 + # Pydantic 英文 "List should have at most ... items" 给前端。 + account_ids: list[int] = Field(..., min_length=1, max_length=500) max_login_retries: int = Field(0, ge=0) max_total_time: float = Field(300, ge=0) concurrency: int = Field(3, ge=1, le=10) diff --git a/web/frontend/src/pages/LoginTasksPage.tsx b/web/frontend/src/pages/LoginTasksPage.tsx index 98981f4..6f82d6e 100644 --- a/web/frontend/src/pages/LoginTasksPage.tsx +++ b/web/frontend/src/pages/LoginTasksPage.tsx @@ -68,6 +68,7 @@ const CK_STATUS_COLORS: Record = { type BatchMode = 'login' | 'check'; const ACCOUNT_PAGE_SIZE = 50; +const LOGIN_BATCH_MAX_ACCOUNTS = 50; interface SelectGroupOption { label: string; @@ -363,6 +364,12 @@ export default function LoginTasksPage() { message.warning('请选择账号'); return; } + if (accountIds.length > LOGIN_BATCH_MAX_ACCOUNTS) { + message.warning( + `单次最多选择 ${LOGIN_BATCH_MAX_ACCOUNTS} 个账号,当前选择了 ${accountIds.length} 个,请分批操作`, + ); + return; + } setStarting(true); setLoading(true); try {