| 1 |
""" |
| 2 |
Pytest configuration for wire tests. |
| 3 |
|
| 4 |
This module provides helpers for creating a configured client that talks to |
| 5 |
WireMock and for verifying requests in WireMock. |
| 6 |
|
| 7 |
The WireMock container lifecycle itself is managed by a top-level pytest |
| 8 |
plugin (tests/conftest.py) so that the container is started exactly once |
| 9 |
per test run, even when using pytest-xdist. |
| 10 |
""" |
| 11 |
|
| 12 |
import inspect |
| 13 |
import os |
| 14 |
from typing import Any, Dict, Optional |
| 15 |
|
| 16 |
import httpx |
| 17 |
|
| 18 |
from auth0.management.client import Auth0 |
| 19 |
|
| 20 |
|
| 21 |
try: |
| 22 |
_CLIENT_SUPPORTS_HEADERS: bool = "headers" in inspect.signature(Auth0).parameters |
| 23 |
except (TypeError, ValueError): |
| 24 |
_CLIENT_SUPPORTS_HEADERS = False |
| 25 |
|
| 26 |
|
| 27 |
def _get_wiremock_base_url() -> str: |
| 28 |
"""Returns the WireMock base URL from the WIREMOCK_URL environment variable.""" |
| 29 |
return os.environ.get("WIREMOCK_URL", "http://localhost:8080") |
| 30 |
|
| 31 |
|
| 32 |
def get_client(test_id: str) -> Auth0: |
| 33 |
""" |
| 34 |
Creates a configured client instance for wire tests. |
| 35 |
|
| 36 |
Args: |
| 37 |
test_id: Unique identifier for the test, used for request tracking. |
| 38 |
|
| 39 |
Returns: |
| 40 |
A configured client instance with all required auth parameters. |
| 41 |
""" |
| 42 |
test_headers = {"X-Test-Id": test_id} |
| 43 |
base_url = _get_wiremock_base_url() |
| 44 |
|
| 45 |
if _CLIENT_SUPPORTS_HEADERS: |
| 46 |
return Auth0( |
| 47 |
base_url=base_url, |
| 48 |
headers=test_headers, |
| 49 |
token="test_token", |
| 50 |
) |
| 51 |
|
| 52 |
return Auth0( |
| 53 |
base_url=base_url, |
| 54 |
httpx_client=httpx.Client(headers=test_headers), |
| 55 |
token="test_token", |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
def verify_request_count( |
| 60 |
test_id: str, |
| 61 |
method: str, |
| 62 |
url_path: str, |
| 63 |
query_params: Optional[Dict[str, Any]], |
| 64 |
expected: int, |
| 65 |
) -> None: |
| 66 |
"""Verifies the number of requests made to WireMock filtered by test ID for concurrency safety.""" |
| 67 |
wiremock_admin_url = f"{_get_wiremock_base_url()}/__admin" |
| 68 |
request_body: Dict[str, Any] = { |
| 69 |
"method": method, |
| 70 |
"urlPath": url_path, |
| 71 |
"headers": {"X-Test-Id": {"equalTo": test_id}}, |
| 72 |
} |
| 73 |
if query_params: |
| 74 |
query_parameters = {} |
| 75 |
for k, v in query_params.items(): |
| 76 |
if isinstance(v, list): |
| 77 |
query_parameters[k] = {"hasExactly": [{"equalTo": item} for item in v]} |
| 78 |
else: |
| 79 |
query_parameters[k] = {"equalTo": v} |
| 80 |
request_body["queryParameters"] = query_parameters |
| 81 |
response = httpx.post(f"{wiremock_admin_url}/requests/find", json=request_body) |
| 82 |
assert response.status_code == 200, "Failed to query WireMock requests" |
| 83 |
result = response.json() |
| 84 |
requests_found = len(result.get("requests", [])) |
| 85 |
assert requests_found == expected, f"Expected {expected} requests, found {requests_found}" |