- 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()
126 lines
4.7 KiB
Python
126 lines
4.7 KiB
Python
"""
|
|
Tests for Schema validation - Task 5.2
|
|
|
|
Tests for ImageGenerationRequest schema validation to ensure:
|
|
1. Accepts valid composite IDs (provider/model_key)
|
|
2. Rejects invalid formats
|
|
3. Does not accept separate provider parameter
|
|
"""
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
from src.models.schemas import ImageGenerationRequest
|
|
from src.utils.errors import InvalidParameterException
|
|
|
|
|
|
class TestImageGenerationRequestSchemaValidation:
|
|
"""Test ImageGenerationRequest schema validation for composite ID format"""
|
|
|
|
def test_accepts_valid_composite_id(self):
|
|
"""测试接受有效的复合 ID 格式"""
|
|
# Test with standard composite ID
|
|
request = ImageGenerationRequest(
|
|
prompt="a beautiful landscape",
|
|
model="dashscope/qwen-image"
|
|
)
|
|
assert request.model == "dashscope/qwen-image"
|
|
assert request.prompt == "a beautiful landscape"
|
|
|
|
# Test with different provider
|
|
request2 = ImageGenerationRequest(
|
|
prompt="a cat",
|
|
model="modelscope/qwen-image"
|
|
)
|
|
assert request2.model == "modelscope/qwen-image"
|
|
|
|
# Test with another provider
|
|
request3 = ImageGenerationRequest(
|
|
prompt="a dog",
|
|
model="volcengine/doubao-image"
|
|
)
|
|
assert request3.model == "volcengine/doubao-image"
|
|
|
|
def test_rejects_invalid_format_no_separator(self):
|
|
"""测试拒绝无效格式 - 缺少分隔符"""
|
|
with pytest.raises((ValidationError, InvalidParameterException)) as exc_info:
|
|
ImageGenerationRequest(
|
|
prompt="a cat",
|
|
model="qwen-image" # Missing provider
|
|
)
|
|
|
|
# Verify error message mentions the correct format
|
|
if isinstance(exc_info.value, ValidationError):
|
|
errors = exc_info.value.errors()
|
|
assert any("provider/model_key" in str(e.get("ctx", {})) for e in errors)
|
|
|
|
def test_rejects_invalid_format_multiple_separators(self):
|
|
"""测试拒绝无效格式 - 多个分隔符"""
|
|
with pytest.raises((ValidationError, InvalidParameterException)):
|
|
ImageGenerationRequest(
|
|
prompt="a cat",
|
|
model="dash/scope/qwen" # Too many separators
|
|
)
|
|
|
|
def test_rejects_invalid_format_empty_provider(self):
|
|
"""测试拒绝无效格式 - 空的 provider"""
|
|
with pytest.raises((ValidationError, InvalidParameterException)):
|
|
ImageGenerationRequest(
|
|
prompt="a cat",
|
|
model="/qwen-image" # Empty provider
|
|
)
|
|
|
|
def test_rejects_invalid_format_empty_model_key(self):
|
|
"""测试拒绝无效格式 - 空的 model_key"""
|
|
with pytest.raises((ValidationError, InvalidParameterException)):
|
|
ImageGenerationRequest(
|
|
prompt="a cat",
|
|
model="dashscope/" # Empty model_key
|
|
)
|
|
|
|
def test_does_not_accept_separate_provider_parameter(self):
|
|
"""测试不接受单独的 provider 参数"""
|
|
# Create a valid request
|
|
request = ImageGenerationRequest(
|
|
prompt="a cat",
|
|
model="dashscope/qwen-image"
|
|
)
|
|
|
|
# Verify provider field is not in the schema
|
|
schema_fields = ImageGenerationRequest.model_fields.keys()
|
|
assert "provider" not in schema_fields
|
|
|
|
# Verify provider is not in the dumped data
|
|
dumped = request.model_dump()
|
|
assert "provider" not in dumped
|
|
|
|
# Verify provider is not in the dumped data with aliases
|
|
dumped_with_alias = request.model_dump(by_alias=True)
|
|
assert "provider" not in dumped_with_alias
|
|
|
|
def test_complete_valid_request(self):
|
|
"""测试完整的有效请求"""
|
|
request = ImageGenerationRequest(
|
|
prompt="a beautiful sunset over mountains",
|
|
model="dashscope/qwen-image",
|
|
negativePrompt="ugly, blurry", # Use camelCase alias
|
|
imageInputs=["http://example.com/ref.jpg"], # Use camelCase alias
|
|
resolution="1080P",
|
|
aspectRatio="16:9", # Use camelCase alias
|
|
n=2,
|
|
projectId="project-123", # Use camelCase alias
|
|
source="storyboard",
|
|
sourceId="story-456", # Use camelCase alias
|
|
extraParams={"style": "anime"} # Use camelCase alias
|
|
)
|
|
|
|
assert request.model == "dashscope/qwen-image"
|
|
assert request.prompt == "a beautiful sunset over mountains"
|
|
assert request.negative_prompt == "ugly, blurry"
|
|
assert request.n == 2
|
|
assert request.project_id == "project-123"
|
|
assert "provider" not in request.model_dump()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main([__file__, "-v"])
|