Skip to content

Commit

Permalink
test: add missing tests for new sync router
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Feb 2, 2024
1 parent 40f35f2 commit 14df49d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
68 changes: 68 additions & 0 deletions event_routing_backends/backends/tests/test_events_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
6 changes: 3 additions & 3 deletions event_routing_backends/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 14df49d

Please sign in to comment.