Spaces:
Sleeping
Sleeping
File size: 16,058 Bytes
cc036ff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | """
Coverage expansion tests for workflow API routes.
Tests cover critical code paths in:
- api/workflow_template_routes.py: Workflow template CRUD, execution, import
- api/ai_workflows_routes.py: AI workflow completion, NLU parsing
- Workflow instantiation, search, and provider management
Target: Cover critical paths (happy path + error paths) to increase coverage.
Uses extensive mocking to avoid database dependencies.
"""
import pytest
from unittest.mock import Mock, patch, MagicMock
from fastapi.testclient import TestClient
from main import app
class TestWorkflowTemplateRoutesCoverage:
"""Coverage expansion for workflow template API routes."""
@pytest.fixture
def test_client(self):
"""Get FastAPI test client."""
return TestClient(app)
# Test: POST /api/workflow-templates/ - Create workflow template
@patch('api.workflow_template_routes.get_current_user')
@patch('api.workflow_template_routes.WorkflowTemplateService')
def test_create_workflow_template_success(self, mock_service_class, mock_get_user, test_client):
"""Successfully create workflow template."""
mock_user = MagicMock()
mock_user.id = "user-123"
mock_user.tenant_id = "default"
mock_get_user.return_value = mock_user
mock_template = MagicMock()
mock_template.id = "template-123"
mock_template.name = "Test Workflow"
mock_service = MagicMock()
mock_service.create_template.return_value = mock_template
mock_service_class.return_value = mock_service
response = test_client.post(
"/api/workflow-templates/",
json={
"name": "Test Workflow",
"description": "Test workflow template",
"nodes": [],
"edges": []
},
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 201, 401, 422]
def test_create_workflow_template_missing_name(self, test_client):
"""Create template without name returns validation error."""
response = test_client.post(
"/api/workflow-templates/",
json={
"description": "Template without name"
},
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 401, 422]
# Test: GET /api/workflow-templates/ - List workflow templates
@patch('api.workflow_template_routes.get_current_user')
@patch('api.workflow_template_routes.WorkflowTemplateService')
def test_list_workflow_templates_success(self, mock_service_class, mock_get_user, test_client):
"""Successfully list workflow templates."""
mock_user = MagicMock()
mock_user.id = "user-123"
mock_user.tenant_id = "default"
mock_get_user.return_value = mock_user
mock_service = MagicMock()
mock_service.list_templates.return_value = [
{"id": "template-1", "name": "Workflow 1"},
{"id": "template-2", "name": "Workflow 2"}
]
mock_service_class.return_value = mock_service
response = test_client.get(
"/api/workflow-templates/",
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 401]
@patch('api.workflow_template_routes.get_current_user')
@patch('api.workflow_template_routes.WorkflowTemplateService')
def test_list_workflow_templates_with_category_filter(self, mock_service_class, mock_get_user, test_client):
"""List templates with category filter."""
mock_user = MagicMock()
mock_user.id = "user-123"
mock_user.tenant_id = "default"
mock_get_user.return_value = mock_user
mock_service = MagicMock()
mock_service.list_templates.return_value = [
{"id": "template-1", "name": "Data Processing", "category": "data"}
]
mock_service_class.return_value = mock_service
response = test_client.get(
"/api/workflow-templates/?category=data",
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 401]
# Test: GET /api/workflow-templates/{template_id} - Get specific template
@patch('api.workflow_template_routes.get_current_user')
@patch('api.workflow_template_routes.WorkflowTemplateService')
def test_get_workflow_template_success(self, mock_service_class, mock_get_user, test_client):
"""Successfully get specific workflow template."""
mock_user = MagicMock()
mock_user.id = "user-123"
mock_user.tenant_id = "default"
mock_get_user.return_value = mock_user
mock_template = {
"id": "template-123",
"name": "Test Workflow",
"nodes": [],
"edges": []
}
mock_service = MagicMock()
mock_service.get_template.return_value = mock_template
mock_service_class.return_value = mock_service
response = test_client.get(
"/api/workflow-templates/template-123",
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 401, 404]
# Test: PUT /api/workflow-templates/{template_id} - Update template
@patch('api.workflow_template_routes.get_current_user')
@patch('api.workflow_template_routes.WorkflowTemplateService')
def test_update_workflow_template_success(self, mock_service_class, mock_get_user, test_client):
"""Successfully update workflow template."""
mock_user = MagicMock()
mock_user.id = "user-123"
mock_user.tenant_id = "default"
mock_get_user.return_value = mock_user
mock_template = MagicMock()
mock_template.id = "template-123"
mock_template.name = "Updated Workflow"
mock_service = MagicMock()
mock_service.update_template.return_value = mock_template
mock_service_class.return_value = mock_service
response = test_client.put(
"/api/workflow-templates/template-123",
json={
"name": "Updated Workflow",
"description": "Updated description"
},
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 401, 404]
# Test: POST /api/workflow-templates/{template_id}/instantiate - Instantiate template
@patch('api.workflow_template_routes.get_current_user')
@patch('api.workflow_template_routes.WorkflowTemplateService')
def test_instantiate_workflow_template_success(self, mock_service_class, mock_get_user, test_client):
"""Successfully instantiate workflow template."""
mock_user = MagicMock()
mock_user.id = "user-123"
mock_user.tenant_id = "default"
mock_get_user.return_value = mock_user
mock_workflow = MagicMock()
mock_workflow.id = "workflow-123"
mock_service = MagicMock()
mock_service.instantiate_template.return_value = mock_workflow
mock_service_class.return_value = mock_service
response = test_client.post(
"/api/workflow-templates/template-123/instantiate",
json={
"parameters": {"param1": "value1"}
},
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 201, 401, 404]
# Test: GET /api/workflow-templates/search - Search templates
@patch('api.workflow_template_routes.get_current_user')
@patch('api.workflow_template_routes.WorkflowTemplateService')
def test_search_workflow_templates_success(self, mock_service_class, mock_get_user, test_client):
"""Successfully search workflow templates."""
mock_user = MagicMock()
mock_user.id = "user-123"
mock_user.tenant_id = "default"
mock_get_user.return_value = mock_user
mock_service = MagicMock()
mock_service.search_templates.return_value = [
{"id": "template-1", "name": "Data Processing Workflow"},
{"id": "template-2", "name": "Data Analysis Workflow"}
]
mock_service_class.return_value = mock_service
response = test_client.get(
"/api/workflow-templates/search?q=data",
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 401]
# Test: POST /api/workflow-templates/{template_id}/execute - Execute template
@patch('api.workflow_template_routes.get_current_user')
@patch('api.workflow_template_routes.AgentGovernanceService')
@patch('api.workflow_template_routes.WorkflowTemplateService')
def test_execute_workflow_template_success(self, mock_service_class, mock_gov_class, mock_get_user, test_client):
"""Successfully execute workflow template."""
mock_user = MagicMock()
mock_user.id = "user-123"
mock_user.tenant_id = "default"
mock_get_user.return_value = mock_user
mock_execution = MagicMock()
mock_execution.id = "exec-123"
mock_service = MagicMock()
mock_service.execute_template.return_value = mock_execution
mock_service_class.return_value = mock_service
mock_gov = MagicMock()
mock_gov.can_perform_action.return_value = {"allowed": True, "reason": ""}
mock_gov_class.return_value = mock_gov
response = test_client.post(
"/api/workflow-templates/template-123/execute",
json={
"parameters": {"input": "test"},
"agent_id": "agent-123"
},
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 202, 401, 403]
class TestAIWorkflowsRoutesCoverage:
"""Coverage expansion for AI workflow API routes."""
@pytest.fixture
def test_client(self):
"""Get FastAPI test client."""
return TestClient(app)
# Test: POST /api/ai-workflows/nlu/parse - NLU parsing
@patch('api.ai_workflows_routes.get_current_user')
@patch('api.ai_workflows_routes.NLUService')
def test_nlu_parse_success(self, mock_service_class, mock_get_user, test_client):
"""Successfully parse natural language input."""
mock_user = MagicMock()
mock_user.id = "user-123"
mock_user.tenant_id = "default"
mock_get_user.return_value = mock_user
mock_service = MagicMock()
mock_service.parse_intent.return_value = {
"intent": "create_workflow",
"entities": {"workflow_type": "data_processing"},
"confidence": 0.95
}
mock_service_class.return_value = mock_service
response = test_client.post(
"/api/ai-workflows/nlu/parse",
json={
"text": "Create a data processing workflow",
"context": {}
},
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 401, 422]
def test_nlu_parse_missing_text(self, test_client):
"""NLU parse without text returns validation error."""
response = test_client.post(
"/api/ai-workflows/nlu/parse",
json={
"context": {}
},
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 401, 422]
# Test: GET /api/ai-workflows/providers - List AI providers
@patch('api.ai_workflows_routes.get_current_user')
def test_list_ai_providers_success(self, mock_get_user, test_client):
"""Successfully list AI workflow providers."""
mock_user = MagicMock()
mock_user.id = "user-123"
mock_user.tenant_id = "default"
mock_get_user.return_value = mock_user
response = test_client.get(
"/api/ai-workflows/providers",
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 401]
# Test: POST /api/ai-workflows/complete - AI completion
@patch('api.ai_workflows_routes.get_current_user')
@patch('api.ai_workflows_routes.LLMService')
def test_ai_completion_success(self, mock_service_class, mock_get_user, test_client):
"""Successfully generate AI completion."""
mock_user = MagicMock()
mock_user.id = "user-123"
mock_user.tenant_id = "default"
mock_get_user.return_value = mock_user
mock_service = MagicMock()
mock_service.generate_completion.return_value = {
"text": "Here is the completion",
"finish_reason": "stop",
"usage": {"total_tokens": 50}
}
mock_service_class.return_value = mock_service
response = test_client.post(
"/api/ai-workflows/complete",
json={
"prompt": "Complete this text",
"provider": "openai",
"max_tokens": 100
},
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 401, 422]
def test_ai_completion_missing_prompt(self, test_client):
"""AI completion without prompt returns validation error."""
response = test_client.post(
"/api/ai-workflows/complete",
json={
"provider": "openai"
},
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 401, 422]
class TestWorkflowRoutesErrorHandling:
"""Coverage expansion for workflow routes error handling."""
@pytest.fixture
def test_client(self):
"""Get FastAPI test client."""
return TestClient(app)
# Test: Authentication errors
def test_workflow_templates_without_auth(self, test_client):
"""Access workflow templates without authentication."""
response = test_client.get("/api/workflow-templates/")
assert response.status_code == 401
def test_create_template_without_auth(self, test_client):
"""Create template without authentication."""
response = test_client.post(
"/api/workflow-templates/",
json={"name": "Test"}
)
assert response.status_code == 401
def test_nlu_parse_without_auth(self, test_client):
"""NLU parse without authentication."""
response = test_client.post(
"/api/ai-workflows/nlu/parse",
json={"text": "test"}
)
assert response.status_code == 401
def test_ai_completion_without_auth(self, test_client):
"""AI completion without authentication."""
response = test_client.post(
"/api/ai-workflows/complete",
json={"prompt": "test"}
)
assert response.status_code == 401
# Test: Invalid JSON
def test_create_template_invalid_json(self, test_client):
"""Create template with invalid JSON."""
response = test_client.post(
"/api/workflow-templates/",
data="invalid json",
headers={"Content-Type": "application/json"}
)
assert response.status_code == 422
# Test: Missing required fields
def test_nlu_parse_empty_request(self, test_client):
"""NLU parse with empty request body."""
response = test_client.post(
"/api/ai-workflows/nlu/parse",
json={},
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 401, 422]
def test_ai_completion_empty_request(self, test_client):
"""AI completion with empty request body."""
response = test_client.post(
"/api/ai-workflows/complete",
json={},
headers={"Authorization": "Bearer test-token"}
)
assert response.status_code in [200, 401, 422]
|