def test_with_non_dict_event(self):
     RouterConfigurationFactory.create(
         backend_name=RouterConfiguration.XAPI_BACKEND,
         enabled=True,
         route_url='http://test3.com',
         configurations=ROUTER_CONFIG_FIXTURE[3])
     router = EventsRouter(processors=[],
                           backend_name=RouterConfiguration.XAPI_BACKEND)
     transformed_event = Statement()
     with self.assertRaises(ValueError):
         router.send(transformed_event)
    def test_generic_exception(self, backend_name, mocked_logger, mocked_post):
        RouterConfigurationFactory.create(
            backend_name=backend_name,
            enabled=True,
            route_url='http://test3.com',
            configurations=ROUTER_CONFIG_FIXTURE[2])

        router = EventsRouter(processors=[], backend_name=backend_name)
        router.send(self.transformed_event)
        if backend_name == RouterConfiguration.CALIPER_BACKEND:
            self.assertEqual(
                mocked_logger.exception.call_count,
                getattr(settings, 'EVENT_ROUTING_BACKEND_COUNTDOWN', 3) + 1)
            mocked_post.assert_not_called()
        else:
            mocked_logger.exception.assert_not_called()
Exemplo n.º 3
0
    def test_multiple_routers_of_backend(self):
        backend_name = 'multiple_routers_test'
        test_cache_router = RouterConfigurationFactory(
            configurations='{}',
            enabled=True,
            route_url='http://test2.com',
            backend_name=backend_name)
        test_cache_router1 = RouterConfigurationFactory(
            configurations='{}',
            enabled=True,
            route_url='http://test1.com',
            backend_name=backend_name)

        self.assertEqual(
            list(RouterConfiguration.get_enabled_routers(backend_name)),
            [test_cache_router1, test_cache_router])
Exemplo n.º 4
0
    def test_model_cache(self):
        test_cache_router = RouterConfigurationFactory(
            configurations='{}',
            enabled=True,
            route_url='http://test2.com',
            backend_name='test_cache')
        self.assertEqual(
            RouterConfiguration.get_enabled_routers('test_cache')[0],
            test_cache_router)

        test_cache_router.route_url = 'http://test3.com'
        test_cache_router.save()

        self.assertNotEqual(
            RouterConfiguration.get_enabled_routers('test_cache')[0],
            test_cache_router)
    def test_with_unsupported_routing_strategy(self, mocked_logger,
                                               mocked_post):
        RouterConfigurationFactory.create(
            backend_name='test_backend',
            enabled=True,
            route_url='http://test3.com',
            auth_scheme=RouterConfiguration.AUTH_BEARER,
            auth_key='test_key',
            configurations=ROUTER_CONFIG_FIXTURE[0])

        router = EventsRouter(processors=[], backend_name='test_backend')
        TieredCache.dangerous_clear_all_tiers()
        router.send(self.transformed_event)

        mocked_logger.error.assert_called_once_with(
            'Unsupported routing strategy detected: INVALID_TYPE')
        mocked_post.assert_not_called()
    def test_allowed_hosts(self):
        config_fixture = [
            {
                'match_params': {
                    'context.org_id': 'test'
                },
                'host_configurations': {
                    'url': 'http://test1.com',
                    'headers': {
                        'authorization': 'Token test'
                    }
                }
            },
            {
                'match_params': {
                    'non_existing.id.value': 'test'
                },
                'host_configurations': {
                    'url': 'http://test2.com',
                    'headers': {
                        'authorization': 'Token test'
                    }
                }
            }
        ]

        original_event = {
            'context': {
                'org_id': 'test'
            },
            'data': {
                'id': 'test_id'
            }
        }

        router = RouterConfigurationFactory(
            configurations=config_fixture,
            enabled=True,
            backend_name='first'
        )

        hosts = router.get_allowed_hosts(original_event)
        self.assertEqual(config_fixture[:1], hosts)
    def test_generic_exception_business_critical_event(self, mocked_logger,
                                                       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 = EventsRouter(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)

        self.assertEqual(
            mocked_logger.exception.call_count,
            getattr(settings, 'EVENT_ROUTING_BACKEND_COUNTDOWN', 3) + 1)
        mocked_post.assert_not_called()
    def test_with_no_available_hosts(self, mocked_logger, mocked_post):
        router_config = RouterConfigurationFactory.create(
            backend_name='test_backend',
            enabled=True,
            route_url='http://test3.com',
            configurations=ROUTER_CONFIG_FIXTURE[1])

        router = EventsRouter(processors=[], backend_name='test_backend')
        TieredCache.dangerous_clear_all_tiers()
        router.send(self.transformed_event)

        mocked_post.assert_not_called()

        self.assertIn(
            call(
                'Event %s is not allowed to be sent to any host for router %s with backend "%s"',
                self.transformed_event['name'], router_config.route_url,
                'test_backend'), mocked_logger.info.mock_calls)
Exemplo n.º 9
0
    def create_router_configuration(self,
                                    config_fixture,
                                    backend_name='first'):
        """
        Return RouterConfigurationFactory object for given configurations and backend name.

        Arguments:
            config_fixture    (dict):     router configurations dictionary
            backend_name       (str):     Backend names like caliper/xapi

        Returns:
            obj(RouterConfigurationFactory)
        """

        return RouterConfigurationFactory(configurations=config_fixture,
                                          enabled=True,
                                          route_url='http://test2.com',
                                          backend_name=backend_name)
Exemplo n.º 10
0
    def test_enabled_router_is_returned(self):
        first_router = RouterConfigurationFactory(configurations='{}',
                                                  enabled=True,
                                                  backend_name='first')

        second_router = RouterConfigurationFactory(configurations='{}',
                                                   enabled=False,
                                                   backend_name='second')

        self.assertEqual(RouterConfiguration.get_enabled_router('first'),
                         first_router)
        self.assertEqual(RouterConfiguration.get_enabled_router('second'),
                         None)

        second_router.enabled = True
        second_router.save()

        self.assertEqual(RouterConfiguration.get_enabled_router('second'),
                         second_router)
Exemplo n.º 11
0
    def test_enabled_router_is_returned(self):
        first_router = RouterConfigurationFactory(configurations='{}',
                                                  enabled=True,
                                                  route_url='http://test2.com',
                                                  backend_name='first')
        second_router = RouterConfigurationFactory(
            configurations='{}',
            enabled=False,
            route_url='http://test3.com',
            backend_name='second')
        self.assertEqual(
            RouterConfiguration.get_enabled_routers('first')[0], first_router)
        self.assertEqual(RouterConfiguration.get_enabled_routers('second'),
                         None)

        second_router.enabled = True
        second_router.save()
        TieredCache.dangerous_clear_all_tiers()
        self.assertEqual(
            RouterConfiguration.get_enabled_routers('second')[0],
            second_router)
    def test_successful_routing_of_event(
        self,
        auth_scheme,
        auth_key,
        username,
        password,
        backend_name,
        route_url,
        mocked_lrs,
        mocked_post,
    ):
        TieredCache.dangerous_clear_all_tiers()
        mocked_oauth_client = MagicMock()
        mocked_api_key_client = MagicMock()

        MOCKED_MAP = {
            'AUTH_HEADERS': HttpClient,
            'OAUTH2': mocked_oauth_client,
            'API_KEY': mocked_api_key_client,
            'XAPI_LRS': LrsClient,
        }
        RouterConfigurationFactory.create(
            backend_name=backend_name,
            enabled=True,
            route_url=route_url,
            auth_scheme=auth_scheme,
            auth_key=auth_key,
            username=username,
            password=password,
            configurations=ROUTER_CONFIG_FIXTURE[0])

        router = EventsRouter(processors=[], backend_name=backend_name)

        with patch.dict('event_routing_backends.tasks.ROUTER_STRATEGY_MAPPING',
                        MOCKED_MAP):
            router.send(self.transformed_event)

        overridden_event = self.transformed_event.copy()
        overridden_event['new_key'] = 'new_value'

        if backend_name == RouterConfiguration.XAPI_BACKEND:
            # test LRS Client
            mocked_lrs().save_statement.assert_has_calls([
                call(overridden_event),
            ])
        else:
            # test the HTTP client
            if auth_scheme == RouterConfiguration.AUTH_BASIC:
                mocked_post.assert_has_calls([
                    call(url=route_url,
                         json=overridden_event,
                         headers={},
                         auth=(username, password)),
                ])
            elif auth_scheme == RouterConfiguration.AUTH_BEARER:
                mocked_post.assert_has_calls([
                    call(url=route_url,
                         json=overridden_event,
                         headers={
                             'Authorization':
                             RouterConfiguration.AUTH_BEARER + ' ' + auth_key
                         }),
                ])
            else:
                mocked_post.assert_has_calls([
                    call(
                        url=route_url,
                        json=overridden_event,
                        headers={},
                    ),
                ])

        # test mocked oauth client
        mocked_oauth_client.assert_not_called()