Skip to content

Commit

Permalink
Option to disable receive payload validation
Browse files Browse the repository at this point in the history
Signed-off-by: Simone Orru <[email protected]>
  • Loading branch information
sorru94 committed Nov 9, 2023
1 parent 1e777c6 commit 71405e9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
14 changes: 13 additions & 1 deletion astarte/device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ def __init__(self):
self._on_data_received: Callable[[Device, str, str, object], None] | None = None
self._on_disconnected: Callable[[Device, int], None] | None = None
self._loop = None
self._disable_receive_validation = False

def disable_receive_validation(self):
"""
Disable validation for the message reception.
N.B. This is a temporary workaround specifically designed to bypass bugs in Astarte core.
It should not be used carelessly.
See: https://github.com/astarte-platform/astarte-device-sdk-python/issues/136
"""
self._disable_receive_validation = True

@abstractmethod
def add_interface_from_json(self, interface_json: dict):
Expand Down Expand Up @@ -429,7 +441,7 @@ def _on_message_generic(self, interface_name, path, payload):
return

# Check the payload matches with the interface
if payload:
if payload and not self._disable_receive_validation:
try:
interface.validate_payload(path, payload)
except ValidationError:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,33 @@ def test_device_on_message_generic(self, mock_get_interface, _store_property):
device, interface_name, interface_path, mock_message
)

@mock.patch.multiple(Device, __abstractmethods__=set(), _store_property=mock.DEFAULT)
@mock.patch.object(Introspection, "get_interface")
def test_device_on_message_generic_no_validation(self, mock_get_interface, _store_property):
device = Device()
device.disable_receive_validation()

on_data_received_mock = mock.MagicMock()
device.set_events_callbacks(on_data_received=on_data_received_mock)
interface_name = "interface name"
interface_path = "interface path"
mock_message = mock.MagicMock()
device._on_message_generic(interface_name, interface_path, mock_message)

mock_get_interface.assert_called_once_with(interface_name)
mock_get_interface.return_value.is_server_owned.assert_called_once()
mock_get_interface.return_value.is_property_endpoint_resettable.assert_not_called()
mock_get_interface.return_value.validate_path.assert_called_once_with(
interface_path, mock_message
)
mock_get_interface.return_value.validate_payload.assert_not_called()
_store_property.assert_called_once_with(
mock_get_interface.return_value, interface_path, mock_message
)
on_data_received_mock.assert_called_once_with(
device, interface_name, interface_path, mock_message
)

@mock.patch.multiple(Device, __abstractmethods__=set(), _store_property=mock.DEFAULT)
@mock.patch.object(Introspection, "get_interface")
def test_device_on_message_generic_with_threading(self, mock_get_interface, _store_property):
Expand Down

0 comments on commit 71405e9

Please sign in to comment.