2026-07-29 15:47:50 +08:00
|
|
|
|
"""统一任务接口响应结构回归测试。"""
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
from flask import Flask
|
2026-08-01 19:38:56 +08:00
|
|
|
|
import jwt
|
2026-07-29 15:47:50 +08:00
|
|
|
|
|
|
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "api"))
|
|
|
|
|
|
|
|
|
|
|
|
from insurance.generation import task_service
|
|
|
|
|
|
from insurance.generation.routes import workspace_bp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_task_detail_returns_task_as_direct_data(monkeypatch):
|
|
|
|
|
|
"""任务详情不能重复包裹 code/data,否则前端轮询读不到 status。"""
|
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
|
task_service,
|
|
|
|
|
|
"get_task",
|
|
|
|
|
|
lambda task_id, user_id: {
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"data": {"id": task_id, "userId": user_id, "status": "running"},
|
|
|
|
|
|
},
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
2026-08-01 19:38:56 +08:00
|
|
|
|
app.config["JWT_SECRET"] = "test-secret"
|
2026-07-29 15:47:50 +08:00
|
|
|
|
app.config["GUEST_MODE"] = True
|
|
|
|
|
|
app.register_blueprint(workspace_bp, url_prefix="/insurance/workspace")
|
2026-08-01 19:38:56 +08:00
|
|
|
|
token = jwt.encode({"user_id": "user-1"}, "test-secret", algorithm="HS256")
|
2026-07-29 15:47:50 +08:00
|
|
|
|
|
|
|
|
|
|
response = app.test_client().get(
|
|
|
|
|
|
"/insurance/workspace/tasks/task-1",
|
2026-08-01 19:38:56 +08:00
|
|
|
|
headers={"Authorization": f"Bearer {token}"},
|
2026-07-29 15:47:50 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
body = response.get_json()
|
|
|
|
|
|
assert body["data"]["status"] == "running"
|
|
|
|
|
|
assert "data" not in body["data"]
|
2026-08-01 19:38:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_generation_tasks_reject_guest_identity(monkeypatch):
|
|
|
|
|
|
"""生成物是账号私有数据,访客令牌不能读取任务。"""
|
|
|
|
|
|
called = False
|
|
|
|
|
|
|
|
|
|
|
|
def fake_get_task(_task_id, _user_id):
|
|
|
|
|
|
nonlocal called
|
|
|
|
|
|
called = True
|
|
|
|
|
|
return {"code": 0, "data": {}}
|
|
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(task_service, "get_task", fake_get_task)
|
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
app.config["GUEST_MODE"] = True
|
|
|
|
|
|
app.register_blueprint(workspace_bp, url_prefix="/insurance/workspace")
|
|
|
|
|
|
|
|
|
|
|
|
response = app.test_client().get(
|
|
|
|
|
|
"/insurance/workspace/tasks/task-1",
|
|
|
|
|
|
headers={"Authorization": "Bearer guest_test"},
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 401
|
|
|
|
|
|
assert response.get_json()["message"] == "请先登录账号"
|
|
|
|
|
|
assert called is False
|