- FastAPI backend with SQLModel, Alembic migrations, AgentScope agents - Next.js 15 frontend with React 19, Tailwind, Zustand, React Flow - Multi-provider AI system (DashScope, Kling, MiniMax, Volcengine, OpenAI, etc.) - All HTTP clients migrated from sync requests to async httpx - Admin-managed API keys via environment variables - SSRF vulnerability fixed in ensure_url()
80 lines
2.9 KiB
Python
80 lines
2.9 KiB
Python
"""
|
||
集成测试 - 图片生成 API (Task 5.4)
|
||
|
||
测试图片生成 API 端点的集成测试:
|
||
1. 测试使用复合 ID 生成图片成功
|
||
2. 测试无效格式返回 400
|
||
3. 测试模型不存在返回 404
|
||
"""
|
||
import pytest
|
||
import sys
|
||
import os
|
||
|
||
# 添加项目根目录到路径
|
||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
||
|
||
from fastapi.testclient import TestClient
|
||
from src.main import app
|
||
|
||
client = TestClient(app)
|
||
|
||
|
||
class TestImageGenerationAPI:
|
||
"""图片生成 API 集成测试"""
|
||
|
||
def test_generate_image_with_valid_composite_id(self):
|
||
"""测试使用有效的复合 ID 生成图片成功"""
|
||
response = client.post("/api/v1/generations/image", json={
|
||
"prompt": "a beautiful cat sitting on a windowsill",
|
||
"model": "dashscope/qwen-image",
|
||
"aspectRatio": "1:1",
|
||
"n": 1
|
||
})
|
||
|
||
# 应该返回 200 或 202(任务已创建)
|
||
assert response.status_code in [200, 202], f"Expected 200 or 202, got {response.status_code}: {response.text}"
|
||
|
||
data = response.json()
|
||
assert "data" in data, f"Response missing 'data' field: {data}"
|
||
assert "task_id" in data["data"], f"Response data missing 'task_id': {data}"
|
||
|
||
# 验证 task_id 不为空
|
||
task_id = data["data"]["task_id"]
|
||
assert task_id, "task_id should not be empty"
|
||
assert isinstance(task_id, str), "task_id should be a string"
|
||
|
||
def test_generate_image_invalid_format_no_separator(self):
|
||
"""测试无效的 model 格式(缺少分隔符)返回 400"""
|
||
response = client.post("/api/v1/generations/image", json={
|
||
"prompt": "a cat",
|
||
"model": "qwen-image" # ❌ 缺少 provider
|
||
})
|
||
|
||
# 应该返回 400 或 422(验证错误)
|
||
assert response.status_code in [400, 422], f"Expected 400 or 422, got {response.status_code}: {response.text}"
|
||
|
||
data = response.json()
|
||
# 错误消息应该提示正确的格式
|
||
error_text = str(data).lower()
|
||
assert "provider/model_key" in error_text or "format" in error_text, \
|
||
f"Error message should mention correct format: {data}"
|
||
|
||
def test_generate_image_model_not_found(self):
|
||
"""测试模型不存在返回 404"""
|
||
response = client.post("/api/v1/generations/image", json={
|
||
"prompt": "a cat",
|
||
"model": "invalid/nonexistent-model" # 不存在的模型
|
||
})
|
||
|
||
# 应该返回 404
|
||
assert response.status_code == 404, f"Expected 404, got {response.status_code}: {response.text}"
|
||
|
||
data = response.json()
|
||
# 错误消息应该提示模型未找到
|
||
error_text = str(data).lower()
|
||
assert "not found" in error_text, f"Error message should mention 'not found': {data}"
|
||
|
||
|
||
if __name__ == "__main__":
|
||
pytest.main([__file__, "-v", "--tb=short"])
|