Buckets:

download
raw
4.68 kB
"""
Pytest fixtures for agent orchestrator tests.
"""
import asyncio
import sys
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
# Create a mock package structure
class MockModule:
"""Mock module that returns MagicMock for any attribute."""
def __getattr__(self, name):
return MagicMock()
# Mock problematic imports before importing agents
sys.modules["modules"] = MockModule()
sys.modules["modules.sql_injection_db"] = MockModule()
sys.modules["modules.cve_database"] = MockModule()
sys.modules["sql_injection_db"] = MockModule()
sys.modules["sqlmap"] = MockModule()
sys.modules["sqlmap_integration"] = MockModule()
sys.modules["guardrails"] = MockModule()
sys.modules["guardrails.domain_validator"] = MockModule()
sys.modules["guardrails.ip_validator"] = MockModule()
sys.modules["guardrails.risk_levels"] = MockModule()
sys.modules["vpn"] = MockModule()
sys.modules["utils"] = MockModule()
sys.modules["utils.security"] = MockModule()
# Now import agent modules
from agents.agent_base import AgentMessage, AgentRole, BaseAgent
from agents.agent_orchestrator import AgentOrchestrator
class MockAgent(BaseAgent):
"""Mock agent for testing."""
def __init__(self, name: str, role: AgentRole, orchestrator=None):
super().__init__(name, role, orchestrator)
self.executed_tasks = []
self.received_messages = []
async def execute_task(self, task: dict) -> dict:
"""Execute a task - mock implementation."""
self.executed_tasks.append(task)
return {"agent_id": self.id, "task": task, "status": "completed"}
async def handle_message(self, msg: AgentMessage):
"""Handle incoming messages."""
self.received_messages.append(msg)
@pytest.fixture
def event_loop():
"""Create an instance of the default event loop for each test case."""
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
@pytest.fixture
def orchestrator():
"""Create a fresh AgentOrchestrator instance."""
return AgentOrchestrator()
@pytest.fixture
def mock_zen_orchestrator():
"""Create a mock ZenOrchestrator."""
mock = MagicMock()
mock.submit_task = AsyncMock(return_value="task_123")
mock.get_status = MagicMock(return_value={"running": True})
return mock
@pytest.fixture
def sample_agent():
"""Create a sample mock agent."""
return MockAgent("TestAgent", AgentRole.RESEARCHER)
@pytest.fixture
def sample_agents():
"""Create multiple sample agents for testing."""
return [
MockAgent("Researcher1", AgentRole.RESEARCHER),
MockAgent("Analyst1", AgentRole.ANALYST),
MockAgent("Exploit1", AgentRole.EXPLOIT),
]
@pytest.fixture
def sample_message():
"""Create a sample agent message."""
return AgentMessage(
sender="TestSender[abc123]",
recipient="TestRecipient[def456]",
msg_type="chat",
content="Test message content",
priority=2,
)
@pytest.fixture
def sample_broadcast_message():
"""Create a sample broadcast message."""
return AgentMessage(
sender="TestSender[abc123]",
recipient="all",
msg_type="chat",
content="Broadcast message",
priority=1,
)
@pytest.fixture
def sample_role_message():
"""Create a sample role-targeted message."""
return AgentMessage(
sender="TestSender[abc123]",
recipient="role:researcher",
msg_type="task",
content="Research task",
priority=3,
)
@pytest.fixture
def sample_context_data():
"""Create sample context data."""
return {
"target": "example.com",
"scan_type": "network",
"findings": [],
}
@pytest.fixture
def sample_scan_results():
"""Create sample scan results for post-scan workflow."""
return {
"target": "example.com",
"findings": [
{
"id": "CVE-2021-44228",
"title": "Log4j RCE",
"severity": "critical",
"cvss_score": 10.0,
}
],
"ports": [80, 443],
"services": ["http", "https"],
}
@pytest.fixture
def mock_post_scan_agent():
"""Mock PostScanAgent for testing."""
with patch("agents.agent_orchestrator.PostScanAgent") as mock:
instance = MagicMock()
instance.run = AsyncMock(
return_value={
"verification_completed": True,
"exploitation_attempted": True,
"evidence_collected": True,
"cleanup_performed": True,
}
)
mock.return_value = instance
yield mock

Xet Storage Details

Size:
4.68 kB
·
Xet hash:
81d59c96cd4eed3f5ead578bdc99f649e8bd073088b6671807507c72d56207d8

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