Raise a clear client error for a refused request
socket_comms now raises DaemonError with the server message if the reply has the status ERROR. Before, the reply reached code that expects device data, and that code raised KeyError.
This commit is contained in:
@@ -91,6 +91,28 @@ class TestReadData:
|
||||
call_args = mock_comms.call_args[0][1]
|
||||
assert call_args.get("ups") == "myups"
|
||||
|
||||
@pytest.mark.parametrize("call", [
|
||||
lambda client: client.list_devices("/fake/socket", "test"),
|
||||
lambda client: client.read_data("/fake/socket", "test"),
|
||||
], ids=["list_devices", "read_data"])
|
||||
def test_error_reply_raises_the_daemon_message(self, call):
|
||||
"""An ERROR reply must not reach the code that expects device data."""
|
||||
from bmspy import client
|
||||
reply = json.dumps({"status": "ERROR", "message": "unknown command: 'DEVICES'"}).encode()
|
||||
sock = MagicMock()
|
||||
sock.recv.side_effect = [struct.pack("!I", len(reply)), reply]
|
||||
with patch("bmspy.client.socket.socket", return_value=sock):
|
||||
with pytest.raises(client.DaemonError, match="unknown command: 'DEVICES'"):
|
||||
call(client)
|
||||
sock.close.assert_called()
|
||||
|
||||
def test_registration_logs_an_error_reply(self, capsys):
|
||||
from bmspy import client
|
||||
with patch("bmspy.client.socket_comms", side_effect=client.DaemonError("daemon refused 'REGISTER': no")), \
|
||||
patch("bmspy.client.is_registered", False):
|
||||
client.handle_registration("/fake/socket", "test")
|
||||
assert "daemon refused 'REGISTER': no" in capsys.readouterr().out
|
||||
|
||||
def test_list_devices_returns_configured_names(self):
|
||||
from bmspy.client import list_devices
|
||||
with patch("bmspy.client.socket_comms", return_value={"devices": ["network", "nas"]}) as mock_comms:
|
||||
|
||||
Reference in New Issue
Block a user