Buckets:

download
raw
10.9 kB
import pytest
pytestmark = pytest.mark.skip(reason="Stale integration tests")
"""
Tests for api/main.py - FastAPI Application
Target: 80% Coverage
"""
import json
import sys
from datetime import datetime
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
# Add project root to path
sys.path.insert(0, str(Path(__file__).parent.parent))
# Import after path setup
from api.main import app
# Try to import optional dependencies
try:
from api.main import verify_token
except ImportError:
verify_token = None
try:
from api.main import check_rate_limit
except ImportError:
check_rate_limit = None
from api.schemas import ScanCreate, ScanStatus, Severity
# Create test client
client = TestClient(app)
class TestAppInitialization:
"""Test FastAPI app initialization and middleware"""
def test_app_exists(self):
"""Test that app is created"""
assert app is not None
assert app.title == "Zen-AI-Pentest API"
def test_cors_middleware(self):
"""Test CORS middleware is configured"""
# Check if CORS middleware is present
middleware = [m for m in app.user_middleware if m.cls.__name__ == "CORSMiddleware"]
assert len(middleware) > 0
def test_docs_endpoint(self):
"""Test /docs endpoint (Swagger UI)"""
response = client.get("/docs")
assert response.status_code == 200
assert "swagger" in response.text.lower()
def test_openapi_endpoint(self):
"""Test /openapi.json endpoint"""
response = client.get("/openapi.json")
assert response.status_code == 200
data = response.json()
assert "openapi" in data
assert data["info"]["title"] == "Zen-AI-Pentest API"
class TestHealthEndpoints:
"""Test health check endpoints"""
def test_health_endpoint(self):
"""Test GET /health"""
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert "status" in data
assert data["status"] == "healthy"
def test_ready_endpoint(self):
"""Test GET /ready"""
response = client.get("/ready")
assert response.status_code == 200
data = response.json()
assert "ready" in data
def test_status_endpoint(self):
"""Test GET /status"""
response = client.get("/status")
assert response.status_code == 200
data = response.json()
assert "status" in data
class TestAuthEndpoints:
"""Test authentication endpoints"""
def test_login_without_body(self):
"""Test POST /auth/login without body"""
response = client.post("/auth/login")
assert response.status_code == 422 # Validation error
def test_login_with_invalid_body(self):
"""Test POST /auth/login with invalid body"""
response = client.post("/auth/login", json={})
assert response.status_code == 422
def test_login_with_valid_body(self):
"""Test POST /auth/login with valid credentials"""
response = client.post("/auth/login", json={"username": "admin", "password": "admin"})
# May fail with 401, but should not crash
assert response.status_code in [200, 401]
def test_register_endpoint_exists(self):
"""Test POST /auth/register endpoint exists"""
response = client.post(
"/auth/register",
json={"username": "testuser", "password": "testpass123"},
)
# Should either succeed or give validation error, not 404
assert response.status_code != 404
class TestScanEndpoints:
"""Test scan-related endpoints"""
def test_get_scans_unauthorized(self):
"""Test GET /scans without auth"""
response = client.get("/api/v1/scans")
# Should be 401 or 403 (unauthorized)
assert response.status_code in [401, 403, 200] # 200 if public
def test_create_scan_without_auth(self):
"""Test POST /scans without auth"""
scan_data = {
"name": "Test Scan",
"target": "example.com",
"scan_type": "port_scan",
}
response = client.post("/api/v1/scans", json=scan_data)
assert response.status_code in [401, 403, 422, 200]
def test_get_scan_by_id_invalid(self):
"""Test GET /scans/{id} with invalid ID"""
response = client.get("/api/v1/scans/invalid")
assert response.status_code in [400, 404, 422]
def test_update_scan_invalid_id(self):
"""Test PUT /scans/{id} with invalid ID"""
response = client.put("/api/v1/scans/invalid", json={"status": "running"})
assert response.status_code in [400, 404, 422]
def test_delete_scan_invalid_id(self):
"""Test DELETE /scans/{id} with invalid ID"""
response = client.delete("/api/v1/scans/invalid")
assert response.status_code in [400, 404, 422]
class TestFindingEndpoints:
"""Test finding endpoints"""
def test_get_findings_endpoint_exists(self):
"""Test GET /findings endpoint"""
response = client.get("/api/v1/findings")
# Should exist, may require auth
assert response.status_code in [200, 401, 403]
def test_create_finding_without_auth(self):
"""Test POST /findings without auth"""
finding_data = {
"title": "Test Finding",
"description": "Test",
"severity": "medium",
"target": "example.com",
}
response = client.post("/api/v1/findings", json=finding_data)
assert response.status_code in [401, 403, 200, 422]
class TestReportEndpoints:
"""Test report endpoints"""
def test_get_reports_endpoint(self):
"""Test GET /reports"""
response = client.get("/api/v1/reports")
assert response.status_code in [200, 401, 403]
def test_create_report_endpoint(self):
"""Test POST /reports"""
report_data = {"scan_id": 1, "format": "pdf"}
response = client.post("/api/v1/reports", json=report_data)
assert response.status_code in [200, 401, 403, 422]
class TestToolEndpoints:
"""Test tool execution endpoints"""
def test_list_tools_endpoint(self):
"""Test GET /tools"""
response = client.get("/api/v1/tools")
assert response.status_code in [200, 401, 403]
def test_execute_tool_endpoint(self):
"""Test POST /tools/execute"""
execute_data = {
"tool_name": "nmap",
"parameters": {"target": "127.0.0.1"},
}
response = client.post("/api/v1/tools/execute", json=execute_data)
assert response.status_code in [200, 401, 403, 422]
class TestAgentEndpoints:
"""Test agent endpoints"""
def test_get_agents_endpoint(self):
"""Test GET /agents"""
response = client.get("/api/v1/agents")
assert response.status_code in [200, 401, 403]
def test_create_agent_task_endpoint(self):
"""Test POST /agents/tasks"""
task_data = {"task_type": "recon", "target": "example.com"}
response = client.post("/api/v1/agents/tasks", json=task_data)
assert response.status_code in [200, 401, 403, 422]
class TestUserEndpoints:
"""Test user management endpoints"""
def test_get_users_admin_only(self):
"""Test GET /users (admin only)"""
response = client.get("/api/v1/users")
assert response.status_code in [200, 401, 403]
def test_get_current_user_unauthorized(self):
"""Test GET /users/me without auth"""
response = client.get("/api/v1/users/me")
assert response.status_code in [401, 403]
class TestWebsocketEndpoints:
"""Test WebSocket endpoints"""
def test_websocket_scan_endpoint_exists(self):
"""Test WebSocket /ws/scans/{id} exists"""
# WebSocket connections can't be tested with standard client
# but we can check if endpoint is registered
routes = [r for r in app.routes if hasattr(r, "path") and "/ws" in r.path]
assert len(routes) > 0
class TestErrorHandling:
"""Test error handling"""
def test_404_handler(self):
"""Test 404 error handler"""
response = client.get("/nonexistent-endpoint-12345")
assert response.status_code == 404
def test_invalid_json_body(self):
"""Test handling of invalid JSON"""
response = client.post(
"/api/v1/scans",
data="invalid json",
headers={"Content-Type": "application/json"},
)
assert response.status_code == 422
def test_method_not_allowed(self):
"""Test 405 method not allowed"""
response = client.delete("/health") # DELETE not allowed on /health
assert response.status_code == 405
class TestMiddleware:
"""Test middleware functionality"""
def test_request_id_header(self):
"""Test that request ID is added to response"""
response = client.get("/health")
# Check for common request ID headers
assert "x-request-id" in response.headers or True # May not be implemented
def test_security_headers(self):
"""Test security headers are present"""
response = client.get("/health")
# Common security headers
headers = response.headers
# At least some security headers should be present
security_headers = [
"x-content-type-options",
"x-frame-options",
"x-xss-protection",
"content-security-policy",
]
has_security = any(h in headers for h in security_headers)
# May not have all, but should have CORS at minimum
class TestConfiguration:
"""Test configuration loading"""
def test_app_configuration_exists(self):
"""Test that app has configuration"""
assert hasattr(app, "state")
def test_debug_mode_not_enabled(self):
"""Test that debug mode is not enabled in production"""
# Debug should be False for production
assert not app.debug
# Parameterized tests for multiple endpoints
@pytest.mark.parametrize(
"endpoint,method",
[
("/health", "GET"),
("/ready", "GET"),
("/status", "GET"),
("/docs", "GET"),
("/openapi.json", "GET"),
],
)
def test_common_endpoints(endpoint, method):
"""Test common endpoints return 200"""
func = getattr(client, method.lower())
response = func(endpoint)
assert response.status_code == 200
@pytest.mark.parametrize(
"endpoint",
[
"/api/v1/scans",
"/api/v1/findings",
"/api/v1/reports",
"/api/v1/tools",
"/api/v1/agents",
],
)
def test_api_endpoints_require_auth(endpoint):
"""Test API endpoints either return data or require auth"""
response = client.get(endpoint)
assert response.status_code in [200, 401, 403]

Xet Storage Details

Size:
10.9 kB
·
Xet hash:
d9d1a4714e2710c850fe56671d6e7e4b622a7be93e63c1bed5bbc630b244cb52

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