def test_load_output_config(): """Load outputs configuration file""" config = _load_output_config('tests/unit/conf/outputs.json') assert_equal(set(config.keys()), { 'aws-firehose', 'aws-s3', 'aws-lambda', 'pagerduty', 'phantom', 'slack' })
def test_handler_run(run_mock): """Main handler `run` call params""" context = get_mock_context() handler(None, context) # This test will load the actual config, so we should compare the # function call against the same config here. run_mock.assert_called_with(None, REGION, FUNCTION_NAME, _load_output_config())
def test_handler_run(run_mock): """Main handler `run` call params""" context = _get_mock_context() message = {'default': {'record': {'size': '9982'}}} event = {'Records': [{'Sns': {'Message': json.dumps(message)}}]} handler(event, context) # This test will load the actual config, so we should compare the # function call against the same config here. run_mock.assert_called_with(message, REGION, FUNCTION_NAME, _load_output_config())
def test_running_no_dispatcher(dispatch_mock, config_mock): """Alert Processor run handler - no dispatcher""" config_mock.return_value = _load_output_config('tests/unit/conf/outputs.json') dispatch_mock.return_value = None alert = get_alert() context = get_mock_context() result = handler(alert, context) assert_is_instance(result, list) assert_list_equal(result, [])
def test_running_success(creds_mock, config_mock, get_mock): """Alert Processor run handler - success""" config_mock.return_value = _load_output_config('tests/unit/conf/outputs.json') creds_mock.return_value = {'url': 'http://mock.url'} get_mock.return_value.status_code = 200 alert = get_alert() context = get_mock_context() result = handler(alert, context) assert_is_instance(result, list) assert_true(result[0][0])
def test_running_exception_occurred(creds_mock, dispatch_mock, config_mock, get_mock, log_mock): """Alert Processor run handler - exception occurred""" # Use TypeError as the mock's side_effect err = TypeError('bad error') creds_mock.return_value = {'url': 'mock.url'} dispatch_mock.return_value.dispatch.side_effect = err config_mock.return_value = _load_output_config('tests/unit/conf/outputs.json') get_mock.return_value.status_code = 200 alert = _sort_dict(get_alert()) context = get_mock_context() handler(alert, context) log_mock.assert_called_with( 'An error occurred while sending alert ' 'to %s:%s: %s. alert:\n%s', 'slack', 'unit_test_channel', err, json.dumps(alert, indent=2))
def test_running_bad_output(config_mock, log_mock): """Alert Processor run handler - bad output""" config_mock.return_value = _load_output_config('tests/unit/conf/outputs.json') alert = get_alert() alert['outputs'] = ['slack'] context = get_mock_context() handler(alert, context) log_mock.assert_called_with( 'Improperly formatted output [%s]. Outputs for rules must ' 'be declared with both a service and a descriptor for the ' 'integration (ie: \'slack:my_channel\')', 'slack') alert['outputs'] = ['slakc:test'] handler(alert, context) log_mock.assert_called_with( 'The output \'%s\' does not exist!', 'slakc:test')
''' Copyright 2017-present, Airbnb Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ''' from stream_alert.alert_processor.main import _load_output_config REGION = 'us-east-1' FUNCTION_NAME = 'corp-prefix_prod_streamalert_alert_processor' CONFIG = _load_output_config('tests/unit/conf/outputs.json') KMS_ALIAS = 'alias/stream_alert_secrets_test'