Spaces:
Sleeping
Sleeping
| """Pytest fixtures for testing FastAPI application components""" | |
| import pytest | |
| from fastapi import FastAPI | |
| from fastapi.testclient import TestClient | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.templating import Jinja2Templates | |
| from starlette.middleware.sessions import SessionMiddleware | |
| from utils.utility import USER_CREDENTIALS | |
| from main import app, SECRET_KEY, SESSION_TIMEOUT | |
| def client(): | |
| """Create a test client with session middleware""" | |
| # Create a fresh FastAPI application for testing | |
| test_app = FastAPI() | |
| # Add SessionMiddleware | |
| test_app.add_middleware( | |
| SessionMiddleware, | |
| secret_key=SECRET_KEY, | |
| session_cookie="api_comparator_session", | |
| max_age=SESSION_TIMEOUT | |
| ) | |
| # Configure templates and static files | |
| test_app.mount("/static", StaticFiles(directory="static"), name="static") | |
| templates = Jinja2Templates(directory="templates") | |
| test_app.state.templates = templates | |
| # Include all routes from the main app | |
| for route in app.routes: | |
| test_app.router.routes.append(route) | |
| # Create and return the test client | |
| return TestClient(test_app) | |
| def test_user(): | |
| """Return test user credentials""" | |
| return { | |
| "username": next(iter(USER_CREDENTIALS.keys())), | |
| "password": next(iter(USER_CREDENTIALS.values())) | |
| } | |
| def mock_apis(): | |
| """Return mock API configurations""" | |
| return { | |
| "api1": { | |
| "url": "http://api1.example.com", | |
| "method": "GET", | |
| "payload": "{}", | |
| "headers": "{}" | |
| }, | |
| "api2": { | |
| "url": "http://api2.example.com", | |
| "method": "GET", | |
| "payload": "{}", | |
| "headers": "{}" | |
| } | |
| } |