Files
geMoldInsight/tests/test_status_endpoint_auth.py
T
cjw 0e6b3b1811 后端设计治理:批次 0-4 全部完成(安全/部署/一致性/结构/架构)
按 ROADMAP §3.1 治理批次推进的后端设计审查整改:

- 批次 0(安全):/api/status/{task_id} 补 JWT 鉴权与任务归属校验;
  pythonocc_available 真实探测;bcrypt 超 72 字节显式拒绝;
  SECRET_KEY/RUSTFS_* 惰性校验,代码侧弱默认移除
- 批次 1(部署正确性):主处理链路改走 RustFS(分派入参 stp_file_id 化,
  worker 按 object_key 下载);AUTO_MIGRATE 开关 + 迁移目录 alembic/→migrations/
  修复包遮蔽(自动迁移此前从未真正生效);OCC 镜像改 conda 原生执行 +
  基础镜像 tag 锁定;compose 关键项改 ${VAR:?} 强制显式配置
- 批次 2(任务一致性):删除 Redis 进程内存回退,PG 为任务状态单一事实源;
  批量元数据入库(processing_tasks.batch_id,迁移 a3f8c2d91e47);
  型腔失败任务标 failed 不再静默 completed;事务边界收口
  (数据本体写 flush-only、失败先回滚再置 failed、进度更新保留即时 commit)
- 批次 3(API 与代码结构):592 行 advanced_router 拆为 design/cost/machining/
  export 四子路由,请求体全量 Pydantic 化;ROUTE_MODULES + route_registry
  (/api/health 呈现 degraded,DEBUG fail fast);纯计算端点统一 to_thread;
  StorageIntegrationService 按职责三拆;MAX_FILE_SIZE 接线生效、
  celery 复用 Settings.redis_url;管理员重置密码改 JSON body(端到端断裂修复);
  openapi.json 重导出(76 paths)+ 前端 gen:api
- 批次 4(架构演进):共享 ORM 按模块拆分(shared/models/base.py + identity.py、
  moldinsight/models/、inventory/models/,删除三条无使用方的跨模块
  relationship,跨模块桥接收敛为裸 FK 硬规则,无兼容 facade);
  OCC executor 重建补 cancel_futures=True(消除旧队列被慢恢复线程
  并行消化的数据竞争);OCC 吞吐方案设计先行
  (docs/topics/performance/OCC_THROUGHPUT.md);顺手清偿 D15
  (vite.config.ts 未用参数致 npm run build 失败)

测试基线:125 passed, 2 skipped(pytest + sqlite+aiosqlite;归属边界、
路由契约、配置治理、鉴权回归等随批新增)
文档同步:STATUS / TECH_DEBT / ROADMAP / ARCHITECTURE / API_CONTRACT /
OPERATIONS / AGENTS

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-09-17 16:15:49 +08:00

141 lines
5.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""批次 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.identity import User
from moldinsight.models import 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"})