From 10cd07973dfe8fc225c0cfcd64dfe9fd5ebc67fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 4 Sep 2019 17:13:58 +0200 Subject: [PATCH] Implement unit tests for EventHandler subclasses --- .../test_event_handling/__init__.py | 0 .../test_control_event_handler.py | 33 +++++++++ .../test_event_handler.py | 8 +-- .../test_fsm_aware_event_handler.py | 69 +++++++++++++++++++ .../test_signed_event_handler.py | 32 +++++++++ .../test_event_handling/util.py | 11 +++ 6 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 tfw/internals/event_handling/test_event_handling/__init__.py create mode 100644 tfw/internals/event_handling/test_event_handling/test_control_event_handler.py rename tfw/internals/event_handling/{ => test_event_handling}/test_event_handler.py (96%) create mode 100644 tfw/internals/event_handling/test_event_handling/test_fsm_aware_event_handler.py create mode 100644 tfw/internals/event_handling/test_event_handling/test_signed_event_handler.py create mode 100644 tfw/internals/event_handling/test_event_handling/util.py diff --git a/tfw/internals/event_handling/test_event_handling/__init__.py b/tfw/internals/event_handling/test_event_handling/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tfw/internals/event_handling/test_event_handling/test_control_event_handler.py b/tfw/internals/event_handling/test_event_handling/test_control_event_handler.py new file mode 100644 index 0000000..057a9c5 --- /dev/null +++ b/tfw/internals/event_handling/test_event_handling/test_control_event_handler.py @@ -0,0 +1,33 @@ +from tfw.internals.networking import Intent + +from .util import DummyConnector, simulate_event +from ..control_event_handler import ControlEventHandler + + +def test_receives_control_intent(): + eh = ControlEventHandler(DummyConnector()) + messages = [] + eh.handle_event = lambda msg, _: messages.append(msg) + + test_msg = {"key": "cica", "intent": Intent.CONTROL.value} + simulate_event(eh, test_msg) + assert messages[0] == test_msg + + +def test_no_intent_defaults_to_control(): + eh = ControlEventHandler(DummyConnector()) + messages = [] + eh.handle_event = lambda msg, _: messages.append(msg) + + test_msg = {"key": "cica"} + simulate_event(eh, test_msg) + assert messages[0] == test_msg + + +def test_ignores_event_intent(): + eh = ControlEventHandler(DummyConnector()) + messages = [] + eh.handle_event = lambda msg, _: messages.append(msg) + + simulate_event(eh, {"key": "cica", "intent": Intent.EVENT.value}) + assert not messages diff --git a/tfw/internals/event_handling/test_event_handler.py b/tfw/internals/event_handling/test_event_handling/test_event_handler.py similarity index 96% rename from tfw/internals/event_handling/test_event_handler.py rename to tfw/internals/event_handling/test_event_handling/test_event_handler.py index 5082ca8..e206b46 100644 --- a/tfw/internals/event_handling/test_event_handler.py +++ b/tfw/internals/event_handling/test_event_handling/test_event_handler.py @@ -4,10 +4,10 @@ from random import randint import pytest -from .event_handler_factory_base import EventHandlerFactoryBase -from .event_handler import EventHandler -from .control_event_handler import ControlEventHandler -from .fsm_aware_event_handler import FSMAwareEventHandler +from ..event_handler_factory_base import EventHandlerFactoryBase +from ..event_handler import EventHandler +from ..control_event_handler import ControlEventHandler +from ..fsm_aware_event_handler import FSMAwareEventHandler class MockEventHandlerFactory(EventHandlerFactoryBase): diff --git a/tfw/internals/event_handling/test_event_handling/test_fsm_aware_event_handler.py b/tfw/internals/event_handling/test_event_handling/test_fsm_aware_event_handler.py new file mode 100644 index 0000000..2c37874 --- /dev/null +++ b/tfw/internals/event_handling/test_event_handling/test_fsm_aware_event_handler.py @@ -0,0 +1,69 @@ +# pylint: disable=redefined-outer-name +import pytest + +from tfw.internals.networking import Intent +from tfw.internals.crypto import KeyManager, sign_message + +from .util import DummyConnector, simulate_event +from ..fsm_aware_event_handler import FSMAwareEventHandler + + +@pytest.fixture +def key(): + yield KeyManager().auth_key + + +@pytest.fixture +def fsm_update_msg(): + yield { + 'key': 'fsm.update', + 'intent': Intent.EVENT.value, + 'scope': 'broadcast', + 'current_state': '1', + 'in_accepted_state': False, + 'last_event': { + 'from_state': '0', + 'timestamp': '2019-09-04T16:51:15.587555', + 'to_state': '1', 'trigger': + 'step_1' + }, + 'valid_transitions': [{'trigger': 'step_2'}] + } + + +def test_ignores_other_keys(): + messages = [] + eh = FSMAwareEventHandler(DummyConnector()) + eh.handle_event = lambda msg, _: messages.append(msg) + + simulate_event(eh, {"key": "not.fsm.update"}) + assert not messages + + +def test_ignores_unauthenticated(fsm_update_msg): + messages = [] + eh = FSMAwareEventHandler(DummyConnector()) + eh.handle_event = lambda msg, _: messages.append(msg) + + simulate_event(eh, fsm_update_msg) + assert not messages + + +def test_accepts_authenticated(key, fsm_update_msg): + messages = [] + eh = FSMAwareEventHandler(DummyConnector()) + eh.handle_event = lambda msg, _: messages.append(msg) + + sign_message(key, fsm_update_msg) + simulate_event(eh, fsm_update_msg) + assert messages[0] == fsm_update_msg + assert eh.fsm_state == '1' + assert not eh.fsm_in_accepted_state + assert len(eh.fsm_event_log) == 1 + + fsm_update_msg['in_accepted_state'] = True + fsm_update_msg['current_state'] = '2' + sign_message(key, fsm_update_msg) + simulate_event(eh, fsm_update_msg) + assert eh.fsm_state == '2' + assert eh.fsm_in_accepted_state diff --git a/tfw/internals/event_handling/test_event_handling/test_signed_event_handler.py b/tfw/internals/event_handling/test_event_handling/test_signed_event_handler.py new file mode 100644 index 0000000..fbf7e43 --- /dev/null +++ b/tfw/internals/event_handling/test_event_handling/test_signed_event_handler.py @@ -0,0 +1,32 @@ +# pylint: disable=redefined-outer-name +import pytest + +from tfw.internals.crypto import sign_message, KeyManager + +from .util import DummyConnector, simulate_event +from ..signed_event_handler import SignedEventHandler + + +@pytest.fixture +def key(): + yield KeyManager().auth_key + + +def test_ignores_unauthenticated(): + eh = SignedEventHandler(DummyConnector()) + messages = [] + eh.handle_event = lambda msg, _: messages.append(msg) + + simulate_event(eh, {"key": "cica"}) + assert not messages + + +def test_accepts_authenticated(key): + eh = SignedEventHandler(DummyConnector()) + messages = [] + eh.handle_event = lambda msg, _: messages.append(msg) + + test_msg = {"key": "cica"} + sign_message(key, test_msg) + simulate_event(eh, test_msg) + assert messages[0] == test_msg diff --git a/tfw/internals/event_handling/test_event_handling/util.py b/tfw/internals/event_handling/test_event_handling/util.py new file mode 100644 index 0000000..1dbd171 --- /dev/null +++ b/tfw/internals/event_handling/test_event_handling/util.py @@ -0,0 +1,11 @@ +class DummyConnector(): + def send_message(self, message, scope=None, intent=None): + pass + + def close(self): + pass + + +def simulate_event(event_handler, message): + # pylint: disable=protected-access + event_handler._event_callback(message)