Esempio n. 1
0
    def test_credentials_provider(self, provider_constructor):
        """OutputDispatcher - Constructor"""
        provider = MagicMock()
        provider_constructor.return_value = provider

        _ = OutputDispatcher(CONFIG)

        provider_constructor.assert_called_with('test_service',
                                                config=CONFIG, defaults=None, region=REGION)
        assert_equal(self._dispatcher._credentials_provider._service_name, 'test_service')
Esempio n. 2
0
class TestOutputDispatcher:
    """Test class for OutputDispatcher"""

    @patch.object(OutputDispatcher, '__service__', 'test_service')
    @patch.object(OutputDispatcher, '__abstractmethods__', frozenset())
    @patch.dict('os.environ', MOCK_ENV)
    def setup(self):
        """Setup before each method"""
        self._dispatcher = OutputDispatcher(CONFIG)
        self._descriptor = 'desc_test'

    @patch.object(OutputDispatcher, '__service__', 'test_service')
    @patch.object(OutputDispatcher, '__abstractmethods__', frozenset())
    @patch('streamalert.alert_processor.outputs.output_base.OutputCredentialsProvider')
    def test_credentials_provider(self, provider_constructor):
        """OutputDispatcher - Constructor"""
        provider = MagicMock()
        provider_constructor.return_value = provider

        _ = OutputDispatcher(CONFIG)

        provider_constructor.assert_called_with('test_service',
                                                config=CONFIG, defaults=None, region=REGION)
        assert_equal(self._dispatcher._credentials_provider._service_name, 'test_service')

    @patch('logging.Logger.info')
    def test_log_status_success(self, log_mock):
        """OutputDispatcher - Log status success"""
        self._dispatcher._log_status(True, self._descriptor)
        log_mock.assert_called_with('Successfully sent alert to %s:%s',
                                    'test_service', self._descriptor)

    @patch('logging.Logger.error')
    def test_log_status_failed(self, log_mock):
        """OutputDispatcher - Log status failed"""
        self._dispatcher._log_status(False, self._descriptor)
        log_mock.assert_called_with('Failed to send alert to %s:%s',
                                    'test_service', self._descriptor)

    @patch('requests.Response')
    def test_check_http_response(self, mock_response):
        """OutputDispatcher - Check HTTP Response"""
        # Test with a good response code
        mock_response.status_code = 200
        result = self._dispatcher._check_http_response(mock_response)
        assert_equal(result, True)

        # Test with a bad response code
        mock_response.status_code = 440
        result = self._dispatcher._check_http_response(mock_response)
        assert_equal(result, False)

    @mock_ssm
    @mock_kms
    def test_load_creds(self):
        """OutputDispatcher - Load Credentials"""
        param_name = '/{}/streamalert/outputs/test_service/desc_test'.format(PREFIX)
        creds = {
            'url': 'http://www.foo.bar/test',
            'token': 'token_to_encrypt'
        }

        put_mock_ssm_parameters(param_name, creds, KMS_ALIAS, region=REGION)

        loaded_creds = self._dispatcher._load_creds(self._descriptor)

        assert_is_not_none(loaded_creds)
        assert_equal(len(loaded_creds), 2)
        assert_equal(loaded_creds['url'], creds['url'])
        assert_equal(loaded_creds['token'], creds['token'])

    def test_format_output_config(self):
        """OutputDispatcher - Format Output Config"""
        with patch.object(OutputDispatcher, '__service__', 'slack'):
            props = {'descriptor': OutputProperty('test_desc', 'test_channel')}

            formatted = self._dispatcher.format_output_config(CONFIG, props)

            assert_equal(len(formatted), 2)
            assert_equal(formatted[0], 'unit_test_channel')
            assert_equal(formatted[1], 'test_channel')

    @patch.object(OutputDispatcher, '_get_exceptions_to_catch', Mock(return_value=(ValueError)))
    def test_catch_exceptions_non_default(self):
        """OutputDispatcher - Catch Non Default Exceptions"""
        exceptions = self._dispatcher._catch_exceptions()

        assert_equal(exceptions, (OutputRequestFailure, ReqTimeout, ValueError))

    @patch.object(OutputDispatcher,
                  '_get_exceptions_to_catch', Mock(return_value=(ValueError, TypeError)))
    def test_catch_exceptions_non_default_tuple(self):
        """OutputDispatcher - Catch Non Default Exceptions Tuple"""
        exceptions = self._dispatcher._catch_exceptions()

        assert_equal(exceptions, (OutputRequestFailure, ReqTimeout, ValueError, TypeError))

    @patch.object(OutputDispatcher, '_get_exceptions_to_catch', Mock(return_value=()))
    def test_catch_exceptions_default(self):
        """OutputDispatcher - Catch Default Exceptions"""
        exceptions = self._dispatcher._catch_exceptions()

        assert_equal(exceptions, (OutputRequestFailure, ReqTimeout))
Esempio n. 3
0
 def setup(self):
     """Setup before each method"""
     self._dispatcher = OutputDispatcher(CONFIG)
     self._descriptor = 'desc_test'
Esempio n. 4
0
 def __init__(self, *args, **kwargs):
     OutputDispatcher.__init__(self, *args, **kwargs)
     self._base_url = None
     self._auth_cookie = None