Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /tests /test_90pct_final_push.py
| """ | |
| Final targeted tests to push coverage from 61.76% → 90%+ | |
| Targets highest-impact remaining gaps with CORRECT API signatures. | |
| """ | |
| import asyncio | |
| import os | |
| import sys | |
| from datetime import datetime, timedelta | |
| from unittest.mock import AsyncMock, MagicMock, Mock, patch | |
| import pytest | |
| sys.path.insert(0, "/home/atakan/zen-ai-pentest") | |
| # =========================================== | |
| # Test 1: Cache Global Backend Functions | |
| # =========================================== | |
| class TestCacheGlobalBackend: | |
| """Test global cache backend initialization""" | |
| def test_get_cache_backend_requires_param(self): | |
| """Test _get_cache_backend requires backend parameter""" | |
| from core.cache import CacheBackend, _get_cache_backend | |
| # Should work with a backend instance | |
| backend = Mock(spec=CacheBackend) | |
| result = _get_cache_backend(backend) | |
| assert result is not None | |
| async def test_get_cached_cve_miss(self): | |
| """Test get_cached_cve when CVE not in cache (lines 968-971)""" | |
| from core.cache import get_cached_cve | |
| mock_cache = Mock() | |
| mock_cache.get = AsyncMock(return_value=None) | |
| with patch("core.cache._get_cache_backend", return_value=mock_cache): | |
| result = await get_cached_cve("CVE-2024-0001") | |
| assert result is None | |
| mock_cache.get.assert_called_once() | |
| async def test_get_cached_cve_hit(self): | |
| """Test get_cached_cve when CVE in cache (lines 968-971)""" | |
| from core.cache import get_cached_cve | |
| cve_data = {"id": "CVE-2024-0001", "severity": "HIGH"} | |
| mock_cache = Mock() | |
| mock_cache.get = AsyncMock(return_value=cve_data) | |
| with patch("core.cache._get_cache_backend", return_value=mock_cache): | |
| result = await get_cached_cve("CVE-2024-0001") | |
| assert result == cve_data | |
| async def test_cache_cve(self): | |
| """Test cache_cve stores CVE data (lines 974-977)""" | |
| from core.cache import cache_cve | |
| cve_data = {"id": "CVE-2024-0001", "severity": "HIGH"} | |
| mock_cache = Mock() | |
| mock_cache.set = AsyncMock() | |
| with patch("core.cache._get_cache_backend", return_value=mock_cache): | |
| await cache_cve("CVE-2024-0001", cve_data, 3600) | |
| mock_cache.set.assert_called_once() | |
| # =========================================== | |
| # Test 2: Tool Executor Main Execution | |
| # =========================================== | |
| class TestToolExecutorExecution: | |
| """Test tool_executor main execution paths""" | |
| def executor(self): | |
| from autonomous.tool_executor import ToolExecutor | |
| return ToolExecutor() | |
| def test_execute_tool_basic(self, executor): | |
| """Test basic tool execution""" | |
| with patch.object(executor, "execute") as mock_exec: | |
| mock_exec.return_value = {"success": True, "result": "test"} | |
| result = executor.execute("nmap", {"target": "127.0.0.1"}) | |
| assert result["success"] is True | |
| def test_get_available_tools(self, executor): | |
| """Test getting available tools""" | |
| tools = executor.get_available_tools() | |
| assert isinstance(tools, list) | |
| def test_execution_log(self, executor): | |
| """Test execution log access""" | |
| log = executor.execution_log | |
| assert log is not None | |
| # =========================================== | |
| # Test 3: Memory Manager Workflows | |
| # =========================================== | |
| class TestMemoryManagerWorkflows: | |
| """Test MemoryManager, WorkingMemory, EpisodicMemory with correct APIs""" | |
| def test_working_memory_with_max_size(self): | |
| """Test WorkingMemory with max_size parameter""" | |
| from autonomous.memory import WorkingMemory | |
| wm = WorkingMemory(max_size=5) | |
| assert wm is not None | |
| def test_memory_entry_fields(self): | |
| """Test MemoryEntry has correct fields""" | |
| from autonomous.memory import MemoryEntry | |
| entry = MemoryEntry( | |
| id="test-1", | |
| content="Test content", | |
| memory_type="finding", | |
| metadata={"severity": "HIGH"}, | |
| timestamp=datetime.now(), | |
| embedding=[0.1, 0.2, 0.3], | |
| session_id="session-1", | |
| ) | |
| assert entry.id == "test-1" | |
| assert entry.content == "Test content" | |
| assert entry.memory_type == "finding" | |
| # =========================================== | |
| # Test 4: RedisCache Operations | |
| # =========================================== | |
| class TestRedisCacheOperations: | |
| """Test RedisCache methods""" | |
| def redis_cache(self): | |
| from core.cache import RedisCache | |
| return RedisCache() | |
| async def test_redis_cache_get(self, redis_cache): | |
| """Test RedisCache get""" | |
| mock_redis = Mock() | |
| mock_redis.get.return_value = None | |
| redis_cache._redis = mock_redis | |
| result = await redis_cache.get("test_key") | |
| assert result is None | |
| async def test_redis_cache_set(self, redis_cache): | |
| """Test RedisCache set""" | |
| mock_redis = AsyncMock() | |
| redis_cache._redis = mock_redis | |
| await redis_cache.set("key", "value", ttl=300) | |
| # Should not raise | |
| async def test_redis_cache_delete(self, redis_cache): | |
| """Test RedisCache delete""" | |
| mock_redis = AsyncMock() | |
| redis_cache._redis = mock_redis | |
| await redis_cache.delete("key") | |
| # Should not raise | |
| async def test_redis_cache_mget(self, redis_cache): | |
| """Test RedisCache mget""" | |
| mock_redis = Mock() | |
| mock_redis.mget.return_value = [b"val1", b"val2"] | |
| redis_cache._redis = mock_redis | |
| result = await redis_cache.mget(["key1", "key2"]) | |
| assert isinstance(result, dict) | |
| async def test_redis_cache_mset(self, redis_cache): | |
| """Test RedisCache mset""" | |
| mock_redis = AsyncMock() | |
| redis_cache._redis = mock_redis | |
| await redis_cache.mset({"key1": "val1", "key2": "val2"}) | |
| # Should not raise | |
| async def test_redis_cache_exists(self, redis_cache): | |
| """Test RedisCache exists""" | |
| mock_redis = Mock() | |
| mock_redis.exists.return_value = 1 | |
| redis_cache._redis = mock_redis | |
| result = await redis_cache.exists("key") | |
| assert result is True | |
| async def test_redis_cache_clear(self, redis_cache): | |
| """Test RedisCache clear""" | |
| mock_redis = AsyncMock() | |
| redis_cache._redis = mock_redis | |
| await redis_cache.clear() | |
| # Should not raise | |
| def test_redis_cache_stats(self, redis_cache): | |
| """Test RedisCache get_stats""" | |
| stats = redis_cache.get_stats() | |
| assert isinstance(stats, dict) | |
| # =========================================== | |
| # Test 5: MultiTierCache Operations | |
| # =========================================== | |
| class TestMultiTierCacheOperations: | |
| """Test MultiTierCache operations""" | |
| def mt_cache(self): | |
| from core.cache import MultiTierCache | |
| return MultiTierCache() | |
| def test_multitier_has_l1_l2_l3(self, mt_cache): | |
| """Test MultiTierCache has tier attributes""" | |
| assert hasattr(mt_cache, "l1") | |
| assert hasattr(mt_cache, "l2") | |
| assert hasattr(mt_cache, "l3") | |
| async def test_multitier_get(self, mt_cache): | |
| """Test MultiTierCache get""" | |
| # Mock L1 to return None | |
| mt_cache.l1 = Mock() | |
| mt_cache.l1.get = AsyncMock(return_value=None) | |
| mt_cache.l2 = Mock() | |
| mt_cache.l2.get = AsyncMock(return_value="value") | |
| result = await mt_cache.get("key") | |
| assert result == "value" | |
| async def test_multitier_set(self, mt_cache): | |
| """Test MultiTierCache set""" | |
| mt_cache.l1 = Mock() | |
| mt_cache.l1.set = AsyncMock() | |
| mt_cache.l2 = Mock() | |
| mt_cache.l2.set = AsyncMock() | |
| await mt_cache.set("key", "value") | |
| mt_cache.l1.set.assert_called_once() | |
| async def test_multitier_mget(self, mt_cache): | |
| """Test MultiTierCache mget""" | |
| mt_cache.l1 = Mock() | |
| mt_cache.l1.mget = AsyncMock(return_value={"k1": "v1"}) | |
| result = await mt_cache.mget(["k1", "k2"]) | |
| assert isinstance(result, dict) | |
| async def test_multitier_delete(self, mt_cache): | |
| """Test MultiTierCache delete""" | |
| mt_cache.l1 = Mock() | |
| mt_cache.l1.delete = AsyncMock() | |
| mt_cache.l2 = Mock() | |
| mt_cache.l2.delete = AsyncMock() | |
| await mt_cache.delete("key") | |
| mt_cache.l1.delete.assert_called_once() | |
| # =========================================== | |
| # Test 6: FalsePositiveEngine Operations | |
| # =========================================== | |
| class TestFalsePositiveEngineOps: | |
| """Test FalsePositiveEngine operations""" | |
| def engine(self): | |
| from risk_engine.false_positive_engine import FalsePositiveEngine | |
| return FalsePositiveEngine() | |
| def test_prioritize_findings_empty(self, engine): | |
| """Test prioritize_findings with empty list""" | |
| result = engine.prioritize_findings([]) | |
| assert result == [] | |
| def test_calculate_risk_score_basic(self, engine): | |
| """Test calculate_risk_score basic""" | |
| finding = { | |
| "cvss_score": 7.5, | |
| "epss_score": 0.5, | |
| "exploit_available": True, | |
| } | |
| score = engine.calculate_risk_score(finding) | |
| assert isinstance(score, (int, float)) | |
| assert 0 <= score <= 100 | |
| def test_check_epss_not_found(self, engine): | |
| """Test check_epss when CVE not found""" | |
| with patch.object(engine, "session") as mock_session: | |
| mock_response = Mock() | |
| mock_response.status_code = 200 | |
| mock_response.json.return_value = {"data": []} | |
| mock_session.get.return_value = mock_response | |
| result = engine.check_epss("CVE-1900-0001") | |
| assert result == 0.0 | |
| def test_fp_confidence_threshold(self, engine): | |
| """Test fp_confidence_threshold property""" | |
| threshold = engine.fp_confidence_threshold | |
| assert isinstance(threshold, float) | |
| def test_confirmed_confidence_threshold(self, engine): | |
| """Test confirmed_confidence_threshold property""" | |
| threshold = engine.confirmed_confidence_threshold | |
| assert isinstance(threshold, float) | |
| # =========================================== | |
| # Test 7: SQLiteCache Operations | |
| # =========================================== | |
| class TestSQLiteCacheOperations: | |
| """Test SQLiteCache operations""" | |
| def sqlite_cache(self): | |
| from core.cache import SQLiteCache | |
| cache = SQLiteCache() | |
| yield cache | |
| cache.close() | |
| async def test_sqlite_cache_set_and_get(self, sqlite_cache): | |
| """Test SQLiteCache set and get""" | |
| await sqlite_cache.set("key1", "value1") | |
| result = await sqlite_cache.get("key1") | |
| assert result == "value1" | |
| async def test_sqlite_cache_delete(self, sqlite_cache): | |
| """Test SQLiteCache delete""" | |
| await sqlite_cache.set("key2", "value2") | |
| await sqlite_cache.delete("key2") | |
| result = await sqlite_cache.get("key2") | |
| assert result is None | |
| async def test_sqlite_cache_exists(self, sqlite_cache): | |
| """Test SQLiteCache exists""" | |
| await sqlite_cache.set("key3", "value3") | |
| result = await sqlite_cache.exists("key3") | |
| assert result is True | |
| def test_sqlite_cache_get_stats(self, sqlite_cache): | |
| """Test SQLiteCache get_stats""" | |
| stats = sqlite_cache.get_stats() | |
| assert isinstance(stats, dict) | |
| assert "size" in stats or "entries" in stats | |
| # =========================================== | |
| # Run Summary | |
| # =========================================== | |
| if __name__ == "__main__": | |
| pytest.main([__file__, "-v", "--tb=short"]) | |
Xet Storage Details
- Size:
- 12.3 kB
- Xet hash:
- 7aa77ee75708a54fa9d291435c4d04cca2aef8c253e7f788fa1cd067f65797a9
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.