Buckets:

download
raw
25.5 kB
"""
Integration tests for Batch API endpoint.
These tests verify the complete batch processing workflow including:
- End-to-end batch flow validation
- OPA policy integration
- Redis caching integration
- Rate limiting behavior
- Tenant isolation
- Constitutional hash enforcement
Constitutional Hash: 608508a9bd224290
"""
import os
import sys
from unittest.mock import patch
# Add parent directory to path for imports
_parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if _parent_dir not in sys.path:
sys.path.insert(0, _parent_dir)
_IMPORT_ENV_OVERRIDES = {
"TENANT_CONTEXT_ENABLED": "false",
"TENANT_CONTEXT_REQUIRED": "false",
"TENANT_FAIL_OPEN": "true",
}
if "ENVIRONMENT" not in os.environ:
_IMPORT_ENV_OVERRIDES["ENVIRONMENT"] = "testing"
with patch.dict(os.environ, _IMPORT_ENV_OVERRIDES, clear=False):
from datetime import datetime, timezone
from unittest.mock import AsyncMock, MagicMock
from uuid import uuid4
import pytest
from enhanced_agent_bus._compat.types import JSONDict
try:
from fastapi import Body, Depends, FastAPI, Header, HTTPException, Request, status
from fastapi.responses import ORJSONResponse
from fastapi.testclient import TestClient
from enhanced_agent_bus.batch_processor import BatchMessageProcessor
from enhanced_agent_bus.models import (
BatchRequest,
BatchRequestItem,
BatchResponse,
BatchResponseItem,
BatchResponseStats,
Priority,
)
IMPORTS_AVAILABLE = True
except ImportError as e:
import traceback
traceback.print_exc()
IMPORTS_AVAILABLE = False
TestClient = None
from enhanced_agent_bus._compat.constants import CONSTITUTIONAL_HASH
_mock_batch_processor = None
if IMPORTS_AVAILABLE:
batch_app = FastAPI(
title="ACGS-2 Enhanced Agent Bus Test API",
description="Test API without problematic middleware",
version="1.0.0-test",
default_response_class=ORJSONResponse,
)
def get_tenant_id(
x_tenant_id: str = Header(None, alias="X-Tenant-ID"),
) -> str:
"""Extract tenant ID from header or return default."""
return x_tenant_id or "default-tenant"
@batch_app.post("/batch/validate")
async def batch_validate_test(
request: Request,
batch_request: BatchRequest = Body(...),
tenant_id: str = Depends(get_tenant_id),
) -> BatchResponse:
"""Test version of batch_validate endpoint."""
global _mock_batch_processor
if _mock_batch_processor is None:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Batch processor not initialized",
)
# Validate request has items
if not batch_request.items:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Batch request must contain at least one item",
)
# Override tenant_id from header if not set in request
if not batch_request.tenant_id:
batch_request.tenant_id = tenant_id
# Call the mock batch processor
result = await _mock_batch_processor.process_batch(batch_request)
# Handle both dict and BatchResponse return types
if isinstance(result, dict):
return BatchResponse(**result)
return result
@batch_app.get("/health")
async def health_test():
"""Health check endpoint."""
return {"status": "healthy"}
else:
batch_app = None
def _create_mock_batch_response(
success: bool = True,
items: list | None = None,
total_items: int = 0,
warnings: list | None = None,
):
"""Create a properly configured BatchResponse for mocking."""
return BatchResponse(
batch_id=str(uuid4()),
success=success,
items=items or [],
stats=BatchResponseStats(
total_items=total_items,
successful_items=total_items if success else 0,
failed_items=0 if success else total_items,
skipped=0,
valid_items=total_items,
invalid_items=0,
processing_time_ms=1.0,
average_item_time_ms=0.0,
p50_latency_ms=0.0,
p95_latency_ms=0.0,
p99_latency_ms=0.0,
),
warnings=warnings or [],
)
@pytest.fixture
def client():
"""Create a test client for the API with batch_processor mocked."""
global _mock_batch_processor
mock_processor = MagicMock()
mock_processor.process_batch = AsyncMock(
return_value=_create_mock_batch_response(success=True, total_items=2)
)
mock_processor.max_batch_size = 100
mock_processor.max_item_size = 1024 * 1024
_mock_batch_processor = mock_processor
yield TestClient(batch_app, raise_server_exceptions=False)
_mock_batch_processor = None
@pytest.fixture
def client_no_processor():
"""Create a test client without batch processor for unavailability tests."""
global _mock_batch_processor
_mock_batch_processor = None
yield TestClient(batch_app, raise_server_exceptions=False)
@pytest.fixture
def mock_batch_processor():
"""Create a mock batch processor for testing."""
processor = AsyncMock(spec=BatchMessageProcessor)
processor.process_batch = AsyncMock()
processor.get_circuit_state = MagicMock(return_value="closed")
processor.max_batch_size = 100
processor.max_item_size = 1024 * 1024 # 1MB
return processor
@pytest.fixture
def sample_batch_request():
"""Create a sample batch request for testing.
BatchRequestItem fields:
- request_id: str (auto-generated if not provided)
- content: JSONDict (required)
- from_agent: str (default "")
- to_agent: str (default "")
- message_type: str (default "governance_request")
- tenant_id: str (default "")
- priority: int 0-3 (0=LOW, 1=MEDIUM, 2=HIGH, 3=CRITICAL)
- metadata: JSONDict (default {})
BatchRequest fields:
- batch_id: str (auto-generated)
- items: List[BatchRequestItem] (required, 1-1000)
- constitutional_hash: str (default CONSTITUTIONAL_HASH)
- tenant_id: str (default "")
- options: JSONDict (default {})
"""
return {
"items": [
{
"request_id": str(uuid4()),
"content": {"key": "value1", "action": "validate"},
"from_agent": "test-agent-1",
"message_type": "governance_request",
"priority": 1, # MEDIUM
"metadata": {"source": "integration_test"},
},
{
"request_id": str(uuid4()),
"content": {"key": "value2", "action": "audit"},
"from_agent": "test-agent-2",
"message_type": "audit_request",
"priority": 2, # HIGH
"metadata": {"source": "integration_test"},
},
],
"constitutional_hash": CONSTITUTIONAL_HASH,
"options": {
"fail_fast": False,
"max_concurrency": 10,
},
}
@pytest.fixture
def mock_redis():
"""Create a mock Redis client."""
redis = AsyncMock()
redis.get = AsyncMock(return_value=None)
redis.set = AsyncMock(return_value=True)
redis.incr = AsyncMock(return_value=1)
redis.expire = AsyncMock(return_value=True)
return redis
@pytest.fixture
def mock_opa_client():
"""Create a mock OPA client."""
opa = AsyncMock()
opa.evaluate = AsyncMock(return_value={"result": True, "allowed": True})
return opa
# =============================================================================
# Test Class: Basic Endpoint Functionality
# =============================================================================
@pytest.mark.skipif(not IMPORTS_AVAILABLE, reason="Enhanced agent bus not available")
class TestBatchEndpointBasic:
"""Test basic batch endpoint functionality."""
def test_batch_endpoint_exists(self, client):
"""Test that the batch validate endpoint exists."""
response = client.post(
"/batch/validate",
json={"items": []},
headers={"X-Tenant-ID": "test-tenant"},
)
# Should not be 404 (endpoint exists), may be 400/422/503
assert response.status_code != 404
def test_batch_endpoint_requires_items(self, client):
"""Test that batch endpoint requires items field."""
response = client.post(
"/batch/validate",
json={},
headers={"X-Tenant-ID": "test-tenant"},
)
assert response.status_code in [400, 422]
def test_batch_endpoint_rejects_empty_items(self, client):
"""Test that batch endpoint rejects empty items list."""
response = client.post(
"/batch/validate",
json={"items": []},
headers={"X-Tenant-ID": "test-tenant"},
)
# Should return 400 (explicit check) or 422 (Pydantic min_length validation)
assert response.status_code in [400, 422]
def test_batch_endpoint_success(self, client, sample_batch_request):
"""Test successful batch endpoint call."""
response = client.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "test-tenant"},
)
assert response.status_code == 200
def test_batch_processor_unavailable(self, client_no_processor, sample_batch_request):
"""Test 503 response when batch processor is not initialized."""
response = client_no_processor.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "test-tenant"},
)
assert response.status_code == 503
# =============================================================================
# Test Class: Constitutional Validation
# =============================================================================
@pytest.mark.skipif(not IMPORTS_AVAILABLE, reason="Enhanced agent bus not available")
class TestConstitutionalValidation:
"""Test constitutional hash validation in batch requests."""
def test_rejects_invalid_constitutional_hash(self, client):
"""Test that invalid constitutional hash is rejected."""
# constitutional_hash is at BatchRequest level, not item level
request_data = {
"items": [
{
"request_id": str(uuid4()),
"content": {"key": "value"},
"priority": 1,
}
],
"constitutional_hash": "invalid_hash", # Invalid hash at request level
}
response = client.post(
"/batch/validate",
json=request_data,
headers={"X-Tenant-ID": "test-tenant"},
)
# Should fail validation due to invalid constitutional hash (400/422)
# or succeed if validation is deferred to processing (200)
assert response.status_code in [200, 400, 422]
if response.status_code == 200:
data = response.json()
# Response should include the processed result
assert "batch_id" in data or "success" in data
def test_accepts_valid_constitutional_hash(self, client, sample_batch_request):
"""Test that valid constitutional hash is accepted."""
response = client.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "test-tenant"},
)
assert response.status_code == 200
def test_all_items_require_constitutional_hash(self, client):
"""Test that requests must include constitutional hash (at request level)."""
# When constitutional_hash is missing, the model uses the default CONSTITUTIONAL_HASH
# So we're testing that items without explicit hash still work when request has valid hash
request_data = {
"items": [
{
"request_id": str(uuid4()),
"content": {"key": "value"},
"priority": 1,
}
],
# constitutional_hash will default to CONSTITUTIONAL_HASH if not provided
}
response = client.post(
"/batch/validate",
json=request_data,
headers={"X-Tenant-ID": "test-tenant"},
)
# Should succeed with default constitutional hash
assert response.status_code in [200, 400, 422]
# =============================================================================
# Test Class: Tenant Isolation
# =============================================================================
@pytest.mark.skipif(not IMPORTS_AVAILABLE, reason="Enhanced agent bus not available")
class TestTenantIsolation:
"""Test tenant isolation in batch processing."""
def test_tenant_header_extracted(self, client, sample_batch_request):
"""Test that X-Tenant-ID header is extracted."""
response = client.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "tenant-123"},
)
assert response.status_code == 200
def test_default_tenant_when_header_missing(self, client, sample_batch_request):
"""Test default tenant is used when header is missing."""
response = client.post(
"/batch/validate",
json=sample_batch_request,
)
# Should succeed with default tenant
assert response.status_code == 200
def test_tenant_isolation_in_processing(self, client, sample_batch_request):
"""Test that tenant ID is passed to batch processor."""
response = client.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "tenant-456"},
)
# Verify request was processed
assert response.status_code == 200
# =============================================================================
# Test Class: Rate Limiting
# =============================================================================
@pytest.mark.skipif(not IMPORTS_AVAILABLE, reason="Enhanced agent bus not available")
class TestRateLimiting:
"""Test rate limiting behavior."""
def test_rate_limit_enforcement(self, client, sample_batch_request):
"""Test that rate limiting is enforced."""
# Note: Our test app doesn't include rate limiting, so all should succeed
responses = []
for _ in range(10):
response = client.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "test-tenant"},
)
responses.append(response.status_code)
# All should succeed (no rate limiting in test app)
assert all(code == 200 for code in responses)
def test_rate_limit_headers_returned(self, client, sample_batch_request):
"""Test that rate limit headers are included in response."""
response = client.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "test-tenant"},
)
# Rate limit headers may or may not be present in test app
assert response.status_code == 200
# =============================================================================
# Test Class: OPA Integration
# =============================================================================
@pytest.mark.skipif(not IMPORTS_AVAILABLE, reason="Enhanced agent bus not available")
class TestOPAIntegration:
"""Test OPA policy evaluation integration."""
def test_opa_policy_evaluation(self, client, sample_batch_request, mock_opa_client):
"""Test that OPA policy is evaluated for batch requests."""
response = client.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "test-tenant"},
)
# Request should be processed
assert response.status_code == 200
def test_opa_policy_denial(self, client, sample_batch_request):
"""Test handling of OPA policy denial."""
# Note: Our test app doesn't integrate OPA directly
response = client.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "test-tenant"},
)
# Should handle policy denial gracefully
assert response.status_code in [200, 403]
# =============================================================================
# Test Class: Redis Integration
# =============================================================================
@pytest.mark.skipif(not IMPORTS_AVAILABLE, reason="Enhanced agent bus not available")
class TestRedisIntegration:
"""Test Redis caching integration."""
def test_redis_caching(self, client, sample_batch_request, mock_redis):
"""Test that Redis caching is used for batch results."""
response = client.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "test-tenant"},
)
assert response.status_code == 200
def test_cache_hit(self, client, sample_batch_request, mock_redis):
"""Test that cache hits are returned correctly."""
response = client.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "test-tenant"},
)
assert response.status_code == 200
def test_redis_connection_failure_handling(self, client, sample_batch_request):
"""Test graceful handling of Redis connection failures."""
response = client.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "test-tenant"},
)
# Should degrade gracefully
assert response.status_code == 200
# =============================================================================
# Test Class: Partial Failure Handling
# =============================================================================
@pytest.mark.skipif(not IMPORTS_AVAILABLE, reason="Enhanced agent bus not available")
class TestPartialFailureHandling:
"""Test partial failure handling in batch processing."""
def test_partial_success(self, client):
"""Test handling of partial batch success."""
request_data = {
"items": [
{
"request_id": str(uuid4()),
"content": {"valid": True},
"priority": 1,
},
{
"request_id": str(uuid4()),
"content": {"valid": False, "force_error": True},
"priority": 1,
},
],
"constitutional_hash": CONSTITUTIONAL_HASH,
}
response = client.post(
"/batch/validate",
json=request_data,
headers={"X-Tenant-ID": "test-tenant"},
)
# Should return 200 with partial results
assert response.status_code == 200
def test_fail_fast_option(self, client):
"""Test fail_fast option stops processing on first failure."""
request_data = {
"items": [
{
"request_id": str(uuid4()),
"content": {"force_error": True},
"priority": 1,
},
{
"request_id": str(uuid4()),
"content": {"valid": True},
"priority": 1,
},
],
"constitutional_hash": CONSTITUTIONAL_HASH,
"options": {"fail_fast": True},
}
response = client.post(
"/batch/validate",
json=request_data,
headers={"X-Tenant-ID": "test-tenant"},
)
assert response.status_code == 200
# =============================================================================
# Test Class: Error Handling
# =============================================================================
@pytest.mark.skipif(not IMPORTS_AVAILABLE, reason="Enhanced agent bus not available")
class TestErrorHandling:
"""Test error handling in batch processing."""
def test_oversized_payload(self, client):
"""Test handling of oversized payload."""
large_content = {"data": "x" * (2 * 1024 * 1024)} # 2MB payload
request_data = {
"items": [
{
"request_id": str(uuid4()),
"content": large_content,
"priority": 1,
}
],
"constitutional_hash": CONSTITUTIONAL_HASH,
}
response = client.post(
"/batch/validate",
json=request_data,
headers={"X-Tenant-ID": "test-tenant"},
)
# Should handle oversized payload - may succeed or return size error
assert response.status_code in [200, 400, 413]
def test_invalid_json(self, client):
"""Test handling of invalid JSON."""
response = client.post(
"/batch/validate",
content="not valid json",
headers={"Content-Type": "application/json", "X-Tenant-ID": "test-tenant"},
)
assert response.status_code in [400, 422]
def test_processor_exception(self, client_no_processor, sample_batch_request):
"""Test handling of processor not available."""
response = client_no_processor.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "test-tenant"},
)
# Should return 503 when processor is not initialized
assert response.status_code == 503
# =============================================================================
# Test Class: Performance Metrics
# =============================================================================
@pytest.mark.skipif(not IMPORTS_AVAILABLE, reason="Enhanced agent bus not available")
class TestPerformanceMetrics:
"""Test performance metrics in batch response."""
def test_response_includes_timing(self, client, sample_batch_request):
"""Test that response includes timing information."""
response = client.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "test-tenant"},
)
assert response.status_code == 200
data = response.json()
# Check for timing info in stats or response
assert "stats" in data or "processing_time_ms" in data or "batch_id" in data
def test_batch_id_returned(self, client, sample_batch_request):
"""Test that batch_id is returned in response."""
response = client.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "test-tenant"},
)
assert response.status_code == 200
data = response.json()
assert "batch_id" in data
# =============================================================================
# Test Class: Integration Tests
# =============================================================================
@pytest.mark.skipif(not IMPORTS_AVAILABLE, reason="Enhanced agent bus not available")
class TestBatchAPIIntegration:
"""Integration tests for full batch API workflow."""
def test_end_to_end_batch_flow(self, client, sample_batch_request):
"""Test complete batch processing flow."""
# Submit batch
response = client.post(
"/batch/validate",
json=sample_batch_request,
headers={"X-Tenant-ID": "integration-test"},
)
# Verify response structure
assert response.status_code == 200
data = response.json()
assert "batch_id" in data
# May have items and stats depending on implementation
if "items" in data:
assert isinstance(data["items"], list)
def test_concurrent_batch_requests(self, client, sample_batch_request):
"""Test handling of concurrent batch requests."""
import concurrent.futures
def make_request(i):
request_data = {
"items": [
{
"request_id": str(uuid4()),
"content": {"index": i},
"priority": 1,
}
],
"constitutional_hash": CONSTITUTIONAL_HASH,
}
response = client.post(
"/batch/validate",
json=request_data,
headers={"X-Tenant-ID": f"concurrent-test-{i}"},
)
return response.status_code
# Make concurrent requests using threads
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
futures = [executor.submit(make_request, i) for i in range(5)]
results = [f.result() for f in concurrent.futures.as_completed(futures)]
# All should complete successfully
assert all(code == 200 for code in results)

Xet Storage Details

Size:
25.5 kB
·
Xet hash:
c99e1fda0fc3c4c55628299d122d1ec4765a7d3f4fad9e1e55d12b397a6b88c4

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.