From 069b819d7e390731c2a428ad66c43fdfa467b2ce Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Fri, 30 Jan 2026 03:41:47 +0000 Subject: [PATCH] Initial commit: Add OpenAPI Mock Server project --- .tests/test_server.py | 135 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 .tests/test_server.py diff --git a/.tests/test_server.py b/.tests/test_server.py new file mode 100644 index 0000000..a596fb3 --- /dev/null +++ b/.tests/test_server.py @@ -0,0 +1,135 @@ +"""Tests for the server module.""" + +import pytest +from fastapi.testclient import TestClient + +from openapi_mock.server.server import ( + create_app, + OpenAPIMockServer, + ResponseDelayMiddleware, + AuthMiddleware, +) + + +class TestCreateApp: + """Tests for create_app function.""" + + def test_create_app_with_spec(self, temp_spec_file): + """Test creating a FastAPI app from OpenAPI spec.""" + app = create_app(temp_spec_file) + assert app is not None + assert app.title == "Test API" + + def test_create_app_with_delay(self, temp_spec_file): + """Test creating app with response delay.""" + app = create_app(temp_spec_file, delay_range=(0.1, 0.5)) + assert app is not None + + def test_create_app_with_auth(self, temp_spec_file): + """Test creating app with authentication.""" + app = create_app(temp_spec_file, auth_type="bearer") + assert app is not None + + +class TestOpenAPIMockServer: + """Tests for OpenAPIMockServer class.""" + + def test_server_initialization(self, temp_spec_file): + """Test server initialization from spec.""" + server = OpenAPIMockServer(temp_spec_file) + assert server.spec_path == temp_spec_file + assert server.app is not None + + def test_server_routes(self, temp_spec_file): + """Test that server has expected routes.""" + server = OpenAPIMockServer(temp_spec_file) + routes = [r.path for r in server.app.routes] + assert "/users" in routes + + +class TestServerEndpoints: + """Tests for server endpoint responses.""" + + def test_get_users_endpoint(self, temp_spec_file, sample_openapi_spec): + """Test GET /users endpoint returns valid response.""" + app = create_app(temp_spec_file) + client = TestClient(app) + response = client.get("/users") + assert response.status_code == 200 + data = response.json() + assert isinstance(data, list) + + def test_get_user_by_id_endpoint(self, temp_spec_file): + """Test GET /users/{userId} endpoint.""" + app = create_app(temp_spec_file) + client = TestClient(app) + response = client.get("/users/123") + assert response.status_code == 200 + data = response.json() + assert isinstance(data, dict) + + def test_create_user_endpoint(self, temp_spec_file): + """Test POST /users endpoint.""" + app = create_app(temp_spec_file) + client = TestClient(app) + response = client.post("/users", json={"name": "test"}) + assert response.status_code == 200 + + +class TestAuthenticationMiddleware: + """Tests for authentication middleware.""" + + def test_no_auth_required(self, temp_spec_file): + """Test endpoints work without auth when auth is none.""" + app = create_app(temp_spec_file, auth_type="none") + client = TestClient(app) + response = client.get("/users") + assert response.status_code == 200 + + def test_bearer_auth_missing(self, temp_spec_file): + """Test request fails without Bearer token.""" + app = create_app(temp_spec_file, auth_type="bearer") + client = TestClient(app) + response = client.get("/users") + assert response.status_code == 401 + + def test_bearer_auth_valid(self, temp_spec_file): + """Test request succeeds with valid Bearer token.""" + app = create_app(temp_spec_file, auth_type="bearer") + client = TestClient(app) + response = client.get("/users", headers={"Authorization": "Bearer test-token"}) + assert response.status_code == 200 + + def test_api_key_missing(self, temp_spec_file): + """Test request fails without API key.""" + app = create_app(temp_spec_file, auth_type="api_key") + client = TestClient(app) + response = client.get("/users") + assert response.status_code == 401 + + def test_api_key_valid(self, temp_spec_file): + """Test request succeeds with valid API key.""" + app = create_app( + temp_spec_file, + auth_type="api_key", + auth_config={"api_keys": ["test-key-123"]} + ) + client = TestClient(app) + response = client.get("/users", headers={"X-API-Key": "test-key-123"}) + assert response.status_code == 200 + + def test_basic_auth_missing(self, temp_spec_file): + """Test request fails without Basic auth.""" + app = create_app(temp_spec_file, auth_type="basic") + client = TestClient(app) + response = client.get("/users") + assert response.status_code == 401 + + def test_basic_auth_valid(self, temp_spec_file): + """Test request succeeds with valid Basic auth.""" + import base64 + app = create_app(temp_spec_file, auth_type="basic") + client = TestClient(app) + credentials = base64.b64encode(b"user:password").decode() + response = client.get("/users", headers={"Authorization": f"Basic {credentials}"}) + assert response.status_code == 200