Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /tests /fixtures /data_factories.py
| """ | |
| Test Data Factories | |
| Factory pattern for creating consistent test data. | |
| Supports unit, integration, and e2e tests. | |
| """ | |
| import random | |
| import uuid | |
| from dataclasses import dataclass | |
| from datetime import datetime, timedelta, timezone | |
| from typing import Any, Dict, List | |
| class UserFactory: | |
| """Factory for creating test users.""" | |
| username_prefix: str = "testuser" | |
| role: str = "user" | |
| def create(self, **overrides) -> Dict[str, Any]: | |
| """Create a single user.""" | |
| user_id = str(uuid.uuid4())[:8] | |
| base = { | |
| "id": f"user-{user_id}", | |
| "username": f"{self.username_prefix}_{user_id}", | |
| "email": f"{self.username_prefix}_{user_id}@example.com", | |
| "password": "TestPassword123!", | |
| "role": self.role, | |
| "is_active": True, | |
| "created_at": datetime.now(timezone.utc).isoformat(), | |
| "last_login": None, | |
| } | |
| base.update(overrides) | |
| return base | |
| def create_batch(self, count: int, **overrides) -> List[Dict[str, Any]]: | |
| """Create multiple users.""" | |
| return [self.create(**overrides) for _ in range(count)] | |
| def admin(self) -> Dict[str, Any]: | |
| """Create an admin user.""" | |
| return self.create(role="admin", username_prefix="admin") | |
| def inactive(self) -> Dict[str, Any]: | |
| """Create an inactive user.""" | |
| return self.create(is_active=False, username_prefix="inactive") | |
| class ScanFactory: | |
| """Factory for creating test scans.""" | |
| target: str = "scanme.nmap.org" | |
| scan_type: str = "port_scan" | |
| def create(self, **overrides) -> Dict[str, Any]: | |
| """Create a single scan.""" | |
| scan_id = str(uuid.uuid4())[:8] | |
| timestamp = datetime.now(timezone.utc) | |
| base = { | |
| "id": f"scan-{scan_id}", | |
| "name": f"Test Scan {scan_id}", | |
| "target": self.target, | |
| "scan_type": self.scan_type, | |
| "status": random.choice(["pending", "running", "completed", "failed"]), | |
| "created_by": f"user-{str(uuid.uuid4())[:8]}", | |
| "created_at": timestamp.isoformat(), | |
| "started_at": None, | |
| "completed_at": None, | |
| "progress": random.randint(0, 100), | |
| "findings_count": random.randint(0, 50), | |
| "options": { | |
| "ports": "22,80,443", | |
| "intensity": "normal", | |
| }, | |
| "description": f"Test scan description {scan_id}", | |
| } | |
| # Set realistic timestamps based on status | |
| if base["status"] in ["running", "completed", "failed"]: | |
| base["started_at"] = (timestamp + timedelta(seconds=1)).isoformat() | |
| if base["status"] in ["completed", "failed"]: | |
| base["completed_at"] = (timestamp + timedelta(minutes=5)).isoformat() | |
| base["progress"] = 100 | |
| base.update(overrides) | |
| return base | |
| def create_batch(self, count: int, **overrides) -> List[Dict[str, Any]]: | |
| """Create multiple scans.""" | |
| return [self.create(**overrides) for _ in range(count)] | |
| def completed(self, **overrides) -> Dict[str, Any]: | |
| """Create a completed scan.""" | |
| return self.create(status="completed", progress=100, **overrides) | |
| def running(self, **overrides) -> Dict[str, Any]: | |
| """Create a running scan.""" | |
| return self.create(status="running", progress=random.randint(10, 90), **overrides) | |
| def failed(self, **overrides) -> Dict[str, Any]: | |
| """Create a failed scan.""" | |
| return self.create( | |
| status="failed", | |
| progress=random.randint(0, 99), | |
| error_message="Connection timeout", | |
| **overrides, | |
| ) | |
| class FindingFactory: | |
| """Factory for creating test findings.""" | |
| SEVERITIES = ["critical", "high", "medium", "low", "info"] | |
| STATUSES = [ | |
| "open", | |
| "in_progress", | |
| "resolved", | |
| "false_positive", | |
| "risk_accepted", | |
| ] | |
| title_prefix: str = "Vulnerability" | |
| def create(self, **overrides) -> Dict[str, Any]: | |
| """Create a single finding.""" | |
| finding_id = str(uuid.uuid4())[:8] | |
| severity = random.choice(self.SEVERITIES) | |
| base = { | |
| "id": f"finding-{finding_id}", | |
| "scan_id": f"scan-{str(uuid.uuid4())[:8]}", | |
| "title": f"{self.title_prefix} {finding_id}", | |
| "description": f"Test finding description {finding_id}", | |
| "severity": severity, | |
| "status": random.choice(self.STATUSES), | |
| "cvss_score": self._cvss_for_severity(severity), | |
| "cve_id": f"CVE-2024-{random.randint(1000, 9999)}", | |
| "affected_host": f"host{random.randint(1, 100)}.example.com", | |
| "port": random.choice([22, 80, 443, 3306, 5432, 8080]), | |
| "service": random.choice(["ssh", "http", "https", "mysql", "postgresql"]), | |
| "evidence": { | |
| "payload": "' OR 1=1 --", | |
| "response": "Error: SQL syntax", | |
| }, | |
| "remediation": "Use parameterized queries", | |
| "discovered_at": datetime.now(timezone.utc).isoformat(), | |
| "verified_at": None, | |
| "false_positive": False, | |
| } | |
| if base["status"] in ["resolved", "false_positive", "risk_accepted"]: | |
| base["verified_at"] = datetime.now(timezone.utc).isoformat() | |
| if base["status"] == "false_positive": | |
| base["false_positive"] = True | |
| base.update(overrides) | |
| return base | |
| def _cvss_for_severity(self, severity: str) -> float: | |
| """Return CVSS score range for severity.""" | |
| ranges = { | |
| "critical": (9.0, 10.0), | |
| "high": (7.0, 8.9), | |
| "medium": (4.0, 6.9), | |
| "low": (0.1, 3.9), | |
| "info": (0.0, 0.0), | |
| } | |
| min_score, max_score = ranges.get(severity, (0.0, 10.0)) | |
| return round(random.uniform(min_score, max_score), 1) | |
| def create_batch(self, count: int, **overrides) -> List[Dict[str, Any]]: | |
| """Create multiple findings.""" | |
| return [self.create(**overrides) for _ in range(count)] | |
| def critical(self, **overrides) -> Dict[str, Any]: | |
| """Create a critical finding.""" | |
| return self.create(severity="critical", **overrides) | |
| def high(self, **overrides) -> Dict[str, Any]: | |
| """Create a high severity finding.""" | |
| return self.create(severity="high", **overrides) | |
| def resolved(self, **overrides) -> Dict[str, Any]: | |
| """Create a resolved finding.""" | |
| return self.create(status="resolved", **overrides) | |
| class AgentFactory: | |
| """Factory for creating test agents.""" | |
| ROLES = ["researcher", "analyzer", "exploit", "coordinator", "scanner"] | |
| STATES = ["idle", "running", "error", "stopped"] | |
| role: str = "researcher" | |
| def create(self, **overrides) -> Dict[str, Any]: | |
| """Create a single agent.""" | |
| agent_id = str(uuid.uuid4())[:8] | |
| state = random.choice(self.STATES) | |
| base = { | |
| "id": f"agent-{agent_id}", | |
| "name": f"Agent {agent_id}", | |
| "role": self.role, | |
| "state": state, | |
| "current_task": None, | |
| "last_activity": datetime.now(timezone.utc).isoformat(), | |
| "messages_processed": random.randint(0, 1000), | |
| "errors_count": random.randint(0, 10), | |
| "created_at": datetime.now(timezone.utc).isoformat(), | |
| "config": { | |
| "max_tasks": 10, | |
| "timeout": 300, | |
| }, | |
| } | |
| if state == "running": | |
| base["current_task"] = f"task-{str(uuid.uuid4())[:8]}" | |
| base.update(overrides) | |
| return base | |
| def create_batch(self, count: int, **overrides) -> List[Dict[str, Any]]: | |
| """Create multiple agents.""" | |
| return [self.create(**overrides) for _ in range(count)] | |
| def running(self, **overrides) -> Dict[str, Any]: | |
| """Create a running agent.""" | |
| return self.create(state="running", **overrides) | |
| def coordinator(self, **overrides) -> Dict[str, Any]: | |
| """Create a coordinator agent.""" | |
| return self.create(role="coordinator", **overrides) | |
| # Singleton instances for easy access | |
| users = UserFactory() | |
| scans = ScanFactory() | |
| findings = FindingFactory() | |
| agents = AgentFactory() | |
Xet Storage Details
- Size:
- 8.38 kB
- Xet hash:
- 9b4c8cd29bfb65a0bc794f8daa972f4a60b9bfcf02bda46a2959ebbcf5247263
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.