def test_handler_return(): """Main handler return value""" context = _get_mock_context() event = {'Records': []} value = handler(event, context) assert_is_instance(value, list)
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( 'test/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_handler_malformed_message(log_mock): """Main handler decode failure logging""" # The @patch() decorator allows us to 'introspect' what occurs down the chain # and verify the params a function was LAST called with. For instance, here # we are checking the last call to `logging.Logger.error` and verifying that the # function was called with two params, the first being 'Malformed SNS: %s' and # the second being the dictionary contained within `message` # This call should happen at stream_alert/alert_processor/main.py:62 context = _get_mock_context() message = {'not_default': {'record': {'size': '9982'}}} event = {'Records': [{'Sns': {'Message': json.dumps(message)}}]} for _ in handler(event, context): pass log_mock.assert_called_with('Malformed SNS: %s', message)
def test_running_success(creds_mock, config_mock, url_mock): """Alert Processor run handler - success""" config_mock.return_value = _load_output_config( 'test/unit/conf/outputs.json') creds_mock.return_value = {'url': 'mock.url'} url_mock.return_value.getcode.return_value = 200 alert = _get_alert() context = _get_mock_context() result = handler(alert, context) assert_is_instance(result, list) assert_true(result[0][0])
def test_handler_bad_message(log_mock): """Main handler decode failure logging""" context = _get_mock_context() event = { 'Records': [{ 'Sns': { 'Message': 'this\nvalue\nshould\nfail\nto\ndecode' } }] } for _ in handler(event, context): pass assert_equal( str(log_mock.call_args_list[0]), str( call('An error occurred while decoding message to JSON: %s', ValueError('No JSON object could be decoded', ))))
def test_running_exception_occurred(creds_mock, dispatch_mock, config_mock, url_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( 'test/unit/conf/outputs.json') url_mock.return_value.getcode.return_value = 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( 'test/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')