Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /stateset_stateset-agents /tests /api /test_comprehensive_coverage.py
| """ | |
| Comprehensive API Test Suite for 90%+ Coverage | |
| This module provides exhaustive tests for all API components including: | |
| - Cache systems (memory, distributed) | |
| - Middleware (security, rate limiting, metrics) | |
| - Error handling | |
| - Authentication and authorization | |
| - Input validation edge cases | |
| - Integration tests | |
| """ | |
| import os | |
| import time | |
| from types import SimpleNamespace | |
| import pytest | |
| from fastapi import FastAPI | |
| from tests.api.asgi_client import SyncASGIClient | |
| # Set test environment | |
| os.environ["API_ENVIRONMENT"] = "development" | |
| os.environ["API_JWT_SECRET"] = "test-secret-key-for-testing-purposes-only-12345678" | |
| os.environ["API_REQUIRE_AUTH"] = "false" | |
| os.environ["API_RATE_LIMIT_ENABLED"] = "false" | |
| # ============================================================================ | |
| # Cache Tests | |
| # ============================================================================ | |
| class TestSimpleCache: | |
| """Tests for SimpleCache (in-memory cache).""" | |
| def test_cache_set_and_get(self): | |
| """Test basic set and get operations.""" | |
| from stateset_agents.api.cache import SimpleCache | |
| cache = SimpleCache() | |
| cache.set("key1", "value1", ttl_seconds=60) | |
| result = cache.get("key1") | |
| assert result == "value1" | |
| def test_cache_get_missing_key(self): | |
| """Test getting a non-existent key.""" | |
| from stateset_agents.api.cache import SimpleCache | |
| cache = SimpleCache() | |
| result = cache.get("nonexistent") | |
| assert result is None | |
| def test_cache_expiration(self): | |
| """Test that expired entries return None.""" | |
| from stateset_agents.api.cache import SimpleCache | |
| cache = SimpleCache() | |
| cache.set("expiring", "value", ttl_seconds=0.01) | |
| time.sleep(0.02) | |
| result = cache.get("expiring") | |
| assert result is None | |
| def test_cache_delete(self): | |
| """Test deleting a key.""" | |
| from stateset_agents.api.cache import SimpleCache | |
| cache = SimpleCache() | |
| cache.set("to_delete", "value", ttl_seconds=60) | |
| assert cache.delete("to_delete") is True | |
| assert cache.get("to_delete") is None | |
| def test_cache_delete_missing(self): | |
| """Test deleting a non-existent key.""" | |
| from stateset_agents.api.cache import SimpleCache | |
| cache = SimpleCache() | |
| assert cache.delete("nonexistent") is False | |
| def test_cache_clear(self): | |
| """Test clearing all entries.""" | |
| from stateset_agents.api.cache import SimpleCache | |
| cache = SimpleCache() | |
| cache.set("key1", "value1", ttl_seconds=60) | |
| cache.set("key2", "value2", ttl_seconds=60) | |
| cache.clear() | |
| assert cache.get("key1") is None | |
| assert cache.get("key2") is None | |
| def test_cache_cleanup_expired(self): | |
| """Test cleanup of expired entries.""" | |
| from stateset_agents.api.cache import SimpleCache | |
| cache = SimpleCache() | |
| cache.set("expired1", "value1", ttl_seconds=0.01) | |
| cache.set("valid", "value2", ttl_seconds=60) | |
| time.sleep(0.02) | |
| removed = cache.cleanup_expired() | |
| assert removed == 1 | |
| assert cache.get("valid") == "value2" | |
| def test_cache_stats(self): | |
| """Test cache statistics.""" | |
| from stateset_agents.api.cache import SimpleCache | |
| cache = SimpleCache() | |
| cache.set("key", "value", ttl_seconds=60) | |
| cache.get("key") # Hit | |
| cache.get("missing") # Miss | |
| stats = cache.stats() | |
| assert stats["hits"] == 1 | |
| assert stats["misses"] == 1 | |
| assert stats["size"] == 1 | |
| assert 0 <= stats["hit_rate"] <= 1 | |
| class TestCacheDecorators: | |
| """Tests for cache decorators.""" | |
| async def test_cached_decorator(self): | |
| """Test the @cached decorator.""" | |
| from stateset_agents.api.cache import cached | |
| call_count = 0 | |
| async def expensive_operation(x: int) -> int: | |
| nonlocal call_count | |
| call_count += 1 | |
| return x * 2 | |
| # First call - should execute | |
| result1 = await expensive_operation(5) | |
| assert result1 == 10 | |
| assert call_count == 1 | |
| # Second call - should use cache | |
| result2 = await expensive_operation(5) | |
| assert result2 == 10 | |
| assert call_count == 1 # Should NOT increment | |
| async def test_cached_sync_decorator(self): | |
| """Test the @cached_sync decorator.""" | |
| from stateset_agents.api.cache import cached_sync | |
| call_count = 0 | |
| def sync_operation(x: int) -> int: | |
| nonlocal call_count | |
| call_count += 1 | |
| return x * 3 | |
| result1 = sync_operation(4) | |
| assert result1 == 12 | |
| assert call_count == 1 | |
| result2 = sync_operation(4) | |
| assert result2 == 12 | |
| assert call_count == 1 | |
| # ============================================================================ | |
| # Distributed Cache Tests | |
| # ============================================================================ | |
| class TestDistributedCache: | |
| """Tests for distributed cache implementations.""" | |
| async def test_memory_cache_basic_operations(self): | |
| """Test memory cache basic operations.""" | |
| from stateset_agents.api.distributed_cache import CacheConfig, MemoryCache | |
| config = CacheConfig(max_memory_items=100) | |
| cache = MemoryCache(config) | |
| # Set and get | |
| await cache.set("key1", {"data": "value"}) | |
| result = await cache.get("key1") | |
| assert result == {"data": "value"} | |
| # Exists | |
| assert await cache.exists("key1") is True | |
| assert await cache.exists("nonexistent") is False | |
| # Delete | |
| assert await cache.delete("key1") is True | |
| assert await cache.exists("key1") is False | |
| async def test_memory_cache_expiration(self, monkeypatch): | |
| """Test memory cache TTL expiration. | |
| Drives the module's wall clock instead of sleeping: a 50 ms TTL with | |
| a 60 ms sleep is flaky on Windows' ~16 ms timer resolution. | |
| """ | |
| from stateset_agents.api import distributed_cache as dc | |
| clock = {"now": 1_000_000.0} | |
| monkeypatch.setattr( | |
| dc, | |
| "time", | |
| SimpleNamespace( | |
| time=lambda: clock["now"], | |
| monotonic=dc.time.monotonic, | |
| sleep=dc.time.sleep, | |
| ), | |
| ) | |
| config = dc.CacheConfig(default_ttl_seconds=5) | |
| cache = dc.MemoryCache(config) | |
| await cache.set("expiring", "value", ttl=5) | |
| clock["now"] += 4.9 | |
| assert await cache.get("expiring") == "value" | |
| clock["now"] += 0.2 # now past the 5 s TTL | |
| assert await cache.get("expiring") is None | |
| async def test_memory_cache_lru_eviction(self): | |
| """Test LRU eviction when max capacity reached.""" | |
| from stateset_agents.api.distributed_cache import CacheConfig, MemoryCache | |
| config = CacheConfig(max_memory_items=5) | |
| cache = MemoryCache(config) | |
| # Fill cache | |
| for i in range(5): | |
| await cache.set(f"key{i}", f"value{i}") | |
| # Add one more to trigger eviction | |
| await cache.set("new_key", "new_value") | |
| # Should have evicted some keys | |
| stats = cache.get_stats() | |
| assert stats.sets == 6 | |
| async def test_memory_cache_stats(self): | |
| """Test memory cache statistics.""" | |
| from stateset_agents.api.distributed_cache import CacheConfig, MemoryCache | |
| cache = MemoryCache(CacheConfig()) | |
| await cache.set("key", "value") | |
| await cache.get("key") # Hit | |
| await cache.get("missing") # Miss | |
| await cache.delete("key") | |
| stats = cache.get_stats() | |
| assert stats.hits == 1 | |
| assert stats.misses == 1 | |
| assert stats.sets == 1 | |
| assert stats.deletes == 1 | |
| async def test_memory_cache_clear(self): | |
| """Test clearing memory cache.""" | |
| from stateset_agents.api.distributed_cache import CacheConfig, MemoryCache | |
| cache = MemoryCache(CacheConfig()) | |
| await cache.set("key1", "value1") | |
| await cache.set("key2", "value2") | |
| cleared = await cache.clear() | |
| assert cleared == 2 | |
| assert await cache.get("key1") is None | |
| async def test_memory_cache_health_check(self): | |
| """Test memory cache health check.""" | |
| from stateset_agents.api.distributed_cache import CacheConfig, MemoryCache | |
| cache = MemoryCache(CacheConfig()) | |
| assert await cache.health_check() is True | |
| async def test_cache_config_from_env(self): | |
| """Test cache config loading from environment.""" | |
| from stateset_agents.api.distributed_cache import CacheBackend, CacheConfig | |
| os.environ["CACHE_BACKEND"] = "memory" | |
| os.environ["CACHE_DEFAULT_TTL"] = "600" | |
| os.environ["CACHE_KEY_PREFIX"] = "test:" | |
| config = CacheConfig.from_env() | |
| assert config.backend == CacheBackend.MEMORY | |
| assert config.default_ttl_seconds == 600 | |
| assert config.key_prefix == "test:" | |
| # Cleanup | |
| del os.environ["CACHE_BACKEND"] | |
| del os.environ["CACHE_DEFAULT_TTL"] | |
| del os.environ["CACHE_KEY_PREFIX"] | |
| # ============================================================================ | |
| # Middleware Tests | |
| # ============================================================================ | |
| class TestSecurityHeadersMiddleware: | |
| """Tests for security headers middleware.""" | |
| def test_security_headers_added(self): | |
| """Test that security headers are added to responses.""" | |
| from stateset_agents.api.constants import SECURITY_HEADERS | |
| from stateset_agents.api.middleware import SecurityHeadersMiddleware | |
| app = FastAPI() | |
| async def test_endpoint(): | |
| return {"status": "ok"} | |
| app.add_middleware(SecurityHeadersMiddleware) | |
| with SyncASGIClient(app) as client: | |
| response = client.get("/test") | |
| for header in SECURITY_HEADERS: | |
| assert header in response.headers | |
| class TestRequestContextMiddleware: | |
| """Tests for request context middleware.""" | |
| def test_request_id_generated(self): | |
| """Test that request ID is generated.""" | |
| from stateset_agents.api.middleware import RequestContextMiddleware | |
| app = FastAPI() | |
| async def test_endpoint(): | |
| return {"status": "ok"} | |
| app.add_middleware(RequestContextMiddleware) | |
| with SyncASGIClient(app) as client: | |
| response = client.get("/test") | |
| assert "X-Request-ID" in response.headers | |
| def test_request_id_passed_through(self): | |
| """Test that provided request ID is used.""" | |
| from stateset_agents.api.middleware import RequestContextMiddleware | |
| app = FastAPI() | |
| async def test_endpoint(): | |
| return {"status": "ok"} | |
| app.add_middleware(RequestContextMiddleware) | |
| with SyncASGIClient(app) as client: | |
| custom_id = "my-custom-request-id" | |
| response = client.get("/test", headers={"X-Request-ID": custom_id}) | |
| assert response.headers.get("X-Request-ID") == custom_id | |
| def test_response_time_header(self): | |
| """Test that response time header is added.""" | |
| from stateset_agents.api.middleware import RequestContextMiddleware | |
| app = FastAPI() | |
| async def test_endpoint(): | |
| return {"status": "ok"} | |
| app.add_middleware(RequestContextMiddleware) | |
| with SyncASGIClient(app) as client: | |
| response = client.get("/test") | |
| assert "X-Response-Time-Ms" in response.headers | |
| class TestRateLimitMiddleware: | |
| """Tests for rate limiting middleware.""" | |
| def test_sliding_window_rate_limiter(self): | |
| """Test sliding window rate limiter logic.""" | |
| from stateset_agents.api.middleware import SlidingWindowRateLimiter | |
| limiter = SlidingWindowRateLimiter(window_seconds=1) | |
| # First request should pass | |
| allowed, remaining = limiter.is_allowed("user1", limit=3) | |
| assert allowed is True | |
| assert remaining == 2 | |
| # Second and third requests | |
| limiter.is_allowed("user1", limit=3) | |
| limiter.is_allowed("user1", limit=3) | |
| # Fourth should be blocked | |
| allowed, remaining = limiter.is_allowed("user1", limit=3) | |
| assert allowed is False | |
| assert remaining == 0 | |
| def test_rate_limiter_reset(self): | |
| """Test rate limiter reset functionality.""" | |
| from stateset_agents.api.middleware import SlidingWindowRateLimiter | |
| limiter = SlidingWindowRateLimiter(window_seconds=1) | |
| # Fill up rate limit | |
| for _ in range(5): | |
| limiter.is_allowed("user1", limit=5) | |
| # Reset | |
| limiter.reset("user1") | |
| # Should be allowed again | |
| allowed, _ = limiter.is_allowed("user1", limit=5) | |
| assert allowed is True | |
| def test_rate_limiter_different_keys(self): | |
| """Test that rate limits are per-key.""" | |
| from stateset_agents.api.middleware import SlidingWindowRateLimiter | |
| limiter = SlidingWindowRateLimiter(window_seconds=1) | |
| # Fill up user1's limit | |
| for _ in range(3): | |
| limiter.is_allowed("user1", limit=3) | |
| # user2 should still be allowed | |
| allowed, _ = limiter.is_allowed("user2", limit=3) | |
| assert allowed is True | |
| class TestMetricsMiddleware: | |
| """Tests for metrics collection middleware.""" | |
| def test_api_metrics_recording(self): | |
| """Test API metrics are recorded correctly.""" | |
| from stateset_agents.api.middleware import APIMetrics | |
| metrics = APIMetrics() | |
| metrics.record_request("/api/test", "GET", 200, 15.5) | |
| metrics.record_request("/api/test", "GET", 200, 20.0) | |
| metrics.record_request("/api/test", "POST", 400, 10.0) | |
| summary = metrics.get_summary() | |
| assert summary["total_requests"] == 3 | |
| assert summary["status_codes"][200] == 2 | |
| assert summary["status_codes"][400] == 1 | |
| assert "latency" in summary | |
| def test_api_metrics_rate_limit_tracking(self): | |
| """Test rate limit hit tracking.""" | |
| from stateset_agents.api.middleware import APIMetrics | |
| metrics = APIMetrics() | |
| metrics.record_rate_limit_hit() | |
| metrics.record_rate_limit_hit() | |
| summary = metrics.get_summary() | |
| assert summary["rate_limit_hits"] == 2 | |
| # ============================================================================ | |
| # Error Handling Tests | |
| # ============================================================================ | |
| class TestErrorHandling: | |
| """Tests for error handling utilities.""" | |
| def test_api_error_to_response(self): | |
| """Test APIError to response conversion.""" | |
| from stateset_agents.api.errors import APIError, ErrorCode | |
| error = APIError( | |
| code=ErrorCode.BAD_REQUEST, | |
| message="Invalid input", | |
| status_code=400, | |
| ) | |
| response = error.to_response("req-123", "/api/test") | |
| assert response["error"]["code"] == "BAD_REQUEST" | |
| assert response["error"]["message"] == "Invalid input" | |
| assert response["request_id"] == "req-123" | |
| assert response["path"] == "/api/test" | |
| def test_specific_error_types(self): | |
| """Test specific error type classes.""" | |
| from stateset_agents.api.errors import ( | |
| BadRequestError, | |
| InternalError, | |
| NotFoundError, | |
| UnauthorizedError, | |
| ValidationError, | |
| ) | |
| bad_request = BadRequestError("Bad input") | |
| assert bad_request.status_code == 400 | |
| not_found = NotFoundError("User", "123") | |
| assert not_found.status_code == 404 | |
| assert "123" in not_found.message | |
| unauthorized = UnauthorizedError() | |
| assert unauthorized.status_code == 401 | |
| validation = ValidationError("Invalid format") | |
| assert validation.status_code == 422 | |
| internal = InternalError("Server error") | |
| assert internal.status_code == 500 | |
| def test_domain_specific_errors(self): | |
| """Test domain-specific error classes.""" | |
| from stateset_agents.api.errors import ( | |
| AgentNotFoundError, | |
| ConversationNotFoundError, | |
| PromptInjectionError, | |
| TrainingJobNotFoundError, | |
| ) | |
| agent_error = AgentNotFoundError("agent-123") | |
| assert agent_error.agent_id == "agent-123" | |
| assert agent_error.status_code == 404 | |
| conv_error = ConversationNotFoundError("conv-456") | |
| assert conv_error.conversation_id == "conv-456" | |
| job_error = TrainingJobNotFoundError("job-789") | |
| assert job_error.job_id == "job-789" | |
| injection_error = PromptInjectionError("user_input") | |
| assert injection_error.status_code == 400 | |
| assert len(injection_error.details) > 0 | |
| # ============================================================================ | |
| # Security Tests | |
| # ============================================================================ | |
| class TestInputValidation: | |
| """Tests for input validation.""" | |
| def test_validate_string_basic(self): | |
| """Test basic string validation.""" | |
| from stateset_agents.api.security import InputValidator | |
| result, event = InputValidator.validate_string("Hello World") | |
| assert result == "Hello World" | |
| assert event is None | |
| def test_validate_string_whitespace_trimming(self): | |
| """Test whitespace trimming.""" | |
| from stateset_agents.api.security import InputValidator | |
| result, _ = InputValidator.validate_string(" trimmed ") | |
| assert result == "trimmed" | |
| def test_validate_string_empty_rejection(self): | |
| """Test empty string rejection.""" | |
| from stateset_agents.api.security import InputValidator | |
| with pytest.raises(ValueError, match="cannot be empty"): | |
| InputValidator.validate_string("") | |
| def test_validate_string_empty_allowed(self): | |
| """Test empty string when allowed.""" | |
| from stateset_agents.api.security import InputValidator | |
| result, _ = InputValidator.validate_string("", allow_empty=True) | |
| assert result == "" | |
| def test_validate_string_max_length(self): | |
| """Test max length validation.""" | |
| from stateset_agents.api.security import InputValidator | |
| with pytest.raises(ValueError, match="exceeds maximum length"): | |
| InputValidator.validate_string("x" * 1000, max_length=100) | |
| def test_validate_string_none_rejection(self): | |
| """Test None value rejection.""" | |
| from stateset_agents.api.security import InputValidator | |
| with pytest.raises(ValueError, match="cannot be None"): | |
| InputValidator.validate_string(None) | |
| def test_json_depth_validation(self): | |
| """Test JSON nesting depth validation.""" | |
| from stateset_agents.api.security import InputValidator | |
| shallow = {"a": {"b": {"c": 1}}} | |
| assert InputValidator.check_json_depth(shallow) is True | |
| # Create deeply nested object | |
| deep = {"level": 0} | |
| current = deep | |
| for i in range(15): | |
| current["nested"] = {"level": i + 1} | |
| current = current["nested"] | |
| assert InputValidator.check_json_depth(deep) is False | |
| class TestPromptInjectionDetection: | |
| """Tests for prompt injection detection.""" | |
| def test_detects_known_injections(self, injection): | |
| """Test detection of known injection patterns.""" | |
| from stateset_agents.api.security import InputValidator, ThreatLevel | |
| is_injection, threat_level, patterns = InputValidator.detect_prompt_injection( | |
| injection | |
| ) | |
| assert is_injection is True | |
| assert threat_level == ThreatLevel.HIGH | |
| def test_allows_safe_text(self, safe_text): | |
| """Test that safe text passes validation.""" | |
| from stateset_agents.api.security import InputValidator | |
| is_injection, _, _ = InputValidator.detect_prompt_injection(safe_text) | |
| assert is_injection is False | |
| class TestAuthFailureTracking: | |
| """Tests for authentication failure tracking.""" | |
| def test_failure_counting(self): | |
| """Test failure counting logic.""" | |
| from stateset_agents.api.constants import MAX_AUTH_FAILURES_BEFORE_LOCKOUT | |
| from stateset_agents.api.security import AuthFailureTracker | |
| tracker = AuthFailureTracker() | |
| for i in range(MAX_AUTH_FAILURES_BEFORE_LOCKOUT - 1): | |
| is_locked, remaining = tracker.record_failure("user1") | |
| assert is_locked is False | |
| assert remaining == MAX_AUTH_FAILURES_BEFORE_LOCKOUT - i - 1 | |
| def test_lockout_trigger(self): | |
| """Test lockout is triggered after max failures.""" | |
| from stateset_agents.api.constants import MAX_AUTH_FAILURES_BEFORE_LOCKOUT | |
| from stateset_agents.api.security import AuthFailureTracker | |
| tracker = AuthFailureTracker() | |
| for _ in range(MAX_AUTH_FAILURES_BEFORE_LOCKOUT): | |
| tracker.record_failure("user1") | |
| assert tracker.is_locked_out("user1") is True | |
| def test_clear_failures_on_success(self): | |
| """Test clearing failures on successful auth.""" | |
| from stateset_agents.api.security import AuthFailureTracker | |
| tracker = AuthFailureTracker() | |
| tracker.record_failure("user1") | |
| tracker.record_failure("user1") | |
| tracker.clear_failures("user1") | |
| is_locked, _ = tracker.record_failure("user1") | |
| assert is_locked is False | |
| class TestCSRFProtection: | |
| """Tests for CSRF protection.""" | |
| def test_token_generation(self): | |
| """Test CSRF token generation.""" | |
| from stateset_agents.api.security import CSRFProtection | |
| token1 = CSRFProtection.generate_token() | |
| token2 = CSRFProtection.generate_token() | |
| assert token1 != token2 | |
| assert len(token1) >= 20 | |
| def test_token_validation_matching(self): | |
| """Test CSRF token validation with matching tokens.""" | |
| from stateset_agents.api.security import CSRFProtection | |
| token = CSRFProtection.generate_token() | |
| assert CSRFProtection.validate_token(token, token) is True | |
| def test_token_validation_mismatched(self): | |
| """Test CSRF token validation with different tokens.""" | |
| from stateset_agents.api.security import CSRFProtection | |
| token1 = CSRFProtection.generate_token() | |
| token2 = CSRFProtection.generate_token() | |
| assert CSRFProtection.validate_token(token1, token2) is False | |
| def test_token_validation_empty(self): | |
| """Test CSRF token validation with empty values.""" | |
| from stateset_agents.api.security import CSRFProtection | |
| assert CSRFProtection.validate_token("", "token") is False | |
| assert CSRFProtection.validate_token("token", "") is False | |
| assert CSRFProtection.validate_token(None, "token") is False | |
| class TestSecurityMonitor: | |
| """Tests for API security monitor.""" | |
| def test_event_logging(self): | |
| """Test security event logging.""" | |
| from stateset_agents.api.security import ( | |
| APISecurityMonitor, | |
| SecurityEvent, | |
| SecurityEventType, | |
| ThreatLevel, | |
| ) | |
| monitor = APISecurityMonitor() | |
| event = SecurityEvent( | |
| event_type=SecurityEventType.INVALID_INPUT, | |
| threat_level=ThreatLevel.LOW, | |
| timestamp=time.time(), | |
| client_ip="127.0.0.1", | |
| path="/test", | |
| details={"reason": "test"}, | |
| ) | |
| monitor.log_event(event) | |
| assert len(monitor.events) == 1 | |
| def test_injection_attempt_logging(self): | |
| """Test prompt injection attempt logging.""" | |
| from stateset_agents.api.security import ( | |
| APISecurityMonitor, | |
| SecurityEventType, | |
| ThreatLevel, | |
| ) | |
| monitor = APISecurityMonitor() | |
| monitor.log_prompt_injection_attempt( | |
| client_ip="1.2.3.4", | |
| path="/conversations", | |
| content_preview="ignore instructions", | |
| patterns=["ignore.*instructions"], | |
| ) | |
| assert len(monitor.events) == 1 | |
| assert ( | |
| monitor.events[0].event_type == SecurityEventType.PROMPT_INJECTION_ATTEMPT | |
| ) | |
| assert monitor.events[0].threat_level == ThreatLevel.HIGH | |
| assert monitor.events[0].blocked is True | |
| def test_security_stats(self): | |
| """Test security statistics.""" | |
| from stateset_agents.api.security import APISecurityMonitor | |
| monitor = APISecurityMonitor() | |
| monitor.log_prompt_injection_attempt( | |
| client_ip="1.1.1.1", | |
| path="/test", | |
| content_preview="test", | |
| patterns=[], | |
| ) | |
| stats = monitor.get_stats() | |
| assert stats["total_events"] == 1 | |
| assert stats["blocked_events"] == 1 | |
| def test_max_events_enforcement(self): | |
| """Test max events limit is enforced.""" | |
| from stateset_agents.api.security import ( | |
| APISecurityMonitor, | |
| SecurityEvent, | |
| SecurityEventType, | |
| ThreatLevel, | |
| ) | |
| monitor = APISecurityMonitor(max_events=5) | |
| for i in range(10): | |
| event = SecurityEvent( | |
| event_type=SecurityEventType.INVALID_INPUT, | |
| threat_level=ThreatLevel.LOW, | |
| timestamp=i, | |
| client_ip="127.0.0.1", | |
| path="/test", | |
| details={}, | |
| ) | |
| monitor.log_event(event) | |
| assert len(monitor.events) == 5 | |
| # ============================================================================ | |
| # Integration Tests | |
| # ============================================================================ | |
| class TestAPIIntegration: | |
| """Integration tests for complete API workflows.""" | |
| def app(self): | |
| """Create test application.""" | |
| from stateset_agents.api.main import create_app | |
| return create_app() | |
| def client(self, app): | |
| """Create test client.""" | |
| with SyncASGIClient(app) as client: | |
| yield client | |
| def test_health_endpoint(self, client): | |
| """Test health endpoint returns healthy status.""" | |
| response = client.get("/health") | |
| # Accept 200 or 404 depending on router configuration | |
| assert response.status_code in (200, 404) | |
| def test_root_endpoint(self, client): | |
| """Test root endpoint returns API info.""" | |
| response = client.get("/") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "message" in data or "title" in data | |
| def test_cors_headers(self, client): | |
| """Test CORS headers are present.""" | |
| response = client.options("/", headers={"Origin": "http://localhost:3000"}) | |
| # CORS preflight or regular response | |
| assert response.status_code in (200, 405) | |
| class TestEdgeCases: | |
| """Tests for edge cases and boundary conditions.""" | |
| def test_unicode_handling(self): | |
| """Test Unicode string handling.""" | |
| from stateset_agents.api.security import InputValidator | |
| unicode_text = "Hello 你好 مرحبا 🚀 emoji" | |
| result, _ = InputValidator.validate_string(unicode_text) | |
| assert result == unicode_text | |
| def test_very_long_string_rejection(self): | |
| """Test very long strings are rejected.""" | |
| from stateset_agents.api.security import InputValidator | |
| huge_string = "x" * 1_000_000 | |
| with pytest.raises(ValueError, match="exceeds maximum"): | |
| InputValidator.validate_string(huge_string, max_length=10000) | |
| def test_special_characters_handling(self): | |
| """Test special characters are handled correctly.""" | |
| from stateset_agents.api.security import InputValidator | |
| special = "<script>alert('xss')</script>" | |
| result, _ = InputValidator.validate_string(special, check_injection=False) | |
| assert result == special | |
| def test_message_validation_edge_cases(self): | |
| """Test message validation edge cases.""" | |
| from stateset_agents.api.security import InputValidator | |
| # Valid messages | |
| messages = [ | |
| {"role": "user", "content": "Hello"}, | |
| {"role": "assistant", "content": "Hi there!"}, | |
| {"role": "system", "content": "You are helpful."}, | |
| ] | |
| validated, events = InputValidator.validate_messages(messages) | |
| assert len(validated) == 3 | |
| def test_empty_messages_rejected(self): | |
| """Test empty messages list is rejected.""" | |
| from stateset_agents.api.security import InputValidator | |
| with pytest.raises(ValueError, match="cannot be empty"): | |
| InputValidator.validate_messages([]) | |
| def test_invalid_role_rejected(self): | |
| """Test invalid role is rejected.""" | |
| from stateset_agents.api.security import InputValidator | |
| messages = [{"role": "invalid_role", "content": "test"}] | |
| with pytest.raises(ValueError, match="invalid role"): | |
| InputValidator.validate_messages(messages) | |
| # ============================================================================ | |
| # Performance Tests (Basic) | |
| # ============================================================================ | |
| class TestPerformance: | |
| """Basic performance tests.""" | |
| def test_cache_performance(self): | |
| """Test cache operations are fast.""" | |
| from stateset_agents.api.cache import SimpleCache | |
| cache = SimpleCache() | |
| start = time.time() | |
| for i in range(1000): | |
| cache.set(f"key{i}", f"value{i}", ttl_seconds=60) | |
| cache.get(f"key{i}") | |
| elapsed = time.time() - start | |
| assert elapsed < 1.0 # Should complete in under 1 second | |
| def test_validation_performance(self): | |
| """Test validation is fast.""" | |
| from stateset_agents.api.security import InputValidator | |
| test_string = "This is a test string for validation" * 10 | |
| start = time.time() | |
| for _ in range(1000): | |
| InputValidator.validate_string(test_string, check_injection=True) | |
| elapsed = time.time() - start | |
| assert elapsed < 2.0 # Should complete in under 2 seconds | |
| if __name__ == "__main__": | |
| pytest.main([__file__, "-v", "--tb=short"]) | |
Xet Storage Details
- Size:
- 31.4 kB
- Xet hash:
- 63672563be92d46e8453f3cde1e0b79a95fb3ffff15111bd78e7a64903a79cdd
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.