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')
def setup(self): """Setup before each method""" self._dispatcher = OutputDispatcher(REGION, FUNCTION_NAME, CONFIG) self._descriptor = 'desc_test'
class TestOutputDispatcher(object): """Test class for OutputDispatcher""" @patch.object(OutputDispatcher, '__abstractmethods__', frozenset()) def setup(self): """Setup before each method""" self._dispatcher = OutputDispatcher(REGION, FUNCTION_NAME, CONFIG) self._descriptor = 'desc_test' def test_local_temp_dir(self): """OutputDispatcher - Local Temp Dir""" temp_dir = self._dispatcher._local_temp_dir() assert_equal(temp_dir.split('/')[-1], 'stream_alert_secrets') def test_get_secrets_bucket_name(self): """OutputDispatcher - Get Secrets Bucket Name""" bucket_name = self._dispatcher._get_secrets_bucket_name(FUNCTION_NAME) assert_equal(bucket_name, 'corp-prefix.streamalert.secrets') def test_output_cred_name(self): """OutputDispatcher - Output Cred Name""" output_name = self._dispatcher.output_cred_name('creds') assert_equal(output_name, 'test_service/creds') @mock_s3 def test_get_creds_from_s3(self): """OutputDispatcher - Get Creds From S3""" test_data = 'credential test string' bucket_name = self._dispatcher.secrets_bucket key = self._dispatcher.output_cred_name(self._descriptor) local_cred_location = os.path.join(self._dispatcher._local_temp_dir(), key) put_mock_s3_object(bucket_name, key, test_data, REGION) self._dispatcher._get_creds_from_s3(local_cred_location, self._descriptor) with open(local_cred_location) as creds: line = creds.readline() assert_equal(line, test_data) @mock_kms def test_kms_decrypt(self): """OutputDispatcher - KMS Decrypt""" test_data = 'data to encrypt' encrypted = encrypt_with_kms(test_data, REGION, KMS_ALIAS) decrypted = self._dispatcher._kms_decrypt(encrypted) assert_equal(decrypted, test_data) @patch('logging.Logger.info') def test_log_status_success(self, log_mock): """OutputDispatcher - Log status success""" self._dispatcher._log_status(True) log_mock.assert_called_with('Successfully sent alert to %s', 'test_service') @patch('logging.Logger.error') def test_log_status_failed(self, log_mock): """OutputDispatcher - Log status failed""" self._dispatcher._log_status(False) log_mock.assert_called_with('Failed to send alert to %s', 'test_service') @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_s3 @mock_kms def test_load_creds(self): """OutputDispatcher - Load Credentials""" remove_temp_secrets() output_name = self._dispatcher.output_cred_name(self._descriptor) creds = {'url': 'http://www.foo.bar/test', 'token': 'token_to_encrypt'} put_mock_creds(output_name, creds, self._dispatcher.secrets_bucket, REGION, KMS_ALIAS) 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'], u'http://www.foo.bar/test') assert_equal(loaded_creds['token'], u'token_to_encrypt') 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')
def __init__(self, *args, **kwargs): OutputDispatcher.__init__(self, *args, **kwargs) self._base_url = None self._headers = None self._escalation_policy = None
def __init__(self, *args, **kwargs): OutputDispatcher.__init__(self, *args, **kwargs) self._base_url = None self._auth_cookie = None
def setup(self): """Setup before each method""" self._dispatcher = OutputDispatcher(CONFIG) self._descriptor = 'desc_test'
class TestOutputDispatcher(object): """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('stream_alert.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_s3 @mock_kms def test_load_creds(self): """OutputDispatcher - Load Credentials""" remove_temp_secrets() key = get_formatted_output_credentials_name( 'test_service', self._descriptor ) creds = {'url': 'http://www.foo.bar/test', 'token': 'token_to_encrypt'} put_mock_creds(key, creds, self._dispatcher._credentials_provider._core_driver._bucket, REGION, KMS_ALIAS) 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'], u'http://www.foo.bar/test') assert_equal(loaded_creds['token'], u'token_to_encrypt') 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))