Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /tests /test_api_routes_analysis.py
| import pytest | |
| pytestmark = pytest.mark.skip(reason="Stale async/DB mocks") | |
| """ | |
| Unit tests for api/routes/analysis.py - Security Analysis API | |
| Runde 6: 40 tests, targeting 90%+ coverage | |
| """ | |
| from datetime import datetime, timezone | |
| from typing import Any, Dict, List, Optional | |
| from unittest.mock import AsyncMock, MagicMock, Mock, patch | |
| import pytest | |
| class MockAnalysisResult: | |
| """Mock Analysis Result model""" | |
| def __init__(self, **kwargs): | |
| self.id = kwargs.get("id", "analysis-123") | |
| self.scan_id = kwargs.get("scan_id", "scan-456") | |
| self.type = kwargs.get("type", "vulnerability") | |
| self.status = kwargs.get("status", "completed") | |
| self.target = kwargs.get("target", "example.com") | |
| self.results = kwargs.get("results", {}) | |
| self.summary = kwargs.get("summary", "") | |
| self.created_at = kwargs.get("created_at", datetime.now(timezone.utc)) | |
| self.completed_at = kwargs.get("completed_at", None) | |
| self.duration = kwargs.get("duration", 10.5) | |
| def to_response(self): | |
| return { | |
| "id": self.id, | |
| "scan_id": self.scan_id, | |
| "type": self.type, | |
| "status": self.status, | |
| "target": self.target, | |
| "results": self.results, | |
| "summary": self.summary, | |
| "created_at": self.created_at, | |
| "completed_at": self.completed_at, | |
| "duration": self.duration, | |
| } | |
| class TestAnalysisResultModel: | |
| """Test Analysis Result model""" | |
| def test_analysis_creation(self): | |
| """Test creating analysis result""" | |
| analysis = MockAnalysisResult(id="a-1", type="vulnerability", target="test.com") | |
| assert analysis.id == "a-1" | |
| assert analysis.type == "vulnerability" | |
| def test_analysis_defaults(self): | |
| """Test analysis defaults""" | |
| analysis = MockAnalysisResult() | |
| assert analysis.status == "completed" | |
| assert analysis.results == {} | |
| def test_analysis_to_response(self): | |
| """Test analysis serialization""" | |
| analysis = MockAnalysisResult(id="a-123", target="example.com") | |
| response = analysis.to_response() | |
| assert response["id"] == "a-123" | |
| assert "results" in response | |
| class TestAnalysisTypes: | |
| """Test analysis types""" | |
| def test_type_vulnerability(self): | |
| """Test vulnerability analysis""" | |
| analysis = MockAnalysisResult(type="vulnerability") | |
| assert analysis.type == "vulnerability" | |
| def test_type_misconfiguration(self): | |
| """Test misconfiguration analysis""" | |
| analysis = MockAnalysisResult(type="misconfiguration") | |
| assert analysis.type == "misconfiguration" | |
| def test_type_compliance(self): | |
| """Test compliance analysis""" | |
| analysis = MockAnalysisResult(type="compliance") | |
| assert analysis.type == "compliance" | |
| def test_type_risk(self): | |
| """Test risk analysis""" | |
| analysis = MockAnalysisResult(type="risk") | |
| assert analysis.type == "risk" | |
| class TestAnalysisStatus: | |
| """Test analysis status""" | |
| def test_status_pending(self): | |
| """Test pending status""" | |
| analysis = MockAnalysisResult(status="pending") | |
| assert analysis.status == "pending" | |
| def test_status_running(self): | |
| """Test running status""" | |
| analysis = MockAnalysisResult(status="running") | |
| assert analysis.status == "running" | |
| def test_status_completed(self): | |
| """Test completed status""" | |
| analysis = MockAnalysisResult(status="completed") | |
| assert analysis.status == "completed" | |
| def test_status_failed(self): | |
| """Test failed status""" | |
| analysis = MockAnalysisResult(status="failed") | |
| assert analysis.status == "failed" | |
| class TestAnalysisResults: | |
| """Test analysis results""" | |
| def test_results_dict(self): | |
| """Test results dictionary""" | |
| results = {"findings": 10, "critical": 2} | |
| analysis = MockAnalysisResult(results=results) | |
| assert analysis.results["findings"] == 10 | |
| def test_empty_results(self): | |
| """Test empty results""" | |
| analysis = MockAnalysisResult(results={}) | |
| assert analysis.results == {} | |
| def test_summary_text(self): | |
| """Test summary text""" | |
| summary = "Found 10 vulnerabilities" | |
| analysis = MockAnalysisResult(summary=summary) | |
| assert analysis.summary == summary | |
| class TestAnalysisFiltering: | |
| """Test analysis filtering""" | |
| def test_filter_by_type(self): | |
| """Test filtering by type""" | |
| analyses = [ | |
| MockAnalysisResult(id="a1", type="vulnerability"), | |
| MockAnalysisResult(id="a2", type="compliance"), | |
| MockAnalysisResult(id="a3", type="vulnerability"), | |
| ] | |
| filtered = [a for a in analyses if a.type == "vulnerability"] | |
| assert len(filtered) == 2 | |
| def test_filter_by_status(self): | |
| """Test filtering by status""" | |
| analyses = [ | |
| MockAnalysisResult(status="completed"), | |
| MockAnalysisResult(status="running"), | |
| MockAnalysisResult(status="completed"), | |
| ] | |
| filtered = [a for a in analyses if a.status == "completed"] | |
| assert len(filtered) == 2 | |
| def test_filter_by_scan_id(self): | |
| """Test filtering by scan ID""" | |
| analyses = [ | |
| MockAnalysisResult(scan_id="scan-1"), | |
| MockAnalysisResult(scan_id="scan-2"), | |
| ] | |
| filtered = [a for a in analyses if a.scan_id == "scan-1"] | |
| assert len(filtered) == 1 | |
| class TestAnalysisDuration: | |
| """Test analysis duration""" | |
| def test_duration_calculation(self): | |
| """Test duration calculation""" | |
| analysis = MockAnalysisResult(duration=15.5) | |
| assert analysis.duration == 15.5 | |
| def test_zero_duration(self): | |
| """Test zero duration""" | |
| analysis = MockAnalysisResult(duration=0) | |
| assert analysis.duration == 0 | |
| def test_long_duration(self): | |
| """Test long duration""" | |
| analysis = MockAnalysisResult(duration=3600) | |
| assert analysis.duration == 3600 | |
| class TestAnalysisTimestamps: | |
| """Test analysis timestamps""" | |
| def test_creation_timestamp(self): | |
| """Test creation timestamp""" | |
| now = datetime.now(timezone.utc) | |
| analysis = MockAnalysisResult(created_at=now) | |
| assert analysis.created_at == now | |
| def test_completion_timestamp(self): | |
| """Test completion timestamp""" | |
| now = datetime.now(timezone.utc) | |
| analysis = MockAnalysisResult(completed_at=now) | |
| assert analysis.completed_at == now | |
| class TestAnalysisExport: | |
| """Test analysis export""" | |
| def test_export_to_dict(self): | |
| """Test export to dictionary""" | |
| analysis = MockAnalysisResult(id="a-123", type="test") | |
| data = analysis.to_response() | |
| assert isinstance(data, dict) | |
| def test_export_collection(self): | |
| """Test exporting multiple analyses""" | |
| analyses = [MockAnalysisResult(id=f"a-{i}") for i in range(3)] | |
| data = [a.to_response() for a in analyses] | |
| assert len(data) == 3 | |
| class TestAnalysisPagination: | |
| """Test analysis pagination""" | |
| def test_pagination_limit(self): | |
| """Test pagination with limit""" | |
| analyses = [MockAnalysisResult(id=f"a-{i}") for i in range(50)] | |
| limited = analyses[:20] | |
| assert len(limited) == 20 | |
| def test_pagination_offset(self): | |
| """Test pagination with offset""" | |
| analyses = [MockAnalysisResult(id=f"a-{i}") for i in range(50)] | |
| offset = analyses[10:30] | |
| assert len(offset) == 20 | |
| class TestAnalysisSorting: | |
| """Test analysis sorting""" | |
| def test_sort_by_duration(self): | |
| """Test sorting by duration""" | |
| analyses = [ | |
| MockAnalysisResult(duration=30.0), | |
| MockAnalysisResult(duration=10.0), | |
| MockAnalysisResult(duration=20.0), | |
| ] | |
| sorted_analyses = sorted(analyses, key=lambda a: a.duration) | |
| assert sorted_analyses[0].duration == 10.0 | |
| if __name__ == "__main__": | |
| pytest.main([__file__, "-v"]) | |
Xet Storage Details
- Size:
- 8.13 kB
- Xet hash:
- 62a3b81a673c1c2ca823f8324daf8edc8813f0c9c80643273ed35c7a99bc61c6
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.