Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

test: remove 2 shadow files in database/ and security/ (#4124)

LearningCircuit57e1d267d30c
diff --git a/tests/database/test_encrypted_db_extended.py b/tests/database/test_encrypted_db_extended.py
deleted file mode 100644
index c3fee01305..0000000000
--- a/tests/database/test_encrypted_db_extended.py
+++ /dev/null
@@ -1,455 +0,0 @@
-"""
-Tests for encrypted database extended functionality.
-
-Tests cover:
-- Thread local engine management
-- SQLCipher pragma configuration
-- Pool management
-"""
-
-from unittest.mock import Mock
-import threading
-import time
-
-
-class TestThreadLocalEngineManagement:
- """Tests for thread local engine management."""
-
- def test_thread_local_engine_creation(self):
- """Thread local engine is created on first access."""
- thread_local = threading.local()
-
- if not hasattr(thread_local, "engine"):
- thread_local.engine = Mock(name="engine")
-
- assert hasattr(thread_local, "engine")
-
- def test_thread_local_engine_isolation(self):
- """Engines are isolated between threads."""
- thread_local = threading.local()
- results = {}
-
- def set_engine(thread_id, engine_value):
- thread_local.engine = engine_value
- time.sleep(0.01) # Allow other thread to run
- results[thread_id] = thread_local.engine
-
- t1 = threading.Thread(target=set_engine, args=(1, "engine_1"))
- t2 = threading.Thread(target=set_engine, args=(2, "engine_2"))
-
- t1.start()
- t2.start()
- t1.join()
- t2.join()
-
- assert results[1] == "engine_1"
- assert results[2] == "engine_2"
-
- def test_thread_local_engine_reuse(self):
- """Engine is reused within same thread."""
- thread_local = threading.local()
-
- thread_local.engine = Mock(name="engine")
- first_access = thread_local.engine
-
- second_access = thread_local.engine
-
- assert first_access is second_access
-
- def test_thread_local_engine_cleanup(self):
- """Engine is cleaned up on thread exit."""
- cleaned_up = {"value": False}
- thread_local = threading.local()
-
- def worker():
- thread_local.engine = Mock(name="engine")
- # Simulate cleanup
- del thread_local.engine
- cleaned_up["value"] = True
-
- t = threading.Thread(target=worker)
- t.start()
- t.join()
-
- assert cleaned_up["value"]
-
- def test_thread_local_multiple_threads(self):
- """Multiple threads have independent storage."""
- thread_local = threading.local()
- results = {}
-
- def worker(thread_id):
- thread_local.value = thread_id * 10
- time.sleep(0.01)
- results[thread_id] = thread_local.value
-
- threads = [threading.Thread(target=worker, args=(i,)) for i in range(5)]
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- for i in range(5):
- assert results[i] == i * 10
-
- def test_thread_local_concurrent_access(self):
- """Concurrent access to thread local is safe."""
- thread_local = threading.local()
- errors = []
-
- def worker():
- try:
- for i in range(100):
- thread_local.counter = i
- _ = thread_local.counter
- except Exception as e:
- errors.append(e)
-
- threads = [threading.Thread(target=worker) for _ in range(10)]
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert len(errors) == 0
-
- def test_thread_local_session_binding(self):
- """Session is bound to thread local engine."""
- thread_local = threading.local()
- thread_local.engine = Mock()
- thread_local.session = Mock()
-
- thread_local.session.bind = thread_local.engine
-
- assert thread_local.session.bind is thread_local.engine
-
- def test_thread_local_transaction_isolation(self):
- """Transactions are isolated between threads."""
- transactions = {}
- lock = threading.Lock()
-
- def worker(thread_id):
- # Simulate transaction
- transaction = {"id": thread_id, "committed": False}
- time.sleep(0.01)
- transaction["committed"] = True
- with lock:
- transactions[thread_id] = transaction
-
- threads = [threading.Thread(target=worker, args=(i,)) for i in range(3)]
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- for i in range(3):
- assert transactions[i]["committed"]
- assert transactions[i]["id"] == i
-
- def test_thread_local_error_recovery(self):
- """Thread local recovers from errors."""
- thread_local = threading.local()
-
- try:
- thread_local.value = "test"
- raise ValueError("Test error")
- except ValueError:
- pass
-
- # Thread local should still work
- thread_local.value = "recovered"
- assert thread_local.value == "recovered"
-
- def test_thread_local_memory_management(self):
- """Thread local doesn't leak memory."""
- thread_local = threading.local()
- large_data = "x" * 10000
-
- thread_local.data = large_data
- del thread_local.data
-
- assert not hasattr(thread_local, "data")
-
-
-class TestSQLCipherPragma:
- """Tests for SQLCipher pragma configuration."""
-
- def test_sqlcipher_pragma_application(self):
- """Pragma statements are applied to connection."""
- pragmas = [
- "PRAGMA key = 'secret_key'",
- "PRAGMA cipher_page_size = 4096",
- ]
-
- applied = []
- for pragma in pragmas:
- applied.append(pragma)
-
- assert len(applied) == 2
-
- def test_sqlcipher_pragma_key_setting(self):
- """Encryption key pragma is set."""
- key = "my_secret_key"
-
- pragma = f"PRAGMA key = '{key}'"
-
- assert "my_secret_key" in pragma
-
- def test_sqlcipher_pragma_cipher_settings(self):
- """Cipher settings are configured."""
- settings = {
- "cipher": "aes-256-cbc",
- "kdf_iter": 256000,
- "cipher_page_size": 4096,
- }
-
- pragmas = [
- f"PRAGMA cipher = '{settings['cipher']}'",
- f"PRAGMA kdf_iter = {settings['kdf_iter']}",
- f"PRAGMA cipher_page_size = {settings['cipher_page_size']}",
- ]
-
- assert len(pragmas) == 3
- assert "256000" in pragmas[1]
-
- def test_sqlcipher_pragma_kdf_iterations(self):
- """KDF iterations are set correctly."""
- kdf_iter = 256000
-
- pragma = f"PRAGMA kdf_iter = {kdf_iter}"
-
- assert "256000" in pragma
-
- def test_sqlcipher_pragma_page_size(self):
- """Page size pragma is configured."""
- page_size = 4096
-
- pragma = f"PRAGMA cipher_page_size = {page_size}"
-
- assert "4096" in pragma
-
- def test_sqlcipher_pragma_journal_mode(self):
- """Journal mode is set."""
- journal_mode = "WAL"
-
- pragma = f"PRAGMA journal_mode = {journal_mode}"
-
- assert journal_mode in pragma
-
- def test_sqlcipher_pragma_synchronous(self):
- """Synchronous pragma is configured."""
- sync_mode = "NORMAL"
-
- pragma = f"PRAGMA synchronous = {sync_mode}"
-
- assert sync_mode in pragma
-
- def test_sqlcipher_unavailable_fallback(self):
- """Fallback when SQLCipher unavailable."""
- sqlcipher_available = False
-
- if not sqlcipher_available:
- engine_type = "sqlite3"
- encryption_enabled = False
- else:
- engine_type = "sqlcipher"
- encryption_enabled = True
-
- assert engine_type == "sqlite3"
- assert not encryption_enabled
-
-
-class TestPoolManagement:
- """Tests for connection pool management."""
-
- def test_pool_exhaustion_scenario(self):
- """Pool exhaustion is handled."""
- pool_size = 5
- active_connections = 5
-
- pool_available = pool_size - active_connections
-
- if pool_available <= 0:
- wait_for_connection = True
- else:
- wait_for_connection = False
-
- assert wait_for_connection
-
- def test_pool_connection_recycling(self):
- """Connections are recycled after use."""
- connections = []
- pool_size = 3
-
- # Acquire and release
- for _ in range(pool_size):
- conn = Mock()
- connections.append(conn)
-
- # Return to pool
- for conn in connections:
- conn.close = Mock()
-
- # Connections should be reusable
- assert len(connections) == pool_size
-
- def test_pool_timeout_handling(self):
- """Pool timeout raises exception."""
- pool_timeout = 30
- wait_time = 35
-
- if wait_time > pool_timeout:
- timed_out = True
- else:
- timed_out = False
-
- assert timed_out
-
- def test_pool_leak_prevention(self):
- """Connection leaks are detected."""
- checked_out = {"count": 0}
- returned = {"count": 0}
-
- # Simulate checkout
- checked_out["count"] += 5
-
- # Simulate return
- returned["count"] += 4
-
- leaked = checked_out["count"] - returned["count"]
-
- assert leaked == 1
-
- def test_pool_max_overflow(self):
- """Max overflow connections are allowed."""
- pool_size = 5
- max_overflow = 10
- current_connections = 12
-
- within_limits = current_connections <= (pool_size + max_overflow)
-
- assert within_limits
-
- def test_pool_pre_ping(self):
- """Pre-ping validates connections."""
- connection = Mock()
- connection.is_valid = Mock(return_value=True)
-
- # Pre-ping check
- is_valid = connection.is_valid()
-
- assert is_valid
-
- def test_pool_connection_invalidation(self):
- """Invalid connections are removed from pool."""
- pool = [Mock(valid=True), Mock(valid=False), Mock(valid=True)]
-
- valid_connections = [c for c in pool if c.valid]
-
- assert len(valid_connections) == 2
-
-
-class TestDatabaseEncryption:
- """Tests for database encryption handling."""
-
- def test_encryption_key_from_password(self):
- """Encryption key is derived from password."""
- password = "user_password"
-
- # Simulate key derivation
- import hashlib
-
- key = hashlib.sha256(password.encode()).hexdigest()
-
- assert len(key) == 64
-
- def test_encryption_key_caching(self):
- """Encryption keys are cached per user."""
- key_cache = {}
- username = "testuser"
- password = "password123"
-
- if username not in key_cache:
- import hashlib
-
- key_cache[username] = hashlib.sha256(password.encode()).hexdigest()
-
- cached_key = key_cache.get(username)
-
- assert cached_key is not None
-
- def test_encryption_rekey_database(self):
- """Database can be rekeyed."""
- old_key = "old_secret"
- new_key = "new_secret"
-
- pragmas = [
- f"PRAGMA key = '{old_key}'",
- f"PRAGMA rekey = '{new_key}'",
- ]
-
- assert len(pragmas) == 2
- assert "rekey" in pragmas[1]
-
- def test_encryption_verify_key(self):
- """Key verification check."""
- # Simulate key verification by querying
- # Would execute: SELECT count(*) FROM sqlite_master
- result = 1
- key_valid = result >= 0
-
- assert key_valid
-
- def test_encryption_wrong_key_handling(self):
- """Wrong encryption key is detected."""
- correct_key = "correct_key"
- provided_key = "wrong_key"
-
- key_matches = correct_key == provided_key
-
- assert not key_matches
-
-
-class TestEncryptedDbExceptionLogging:
- """Tests for improved exception logging in encrypted_db (PR #2006).
-
- PR #2006 adds logger.debug() to exception handlers for:
- - Temp file cleanup (OSError)
- - Pragma application failures
- - Stale engine disposal
- """
-
- def test_os_error_is_subclass_hierarchy(self):
- """OSError catches FileNotFoundError and PermissionError."""
- assert issubclass(FileNotFoundError, OSError)
- assert issubclass(PermissionError, OSError)
-
- def test_pragma_failure_caught_by_exception(self):
- """Pragma execution errors are caught by except Exception."""
- errors = []
- pragmas = ["PRAGMA invalid_pragma = 1", "PRAGMA valid = 1"]
-
- for pragma in pragmas:
- try:
- if "invalid" in pragma:
- raise Exception(f"Could not apply pragma '{pragma}'")
- except Exception as e:
- errors.append(str(e))
-
- assert len(errors) == 1
- assert "invalid" in errors[0]
-
- def test_engine_dispose_error_caught(self):
- """Engine disposal errors are caught and logged."""
- from unittest.mock import Mock
-
- mock_engine = Mock()
- mock_engine.dispose.side_effect = Exception("Connection pool error")
-
- caught = False
- try:
- mock_engine.dispose()
- except Exception:
- caught = True
-
- assert caught
diff --git a/tests/security/test_rate_limiter_extended.py b/tests/security/test_rate_limiter_extended.py
deleted file mode 100644
index 41ded124f4..0000000000
--- a/tests/security/test_rate_limiter_extended.py
+++ /dev/null
@@ -1,602 +0,0 @@
-"""
-Extended tests for rate_limiter module - Comprehensive rate limiting coverage.
-
-Tests cover:
-- Sliding window and token bucket patterns
-- Per-endpoint and per-user rate limiting
-- Concurrent request handling
-- Rate limit headers and retry-after
-- Distributed rate limiting scenarios
-- Edge cases and error conditions
-"""
-
-import time
-import threading
-
-
-class TestRateLimitSlidingWindow:
- """Tests for sliding window rate limiting behavior."""
-
- def test_rate_limit_sliding_window_basic(self):
- """Sliding window should track requests over time."""
- window = {"requests": [], "window_size": 60}
-
- def add_request():
- now = time.time()
- # Remove old requests
- window["requests"] = [
- t for t in window["requests"] if now - t < window["window_size"]
- ]
- window["requests"].append(now)
- return len(window["requests"])
-
- # Add requests
- count1 = add_request()
- count2 = add_request()
-
- assert count1 == 1
- assert count2 == 2
-
- def test_rate_limit_sliding_window_expiry(self):
- """Old requests should expire from sliding window."""
- window = {"requests": [], "window_size": 0.1} # 100ms window
-
- def add_request():
- now = time.time()
- window["requests"] = [
- t for t in window["requests"] if now - t < window["window_size"]
- ]
- window["requests"].append(now)
- return len(window["requests"])
-
- add_request()
- time.sleep(0.15) # allow: unmarked-sleep # Wait for window to expire
- count = add_request()
-
- assert count == 1 # Old request should have expired
-
-
-class TestRateLimitTokenBucket:
- """Tests for token bucket rate limiting pattern."""
-
- def test_token_bucket_basic(self):
- """Token bucket should replenish tokens over time."""
- bucket = {
- "tokens": 10,
- "max_tokens": 10,
- "refill_rate": 1.0, # 1 token per second
- "last_refill": time.time(),
- }
-
- def consume_token():
- now = time.time()
- elapsed = now - bucket["last_refill"]
- bucket["tokens"] = min(
- bucket["max_tokens"],
- bucket["tokens"] + elapsed * bucket["refill_rate"],
- )
- bucket["last_refill"] = now
-
- if bucket["tokens"] >= 1:
- bucket["tokens"] -= 1
- return True
- return False
-
- # Consume all tokens
- for _ in range(10):
- assert consume_token() is True
-
- # Should be denied
- assert consume_token() is False
-
- def test_token_bucket_refill(self):
- """Token bucket should refill over time."""
- bucket = {
- "tokens": 0,
- "max_tokens": 10,
- "refill_rate": 100.0, # Fast refill for testing
- "last_refill": time.time(),
- }
-
- time.sleep(0.05) # Wait for some refill
-
- now = time.time()
- elapsed = now - bucket["last_refill"]
- bucket["tokens"] = min(
- bucket["max_tokens"],
- bucket["tokens"] + elapsed * bucket["refill_rate"],
- )
-
- assert bucket["tokens"] > 0
-
-
-class TestPerEndpointRateLimiting:
- """Tests for per-endpoint rate limiting."""
-
- def test_per_endpoint_limits_isolation(self):
- """Different endpoints should have separate limits."""
- endpoint_limits = {
- "/api/upload": {"limit": 10, "used": 0},
- "/api/search": {"limit": 100, "used": 0},
- "/api/download": {"limit": 50, "used": 0},
- }
-
- def request(endpoint):
- if (
- endpoint_limits[endpoint]["used"]
- < endpoint_limits[endpoint]["limit"]
- ):
- endpoint_limits[endpoint]["used"] += 1
- return True
- return False
-
- # Exhaust upload limit
- for _ in range(10):
- assert request("/api/upload") is True
- assert request("/api/upload") is False
-
- # Search should still work
- assert request("/api/search") is True
-
- def test_per_endpoint_default_limits(self):
- """Unknown endpoints should use default limits."""
- endpoint_limits = {}
- default_limit = 60
-
- def get_limit(endpoint):
- return endpoint_limits.get(endpoint, {"limit": default_limit})[
- "limit"
- ]
-
- assert get_limit("/api/unknown") == default_limit
-
-
-class TestPerUserRateLimiting:
- """Tests for per-user rate limiting."""
-
- def test_per_user_limits_isolation(self):
- """Different users should have separate limits."""
- user_limits = {}
- limit_per_user = 10
-
- def request(user_id):
- if user_id not in user_limits:
- user_limits[user_id] = {"used": 0, "limit": limit_per_user}
-
- if user_limits[user_id]["used"] < user_limits[user_id]["limit"]:
- user_limits[user_id]["used"] += 1
- return True
- return False
-
- # User1 exhausts their limit
- for _ in range(10):
- assert request("user1") is True
- assert request("user1") is False
-
- # User2 should still work
- assert request("user2") is True
-
- def test_per_user_anonymous_fallback(self):
- """Anonymous users should fall back to IP-based limiting."""
-
- def get_identifier(user_id, ip_address):
- if user_id:
- return f"user:{user_id}"
- return f"ip:{ip_address}"
-
- assert get_identifier("testuser", "192.168.1.1") == "user:testuser"
- assert get_identifier(None, "192.168.1.1") == "ip:192.168.1.1"
-
-
-class TestGlobalRateLimiting:
- """Tests for global rate limiting."""
-
- def test_global_rate_limit(self):
- """Global limit should apply across all users."""
- global_state = {"total_requests": 0, "limit": 1000}
-
- def request():
- if global_state["total_requests"] < global_state["limit"]:
- global_state["total_requests"] += 1
- return True
- return False
-
- # Simulate many requests
- for _ in range(1000):
- assert request() is True
- assert request() is False
-
-
-class TestBurstHandling:
- """Tests for burst request handling."""
-
- def test_burst_handling_with_burst_capacity(self):
- """System should handle bursts within capacity."""
- burst_capacity = 20
- rate_per_second = 10
- bucket = {
- "tokens": burst_capacity,
- "max_tokens": burst_capacity,
- "rate": rate_per_second,
- }
-
- def consume(n):
- if bucket["tokens"] >= n:
- bucket["tokens"] -= n
- return True
- return False
-
- # Burst of 20 requests should succeed
- assert consume(20) is True
-
- # Next request should fail (no tokens left)
- assert consume(1) is False
-
- def test_burst_recovery(self):
- """System should recover from burst after cooldown."""
- bucket = {
- "tokens": 0,
- "max_tokens": 10,
- "refill_per_ms": 10, # Fast refill for testing
- }
-
- def refill(elapsed_ms):
- bucket["tokens"] = min(
- bucket["max_tokens"],
- bucket["tokens"] + elapsed_ms * bucket["refill_per_ms"],
- )
-
- # Simulate time passing
- refill(1) # 1ms
-
- assert bucket["tokens"] >= 1
-
-
-class TestRateLimitRecovery:
- """Tests for rate limit recovery scenarios."""
-
- def test_rate_limit_recovery_after_window(self):
- """Rate limit should reset after window expires."""
- window = {
- "requests": 10,
- "limit": 10,
- "window_start": time.time() - 100, # Window already expired
- "window_size": 60,
- }
-
- def check_and_reset():
- now = time.time()
- if now - window["window_start"] > window["window_size"]:
- window["requests"] = 0
- window["window_start"] = now
- return window["requests"] < window["limit"]
-
- assert check_and_reset() is True
- assert window["requests"] == 0
-
-
-class TestRateLimitHeaders:
- """Tests for rate limit headers in responses."""
-
- def test_rate_limit_headers_present(self):
- """Response should include rate limit headers."""
- headers = {
- "X-RateLimit-Limit": "100",
- "X-RateLimit-Remaining": "95",
- "X-RateLimit-Reset": "1640000000",
- }
-
- assert "X-RateLimit-Limit" in headers
- assert "X-RateLimit-Remaining" in headers
- assert "X-RateLimit-Reset" in headers
-
- def test_rate_limit_remaining_decrements(self):
- """Remaining count should decrement with each request."""
- state = {"limit": 100, "remaining": 100}
-
- def make_request():
- headers = {
- "X-RateLimit-Remaining": str(state["remaining"]),
- }
- state["remaining"] -= 1
- return headers
-
- h1 = make_request()
- h2 = make_request()
-
- assert int(h1["X-RateLimit-Remaining"]) > int(
- h2["X-RateLimit-Remaining"]
- )
-
-
-class TestRetryAfterHeader:
- """Tests for Retry-After header behavior."""
-
- def test_retry_after_header_on_limit_exceeded(self):
- """Retry-After header should be present when limit exceeded."""
- headers = {}
- limit_exceeded = True
-
- if limit_exceeded:
- headers["Retry-After"] = "60" # Seconds until reset
-
- assert "Retry-After" in headers
-
- def test_retry_after_calculation(self):
- """Retry-After should reflect actual wait time."""
- window_start = time.time()
- window_size = 60
- current_time = window_start + 30
-
- retry_after = max(0, window_start + window_size - current_time)
-
- assert retry_after == 30
-
-
-class TestConcurrentRequests:
- """Tests for concurrent request handling."""
-
- def test_concurrent_requests_thread_safe(self):
- """Rate limiting should be thread-safe."""
- state = {"count": 0, "limit": 100}
- lock = threading.Lock()
- results = []
-
- def make_request():
- with lock:
- if state["count"] < state["limit"]:
- state["count"] += 1
- results.append(True)
- else:
- results.append(False)
-
- threads = [threading.Thread(target=make_request) for _ in range(150)]
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert results.count(True) == 100
- assert results.count(False) == 50
-
- def test_concurrent_requests_no_race_condition(self):
- """Should not exceed limit due to race conditions."""
- state = {"count": 0, "limit": 100}
- lock = threading.Lock()
-
- def increment():
- with lock:
- if state["count"] < state["limit"]:
- state["count"] += 1
- return True
- return False
-
- threads = [threading.Thread(target=increment) for _ in range(200)]
- for t in threads:
- t.start()
- for t in threads:
- t.join()
-
- assert state["count"] == 100
-
-
-class TestDistributedRateLimiting:
- """Tests for distributed rate limiting scenarios."""
-
- def test_distributed_state_sync(self):
- """Distributed nodes should sync state."""
- nodes = {
- "node1": {"count": 5},
- "node2": {"count": 3},
- }
-
- def get_total_count():
- return sum(n["count"] for n in nodes.values())
-
- assert get_total_count() == 8
-
- def test_distributed_limit_check(self):
- """Should check global limit across nodes."""
- global_limit = 100
- nodes = {
- "node1": {"count": 50},
- "node2": {"count": 49},
- }
-
- def can_accept():
- total = sum(n["count"] for n in nodes.values())
- return total < global_limit
-
- assert can_accept() is True
- nodes["node2"]["count"] = 50
- assert can_accept() is False
-
-
-class TestRateLimitWhitelist:
- """Tests for rate limit whitelist functionality."""
-
- def test_whitelist_bypasses_limit(self):
- """Whitelisted IPs should bypass rate limits."""
- whitelist = {"192.168.1.1", "10.0.0.1"}
-
- def is_rate_limited(ip, requests):
- if ip in whitelist:
- return False # Never rate limited
- return requests > 100
-
- assert is_rate_limited("192.168.1.1", 1000) is False
- assert is_rate_limited("8.8.8.8", 101) is True
-
-
-class TestRateLimitBlacklist:
- """Tests for rate limit blacklist functionality."""
-
- def test_blacklist_always_limited(self):
- """Blacklisted IPs should always be limited."""
- blacklist = {"malicious.ip.1", "malicious.ip.2"}
-
- def is_blocked(ip):
- return ip in blacklist
-
- assert is_blocked("malicious.ip.1") is True
- assert is_blocked("good.ip.1") is False
-
-
-class TestDynamicRateLimitAdjustment:
- """Tests for dynamic rate limit adjustment."""
-
- def test_dynamic_adjustment_under_load(self):
- """Limits should adjust based on system load."""
- base_limit = 100
-
- def get_adjusted_limit(load):
- if load > 0.9:
- return base_limit * 0.5
- if load > 0.7:
- return base_limit * 0.75
- return base_limit
-
- assert get_adjusted_limit(0.8) == 75
-
- def test_dynamic_adjustment_low_load(self):
- """Limits should be higher when load is low."""
- base_limit = 100
-
- def get_adjusted_limit(load):
- if load < 0.3:
- return base_limit * 1.5 # Allow more when idle
- return base_limit
-
- assert get_adjusted_limit(0.2) == 150
-
-
-class TestRateLimitLogging:
- """Tests for rate limit logging."""
-
- def test_rate_limit_exceeded_logged(self):
- """Rate limit exceeded events should be logged."""
- logged_events = []
-
- def log_rate_limit(user_id, endpoint, limit):
- logged_events.append(
- {
- "event": "rate_limit_exceeded",
- "user": user_id,
- "endpoint": endpoint,
- "limit": limit,
- }
- )
-
- log_rate_limit("user123", "/api/upload", 10)
-
- assert len(logged_events) == 1
- assert logged_events[0]["event"] == "rate_limit_exceeded"
-
-
-class TestRateLimitMetrics:
- """Tests for rate limit metrics collection."""
-
- def test_metrics_collection(self):
- """Rate limit metrics should be collected."""
- metrics = {
- "total_requests": 0,
- "limited_requests": 0,
- "by_endpoint": {},
- }
-
- def record_request(endpoint, was_limited):
- metrics["total_requests"] += 1
- if was_limited:
- metrics["limited_requests"] += 1
- metrics["by_endpoint"][endpoint] = (
- metrics["by_endpoint"].get(endpoint, 0) + 1
- )
-
- record_request("/api/upload", True)
- record_request("/api/upload", False)
- record_request("/api/search", False)
-
- assert metrics["total_requests"] == 3
- assert metrics["limited_requests"] == 1
- assert metrics["by_endpoint"]["/api/upload"] == 2
-
-
-class TestRateLimitTTLCleanup:
- """Tests for TTL-based cleanup of rate limit data."""
-
- def test_ttl_cleanup_expired_entries(self):
- """Expired entries should be cleaned up."""
- entries = {
- "user1": {"count": 5, "expires": time.time() - 100}, # Expired
- "user2": {"count": 3, "expires": time.time() + 100}, # Valid
- }
-
- def cleanup():
- now = time.time()
- expired = [k for k, v in entries.items() if v["expires"] < now]
- for k in expired:
- del entries[k]
-
- cleanup()
-
- assert "user1" not in entries
- assert "user2" in entries
-
-
-class TestRateLimitPersistence:
- """Tests for rate limit state persistence."""
-
- def test_persistence_save_and_load(self):
- """Rate limit state should persist across restarts."""
- state = {"user1": {"count": 5, "window_start": time.time()}}
-
- # Simulate save
- saved = dict(state)
-
- # Simulate load
- loaded = dict(saved)
-
- assert loaded == state
-
-
-class TestRateLimitOverflowProtection:
- """Tests for overflow protection in rate limiting."""
-
- def test_counter_overflow_protection(self):
- """Counters should not overflow."""
- import sys
-
- max_safe = sys.maxsize
- counter = max_safe - 5
-
- def safe_increment(value):
- if value >= max_safe - 1:
- return max_safe - 1 # Cap at max safe value
- return value + 1
-
- result = safe_increment(counter)
- assert result <= max_safe
-
-
-class TestAPIvsWebRateLimiting:
- """Tests for different limits for API vs web requests."""
-
- def test_api_has_higher_limits(self):
- """API endpoints should have higher limits than web."""
- limits = {
- "api": {"limit": 1000, "window": 60},
- "web": {"limit": 100, "window": 60},
- }
-
- assert limits["api"]["limit"] > limits["web"]["limit"]
-
- def test_endpoint_type_detection(self):
- """Should detect endpoint type correctly."""
-
- def get_endpoint_type(path):
- if path.startswith("/api/"):
- return "api"
- return "web"
-
- assert get_endpoint_type("/api/v1/research") == "api"
- assert get_endpoint_type("/dashboard") == "web"