Ejemplo n.º 1
0
def test_handler_return():
    """Main handler return value"""
    context = get_mock_context()
    event = {'Records': []}
    value = handler(event, context)

    assert_is_instance(value, list)
Ejemplo n.º 2
0
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())
Ejemplo n.º 3
0
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, [])
Ejemplo n.º 4
0
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])
Ejemplo n.º 5
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))
Ejemplo n.º 6
0
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')