File size: 1,794 Bytes
2d9b352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""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


@pytest.fixture
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)


@pytest.fixture
def test_user():
    """Return test user credentials"""
    return {
        "username": next(iter(USER_CREDENTIALS.keys())),
        "password": next(iter(USER_CREDENTIALS.values()))
    }


@pytest.fixture
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": "{}"
        }
    }