46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
import os
|
|
import pytest
|
|
import requests
|
|
|
|
@pytest.fixture(scope="session")
|
|
def base_url() -> str:
|
|
# Example: https://dev.api.mycompany.com
|
|
url = os.environ.get("API_BASE_URL")
|
|
if not url:
|
|
url = "http://127.0.0.1:5000"
|
|
# raise RuntimeError("Set API_BASE_URL")
|
|
return url.rstrip("/")
|
|
|
|
@pytest.fixture(scope="session")
|
|
def login_payload() -> dict[str, str]:
|
|
username = os.environ.get("API_USERNAME")
|
|
if not username:
|
|
username = "Londo"
|
|
password = os.environ.get("API_PASSWORD")
|
|
if not password:
|
|
password = "Hallowach"
|
|
# if not username or not password:
|
|
# raise RuntimeError("Set API_USERNAME and API_PASSWORD")
|
|
return {"username": username, "password": password}
|
|
|
|
@pytest.fixture(scope="session")
|
|
def jwt_token(base_url: str, login_payload: dict[str, str]) -> str:
|
|
# Adapt these to your auth endpoint + response shape:
|
|
login_path = os.environ.get("API_LOGIN_PATH", "/login")
|
|
resp = requests.post(
|
|
f"{base_url}{login_path}",
|
|
json=login_payload,
|
|
timeout=30,
|
|
)
|
|
resp.raise_for_status()
|
|
|
|
data = resp.json()
|
|
token = data.get("access_token") or data.get("token")
|
|
if not token:
|
|
raise RuntimeError("Could not find JWT token in login response JSON")
|
|
return token
|
|
|
|
@pytest.fixture(scope="session")
|
|
def auth_headers(jwt_token: str) -> dict[str, str]:
|
|
return {"Authorization": f"Bearer {jwt_token}"}
|