Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /api /tests /smart /test_schemas.py
| """ | |
| Smart tests for api/schemas.py module. | |
| Tests cover: | |
| 1. Module importability | |
| 2. Schema validation | |
| 3. Enum validation | |
| 4. Model instantiation | |
| 5. Edge cases | |
| """ | |
| import pytest | |
| from pydantic import ValidationError | |
| class TestSchemasImportable: | |
| """Test that schemas module can be imported.""" | |
| def test_module_importable(self): | |
| """Test that schemas module can be imported.""" | |
| import api.schemas as schemas | |
| assert schemas is not None | |
| def test_all_enums_available(self): | |
| """Test that all enums are available.""" | |
| from api.schemas import ( | |
| ReportFormat, | |
| ScanStatus, | |
| ScheduleFrequency, | |
| Severity, | |
| ) | |
| assert ScanStatus is not None | |
| assert Severity is not None | |
| assert ReportFormat is not None | |
| assert ScheduleFrequency is not None | |
| def test_all_schemas_available(self): | |
| """Test that all schemas are available.""" | |
| from api.schemas import ( | |
| FindingCreate, | |
| ScanCreate, | |
| UserLogin, | |
| ) | |
| assert UserLogin is not None | |
| assert ScanCreate is not None | |
| assert FindingCreate is not None | |
| class TestScanStatusEnum: | |
| """Test ScanStatus enum.""" | |
| def test_scan_status_values(self): | |
| """Test ScanStatus enum values.""" | |
| from api.schemas import ScanStatus | |
| assert ScanStatus.PENDING == "pending" | |
| assert ScanStatus.RUNNING == "running" | |
| assert ScanStatus.COMPLETED == "completed" | |
| assert ScanStatus.FAILED == "failed" | |
| assert ScanStatus.CANCELLED == "cancelled" | |
| def test_scan_status_from_string(self): | |
| """Test creating ScanStatus from string.""" | |
| from api.schemas import ScanStatus | |
| status = ScanStatus("running") | |
| assert status == ScanStatus.RUNNING | |
| class TestSeverityEnum: | |
| """Test Severity enum.""" | |
| def test_severity_values(self): | |
| """Test Severity enum values.""" | |
| from api.schemas import Severity | |
| assert Severity.CRITICAL == "critical" | |
| assert Severity.HIGH == "high" | |
| assert Severity.MEDIUM == "medium" | |
| assert Severity.LOW == "low" | |
| assert Severity.INFO == "info" | |
| def test_severity_comparison(self): | |
| """Test severity value comparison.""" | |
| from api.schemas import Severity | |
| # Test that all severity values exist | |
| severities = [ | |
| Severity.LOW, | |
| Severity.MEDIUM, | |
| Severity.HIGH, | |
| Severity.CRITICAL, | |
| ] | |
| assert len(severities) == 4 | |
| # Test alphabetical sorting | |
| sorted_severities = sorted(severities, key=lambda x: x.value) | |
| assert sorted_severities[0] == Severity.CRITICAL # 'critical' comes first alphabetically | |
| assert sorted_severities[-1] == Severity.MEDIUM # 'medium' comes last alphabetically | |
| class TestReportFormatEnum: | |
| """Test ReportFormat enum.""" | |
| def test_report_format_values(self): | |
| """Test ReportFormat enum values.""" | |
| from api.schemas import ReportFormat | |
| assert ReportFormat.PDF == "pdf" | |
| assert ReportFormat.HTML == "html" | |
| assert ReportFormat.JSON == "json" | |
| assert ReportFormat.XML == "xml" | |
| class TestUserLoginSchema: | |
| """Test UserLogin schema.""" | |
| def test_user_login_valid(self): | |
| """Test valid UserLogin creation.""" | |
| from api.schemas import UserLogin | |
| user = UserLogin(username="testuser", password="password123") | |
| assert user.username == "testuser" | |
| assert user.password == "password123" | |
| def test_user_login_username_too_short(self): | |
| """Test UserLogin with too short username.""" | |
| from api.schemas import UserLogin | |
| with pytest.raises(ValidationError): | |
| UserLogin(username="ab", password="password123") | |
| def test_user_login_username_too_long(self): | |
| """Test UserLogin with too long username.""" | |
| from api.schemas import UserLogin | |
| with pytest.raises(ValidationError): | |
| UserLogin(username="a" * 51, password="password123") | |
| def test_user_login_password_too_short(self): | |
| """Test UserLogin with too short password.""" | |
| from api.schemas import UserLogin | |
| with pytest.raises(ValidationError): | |
| UserLogin(username="testuser", password="short") | |
| class TestUserCreateSchema: | |
| """Test UserCreate schema.""" | |
| def test_user_create_valid(self): | |
| """Test valid UserCreate creation.""" | |
| from api.schemas import UserCreate | |
| user = UserCreate(username="testuser", password="password123", role="admin") | |
| assert user.username == "testuser" | |
| assert user.password == "password123" | |
| assert user.role == "admin" | |
| def test_user_create_default_role(self): | |
| """Test UserCreate with default role.""" | |
| from api.schemas import UserCreate | |
| user = UserCreate(username="testuser", password="password123") | |
| assert user.role == "user" | |
| def test_user_create_invalid_role(self): | |
| """Test UserCreate with invalid role.""" | |
| from api.schemas import UserCreate | |
| with pytest.raises(ValidationError): | |
| UserCreate(username="testuser", password="password123", role="invalid") | |
| class TestTokenResponseSchema: | |
| """Test TokenResponse schema.""" | |
| def test_token_response_valid(self): | |
| """Test valid TokenResponse creation.""" | |
| from api.schemas import TokenResponse | |
| token = TokenResponse( | |
| access_token="test_token", | |
| token_type="bearer", | |
| expires_in=3600, | |
| username="testuser", | |
| role="admin", | |
| ) | |
| assert token.access_token == "test_token" | |
| assert token.token_type == "bearer" | |
| assert token.expires_in == 3600 | |
| assert token.username == "testuser" | |
| assert token.role == "admin" | |
| def test_token_response_default_token_type(self): | |
| """Test TokenResponse with default token type.""" | |
| from api.schemas import TokenResponse | |
| token = TokenResponse( | |
| access_token="test_token", | |
| expires_in=3600, | |
| username="testuser", | |
| role="admin", | |
| ) | |
| assert token.token_type == "bearer" | |
| class TestScanCreateSchema: | |
| """Test ScanCreate schema.""" | |
| def test_scan_create_valid(self): | |
| """Test valid ScanCreate creation.""" | |
| from api.schemas import ScanCreate | |
| scan = ScanCreate( | |
| name="Test Scan", | |
| target="example.com", | |
| scan_type="nmap", | |
| config={"ports": "80,443"}, | |
| ) | |
| assert scan.name == "Test Scan" | |
| assert scan.target == "example.com" | |
| assert scan.scan_type == "nmap" | |
| assert scan.config == {"ports": "80,443"} | |
| def test_scan_create_default_config(self): | |
| """Test ScanCreate with default config.""" | |
| from api.schemas import ScanCreate | |
| scan = ScanCreate(name="Test Scan", target="example.com", scan_type="nmap") | |
| assert scan.config == {} | |
| def test_scan_create_default_objective(self): | |
| """Test ScanCreate with default objective.""" | |
| from api.schemas import ScanCreate | |
| scan = ScanCreate(name="Test Scan", target="example.com", scan_type="nmap") | |
| assert scan.objective == "comprehensive security scan" | |
| def test_scan_create_name_too_long(self): | |
| """Test ScanCreate with too long name.""" | |
| from api.schemas import ScanCreate | |
| with pytest.raises(ValidationError): | |
| ScanCreate(name="a" * 256, target="example.com", scan_type="nmap") | |
| class TestFindingCreateSchema: | |
| """Test FindingCreate schema.""" | |
| def test_finding_create_valid(self): | |
| """Test valid FindingCreate creation.""" | |
| from api.schemas import FindingCreate, Severity | |
| finding = FindingCreate( | |
| title="Test Finding", | |
| description="Test description", | |
| severity=Severity.HIGH, | |
| cvss_score=7.5, | |
| ) | |
| assert finding.title == "Test Finding" | |
| assert finding.description == "Test description" | |
| assert finding.severity == Severity.HIGH | |
| assert finding.cvss_score == 7.5 | |
| def test_finding_create_default_severity(self): | |
| """Test FindingCreate with default severity.""" | |
| from api.schemas import FindingCreate, Severity | |
| finding = FindingCreate(title="Test Finding") | |
| assert finding.severity == Severity.MEDIUM | |
| def test_finding_create_cvss_out_of_range(self): | |
| """Test FindingCreate with CVSS score out of range.""" | |
| from api.schemas import FindingCreate | |
| with pytest.raises(ValidationError): | |
| FindingCreate(title="Test Finding", cvss_score=11.0) | |
| def test_finding_create_cvss_negative(self): | |
| """Test FindingCreate with negative CVSS score.""" | |
| from api.schemas import FindingCreate | |
| with pytest.raises(ValidationError): | |
| FindingCreate(title="Test Finding", cvss_score=-1.0) | |
| class TestReportCreateSchema: | |
| """Test ReportCreate schema.""" | |
| def test_report_create_valid(self): | |
| """Test valid ReportCreate creation.""" | |
| from api.schemas import ReportCreate, ReportFormat | |
| report = ReportCreate(scan_id=1, format=ReportFormat.PDF, template="default") | |
| assert report.scan_id == 1 | |
| assert report.format == ReportFormat.PDF | |
| assert report.template == "default" | |
| def test_report_create_default_format(self): | |
| """Test ReportCreate with default format.""" | |
| from api.schemas import ReportCreate, ReportFormat | |
| report = ReportCreate(scan_id=1) | |
| assert report.format == ReportFormat.PDF | |
| class TestToolExecuteRequestSchema: | |
| """Test ToolExecuteRequest schema.""" | |
| def test_tool_execute_request_valid(self): | |
| """Test valid ToolExecuteRequest creation.""" | |
| from api.schemas import ToolExecuteRequest | |
| request = ToolExecuteRequest( | |
| tool_name="nmap", | |
| target="example.com", | |
| parameters={"ports": "80,443"}, | |
| timeout=300, | |
| ) | |
| assert request.tool_name == "nmap" | |
| assert request.target == "example.com" | |
| assert request.parameters == {"ports": "80,443"} | |
| assert request.timeout == 300 | |
| def test_tool_execute_request_default_parameters(self): | |
| """Test ToolExecuteRequest with default parameters.""" | |
| from api.schemas import ToolExecuteRequest | |
| request = ToolExecuteRequest(tool_name="nmap", target="example.com") | |
| assert request.parameters == {} | |
| def test_tool_execute_request_default_timeout(self): | |
| """Test ToolExecuteRequest with default timeout.""" | |
| from api.schemas import ToolExecuteRequest | |
| request = ToolExecuteRequest(tool_name="nmap", target="example.com") | |
| assert request.timeout == 300 | |
| class TestDashboardStatsSchema: | |
| """Test DashboardStats schema.""" | |
| def test_dashboard_stats_valid(self): | |
| """Test valid DashboardStats creation.""" | |
| from api.schemas import DashboardStats | |
| stats = DashboardStats( | |
| total_scans=100, | |
| active_scans=5, | |
| completed_scans=90, | |
| failed_scans=5, | |
| total_findings=50, | |
| critical_findings=2, | |
| high_findings=10, | |
| reports_generated=20, | |
| ) | |
| assert stats.total_scans == 100 | |
| assert stats.active_scans == 5 | |
| assert stats.critical_findings == 2 | |
| class TestWSMessageSchema: | |
| """Test WSMessage schema.""" | |
| def test_ws_message_valid(self): | |
| """Test valid WSMessage creation.""" | |
| from api.schemas import WSMessage | |
| message = WSMessage( | |
| type="status", | |
| scan_id=1, | |
| message="Scan started", | |
| data={"progress": 50}, | |
| ) | |
| assert message.type == "status" | |
| assert message.scan_id == 1 | |
| assert message.message == "Scan started" | |
| assert message.data == {"progress": 50} | |
| def test_ws_message_optional_scan_id(self): | |
| """Test WSMessage without scan_id.""" | |
| from api.schemas import WSMessage | |
| message = WSMessage(type="notification", message="Test message") | |
| assert message.scan_id is None | |
| class TestScheduledScanCreateSchema: | |
| """Test ScheduledScanCreate schema.""" | |
| def test_scheduled_scan_create_valid(self): | |
| """Test valid ScheduledScanCreate creation.""" | |
| from api.schemas import ScheduledScanCreate, ScheduleFrequency | |
| scan = ScheduledScanCreate( | |
| name="Daily Scan", | |
| target="example.com", | |
| scan_type="nmap", | |
| frequency=ScheduleFrequency.DAILY, | |
| schedule_time="02:00", | |
| ) | |
| assert scan.name == "Daily Scan" | |
| assert scan.frequency == ScheduleFrequency.DAILY | |
| assert scan.schedule_time == "02:00" | |
| def test_scheduled_scan_create_invalid_time_format(self): | |
| """Test ScheduledScanCreate with invalid time format.""" | |
| from api.schemas import ScheduledScanCreate | |
| with pytest.raises(ValidationError): | |
| ScheduledScanCreate( | |
| name="Daily Scan", | |
| target="example.com", | |
| scan_type="nmap", | |
| schedule_time="25:00", # Invalid hour | |
| ) | |
| def test_scheduled_scan_create_schedule_day_out_of_range(self): | |
| """Test ScheduledScanCreate with schedule_day out of range.""" | |
| from api.schemas import ScheduledScanCreate | |
| with pytest.raises(ValidationError): | |
| ScheduledScanCreate( | |
| name="Weekly Scan", | |
| target="example.com", | |
| scan_type="nmap", | |
| schedule_time="02:00", | |
| schedule_day=7, # Out of range (0-6) | |
| ) | |
| class TestPaginationParamsSchema: | |
| """Test PaginationParams schema.""" | |
| def test_pagination_params_valid(self): | |
| """Test valid PaginationParams creation.""" | |
| from api.schemas import PaginationParams | |
| params = PaginationParams(page=2, page_size=50) | |
| assert params.page == 2 | |
| assert params.page_size == 50 | |
| assert params.skip == 50 # (2-1) * 50 | |
| def test_pagination_params_defaults(self): | |
| """Test PaginationParams with defaults.""" | |
| from api.schemas import PaginationParams | |
| params = PaginationParams() | |
| assert params.page == 1 | |
| assert params.page_size == 20 | |
| assert params.skip == 0 | |
| def test_pagination_params_page_too_small(self): | |
| """Test PaginationParams with page too small.""" | |
| from api.schemas import PaginationParams | |
| with pytest.raises(ValidationError): | |
| PaginationParams(page=0) | |
| def test_pagination_params_page_size_too_large(self): | |
| """Test PaginationParams with page_size too large.""" | |
| from api.schemas import PaginationParams | |
| with pytest.raises(ValidationError): | |
| PaginationParams(page_size=101) | |
| class TestEmptyInput: | |
| """Test handling of empty input.""" | |
| def test_empty_string_fields(self): | |
| """Test schemas with empty string fields.""" | |
| from api.schemas import UserLogin | |
| with pytest.raises(ValidationError): | |
| UserLogin(username="", password="password123") | |
| def test_empty_target(self): | |
| """Test ScanCreate with empty target.""" | |
| from api.schemas import ScanCreate | |
| with pytest.raises(ValidationError): | |
| ScanCreate(name="Test", target="", scan_type="nmap") | |
| class TestInvalidOutput: | |
| """Test handling of invalid output.""" | |
| def test_invalid_severity_string(self): | |
| """Test FindingCreate with invalid severity string.""" | |
| from api.schemas import FindingCreate | |
| with pytest.raises(ValidationError): | |
| FindingCreate(title="Test", severity="invalid") | |
| def test_invalid_report_format(self): | |
| """Test ReportCreate with invalid report format.""" | |
| from api.schemas import ReportCreate | |
| with pytest.raises(ValidationError): | |
| ReportCreate(scan_id=1, format="invalid") | |
Xet Storage Details
- Size:
- 16.1 kB
- Xet hash:
- 8749d97c1fab187e38e4385fe21e10697c226eb785c301fd604e4475ada0fda1
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.