Files
pixel/backend/tests/test_image_generation_api.py
张鹏 f9f4560459 Initial commit: Pixel AI comic/video creation platform
- 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()
2026-04-29 01:20:12 +08:00

80 lines
2.9 KiB
Python
Raw Permalink 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.
"""
集成测试 - 图片生成 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"])