From ea77b6ee4af3178e113049f30928510c3e4d63c8 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Fri, 6 Feb 2026 04:51:43 +0000 Subject: [PATCH] Add unit tests for spec parser and auth --- tests/conftest.py | 212 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..a0fcb4f --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,212 @@ +"""Pytest configuration and fixtures.""" +import pytest +import tempfile +import json +from pathlib import Path + + +@pytest.fixture +def sample_openapi_spec(): + """Sample OpenAPI 3.0 specification for testing.""" + return {{ + "openapi": "3.0.0", + "info": {{ + "title": "Test Pet Store API", + "version": "1.0.0", + "description": "A sample API for testing" + }}, + "servers": [ + {{"url": "https://api.example.com/v1"}} + ], + "paths": {{ + "/pets": {{ + "get": {{ + "summary": "List all pets", + "description": "Returns a list of pets", + "operationId": "listPets", + "tags": ["pets"], + "parameters": [ + {{ + "name": "limit", + "in": "query", + "description": "Maximum number of pets to return", + "schema": {{"type": "integer"}} + }}, + {{ + "name": "status", + "in": "query", + "description": "Filter by status", + "schema": {{"type": "string"}} + }} + ], + "responses": {{ + "200": {{ + "description": "A list of pets", + "content": {{ + "application/json": {{ + "schema": {{ + "type": "array", + "items": {{"$ref": "#/components/schemas/Pet"}} + }} + }} + }} + }} + }} + }}, + "post": {{ + "summary": "Create a pet", + "description": "Creates a new pet", + "operationId": "createPet", + "tags": ["pets"], + "requestBody": {{ + "required": True, + "content": {{ + "application/json": {{ + "schema": {{"$ref": "#/components/schemas/Pet"}} + }} + }} + }}, + "responses": {{ + "201": {{ + "description": "Pet created successfully" + }} + }} + }} + }}, + "/pets/{petId}": {{ + "get": {{ + "summary": "Get a pet by ID", + "description": "Returns a single pet", + "operationId": "getPetById", + "tags": ["pets"], + "parameters": [ + {{ + "name": "petId", + "in": "path", + "required": True, + "schema": {{"type": "string"}} + }} + ], + "responses": {{ + "200": {{ + "description": "A single pet", + "content": {{ + "application/json": {{ + "schema": {{"$ref": "#/components/schemas/Pet"}} + }} + }} + }}, + "404": {{ + "description": "Pet not found" + }} + }} + }}, + "delete": {{ + "summary": "Delete a pet", + "description": "Deletes a pet by ID", + "operationId": "deletePet", + "tags": ["pets"], + "parameters": [ + {{ + "name": "petId", + "in": "path", + "required": True, + "schema": {{"type": "string"}} + }} + ], + "responses": {{ + "204": {{ + "description": "Pet deleted successfully" + }} + }} + }} + }} + }}, + "components": {{ + "securitySchemes": {{ + "ApiKeyAuth": {{ + "type": "apiKey", + "name": "X-API-Key", + "in": "header" + }}, + "BearerAuth": {{ + "type": "http", + "scheme": "bearer" + }} + }}, + "schemas": {{ + "Pet": {{ + "type": "object", + "required": ["name", "status"], + "properties": {{ + "id": {{"type": "string"}}, + "name": {{"type": "string"}}, + "status": {{"type": "string", "enum": ["available", "pending", "sold"]}}, + "tags": {{"type": "array", "items": {{"type": "string"}}} + }} + }} + }} + }} + }} + + +@pytest.fixture +def sample_swagger_spec(): + """Sample OpenAPI 2.0 (Swagger) specification for testing.""" + return {{ + "swagger": "2.0", + "info": {{ + "title": "Test Swagger API", + "version": "1.0.0" + }}, + "basePath": "/v1", + "paths": {{ + "/users": {{ + "get": {{ + "summary": "List users", + "responses": {{ + "200": {{ + "description": "A list of users", + "schema": {{ + "type": "array", + "items": {{"$ref": "#/definitions/User"}} + }} + }} + }} + }} + }} + }}, + "definitions": {{ + "User": {{ + "type": "object", + "properties": {{ + "id": {{"type": "string"}}, + "name": {{"type": "string"}} + }} + }} + }}, + "securityDefinitions": {{ + "api_key": {{ + "type": "apiKey", + "name": "api_key", + "in": "header" + }} + }} + }} + + +@pytest.fixture +def temp_spec_file(sample_openapi_spec): + """Create a temporary OpenAPI spec file.""" + with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: + import yaml + yaml.dump(sample_openapi_spec, f) + return Path(f.name) + + +@pytest.fixture +def temp_json_spec_file(sample_openapi_spec): + """Create a temporary OpenAPI spec JSON file.""" + with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: + json.dump(sample_openapi_spec, f) + return Path(f.name)