2025-05-24 07:34:07 +02:00

74 lines
2.4 KiB
Python

import pytest
from fastapi.testclient import TestClient
def test_root_endpoint(client: TestClient):
"""Test the root endpoint of the API"""
response = client.get("/")
# Should get a successful response
assert response.status_code == 200
assert "message" in response.json()
def test_docs_endpoint(client: TestClient):
"""Test the docs endpoint is accessible"""
response = client.get("/docs")
# Should get the Swagger UI
assert response.status_code == 200
assert "swagger" in response.text.lower()
def test_openapi_endpoint(client: TestClient):
"""Test the OpenAPI schema is accessible"""
response = client.get("/openapi.json")
# Should get the OpenAPI schema
assert response.status_code == 200
data = response.json()
# Check basic OpenAPI schema structure
assert "openapi" in data
assert "info" in data
assert "paths" in data
# Check some expected paths
paths = data["paths"]
# Admin endpoints
assert "/api/v1/teams" in paths
assert "/api/v1/auth/api-keys" in paths
# Basic endpoints
assert "/api/v1/auth/verify" in paths
def test_cors_headers(client: TestClient):
"""Test that CORS headers are properly set"""
response = client.options(
"/api/v1/auth/verify",
headers={"Origin": "http://example.com", "Access-Control-Request-Method": "GET"}
)
# Should get a successful response with CORS headers
assert response.status_code == 200
assert "access-control-allow-origin" in response.headers
assert "access-control-allow-methods" in response.headers
assert "access-control-allow-headers" in response.headers
# The allowed origin should be either the request origin or "*"
allowed_origin = response.headers["access-control-allow-origin"]
assert allowed_origin in ["http://example.com", "*"]
def test_error_handling(client: TestClient):
"""Test API error handling for invalid paths"""
response = client.get("/non-existent-path")
# Should get a 404 response with appropriate error details
assert response.status_code == 404
assert "detail" in response.json()
# Test invalid method
response = client.put("/")
assert response.status_code in [404, 405] # Either not found or method not allowed