Buckets:

download
raw
11.8 kB
"""Tests for api/main.py - FastAPI Main Application.
Note: This tests the structural and configurable parts of the main API module.
Full endpoint testing requires complex mocking of database and auth systems.
"""
import os
import warnings
from unittest.mock import MagicMock, patch
import pytest
class TestAppMetadata:
"""Test FastAPI app metadata."""
def test_import(self):
"""Test that main module can be imported."""
try:
from api import main
assert main.app is not None
except ImportError as e:
# May fail due to missing dependencies in test environment
pytest.skip(f"Cannot import main module: {e}")
def test_app_metadata(self):
"""Test FastAPI app metadata."""
try:
from api.main import app
assert app.title == "Zen-AI-Pentest API"
assert "Professional Pentesting Framework API" in app.description
assert app.version == "2.2.0"
except ImportError:
pytest.skip("Cannot import main module")
class TestCORSConfiguration:
"""Test CORS configuration."""
def test_default_cors_origins(self):
"""Test default CORS origins parsing."""
cors_str = "http://localhost:3000,http://localhost:8000"
origins = [origin.strip() for origin in cors_str.split(",")]
assert origins == ["http://localhost:3000", "http://localhost:8000"]
def test_custom_cors_origins(self):
"""Test custom CORS origins parsing."""
cors_str = "https://app.example.com, https://api.example.com"
origins = [origin.strip() for origin in cors_str.split(",")]
assert origins == [
"https://app.example.com",
"https://api.example.com",
]
def test_single_origin(self):
"""Test single CORS origin."""
cors_str = "https://example.com"
origins = [origin.strip() for origin in cors_str.split(",")]
assert origins == ["https://example.com"]
class TestAdminCredentials:
"""Test admin credential configuration."""
def test_verify_admin_credentials_success(self):
"""Test successful admin credential verification."""
import hmac
# Simulate the verification function
admin_username = "admin"
admin_password = "secret123"
username = "admin"
password = "secret123"
result = hmac.compare_digest(username, admin_username) and hmac.compare_digest(password, admin_password)
assert result is True
def test_verify_admin_credentials_wrong_username(self):
"""Test failed verification with wrong username."""
import hmac
admin_username = "admin"
admin_password = "secret123"
username = "wrong"
password = "secret123"
result = hmac.compare_digest(username, admin_username) and hmac.compare_digest(password, admin_password)
assert result is False
def test_verify_admin_credentials_wrong_password(self):
"""Test failed verification with wrong password."""
import hmac
admin_username = "admin"
admin_password = "secret123"
username = "admin"
password = "wrong"
result = hmac.compare_digest(username, admin_username) and hmac.compare_digest(password, admin_password)
assert result is False
def test_hmac_timing_safe(self):
"""Test that HMAC comparison is timing-safe."""
import hmac
# hmac.compare_digest should work with any strings
result1 = hmac.compare_digest("test", "test")
result2 = hmac.compare_digest("test", "different")
assert result1 is True
assert result2 is False
class TestEnvironmentVariables:
"""Test environment variable configuration."""
def test_cors_origins_from_env(self):
"""Test CORS origins from environment variable."""
with patch.dict(os.environ, {"CORS_ORIGINS": "https://app.com,https://api.com"}):
cors_str = os.getenv("CORS_ORIGINS", "http://localhost:3000")
origins = [origin.strip() for origin in cors_str.split(",")]
assert origins == ["https://app.com", "https://api.com"]
def test_cors_origins_fallback(self):
"""Test CORS origins fallback when env not set."""
with patch.dict(os.environ, {}, clear=True):
cors_str = os.getenv("CORS_ORIGINS", "http://localhost:3000,http://localhost:8000")
origins = [origin.strip() for origin in cors_str.split(",")]
assert origins == [
"http://localhost:3000",
"http://localhost:8000",
]
def test_admin_username_from_env(self):
"""Test admin username from environment variable."""
with patch.dict(os.environ, {"ADMIN_USERNAME": "custom_admin"}):
username = os.getenv("ADMIN_USERNAME", "admin")
assert username == "custom_admin"
def test_admin_username_default(self):
"""Test admin username default value."""
with patch.dict(os.environ, {}, clear=True):
username = os.getenv("ADMIN_USERNAME", "admin")
assert username == "admin"
def test_admin_password_from_env(self):
"""Test admin password from environment variable."""
with patch.dict(os.environ, {"ADMIN_PASSWORD": "secure_pass"}):
password = os.getenv("ADMIN_PASSWORD")
assert password == "secure_pass"
class TestAuthConstants:
"""Test authentication constants."""
def test_allowed_methods(self):
"""Test allowed HTTP methods for CORS."""
allowed_methods = ["GET", "POST", "PUT", "DELETE", "PATCH"]
assert "GET" in allowed_methods
assert "POST" in allowed_methods
assert "OPTIONS" not in allowed_methods # Not in the list
def test_allowed_headers(self):
"""Test allowed headers for CORS."""
allowed_headers = ["Authorization", "Content-Type", "X-Request-ID"]
assert "Authorization" in allowed_headers
assert "Content-Type" in allowed_headers
assert "X-Request-ID" in allowed_headers
def test_exposed_headers(self):
"""Test exposed headers."""
exposed_headers = ["X-Request-ID"]
assert "X-Request-ID" in exposed_headers
class TestTokenExpiration:
"""Test token expiration times."""
def test_legacy_token_expiry(self):
"""Test legacy token expiry time (24 hours)."""
expires_in = 86400 # 24 hours in seconds
assert expires_in == 86400
assert expires_in / 3600 == 24 # 24 hours
def test_new_auth_token_expiry(self):
"""Test new auth token expiry time (15 minutes)."""
expires_in = 900 # 15 minutes in seconds
assert expires_in == 900
assert expires_in / 60 == 15 # 15 minutes
class TestAPIVersions:
"""Test API versioning."""
def test_api_version(self):
"""Test current API version."""
try:
from api.main import app
assert app.version == "2.2.0"
except ImportError:
pytest.skip("Cannot import main module")
def test_version_format(self):
"""Test version follows semantic versioning."""
version = "2.2.0"
parts = version.split(".")
assert len(parts) == 3
assert all(p.isdigit() for p in parts)
class TestRateLimitingConfig:
"""Test rate limiting configuration."""
def test_rate_limit_config_exists(self):
"""Test that rate limiting is imported."""
try:
from api.main import check_auth_rate_limit
assert callable(check_auth_rate_limit)
except ImportError:
pytest.skip("Cannot import rate limiter")
class TestMiddlewareConfig:
"""Test middleware configuration."""
def test_cors_max_age(self):
"""Test CORS max age configuration."""
max_age = 600 # 10 minutes
assert max_age == 600
assert max_age / 60 == 10 # 10 minutes
class TestImportStructure:
"""Test import structure of main module."""
def test_schemas_import(self):
"""Test that schemas are imported."""
try:
from api import schemas
assert hasattr(schemas, "ScanCreate")
assert hasattr(schemas, "ScanResponse")
assert hasattr(schemas, "TokenResponse")
assert hasattr(schemas, "UserLogin")
except ImportError:
pytest.skip("Cannot import schemas")
def test_database_models_import(self):
"""Test that database models are imported."""
try:
from database import models
assert hasattr(models, "Report")
assert hasattr(models, "SessionLocal")
except ImportError:
pytest.skip("Cannot import database models")
class TestAuthFlowConstants:
"""Test authentication flow constants."""
def test_www_authenticate_header(self):
"""Test WWW-Authenticate header value."""
header_value = "Bearer"
assert header_value == "Bearer"
def test_authorization_header_prefix(self):
"""Test Authorization header prefix."""
prefix = "Bearer "
assert prefix == "Bearer "
assert len(prefix) == 7
def test_token_type(self):
"""Test token type in response."""
token_type = "bearer"
assert token_type == "bearer"
class TestSecurityHeaders:
"""Test security-related configurations."""
def test_csrf_protection_import(self):
"""Test CSRF protection is imported."""
try:
from api.csrf_protection import csrf_protection, require_csrf
# csrf_protection is an object, not a function
assert csrf_protection is not None
assert callable(require_csrf)
except ImportError:
pytest.skip("Cannot import CSRF protection")
def test_websocket_manager_import(self):
"""Test WebSocket manager is imported."""
try:
from api.websocket import ConnectionManager
assert ConnectionManager is not None
except ImportError:
pytest.skip("Cannot import WebSocket manager")
class TestDefaultValues:
"""Test default configuration values."""
def test_default_page_size(self):
"""Test default pagination (if applicable)."""
# Common default page sizes
default_page_sizes = [10, 20, 50, 100]
assert 20 in default_page_sizes
assert 50 in default_page_sizes
def test_default_timeout(self):
"""Test default timeout values."""
# Common timeout values in seconds
default_timeouts = [10, 30, 60]
assert 30 in default_timeouts
assert 60 in default_timeouts
class TestLoggingConfiguration:
"""Test logging configuration."""
def test_logger_name(self):
"""Test logger name follows convention."""
logger_name = "ZenAI"
assert "ZenAI" in logger_name
def test_log_level_info(self):
"""Test default log level is INFO."""
import logging
assert logging.INFO == 20
class TestAllExports:
"""Test that key exports are available."""
def test_main_exports(self):
"""Test main module exports."""
try:
from api import main
# Check key exports exist
assert hasattr(main, "app")
assert hasattr(main, "verify_token")
assert hasattr(main, "ws_manager")
# Check configuration variables
assert hasattr(main, "ALLOWED_ORIGINS")
assert hasattr(main, "ADMIN_USERNAME")
except ImportError as e:
pytest.skip(f"Cannot import main module: {e}")

Xet Storage Details

Size:
11.8 kB
·
Xet hash:
2deab4c63964abc717fce8cf83420030d28f3b154a1899a2897fb18f95e3fb93

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.