The controller checks every route against the signed-in user's role, with levels none, view, control and edit per area. Guests get the permissions of a built-in guest role. Auth stays off until the first user exists; LED_ADMIN_USER and LED_ADMIN_PASSWORD create the first admin. Sessions are cookies, passwords are PBKDF2-SHA256, and logins lock per username after five failures. The simulator's WebSocket checks the view permission at handshake and per frame; /ws needs devices control. The web UI gets a sign-in button, a sign-in prompt on refused changes, and a users and roles screen. Also: the device build gets the bridge include path and forward type it was missing, Dockerfile.sim runs non-root, and Dockerfile.prod with docker-compose.prod.yml runs the controller on an internal network behind Caddy for HTTPS. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
96 lines
3.1 KiB
Python
96 lines
3.1 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"]
|
|
|
|
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}"
|