Compare commits

...

2 Commits

Author SHA1 Message Date
04cd85fe59 First working version 2026-01-07 18:18:28 +01:00
373b57cec8 first step 2026-01-04 15:08:51 +01:00
6 changed files with 94 additions and 1 deletions

21
misc/Readme_test.md Normal file
View File

@ -0,0 +1,21 @@
# Tests
___
## schemathesis
Unter dem Verzeichnis ./src/server/tests befinden sich die Test-Cases, die Max bei der ersten Implementierung der Validierung angelegt hat.
Diese sind derzeit nicht "aktiv", bzw. noch nicht bereinigt.
Ich möchte gern ein automatisches Framework verwenden, das aber nur "manuell" betrieben wird. Änderungen an der API sind selten.
Aktueller Stand (7.1.26):
Im Verzeichnis ./src/server/tests kann folgendes ausgeführt werden:
```pytest -q --maxfail=1 contract/test_openapi_fuzz.py```
Das Ganze funktioniert nur, wenn auch schemathesis und hypothesis in den passenden(!) Versionen im lokalen _venv_ installiert sind.
Aktuell habe ich schemathesis ("latest") und hypothesis 6.120.0:
```pip install "hypothesis==6.120.0"```
Das muss wegen dependencies so blöd gepinnt werden.

View File

@ -0,0 +1,6 @@
[project.optional-dependencies]
test = [
"pytest>=7.0",
"schemathesis>=3.0",
"requests>=2.31",
]

View File

@ -20,4 +20,4 @@ pytest
pytest-cov
coverage
../server/.

View File

@ -0,0 +1,45 @@
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://neptun.fritz.box"
# 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}"}

View File

@ -0,0 +1,18 @@
import schemathesis
schema = schemathesis.openapi.from_path("../../../misc/BreCalApi.yaml")
@schema.parametrize()
def test_api_conformance(
case,
base_url: str,
auth_headers: dict[str, str],
login_payload: dict[str, str],
) -> None:
# Calls your real service:
if case.path == "/login" and case.method.upper() == "POST":
response = case.call(base_url=base_url, json=login_payload)
else:
response = case.call(base_url=base_url, headers=auth_headers)
# Validates status code, headers, and body against the OpenAPI schema:
case.validate_response(response)

View File

@ -0,0 +1,3 @@
[pytest]
addopts = -ra
testpaths = tests