Add model allow-list validation to analysis endpoints
Reject unsupported LLM model identifiers with HTTP 400 on all analysis endpoints (single, batch, async batch). The SUPPORTED_MODELS list was already defined for the /models endpoint but not enforced on incoming requests. This completes the multi-model support feature by adding the missing server-side validation. Closes leeworks-agents/SPARC#1013 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -182,3 +182,47 @@ class TestJobEndpoints:
|
||||
"""Test listing jobs with status filter."""
|
||||
response = client.get("/jobs?status=completed")
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
class TestModelValidation:
|
||||
"""Test that unsupported model identifiers are rejected."""
|
||||
|
||||
def test_analyze_rejects_unsupported_model(self, client, mock_analyzer):
|
||||
"""GET /analyze/{company} with unsupported model returns 400."""
|
||||
response = client.get("/analyze/nvidia?model=fake/nonexistent-model")
|
||||
assert response.status_code == 400
|
||||
assert "Unsupported model" in response.json()["detail"]
|
||||
|
||||
def test_analyze_accepts_supported_model(self, client, mock_analyzer):
|
||||
"""GET /analyze/{company} with a supported model succeeds."""
|
||||
mock_result = CompanyAnalysisResult(
|
||||
company_name="nvidia",
|
||||
analysis="test",
|
||||
patent_count=1,
|
||||
success=True,
|
||||
timestamp=datetime.now(),
|
||||
model="anthropic/claude-3.5-sonnet",
|
||||
)
|
||||
mock_analyzer._analyze_company_safe.return_value = mock_result
|
||||
|
||||
response = client.get("/analyze/nvidia?model=anthropic/claude-3.5-sonnet")
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_batch_rejects_unsupported_model(self, client, mock_analyzer):
|
||||
"""POST /analyze/batch with unsupported model returns 400."""
|
||||
response = client.post(
|
||||
"/analyze/batch",
|
||||
json={"companies": ["nvidia"], "model": "fake/nonexistent-model"},
|
||||
)
|
||||
assert response.status_code == 400
|
||||
assert "Unsupported model" in response.json()["detail"]
|
||||
|
||||
def test_list_models_returns_supported(self, client):
|
||||
"""GET /models returns the allow-list."""
|
||||
response = client.get("/models")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "models" in data
|
||||
assert "default" in data
|
||||
assert len(data["models"]) > 0
|
||||
assert all("id" in m and "name" in m and "provider" in m for m in data["models"])
|
||||
|
||||
Reference in New Issue
Block a user