|
12 | 12 | from collections.abc import Generator |
13 | 13 | from contextlib import asynccontextmanager |
14 | 14 | from pathlib import Path |
| 15 | +from typing import Any |
15 | 16 | from unittest.mock import Mock, patch |
16 | 17 | from urllib.parse import urlparse |
17 | 18 |
|
18 | 19 | import pytest |
19 | 20 | import requests |
20 | 21 | import schemathesis |
21 | 22 | import uvloop |
| 23 | +from schemathesis import Case, Response |
| 24 | +from schemathesis.generation.stateful.state_machine import APIStateMachine |
22 | 25 | from schemathesis.specs.openapi.schemas import BaseOpenAPISchema |
23 | 26 |
|
24 | 27 | from model_registry import ModelRegistry |
@@ -385,3 +388,36 @@ def _artifact_resource(auth_headers: dict, payload: dict) -> Generator[str, None |
385 | 388 | except Exception as e: |
386 | 389 | print(f"Failed to delete artifact {artifact_id}: {e}") |
387 | 390 | return _artifact_resource |
| 391 | + |
| 392 | +@pytest.fixture |
| 393 | +def auth_headers(setup_env_user_token): |
| 394 | + """Provides authorization headers for API requests.""" |
| 395 | + return { |
| 396 | + "Content-Type": "application/json", |
| 397 | + "Authorization": f"Bearer {setup_env_user_token}" |
| 398 | + } |
| 399 | + |
| 400 | +@pytest.fixture |
| 401 | +def state_machine(generated_schema: BaseOpenAPISchema, auth_headers: str) -> APIStateMachine: |
| 402 | + BaseAPIWorkflow = generated_schema.as_state_machine() |
| 403 | + |
| 404 | + class APIWorkflow(BaseAPIWorkflow): # type: ignore |
| 405 | + headers: dict[str, str] |
| 406 | + |
| 407 | + def setup(self) -> None: |
| 408 | + print("Cleaning up database") |
| 409 | + subprocess.run( |
| 410 | + ["../../scripts/cleanup.sh"], |
| 411 | + capture_output=True, |
| 412 | + check=True |
| 413 | + ) |
| 414 | + self.headers = auth_headers |
| 415 | + |
| 416 | + def before_call(self, case: Case) -> None: |
| 417 | + print(f"Checking: {case.method} {case.path}") |
| 418 | + def get_call_kwargs(self, case: Case) -> dict[str, Any]: |
| 419 | + return {"verify": False, "headers": self.headers} |
| 420 | + |
| 421 | + def after_call(self, response: Response, case: Case) -> None: |
| 422 | + print(f"{case.method} {case.path} -> {response.status_code},") |
| 423 | + return APIWorkflow |
0 commit comments