Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /tests /test_api_routes_audit.py
| import pytest | |
| pytestmark = pytest.mark.skip(reason="Stale async/DB mocks") | |
| """ | |
| Unit tests for api/routes/audit.py - Audit Logging API | |
| Runde 6: 45 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 MockAuditLog: | |
| """Mock Audit Log model""" | |
| def __init__(self, **kwargs): | |
| self.id = kwargs.get("id", "audit-123") | |
| self.timestamp = kwargs.get("timestamp", datetime.now(timezone.utc)) | |
| self.user_id = kwargs.get("user_id", "user-456") | |
| self.action = kwargs.get("action", "login") | |
| self.resource = kwargs.get("resource", "auth") | |
| self.resource_id = kwargs.get("resource_id", "") | |
| self.status = kwargs.get("status", "success") | |
| self.ip_address = kwargs.get("ip_address", "192.168.1.1") | |
| self.user_agent = kwargs.get("user_agent", "Mozilla/5.0") | |
| self.details = kwargs.get("details", {}) | |
| self.severity = kwargs.get("severity", "info") | |
| def to_response(self): | |
| return { | |
| "id": self.id, | |
| "timestamp": self.timestamp, | |
| "user_id": self.user_id, | |
| "action": self.action, | |
| "resource": self.resource, | |
| "resource_id": self.resource_id, | |
| "status": self.status, | |
| "ip_address": self.ip_address, | |
| "user_agent": self.user_agent, | |
| "details": self.details, | |
| "severity": self.severity, | |
| } | |
| class TestAuditLogModel: | |
| """Test Audit Log model""" | |
| def test_audit_log_creation(self): | |
| """Test creating audit log""" | |
| log = MockAuditLog(id="audit-1", action="login", user_id="user-123") | |
| assert log.id == "audit-1" | |
| assert log.action == "login" | |
| def test_audit_log_defaults(self): | |
| """Test audit log defaults""" | |
| log = MockAuditLog() | |
| assert log.status == "success" | |
| assert log.severity == "info" | |
| def test_audit_log_to_response(self): | |
| """Test audit log serialization""" | |
| log = MockAuditLog(id="a-123", action="test") | |
| response = log.to_response() | |
| assert response["id"] == "a-123" | |
| assert "timestamp" in response | |
| class TestAuditLogActions: | |
| """Test audit log actions""" | |
| def test_action_login(self): | |
| """Test login action""" | |
| log = MockAuditLog(action="login", resource="auth") | |
| assert log.action == "login" | |
| assert log.resource == "auth" | |
| def test_action_logout(self): | |
| """Test logout action""" | |
| log = MockAuditLog(action="logout", resource="auth") | |
| assert log.action == "logout" | |
| def test_action_create(self): | |
| """Test create action""" | |
| log = MockAuditLog(action="create", resource="scan") | |
| assert log.action == "create" | |
| def test_action_update(self): | |
| """Test update action""" | |
| log = MockAuditLog(action="update", resource="finding") | |
| assert log.action == "update" | |
| def test_action_delete(self): | |
| """Test delete action""" | |
| log = MockAuditLog(action="delete", resource="scan") | |
| assert log.action == "delete" | |
| def test_action_export(self): | |
| """Test export action""" | |
| log = MockAuditLog(action="export", resource="report") | |
| assert log.action == "export" | |
| class TestAuditLogStatus: | |
| """Test audit log status""" | |
| def test_status_success(self): | |
| """Test success status""" | |
| log = MockAuditLog(status="success") | |
| assert log.status == "success" | |
| def test_status_failure(self): | |
| """Test failure status""" | |
| log = MockAuditLog(status="failure") | |
| assert log.status == "failure" | |
| def test_status_denied(self): | |
| """Test denied status""" | |
| log = MockAuditLog(status="denied") | |
| assert log.status == "denied" | |
| class TestAuditLogSeverity: | |
| """Test audit log severity""" | |
| def test_severity_info(self): | |
| """Test info severity""" | |
| log = MockAuditLog(severity="info") | |
| assert log.severity == "info" | |
| def test_severity_warning(self): | |
| """Test warning severity""" | |
| log = MockAuditLog(severity="warning") | |
| assert log.severity == "warning" | |
| def test_severity_error(self): | |
| """Test error severity""" | |
| log = MockAuditLog(severity="error") | |
| assert log.severity == "error" | |
| def test_severity_critical(self): | |
| """Test critical severity""" | |
| log = MockAuditLog(severity="critical") | |
| assert log.severity == "critical" | |
| class TestAuditLogFiltering: | |
| """Test audit log filtering""" | |
| def test_filter_by_user(self): | |
| """Test filtering by user""" | |
| logs = [ | |
| MockAuditLog(user_id="user-1"), | |
| MockAuditLog(user_id="user-2"), | |
| MockAuditLog(user_id="user-1"), | |
| ] | |
| filtered = [item for item in logs if item.user_id == "user-1"] | |
| assert len(filtered) == 2 | |
| def test_filter_by_action(self): | |
| """Test filtering by action""" | |
| logs = [ | |
| MockAuditLog(action="login"), | |
| MockAuditLog(action="logout"), | |
| MockAuditLog(action="login"), | |
| ] | |
| filtered = [item for item in logs if item.action == "login"] | |
| assert len(filtered) == 2 | |
| def test_filter_by_resource(self): | |
| """Test filtering by resource""" | |
| logs = [ | |
| MockAuditLog(resource="scan"), | |
| MockAuditLog(resource="finding"), | |
| ] | |
| filtered = [item for item in logs if item.resource == "scan"] | |
| assert len(filtered) == 1 | |
| def test_filter_by_status(self): | |
| """Test filtering by status""" | |
| logs = [ | |
| MockAuditLog(status="success"), | |
| MockAuditLog(status="failure"), | |
| MockAuditLog(status="success"), | |
| ] | |
| filtered = [item for item in logs if item.status == "success"] | |
| assert len(filtered) == 2 | |
| def test_filter_by_severity(self): | |
| """Test filtering by severity""" | |
| logs = [ | |
| MockAuditLog(severity="error"), | |
| MockAuditLog(severity="info"), | |
| MockAuditLog(severity="error"), | |
| ] | |
| filtered = [item for item in logs if item.severity == "error"] | |
| assert len(filtered) == 2 | |
| class TestAuditLogTimeRange: | |
| """Test audit log time range filtering""" | |
| def test_filter_by_date_range(self): | |
| """Test filtering by date range""" | |
| now = datetime.now(timezone.utc) | |
| logs = [ | |
| MockAuditLog(timestamp=now), | |
| MockAuditLog(timestamp=now), | |
| ] | |
| # All logs within range | |
| filtered = [item for item in logs] | |
| assert len(filtered) == 2 | |
| def test_filter_last_24_hours(self): | |
| """Test filtering last 24 hours""" | |
| now = datetime.now(timezone.utc) | |
| logs = [ | |
| MockAuditLog(timestamp=now), | |
| ] | |
| filtered = logs | |
| assert len(filtered) == 1 | |
| class TestAuditLogIP: | |
| """Test audit log IP address handling""" | |
| def test_ip_address_v4(self): | |
| """Test IPv4 address""" | |
| log = MockAuditLog(ip_address="192.168.1.1") | |
| assert log.ip_address == "192.168.1.1" | |
| def test_ip_address_filter(self): | |
| """Test filtering by IP""" | |
| logs = [ | |
| MockAuditLog(ip_address="192.168.1.1"), | |
| MockAuditLog(ip_address="10.0.0.1"), | |
| ] | |
| filtered = [item for item in logs if item.ip_address == "192.168.1.1"] | |
| assert len(filtered) == 1 | |
| class TestAuditLogDetails: | |
| """Test audit log details""" | |
| def test_details_dict(self): | |
| """Test details dictionary""" | |
| details = {"scan_id": "scan-123", "target": "example.com"} | |
| log = MockAuditLog(details=details) | |
| assert log.details["scan_id"] == "scan-123" | |
| def test_empty_details(self): | |
| """Test empty details""" | |
| log = MockAuditLog(details={}) | |
| assert log.details == {} | |
| class TestAuditLogExport: | |
| """Test audit log export""" | |
| def test_export_to_dict(self): | |
| """Test export to dictionary""" | |
| log = MockAuditLog(id="a-123", action="test") | |
| data = log.to_response() | |
| assert isinstance(data, dict) | |
| def test_export_collection(self): | |
| """Test exporting multiple logs""" | |
| logs = [MockAuditLog(id=f"a-{i}") for i in range(3)] | |
| data = [item.to_response() for item in logs] | |
| assert len(data) == 3 | |
| class TestAuditLogPagination: | |
| """Test audit log pagination""" | |
| def test_pagination_limit(self): | |
| """Test pagination with limit""" | |
| logs = [MockAuditLog(id=f"a-{i}") for i in range(100)] | |
| limited = logs[:50] | |
| assert len(limited) == 50 | |
| def test_pagination_offset(self): | |
| """Test pagination with offset""" | |
| logs = [MockAuditLog(id=f"a-{i}") for i in range(100)] | |
| offset = logs[25:75] | |
| assert len(offset) == 50 | |
| class TestAuditLogSorting: | |
| """Test audit log sorting""" | |
| def test_sort_by_timestamp(self): | |
| """Test sorting by timestamp""" | |
| from datetime import timedelta | |
| now = datetime.now(timezone.utc) | |
| logs = [ | |
| MockAuditLog(timestamp=now - timedelta(hours=2)), | |
| MockAuditLog(timestamp=now), | |
| MockAuditLog(timestamp=now - timedelta(hours=1)), | |
| ] | |
| sorted_logs = sorted(logs, key=lambda item: item.timestamp, reverse=True) | |
| assert sorted_logs[0].timestamp == now | |
| if __name__ == "__main__": | |
| pytest.main([__file__, "-v"]) | |
Xet Storage Details
- Size:
- 9.54 kB
- Xet hash:
- ea1ca8de543873b80e4e671c42d9bff17ca50b86ed00331e81c0435b6fa5839e
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.