优化
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
"""批次 2(D7)回归测试:批量任务聚合查询以 PG 为单一事实源。
|
||||
|
||||
覆盖:
|
||||
- GET /api/batch/{batch_id} 按 ProcessingTask.batch_id 聚合(此前依赖 Redis batch key + 进程内存降级)
|
||||
- 归属校验:他人批次 403、不存在 404、所有者 200 + 聚合数字正确
|
||||
"""
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession
|
||||
|
||||
from moldinsight.api.batch_router import router as batch_router
|
||||
from shared.database.database import get_db_session
|
||||
from shared.models.database import User, STPFile, ProcessingTask
|
||||
from shared.services.auth_service import get_current_active_user
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
async def batch_client(async_engine, seeded_db):
|
||||
"""带 batch_router 的测试应用:播种 batch-1(user 1,completed+processing)与 batch-2(user 999)。"""
|
||||
session_factory = async_sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False)
|
||||
|
||||
async with session_factory() as session:
|
||||
stp_a = STPFile(
|
||||
id=9101, user_id=1, object_key="test/batch-a.step", storage_bucket="moldinsight",
|
||||
original_filename="batch-a.step", file_size=128, status="completed",
|
||||
)
|
||||
stp_b = STPFile(
|
||||
id=9102, user_id=1, object_key="test/batch-b.step", storage_bucket="moldinsight",
|
||||
original_filename="batch-b.step", file_size=128, status="processing",
|
||||
)
|
||||
stp_c = STPFile(
|
||||
id=9103, user_id=999, object_key="test/batch-c.step", storage_bucket="moldinsight",
|
||||
original_filename="batch-c.step", file_size=128, status="completed",
|
||||
)
|
||||
task_a = ProcessingTask(
|
||||
task_id="task-batch-a", stp_file_id=9101, batch_id="batch-1",
|
||||
task_type="stp_parsing", status="completed", progress=100, parameters={},
|
||||
)
|
||||
task_b = ProcessingTask(
|
||||
task_id="task-batch-b", stp_file_id=9102, batch_id="batch-1",
|
||||
task_type="stp_parsing", status="processing", progress=40,
|
||||
current_step="生成模具型腔", parameters={},
|
||||
)
|
||||
task_c = ProcessingTask(
|
||||
task_id="task-batch-c", stp_file_id=9103, batch_id="batch-2",
|
||||
task_type="stp_parsing", status="failed", progress=40,
|
||||
error_message="处理超时", parameters={},
|
||||
)
|
||||
session.add_all([stp_a, stp_b, stp_c, task_a, task_b, task_c])
|
||||
await session.commit()
|
||||
|
||||
test_app = FastAPI()
|
||||
test_app.include_router(batch_router, prefix="/api")
|
||||
|
||||
async def override_get_db_session():
|
||||
async with session_factory() as session:
|
||||
yield session
|
||||
|
||||
test_app.dependency_overrides[get_db_session] = override_get_db_session
|
||||
|
||||
transport = ASGITransport(app=test_app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
||||
yield ac, test_app
|
||||
|
||||
test_app.dependency_overrides.clear()
|
||||
|
||||
|
||||
def _override_user(app: FastAPI, user_id: int):
|
||||
app.dependency_overrides[get_current_active_user] = lambda: User(id=user_id, username="tester")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_batch_status_owner_aggregates_from_pg(batch_client):
|
||||
"""所有者查询:聚合数字与逐任务字段来自 PG,而非 Redis。"""
|
||||
ac, app = batch_client
|
||||
_override_user(app, 1)
|
||||
|
||||
resp = await ac.get("/api/batch/batch-1")
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
|
||||
assert body["batch_id"] == "batch-1"
|
||||
assert body["total"] == 2
|
||||
assert body["completed"] == 1
|
||||
assert body["processing"] == 1
|
||||
assert body["failed"] == 0
|
||||
assert body["progress_percent"] == 50.0
|
||||
|
||||
by_id = {t["task_id"]: t for t in body["tasks"]}
|
||||
assert by_id["task-batch-a"]["status"] == "completed"
|
||||
assert by_id["task-batch-a"]["filename"] == "batch-a.step"
|
||||
assert by_id["task-batch-b"]["progress"] == 40
|
||||
assert by_id["task-batch-b"]["current_step"] == "生成模具型腔"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_batch_status_non_owner_is_403(batch_client):
|
||||
"""他人批次必须 403(按 STPFile.user_id 校验,无主不等于公共)。"""
|
||||
ac, app = batch_client
|
||||
_override_user(app, 2)
|
||||
|
||||
resp = await ac.get("/api/batch/batch-1")
|
||||
assert resp.status_code == 403
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_batch_status_unknown_is_404(batch_client):
|
||||
ac, app = batch_client
|
||||
_override_user(app, 1)
|
||||
|
||||
resp = await ac.get("/api/batch/batch-missing")
|
||||
assert resp.status_code == 404
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_batch_status_includes_error_from_pg(batch_client):
|
||||
"""failed 任务的 error_message 经 PG 返回(此前 error 只存在于 Redis task dict)。"""
|
||||
ac, app = batch_client
|
||||
_override_user(app, 999)
|
||||
|
||||
resp = await ac.get("/api/batch/batch-2")
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["failed"] == 1
|
||||
assert body["tasks"][0]["error"] == "处理超时"
|
||||
@@ -0,0 +1,37 @@
|
||||
"""批次 1 部署治理回归测试。
|
||||
|
||||
覆盖:
|
||||
- AUTO_MIGRATE 开关:env 解析与默认值(默认 true 保持现行启动行为)
|
||||
- create_admin_user:ADMIN_PASSWORD 未配置时显式报错,不创建空口令管理员
|
||||
"""
|
||||
import pytest
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from shared.config.settings import Settings
|
||||
|
||||
|
||||
def test_auto_migrate_default_true(monkeypatch):
|
||||
"""未配置时默认 true:保持既有单机开发行为(启动即迁移)。"""
|
||||
monkeypatch.delenv("AUTO_MIGRATE", raising=False)
|
||||
assert Settings().AUTO_MIGRATE is True
|
||||
|
||||
|
||||
def test_auto_migrate_env_parsing(monkeypatch):
|
||||
monkeypatch.setenv("AUTO_MIGRATE", "false")
|
||||
assert Settings().AUTO_MIGRATE is False
|
||||
monkeypatch.setenv("AUTO_MIGRATE", "true")
|
||||
assert Settings().AUTO_MIGRATE is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_admin_without_password_is_explicit_error(monkeypatch):
|
||||
"""ADMIN_PASSWORD 缺失必须显式失败(compose 已去弱默认),而非静默创建空口令管理员。"""
|
||||
# init_db 顶层 import alembic;未安装 alembic 的环境跳过(与 OCC 测试同策略)
|
||||
pytest.importorskip("alembic.config")
|
||||
from shared.config.settings import settings
|
||||
from shared.database.init_db import create_admin_user
|
||||
|
||||
monkeypatch.setattr(settings, "ADMIN_PASSWORD", None)
|
||||
# 校验发生在任何 DB 访问之前,MagicMock 会话不会被触碰
|
||||
with pytest.raises(RuntimeError, match="ADMIN_PASSWORD"):
|
||||
await create_admin_user(MagicMock())
|
||||
@@ -0,0 +1,57 @@
|
||||
"""批次 2(D7 / D9)回归测试。
|
||||
|
||||
- D7:Redis 任务管理器不再有进程内存回退——Redis 不可用时写 no-op、读返回 None,
|
||||
状态查询路径落到 PG(PG 为单一事实源)
|
||||
- D9:存储服务的数据本体写方法只 flush 不 commit,事务由编排层收口
|
||||
"""
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
from shared.services.redis_task_manager import RedisTaskManager
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_memory_fallback_when_disconnected():
|
||||
"""Redis 未连接:写 no-op、读 None——不得再出现进程内可见的副本。"""
|
||||
mgr = RedisTaskManager() # 不 connect
|
||||
assert not mgr.is_connected
|
||||
|
||||
await mgr.set_task("t1", {"status": "processing"})
|
||||
assert await mgr.get_task("t1") is None
|
||||
|
||||
await mgr.update_task("t1", {"status": "completed"}) # no-op,不得抛异常
|
||||
|
||||
assert await mgr.get_all_tasks() == {}
|
||||
assert await mgr.get_task_count() == 0
|
||||
await mgr.delete_task("t1") # no-op,不得抛异常
|
||||
|
||||
assert await mgr.get_task("t1") is None
|
||||
|
||||
|
||||
def test_fallback_storage_removed():
|
||||
"""防回归:内存回退存储必须已删除,防止静默回归。"""
|
||||
assert not hasattr(RedisTaskManager, "_fallback_tasks")
|
||||
assert not hasattr(RedisTaskManager, "_fallback_set")
|
||||
assert not hasattr(RedisTaskManager, "cleanup_old_tasks")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_storage_writes_flush_but_never_commit():
|
||||
"""D9:数据本体写方法仅 flush;commit 由编排层/请求侧负责。"""
|
||||
pytest.importorskip("minio")
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from moldinsight.services.storage_integration_rustfs import StorageIntegrationService
|
||||
|
||||
svc = StorageIntegrationService()
|
||||
session = AsyncMock(spec=AsyncSession)
|
||||
# update_task_parameters:select 返回 None(任务不存在)→ 直接 return
|
||||
result_mock = MagicMock()
|
||||
result_mock.scalar_one_or_none.return_value = None
|
||||
session.execute.return_value = result_mock
|
||||
|
||||
await svc.update_task_parameters(session, "task-x", {"a": 1})
|
||||
await svc.create_processing_task(session, "task-y", stp_file_id=1, batch_id="b-1")
|
||||
|
||||
assert session.flush.await_count >= 1
|
||||
# 注意:不能用 .awaited(AsyncMock 上访问会自动创建 truthy 子 mock),用 await_count
|
||||
assert session.commit.await_count == 0, "存储写方法不得自行 commit(D9 事务收口)"
|
||||
@@ -0,0 +1,139 @@
|
||||
"""批次 0 安全修复回归测试(TECH_DEBT D5 等)。
|
||||
|
||||
覆盖:
|
||||
- /api/status/{task_id} 鉴权与任务归属校验(无 token 401 / 他人任务 403 / 不存在 404 / 所有者 200)
|
||||
- bcrypt 72 字节上限:超长密码显式拒绝而非静默截断
|
||||
- SECRET_KEY 惰性校验:未配置时给出明确错误
|
||||
"""
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession
|
||||
|
||||
from moldinsight.api.task_router import router as task_router
|
||||
from shared.database.database import get_db_session
|
||||
from shared.models.database import User, STPFile, ProcessingTask
|
||||
from shared.services.auth_service import (
|
||||
get_current_active_user,
|
||||
get_password_hash,
|
||||
verify_password,
|
||||
create_access_token,
|
||||
)
|
||||
from shared.config.settings import settings
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
async def status_client(async_engine, seeded_db):
|
||||
"""带 task_router 的测试应用:播种两个任务(owner=user 1 / user 999)。"""
|
||||
session_factory = async_sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False)
|
||||
|
||||
async with session_factory() as session:
|
||||
stp_own = STPFile(
|
||||
id=9001, user_id=1, object_key="test/own.step", storage_bucket="moldinsight",
|
||||
original_filename="own.step", file_size=128, status="completed",
|
||||
)
|
||||
task_own = ProcessingTask(
|
||||
task_id="task-owned", stp_file_id=9001,
|
||||
task_type="stp_parsing", status="completed", parameters={},
|
||||
)
|
||||
stp_other = STPFile(
|
||||
id=9002, user_id=999, object_key="test/other.step", storage_bucket="moldinsight",
|
||||
original_filename="other.step", file_size=128, status="completed",
|
||||
)
|
||||
task_other = ProcessingTask(
|
||||
task_id="task-foreign", stp_file_id=9002,
|
||||
task_type="stp_parsing", status="completed", parameters={},
|
||||
)
|
||||
session.add_all([stp_own, task_own, stp_other, task_other])
|
||||
await session.commit()
|
||||
|
||||
test_app = FastAPI()
|
||||
# 生产环境中 /api 前缀由入口层挂载时添加,测试中需显式指定才能对齐真实路径
|
||||
test_app.include_router(task_router, prefix="/api")
|
||||
|
||||
async def override_get_db_session():
|
||||
async with session_factory() as session:
|
||||
yield session
|
||||
|
||||
test_app.dependency_overrides[get_db_session] = override_get_db_session
|
||||
|
||||
transport = ASGITransport(app=test_app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
||||
yield ac, test_app
|
||||
|
||||
test_app.dependency_overrides.clear()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_status_without_token_is_401(status_client):
|
||||
"""未携带 token 访问 /api/status 必须拒绝(此前该端点完全未鉴权)。"""
|
||||
ac, _ = status_client
|
||||
resp = await ac.post("/api/status/task-owned")
|
||||
assert resp.status_code == 401
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_status_owner_can_view(status_client):
|
||||
ac, app = status_client
|
||||
app.dependency_overrides[get_current_active_user] = lambda: User(id=1, username="tester")
|
||||
|
||||
resp = await ac.post("/api/status/task-owned")
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["task_id"] == "task-owned"
|
||||
assert body["status"] == "completed"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_status_non_owner_is_403(status_client):
|
||||
"""他人任务(含无主任务)必须 403,不允许凭任务号枚举。"""
|
||||
ac, app = status_client
|
||||
app.dependency_overrides[get_current_active_user] = lambda: User(id=2, username="intruder")
|
||||
|
||||
resp = await ac.post("/api/status/task-owned")
|
||||
assert resp.status_code == 403
|
||||
|
||||
resp = await ac.post("/api/status/task-foreign")
|
||||
assert resp.status_code == 403
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_status_unknown_task_is_404(status_client):
|
||||
ac, app = status_client
|
||||
app.dependency_overrides[get_current_active_user] = lambda: User(id=1, username="tester")
|
||||
|
||||
resp = await ac.post("/api/status/task-missing")
|
||||
assert resp.status_code == 404
|
||||
|
||||
|
||||
def test_password_hash_rejects_over_72_bytes():
|
||||
"""bcrypt 72 字节上限:必须显式报错,不能静默截断。"""
|
||||
with pytest.raises(ValueError, match="72"):
|
||||
get_password_hash("a" * 73)
|
||||
# 多字节字符按字节数计:25 个汉字 = 75 字节
|
||||
with pytest.raises(ValueError, match="72"):
|
||||
get_password_hash("模" * 25)
|
||||
|
||||
|
||||
def test_password_hash_roundtrip_at_limit():
|
||||
password = "a" * 72
|
||||
hashed = get_password_hash(password)
|
||||
assert verify_password(password, hashed)
|
||||
# 历史口令按 bcrypt 语义截断比较:73 字节输入截断后与 72 字节口令匹配(兼容旧数据),
|
||||
# 但新口令在 get_password_hash 处已被显式拒绝,不会再产生这类哈希
|
||||
assert verify_password(password + "x", hashed)
|
||||
assert not verify_password("b" * 72, hashed)
|
||||
|
||||
|
||||
def test_verify_password_over_72_bytes_returns_false_not_raise():
|
||||
"""超长密码登录不得抛 ValueError(否则登录接口 500),应返回 False 走正常失败路径。"""
|
||||
hashed = get_password_hash("short-password")
|
||||
assert not verify_password("长" * 40, hashed) # 120 字节
|
||||
assert not verify_password("a" * 73, hashed)
|
||||
|
||||
|
||||
def test_create_token_without_secret_key_is_explicit_error(monkeypatch):
|
||||
"""SECRET_KEY 未配置时给出可读错误,而不是 jwt.encode 的晦涩 TypeError。"""
|
||||
monkeypatch.setattr(settings, "SECRET_KEY", None)
|
||||
with pytest.raises(RuntimeError, match="SECRET_KEY"):
|
||||
create_access_token({"sub": "tester"})
|
||||
Reference in New Issue
Block a user