fix(sfm): start/stop HTTP status is an allow-list -- only verified, skipped or dry-run is 200
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
1 parent
c3e7e169c0
commit
ac33c8ffa4
2 files changed
+26
-7
No files matched your search
+4
-3
@@ -388,9 +388,10 @@ def _change_response(fn) -> JSONResponse:
|
||||
DeviceError mapping is _via_device_access's -- not repeated here.
|
||||
"""
|
||||
body = _via_device_access(fn)
|
||||
status = 200
|
||||
if body["outcome"] == "failed":
|
||||
status = 409 if body["verified"] == "mismatch" else 502
|
||||
# An allow-list: only a dry run, a skip, or a change read back as made is a 200.
|
||||
ok = (body["outcome"] in ("dry_run", "skipped")
|
||||
or (body["outcome"] == "sent" and body["verified"] == "ok"))
|
||||
status = 200 if ok else (409 if body["verified"] == "mismatch" else 502)
|
||||
return JSONResponse(status_code=status, content=body)
|
||||
|
||||
|
||||
|
||||
@@ -325,7 +325,19 @@ def test_a_lost_ack_with_a_good_read_back_is_502(monkeypatch):
|
||||
_s4(monkeypatch, fake)
|
||||
resp = srv.device_monitor_start(**CH)
|
||||
assert resp.status_code == 502
|
||||
assert _body(resp)["verified"] == "not_checked" or _body(resp)["outcome"] == "failed"
|
||||
body = _body(resp)
|
||||
assert body["outcome"] == "failed"
|
||||
assert body["verified"] == "not_checked"
|
||||
assert body["sent"] is True
|
||||
assert "no clean ack" in body["detail"]["error"]
|
||||
|
||||
|
||||
def test_sent_but_unverified_is_never_a_200():
|
||||
from sfm.device_access.models import ChangeResult
|
||||
r = ChangeResult(serial="UM20147", series=4, action="start monitoring", dry_run=False,
|
||||
sent=True, outcome="sent", verified="not_checked",
|
||||
state_before="idle", state_after=None, audit_id="c1")
|
||||
assert srv._change_response(lambda: r).status_code == 502
|
||||
|
||||
|
||||
def test_stop_by_serial_works(monkeypatch):
|
||||
@@ -336,9 +348,15 @@ def test_stop_by_serial_works(monkeypatch):
|
||||
|
||||
|
||||
@pytest.mark.parametrize("fn", ["device_monitor_start", "device_monitor_stop"])
|
||||
@pytest.mark.parametrize("run_state,exc,status", [(0x03, CallHomeInProgress, 503)])
|
||||
def test_a_device_error_maps_to_its_own_status(monkeypatch, fn, run_state, exc, status):
|
||||
_s4(monkeypatch, FakeMicromateClient(run_states=[run_state]))
|
||||
@pytest.mark.parametrize("exc,status", [(CallHomeInProgress("calling home"), 503),
|
||||
(WrongUnit("other unit"), 409),
|
||||
(NoAnswer("silent"), 504)])
|
||||
def test_a_device_error_maps_to_its_own_status(monkeypatch, fn, exc, status):
|
||||
class Raises:
|
||||
def start_monitoring(self, *a, **kw):
|
||||
raise exc
|
||||
stop_monitoring = start_monitoring
|
||||
monkeypatch.setattr(srv, "_device_access_instance", Raises())
|
||||
with pytest.raises(HTTPException) as e:
|
||||
getattr(srv, fn)(**CH)
|
||||
assert e.value.status_code == status
|
||||
|
||||
Reference in new issue
Block a user