test: port the v1 endpoint and browser tests to v2
HTTP tests against a running controller (make test-api HOST=ip). They snapshot every collection through /db and restore it afterwards. v1 behaviour v2 does not have yet is marked as a strict xfail. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9d628aa3c8
commit
4b42391a46
15 files changed
+2055
-1
No files matched your search
@@ -0,0 +1,76 @@
|
||||
"""Black-box HTTP tests against a running controller.
|
||||
|
||||
Ported from the v1 Python app's tests (tests/test_endpoints_pytest.py, tests/test_endpoints.py).
|
||||
Point them at a board with --host <ip> or LED_HOST; without one every test is skipped.
|
||||
The board's stored collections are snapshotted first and written back afterwards.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
# Store collections (main/store.c NAMES), all reachable through GET/PUT /db/<name>
|
||||
COLLECTIONS = ["device", "zone", "preset", "profile", "palette", "pattern",
|
||||
"sequence", "scene", "group", "output", "settings"]
|
||||
|
||||
TIMEOUT = 10
|
||||
|
||||
|
||||
def pytest_addoption(parser):
|
||||
parser.addoption("--host", default=os.environ.get("LED_HOST"),
|
||||
help="controller address (or set LED_HOST)")
|
||||
|
||||
|
||||
def pytest_collection_modifyitems(config, items):
|
||||
if config.getoption("--host"):
|
||||
return
|
||||
skip = pytest.mark.skip(reason="no controller: pass --host or set LED_HOST")
|
||||
for item in items:
|
||||
item.add_marker(skip)
|
||||
|
||||
|
||||
class Client(requests.Session):
|
||||
"""requests.Session that takes paths relative to the controller."""
|
||||
|
||||
def __init__(self, base_url):
|
||||
super().__init__()
|
||||
self.base_url = base_url
|
||||
self.headers.update({"User-Agent": "pytest/requests", "Accept": "application/json"})
|
||||
|
||||
def request(self, method, url, *args, **kwargs):
|
||||
kwargs.setdefault("timeout", TIMEOUT)
|
||||
return super().request(method, self.base_url + url, *args, **kwargs)
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def base_url(request):
|
||||
host = request.config.getoption("--host")
|
||||
return host if host.startswith("http") else f"http://{host}"
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def c(base_url):
|
||||
with Client(base_url) as client:
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def restore_db(request):
|
||||
"""Snapshot every collection before the run and write it back afterwards."""
|
||||
if not request.config.getoption("--host"):
|
||||
yield
|
||||
return
|
||||
client = request.getfixturevalue("c")
|
||||
snapshot = {}
|
||||
for name in COLLECTIONS:
|
||||
resp = client.get(f"/db/{name}")
|
||||
resp.raise_for_status()
|
||||
snapshot[name] = resp.json()
|
||||
yield snapshot
|
||||
failed = []
|
||||
for name, data in snapshot.items():
|
||||
resp = client.put(f"/db/{name}", json=data)
|
||||
if resp.status_code != 200:
|
||||
failed.append(f"{name}: {resp.status_code}")
|
||||
assert not failed, f"could not restore the controller's data: {failed}"
|
||||
Reference in new issue
Block a user