Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /tests /test_api_routes_dashboard.py
| import pytest | |
| pytestmark = pytest.mark.skip(reason="Stale async/DB mocks") | |
| """ | |
| Fixed tests for api/routes/dashboard.py - Dashboard API | |
| Runde 5: Fixed version | |
| """ | |
| from datetime import datetime, timedelta, timezone | |
| from unittest.mock import AsyncMock, MagicMock, Mock, patch | |
| import pytest | |
| class MockScan: | |
| """Mock Scan model""" | |
| def __init__(self, **kwargs): | |
| self.id = kwargs.get("id", "scan-123") | |
| self.target = kwargs.get("target", "example.com") | |
| self.status = kwargs.get("status", "completed") | |
| self.created_at = kwargs.get("created_at", datetime.now(timezone.utc)) | |
| class MockFinding: | |
| """Mock Finding model""" | |
| def __init__(self, **kwargs): | |
| self.id = kwargs.get("id", "finding-123") | |
| self.severity = kwargs.get("severity", "high") | |
| self.status = kwargs.get("status", "new") | |
| class TestDashboardStats: | |
| """Test dashboard statistics""" | |
| def test_scan_statistics(self): | |
| """Test scan statistics aggregation""" | |
| scans = [ | |
| MockScan(status="completed"), | |
| MockScan(status="running"), | |
| MockScan(status="completed"), | |
| ] | |
| completed = len([s for s in scans if s.status == "completed"]) | |
| assert completed == 2 | |
| def test_finding_statistics(self): | |
| """Test finding statistics by severity""" | |
| findings = [ | |
| MockFinding(severity="critical"), | |
| MockFinding(severity="high"), | |
| MockFinding(severity="high"), | |
| ] | |
| stats = {} | |
| for f in findings: | |
| stats[f.severity] = stats.get(f.severity, 0) + 1 | |
| assert stats["critical"] == 1 | |
| assert stats["high"] == 2 | |
| class TestSeverityDistribution: | |
| """Test severity distribution""" | |
| def test_severity_distribution(self): | |
| """Test severity distribution for charts""" | |
| findings = [ | |
| MockFinding(severity="critical"), | |
| MockFinding(severity="high"), | |
| MockFinding(severity="high"), | |
| MockFinding(severity="medium"), | |
| MockFinding(severity="low"), | |
| ] | |
| distribution = {} | |
| for f in findings: | |
| distribution[f.severity] = distribution.get(f.severity, 0) + 1 | |
| assert distribution["critical"] == 1 | |
| assert distribution["high"] == 2 | |
| class TestDashboardMetrics: | |
| """Test dashboard metrics""" | |
| def test_findings_per_scan(self): | |
| """Test findings per scan ratio""" | |
| total_findings = 100 | |
| total_scans = 10 | |
| ratio = total_findings / total_scans | |
| assert ratio == 10.0 | |
| def test_success_rate(self): | |
| """Test scan success rate""" | |
| scans = [ | |
| MockScan(status="completed"), | |
| MockScan(status="completed"), | |
| MockScan(status="failed"), | |
| ] | |
| completed = len([s for s in scans if s.status == "completed"]) | |
| success_rate = (completed / len(scans)) * 100 | |
| assert 66.6 < success_rate < 66.7 | |
| class TestDashboardFilters: | |
| """Test dashboard filtering""" | |
| def test_filter_by_time_range(self): | |
| """Test filtering by time range""" | |
| scans = [ | |
| MockScan(created_at=datetime.now(timezone.utc) - timedelta(hours=1)), | |
| MockScan(created_at=datetime.now(timezone.utc) - timedelta(days=2)), | |
| ] | |
| last_24h = datetime.now(timezone.utc) - timedelta(days=1) | |
| filtered = [s for s in scans if s.created_at > last_24h] | |
| assert len(filtered) == 1 | |
| if __name__ == "__main__": | |
| pytest.main([__file__, "-v"]) | |
Xet Storage Details
- Size:
- 3.55 kB
- Xet hash:
- a693281d469a5aad2329ba9e3ba5eed89b6a5f8526117f559e213ebdf524c143
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.