92b062c048
- 新增 app/api/deps.py、app/core/users.py、app/core/sessions.py:会话鉴权依赖、 用户存储(PBKDF2-HMAC-SHA256 + 随机 salt,Redis/内存降级)、会话签发与校验(TTL 12h) - auth.py 新增用户管理端点(列表/创建/重置密码/删除)与 admin/user 角色权限边界, user 访问用户管理返回 1006,禁删自己与最后一个 admin - admin.html 新增用户管理面板(仅 admin 挂载)与 API 指南在线测试台 - Dockerfile 将 uv 放入 PATH;docker-compose 调整 qdrant 依赖为 service_started 并移除依赖 curl 的 healthcheck(官方镜像不含 curl) - 新增用户管理测试(users/sessions/auth_api/auth_integration),全量 461 项测试通过 Co-Authored-By: WorkBuddy <workbuddy@tencent.com>
76 lines
2.7 KiB
Python
76 lines
2.7 KiB
Python
"""文档入库 API 测试(任务管理器为 FakeManager,不真实联网)"""
|
||
|
||
from collections.abc import Iterator
|
||
|
||
import pytest
|
||
from fastapi.testclient import TestClient
|
||
|
||
from app.api.v1 import document as document_module
|
||
from app.main import app
|
||
from app.models.document import DocumentInput
|
||
from app.services.qdrant import QdrantService
|
||
|
||
|
||
class FakeManager:
|
||
"""假入库任务管理器:记录 submit 调用并返回固定 task_id"""
|
||
|
||
def __init__(self, task_id: str = "task-1") -> None:
|
||
self.task_id = task_id
|
||
self.submitted: list[DocumentInput] = []
|
||
|
||
async def submit(self, doc: DocumentInput) -> str:
|
||
self.submitted.append(doc)
|
||
return self.task_id
|
||
|
||
|
||
@pytest.fixture
|
||
def client(monkeypatch: pytest.MonkeyPatch, admin_headers: dict[str, str]) -> Iterator[TestClient]:
|
||
"""TestClient,lifespan 中的 Qdrant 集合初始化替换为空操作;默认携带 admin 认证头"""
|
||
|
||
async def _noop_ensure_collections(self: QdrantService) -> None:
|
||
return None
|
||
|
||
monkeypatch.setattr(QdrantService, "ensure_collections", _noop_ensure_collections)
|
||
with TestClient(app) as test_client:
|
||
test_client.headers.update(admin_headers)
|
||
yield test_client
|
||
|
||
|
||
def test_ingest_submit_accepted(client: TestClient, monkeypatch: pytest.MonkeyPatch) -> None:
|
||
"""正常提交:HTTP 202,code=0,data 含 task_id 与 status=pending,文档已登记给管理器"""
|
||
fake = FakeManager(task_id="task-abc")
|
||
monkeypatch.setattr(document_module, "_get_task_manager", lambda: fake)
|
||
|
||
resp = client.post("/api/v1/documents", json={"text": "正文内容", "title": "标题"})
|
||
|
||
assert resp.status_code == 202
|
||
body = resp.json()
|
||
assert body["code"] == 0
|
||
data = body["data"]
|
||
assert data["task_id"] == "task-abc"
|
||
assert data["status"] == "pending"
|
||
assert [d.title for d in fake.submitted] == ["标题"]
|
||
|
||
|
||
def test_ingest_empty_text(client: TestClient) -> None:
|
||
"""空 text:路由内校验,返回 code=1001"""
|
||
resp = client.post("/api/v1/documents", json={"text": ""})
|
||
|
||
body = resp.json()
|
||
assert body["code"] == 1001
|
||
assert body["message"] == "文档内容不能为空"
|
||
assert body["data"] is None
|
||
|
||
|
||
def test_ingest_no_sync_ingestion_error(client: TestClient, monkeypatch: pytest.MonkeyPatch) -> None:
|
||
"""任务模式下同步路径不再返回 2000:提交即 202,入库失败体现在任务状态中"""
|
||
fake = FakeManager()
|
||
monkeypatch.setattr(document_module, "_get_task_manager", lambda: fake)
|
||
|
||
resp = client.post("/api/v1/documents", json={"text": "正文内容"})
|
||
|
||
assert resp.status_code == 202
|
||
body = resp.json()
|
||
assert body["code"] == 0
|
||
assert body["data"]["status"] == "pending"
|