diff --git a/sfm/server.py b/sfm/server.py index 5f1a320..04ce048 100644 --- a/sfm/server.py +++ b/sfm/server.py @@ -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) diff --git a/tests/test_server_device_access.py b/tests/test_server_device_access.py index 9f28a60..6858caa 100644 --- a/tests/test_server_device_access.py +++ b/tests/test_server_device_access.py @@ -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