Files
led-controller/test/api/conftest.py
T
jimmyandClaude Haiku 5.5 edf35e9c71 Channel 11 defaults, ESP-NOW driver config and rig/desktop updates
Moves the controller, bridge and led-driver default ESP-NOW channel to 11 and
updates the led-driver and led-tool submodule pointers. Includes the rest of the
working tree: web UI, desktop and sim-controller changes, API and bridge code,
graphify output, and the .gitea and fixture-driver folders.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-11-21 01:19:02 +13:00

96 lines
3.2 KiB
Python

"""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
from helpers import new_id, unique
# 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", "dmx_definition", "dmx_bridge"]
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
def profile(c):
"""A new profile, made current for the test and deleted afterwards."""
resp = c.post("/profiles", json={"name": unique("pytest-profile")})
assert resp.status_code == 201
pid = new_id(resp)
assert c.post(f"/profiles/{pid}/apply").status_code == 200
yield pid
c.delete(f"/profiles/{pid}")
@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")
if client.get("/auth").json().get("enabled"):
# test_auth.py turned sign-in on: the snapshot needs editor rights, so the data is left as it is
yield
return
snapshot = {}
for name in COLLECTIONS:
resp = client.get(f"/db/{name}")
resp.raise_for_status()
snapshot[name] = resp.json()
yield snapshot
if client.get("/auth").json().get("enabled"):
return # test_auth.py switched sign-in on during the run: it cannot be restored without an admin
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}"