fix(sfm): one guard in front of every /device route -- UM serials 501 off the serial-aware routes, held lines 409 serial-less requests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
serversdownandClaude Opus 5.5 committed 2026-10-21 01:44:35 -04:00
1 parent 6dd34f8fd7
commit 82b91835fb
4 files changed
+109 -1

No files matched your search

+10
View File
@@ -221,3 +221,13 @@ def test_both_series_return_the_same_model_with_the_same_fields(op):
a, b = getattr(s3, name), getattr(s4, name)
if a is not None and b is not None and name != "detail":
assert type(a) is type(b) or {type(a), type(b)} <= {int, float}, name
def test_held_serial_reports_an_open_line_only():
fake = FakeMicromateClient()
da = access(s4=[fake])
assert da.held_serial(ADDR) is None
da.status(ADDR, "UM20147")
assert da.held_serial(ADDR) == "UM20147"
da.hang_up(ADDR)
assert da.held_serial(ADDR) is None
+45
View File
@@ -90,3 +90,48 @@ def test_without_a_serial_the_legacy_path_runs_unchanged(monkeypatch):
monkeypatch.setattr(srv, "get_cache", lambda: Cache())
out = srv.device_info(force=False, serial=None, **TCP)
assert out == {"serial": "BE18193", "firmware_version": "S338.17", "_cached": True}
# ── the /device/* guard ───────────────────────────────────────────────────────
from sfm.device_access.models import UnitAddress # noqa: E402
ADDR = UnitAddress(host="10.0.0.5", tcp_port=9034, port=None, baud=None)
Q_TCP = {"host": "10.0.0.5", "tcp_port": "9034"}
@pytest.mark.parametrize("path", ["/device/events/erase", "/device/monitor/stop", "/device/config"])
def test_guard_refuses_a_um_serial_on_unsupported_routes_with_501(path):
out = srv._device_guard(path, {"serial": "um20147", **Q_TCP})
assert out is not None and out[0] == 501 and path in out[1]
def test_guard_lets_a_series3_serial_through_on_legacy_routes():
assert srv._device_guard("/device/events/erase", {"serial": "BE18193", **Q_TCP}) is None
@pytest.mark.parametrize("path", ["/device/info", "/device/connect",
"/device/monitor/status", "/device/settings",
"/device/hangup"])
def test_guard_allows_um_serial_on_serial_aware_routes_and_hangup(path):
assert srv._device_guard(path, {"serial": "UM20147", **Q_TCP}) is None
def test_guard_refuses_serialless_requests_at_a_held_address_with_409(da):
da.status(ADDR, "UM20147")
out = srv._device_guard("/device/monitor/stop", dict(Q_TCP))
assert out is not None and out[0] == 409
assert "UM20147" in out[1] and "tcp:10.0.0.5:9034" in out[1] and "hangup" in out[1]
def test_guard_allows_serialless_requests_at_an_unheld_address(da):
assert srv._device_guard("/device/monitor/stop", dict(Q_TCP)) is None
assert srv._device_guard("/device/monitor/stop", {"host": "10.9.9.9"}) is None
def test_guard_ignores_non_device_paths(da):
assert srv._device_guard("/db/events", {"serial": "UM20147", **Q_TCP}) is None
def test_guard_allows_an_unbuildable_address_through(da):
assert srv._device_guard("/device/monitor/stop", {}) is None