From cb0476d7243f333b762adcc8c5e6731ec8d34d42 Mon Sep 17 00:00:00 2001 From: Cristhian Garcia Date: Thu, 2 Nov 2023 15:43:40 -0500 Subject: [PATCH] test: add missing tests for new sync router --- .../backends/tests/test_events_router.py | 68 +++++++++++++++++++ event_routing_backends/handlers.py | 6 +- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/event_routing_backends/backends/tests/test_events_router.py b/event_routing_backends/backends/tests/test_events_router.py index 8f46f208..45cc35ff 100644 --- a/event_routing_backends/backends/tests/test_events_router.py +++ b/event_routing_backends/backends/tests/test_events_router.py @@ -804,6 +804,27 @@ class TestSyncEventsRouter(TestEventsRouter): # pylint: disable=test-inherits-t """ Test the SyncEventsRouter """ + @patch.dict('event_routing_backends.tasks.ROUTER_STRATEGY_MAPPING', { + 'AUTH_HEADERS': MagicMock(side_effect=EventNotDispatched) + }) + @patch('event_routing_backends.utils.http_client.requests.post') + def test_generic_exception_business_critical_event(self, mocked_post): + RouterConfigurationFactory.create( + backend_name=RouterConfiguration.XAPI_BACKEND, + enabled=True, + route_url='http://test3.com', + auth_scheme=RouterConfiguration.AUTH_BEARER, + auth_key='test_key', + configurations=ROUTER_CONFIG_FIXTURE[0] + ) + + router = SyncEventsRouter(processors=[], backend_name=RouterConfiguration.CALIPER_BACKEND) + event_data = self.transformed_event.copy() + business_critical_events = get_business_critical_events() + event_data['name'] = business_critical_events[0] + + router.send(event_data) + mocked_post.assert_not_called() @ddt.data( (RouterConfiguration.AUTH_BASIC, @@ -895,6 +916,8 @@ def test_successful_routing_of_event( router = SyncEventsRouter(processors=[], backend_name=backend_name) + self.transformed_event["name"] = get_business_critical_events()[0] + with patch.dict('event_routing_backends.tasks.ROUTER_STRATEGY_MAPPING', MOCKED_MAP): router.send(self.transformed_event) @@ -1078,3 +1101,48 @@ def test_successful_routing_of_bulk_events( # test mocked oauth client mocked_oauth_client.assert_not_called() + + @patch('event_routing_backends.utils.xapi_lrs_client.RemoteLRS') + @ddt.unpack + def test_failed_bulk_routing(self, mocked_remote_lrs): + mock_response = MagicMock() + mock_response.success = False + mock_response.data = "Fake response data" + mock_response.response.code = 500 + mock_response.request.method = "POST" + mock_response.request.content = "Fake request content" + + mocked_remote_lrs.return_value.save_statements.return_value = mock_response + RouterConfigurationFactory.create( + backend_name=RouterConfiguration.XAPI_BACKEND, + enabled=True, + route_url='http://test3.com', + configurations=ROUTER_CONFIG_FIXTURE[2] + ) + + router = SyncEventsRouter(processors=[], backend_name=RouterConfiguration.XAPI_BACKEND) + with self.assertRaises(EventNotDispatched): + router.bulk_send([self.transformed_event]) + + @patch('event_routing_backends.utils.xapi_lrs_client.RemoteLRS') + @ddt.unpack + def test_failed_routing(self, mocked_remote_lrs): + mock_response = MagicMock() + mock_response.success = False + mock_response.data = "Fake response data" + mock_response.response.code = 500 + mock_response.request.method = "POST" + mock_response.request.content = "Fake request content" + mocked_remote_lrs.side_effect = EventNotDispatched + + mocked_remote_lrs.return_value.save_statements.return_value = mock_response + RouterConfigurationFactory.create( + backend_name=RouterConfiguration.XAPI_BACKEND, + enabled=True, + route_url='http://test3.com', + configurations=ROUTER_CONFIG_FIXTURE[2] + ) + + router = SyncEventsRouter(processors=[], backend_name=RouterConfiguration.XAPI_BACKEND) + with self.assertRaises(EventNotDispatched): + router.send(self.transformed_event) diff --git a/event_routing_backends/handlers.py b/event_routing_backends/handlers.py index dacf63f3..a19e5a77 100644 --- a/event_routing_backends/handlers.py +++ b/event_routing_backends/handlers.py @@ -8,17 +8,17 @@ from eventtracking.backends.event_bus import EventBusRoutingBackend from eventtracking.processors.exceptions import EventEmissionExit from eventtracking.tracker import get_tracker -from openedx_events.analytics.signals import TRACKING_LOG_EVENT_EMITTED +from openedx_events.analytics.signals import TRACKING_EVENT_EMITTED logger = logging.getLogger(__name__) -@receiver(TRACKING_LOG_EVENT_EMITTED) +@receiver(TRACKING_EVENT_EMITTED) def send_tracking_log_to_backends( sender, signal, **kwargs ): # pylint: disable=unused-argument """ - Listen for the TRACKING_LOG_EVENT_EMITTED signal and send the event to the enabled backends. + Listen for the TRACKING_EVENT_EMITTED signal and send the event to the enabled backends. The process is the following: