Buckets:

download
raw
5.61 kB
"""API Authenticated Tests.
Target: +10% Coverage durch authentifizierte API-Tests.
Uses the new v1 API endpoints with proper database initialization.
"""
import pytest
from fastapi.security import HTTPAuthorizationCredentials
from fastapi.testclient import TestClient
from api.auth import create_access_token, decode_token
from api.main import app
from database.models import init_db
client = TestClient(app)
@pytest.fixture(autouse=True)
def setup_database():
"""Initialize database before each test."""
init_db()
yield
@pytest.fixture
def auth_headers():
"""Create authentication headers with a valid v1 JWT token."""
token = create_access_token({"sub": "admin", "role": "admin"})
return {"Authorization": f"Bearer {token}"}
class TestAuthenticatedScans:
"""Authenticated tests for scan endpoints."""
def test_get_scans_authenticated(self, auth_headers):
"""Test GET /scans with authentication."""
response = client.get("/scans", headers=auth_headers)
assert response.status_code in [200, 404, 429]
if response.status_code == 200:
assert isinstance(response.json(), list)
def test_create_scan_authenticated(self, auth_headers):
"""Test POST /scans with authentication."""
scan_data = {
"name": "Test Scan",
"target": "example.com",
"scan_type": "network",
}
response = client.post("/scans", json=scan_data, headers=auth_headers)
assert response.status_code in [200, 201, 400, 403, 422]
class TestAuthenticatedFindings:
"""Authenticated tests for finding endpoints."""
def test_get_findings_authenticated(self, auth_headers):
"""Test GET /findings with authentication."""
response = client.get("/findings", headers=auth_headers)
assert response.status_code in [200, 404, 429]
if response.status_code == 200:
assert isinstance(response.json(), list)
class TestAuthenticatedUsers:
"""Authenticated tests for user endpoints."""
def test_get_current_user_authenticated(self, auth_headers):
"""Test GET /api/v1/auth/me with authentication."""
response = client.get("/api/v1/auth/me", headers=auth_headers)
assert response.status_code in [200, 404, 429]
if response.status_code == 200:
data = response.json()
assert "username" in data or "id" in data
def test_get_users_authenticated(self, auth_headers):
"""Test GET /api/v1/auth/me (users list not exposed directly)."""
response = client.get("/api/v1/auth/me", headers=auth_headers)
assert response.status_code in [200, 403, 404, 429]
class TestAuthenticatedReports:
"""Authenticated tests for report endpoints."""
def test_get_reports_authenticated(self, auth_headers):
"""Test GET /reports with authentication."""
response = client.get("/reports", headers=auth_headers)
assert response.status_code in [200, 404, 429]
if response.status_code == 200:
assert isinstance(response.json(), list)
class TestAuthenticatedTools:
"""Authenticated tests for tool endpoints."""
def test_get_tools_authenticated(self, auth_headers):
"""Test GET /tools with authentication."""
response = client.get("/tools", headers=auth_headers)
assert response.status_code in [200, 404, 429]
class TestAuthenticatedDashboard:
"""Authenticated tests for dashboard endpoints."""
def test_get_dashboard_stats_authenticated(self, auth_headers):
"""Test GET /dashboard/stats with authentication."""
response = client.get("/dashboard/stats", headers=auth_headers)
assert response.status_code in [200, 404, 429]
if response.status_code == 200:
data = response.json()
assert "total_scans" in data
def test_get_dashboard_recent_scans_authenticated(self, auth_headers):
"""Test GET /dashboard/recent-scans with authentication."""
response = client.get("/dashboard/recent-scans", headers=auth_headers)
assert response.status_code in [200, 404, 429]
if response.status_code == 200:
assert isinstance(response.json(), list)
class TestAuthenticatedAgents:
"""Authenticated tests for agent endpoints."""
def test_get_agents_authenticated(self, auth_headers):
"""Test GET /agents with authentication."""
response = client.get("/agents", headers=auth_headers)
assert response.status_code in [200, 404, 429]
class TestAuthenticatedTargets:
"""Authenticated tests for target endpoints."""
def test_get_targets_authenticated(self, auth_headers):
"""Test GET /targets with authentication."""
response = client.get("/targets", headers=auth_headers)
assert response.status_code in [200, 404, 429]
class TestAuthTokenOperations:
"""Tests for token operations."""
def test_token_creation(self):
"""Test that access token can be created."""
from api.auth import create_access_token
token = create_access_token({"sub": "test@example.com"})
assert token is not None
assert isinstance(token, str)
def test_token_decoding(self):
"""Test that access token can be decoded."""
from api.auth import create_access_token
token = create_access_token({"sub": "test@example.com"})
# decode_token returns dict or None
payload = decode_token(token)
assert payload is not None
assert payload.get("sub") == "test@example.com"

Xet Storage Details

Size:
5.61 kB
·
Xet hash:
67300d315a6742f538cf1bd424011fb08d76e5557cad0261f6db058ce7fc94bb

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