90 lines
3.3 KiB
Python
90 lines
3.3 KiB
Python
|
|
# ============================================================
|
|||
|
|
# Web 本地工程数据文件上传黄金测试(AG-08 扩展)
|
|||
|
|
# 覆盖:multipart 上传 → 项目 workDir 自动落位 → 目录分析可见
|
|||
|
|
# ============================================================
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import os
|
|||
|
|
|
|||
|
|
import pytest
|
|||
|
|
from fastapi.testclient import TestClient
|
|||
|
|
|
|||
|
|
from server.aps_domain.folder_pack import analyze_work_dir
|
|||
|
|
from server.state.seed import seed_world
|
|||
|
|
from tests.auth_provider import install_test_auth
|
|||
|
|
|
|||
|
|
|
|||
|
|
@pytest.fixture()
|
|||
|
|
def secure_app(tmp_path, monkeypatch):
|
|||
|
|
monkeypatch.setenv("APS_DB_PATH", str(tmp_path / "tenant.db"))
|
|||
|
|
monkeypatch.setenv("APS_WORLD_PATH", str(tmp_path / "world.json"))
|
|||
|
|
monkeypatch.setenv("APS_DATA_DIR", str(tmp_path / "aps-data"))
|
|||
|
|
import server.state.store as world_store
|
|||
|
|
from server.db.database import reset_engine
|
|||
|
|
|
|||
|
|
install_test_auth(monkeypatch, "tenant-a-000000000000000000000001")
|
|||
|
|
world_store._stores.clear()
|
|||
|
|
reset_engine()
|
|||
|
|
from server.gateway.app import create_app
|
|||
|
|
app = create_app()
|
|||
|
|
yield app
|
|||
|
|
reset_engine()
|
|||
|
|
world_store._stores.clear()
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _login(client: TestClient) -> None:
|
|||
|
|
response = client.post("/api/auth/login", json={
|
|||
|
|
"method": "password", "username": "planner", "password": "test",
|
|||
|
|
})
|
|||
|
|
assert response.status_code == 200
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_upload_local_files_then_folder_analysis(secure_app, monkeypatch):
|
|||
|
|
client = TestClient(secure_app)
|
|||
|
|
_login(client)
|
|||
|
|
created = client.post("/api/projects", json={"name": "浏览器项目"}).json()
|
|||
|
|
pid = created["project"]["id"]
|
|||
|
|
sid = created["session"]["id"]
|
|||
|
|
|
|||
|
|
csv = "订单号,客户,产品编码,数量,交期\nSO-DEMO-1,比亚迪,A0050101-00280,1,2026-08-30\n"
|
|||
|
|
response = client.post(
|
|||
|
|
f"/api/projects/{pid}/files/upload",
|
|||
|
|
files=[("files", ("订单-样例.csv", csv.encode("utf-8"), "text/csv"))],
|
|||
|
|
)
|
|||
|
|
assert response.status_code == 200, response.text
|
|||
|
|
body = response.json()
|
|||
|
|
assert body["saved"] == ["订单-样例.csv"]
|
|||
|
|
assert body["workDir"]
|
|||
|
|
assert os.path.isfile(os.path.join(body["workDir"], "订单-样例.csv"))
|
|||
|
|
|
|||
|
|
snap = client.get("/api/workspace").json()
|
|||
|
|
project = next(p for p in snap["projects"] if p["id"] == pid)
|
|||
|
|
assert project["workDir"] == body["workDir"]
|
|||
|
|
assert any(f["projectId"] == pid and f["name"] == "订单-样例.csv" for f in snap["files"])
|
|||
|
|
|
|||
|
|
class FakePS:
|
|||
|
|
def snapshot(self, include_messages=False):
|
|||
|
|
return {
|
|||
|
|
"projects": [{"id": pid, "name": "浏览器项目", "workDir": body["workDir"]}],
|
|||
|
|
"sessions": [{"id": sid, "projectId": pid}],
|
|||
|
|
"files": [],
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
monkeypatch.setattr("server.state.projects.get_project_store", lambda: FakePS())
|
|||
|
|
report = analyze_work_dir(seed_world(), sid)
|
|||
|
|
assert report["ok"] is True
|
|||
|
|
assert any(f["name"] == "订单-样例.csv" for f in report["files"])
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_upload_rejects_unsupported_extension(secure_app):
|
|||
|
|
client = TestClient(secure_app)
|
|||
|
|
_login(client)
|
|||
|
|
created = client.post("/api/projects", json={"name": "拒绝测试"}).json()
|
|||
|
|
pid = created["project"]["id"]
|
|||
|
|
response = client.post(
|
|||
|
|
f"/api/projects/{pid}/files/upload",
|
|||
|
|
files=[("files", ("说明.docx", b"docx", "application/octet-stream"))],
|
|||
|
|
)
|
|||
|
|
assert response.status_code == 400
|
|||
|
|
assert "仅支持" in response.json()["detail"]
|