Exemple #1
0
def test_handle_api_request_v0_send(mocker):
    from iris.bin.sender import message_send_enqueue
    from iris.sender.rpc import handle_api_request, send_funcs
    from iris.sender.shared import per_mode_send_queues

    send_funcs['message_send_enqueue'] = message_send_enqueue

    send_queue = per_mode_send_queues.setdefault('email', gevent.queue.Queue())

    # support expanding target
    mocker.patch('iris.sender.cache.targets_for_role', lambda role, target: [target])
    mocker.patch('iris.bin.sender.db')
    mocker.patch('iris.metrics.stats')
    mocker.patch('iris.bin.sender.set_target_contact').return_value = True

    mock_address = mocker.MagicMock()
    mock_socket = mocker.MagicMock()
    mock_socket.recv.return_value = msgpack.packb({
        'endpoint': 'v0/send',
        'data': fake_notification,
    })

    while send_queue.qsize() > 0:
        send_queue.get()

    handle_api_request(mock_socket, mock_address)

    assert send_queue.qsize() == 1
    m = send_queue.get()
    assert m['subject'] == '[%s] %s' % (fake_notification['application'],
                                        fake_notification['subject'])
Exemple #2
0
def test_fetch_and_send_message(mocker):
    def check_mark_message_sent(m):
        assert m['message_id'] == fake_message['message_id']

    def mock_set_target_contact(message):
        message['destination'] = '*****@*****.**'
        message['mode'] = 'email'
        message['mode_id'] = 1
        return True

    vendors = IrisVendorManager({}, [])
    mocker.patch('iris.bin.sender.db')
    mocker.patch('iris.bin.sender.distributed_send_message').return_value = True, True
    mocker.patch('iris.bin.sender.quota')
    mocker.patch('iris.metrics.stats')
    mocker.patch('iris.bin.sender.update_message_mode')
    mock_mark_message_sent = mocker.patch('iris.bin.sender.mark_message_as_sent')
    mock_mark_message_sent.side_effect = check_mark_message_sent
    mocker.patch('iris.bin.sender.set_target_contact').side_effect = mock_set_target_contact
    from iris.bin.sender import (
        fetch_and_send_message, per_mode_send_queues
    )

    send_queue = per_mode_send_queues.setdefault('email', gevent.queue.Queue())

    # drain out send queue
    while send_queue.qsize() > 0:
        send_queue.get()
    send_queue.put(fake_message)

    fetch_and_send_message(send_queue, vendors)

    assert send_queue.qsize() == 0
    mock_mark_message_sent.assert_called_once()
Exemple #3
0
def test_aggregate_audit_msg(mocker):
    mock_iris_client = mocker.patch('iris.sender.cache.iris_client')
    mock_iris_client.get.return_value.json.return_value = fake_plan
    from iris.bin.sender import (fetch_and_prepare_message, message_queue,
                                 per_mode_send_queues, plan_aggregate_windows)

    send_queue = per_mode_send_queues.setdefault('email', gevent.queue.Queue())

    init_queue_with_item(message_queue, fake_message)
    init_queue_with_item(send_queue)

    now = time.time()
    msg_aggregate_key = (fake_message['plan_id'], fake_message['application'],
                         fake_message['priority'], fake_message['target'])
    from collections import defaultdict
    plan_aggregate_windows[msg_aggregate_key] = defaultdict(int)
    plan_aggregate_windows[msg_aggregate_key][now] = 10
    plan_aggregate_windows[msg_aggregate_key][now - 60] = 10

    mocker.patch('iris.bin.sender.cache').plans = {fake_plan['id']: fake_plan}

    mocker.patch('iris.bin.sender.spawn')
    from iris.bin.sender import spawn as mock_spawn

    # run code to test
    fetch_and_prepare_message()

    # examine results
    assert send_queue.qsize() == 0
    from iris.sender import auditlog
    mock_spawn.assert_called_with(
        auditlog.message_change, fake_message['message_id'],
        auditlog.SENT_CHANGE, '', '',
        "Aggregated with key (19546, test-app, high, test-user)")
Exemple #4
0
def test_fetch_and_prepare_message(mocker):
    mocker.patch('iris.bin.sender.message_send_enqueue')
    from iris.bin.sender import (fetch_and_prepare_message, message_queue,
                                 per_mode_send_queues)

    init_queue_with_item(message_queue, {'message_id': 1234, 'plan_id': None})
    fetch_and_prepare_message()
    assert message_queue.qsize() == 0

    send_queue = per_mode_send_queues.setdefault('email', gevent.queue.Queue())

    init_queue_with_item(send_queue, {'message_id': 1234, 'plan_id': None})

    assert message_queue.qsize() == 0
    assert send_queue.qsize() == 1
    m = send_queue.get()
    assert m['message_id'] == 1234
Exemple #5
0
def test_handle_api_notification_request_invalid_message(mocker):
    mocker.patch('iris.bin.sender.set_target_contact').return_value = True
    mocker.patch('iris.metrics.stats')

    from iris.bin.sender import message_send_enqueue
    from iris.sender.rpc import handle_api_notification_request, send_funcs
    from iris.sender.shared import per_mode_send_queues

    send_funcs['message_send_enqueue'] = message_send_enqueue
    send_queue = per_mode_send_queues.setdefault('email', gevent.queue.Queue())

    mock_socket = mocker.MagicMock()
    handle_api_notification_request(mock_socket, mocker.MagicMock(), {
        'data': {
            'application': 'test_app',
        }
    })
    mock_socket.sendall.assert_called_with(msgpack.packb('INVALID role'))

    handle_api_notification_request(mock_socket, mocker.MagicMock(), {
        'data': {
            'role': 'user',
            'application': 'test_app',
        }
    })
    mock_socket.sendall.assert_called_with(msgpack.packb('INVALID target'))

    mocker.patch('iris.sender.rpc.cache').targets_for_role.return_value = ['foo']
    handle_api_notification_request(mock_socket, mocker.MagicMock(), {
        'data': {
            'target': 'foo',
            'role': 'user',
            'application': 'test_app',
        }
    })
    mock_socket.sendall.assert_called_with(msgpack.packb('INVALID body'))

    handle_api_notification_request(mock_socket, mocker.MagicMock(), {
        'data': {
            'target': 'foo',
            'role': 'user',
            'application': 'test_app',
            'template': 'test',
        }
    })
    mock_socket.sendall.assert_called_with(msgpack.packb('INVALID context'))

    # should work when user is setting body key
    handle_api_notification_request(mock_socket, mocker.MagicMock(), {
        'data': {
            'target': 'foo',
            'role': 'user',
            'application': 'test_app',
            'body': 'test',
        }
    })
    mock_socket.sendall.assert_called_with(msgpack.packb('OK'))

    # should work when user is setting template key
    handle_api_notification_request(mock_socket, mocker.MagicMock(), {
        'data': {
            'target': 'foo',
            'role': 'user',
            'application': 'test_app',
            'template': 'test',
            'context': {},
        }
    })
    mock_socket.sendall.assert_called_with(msgpack.packb('OK'))

    # should work when user is setting email_html key
    handle_api_notification_request(mock_socket, mocker.MagicMock(), {
        'data': {
            'target': 'foo',
            'role': 'user',
            'application': 'test_app',
            'email_html': '<p>test</p>',
        }
    })
    mock_socket.sendall.assert_called_with(msgpack.packb('OK'))

    # drain out send queue
    while send_queue.qsize() > 0:
        send_queue.get()
Exemple #6
0
def test_message_retry(mocker):
    def check_mark_message_sent(m):
        assert m['message_id'] == fake_message['message_id']

    def mock_set_target_contact(message):
        message['destination'] = '*****@*****.**'
        message['mode'] = 'sms'
        message['mode_id'] = 1
        return True

    vendors = IrisVendorManager({}, [])
    mocker.patch('iris.bin.sender.db')
    mock_distributed_send_message = mocker.patch('iris.bin.sender.distributed_send_message')
    mock_distributed_send_message.return_value = (False, True)
    mocker.patch('iris.bin.sender.quota')
    mocker.patch('iris.bin.sender.update_message_mode')
    mock_mark_message_sent = mocker.patch('iris.bin.sender.mark_message_as_sent')
    mock_mark_message_sent.side_effect = check_mark_message_sent
    mocker.patch('iris.bin.sender.set_target_contact').side_effect = mock_set_target_contact
    mocker.patch('iris.bin.sender.set_target_fallback_mode').side_effect = mock_set_target_contact
    from iris.bin.sender import (
        fetch_and_send_message, per_mode_send_queues, metrics, default_sender_metrics,
        add_mode_stat
    )

    def fail_send_message(message, vendors=None):
        add_mode_stat(message['mode'], None)
        return False, True

    mock_distributed_send_message.side_effect = fail_send_message

    metrics.stats.update(default_sender_metrics)

    send_queue = per_mode_send_queues.setdefault('sms', gevent.queue.Queue())

    # drain out send queue
    while send_queue.qsize() > 0:
        send_queue.get()
    send_queue.put(fake_message.copy())

    mock_distributed_send_message.reset_mock()
    fetch_and_send_message(send_queue, vendors)
    mock_distributed_send_message.assert_called()

    # will retry first time
    assert send_queue.qsize() == 1
    mock_mark_message_sent.assert_not_called()

    mock_distributed_send_message.reset_mock()
    fetch_and_send_message(send_queue, vendors)
    mock_distributed_send_message.assert_called()

    # will retry a 2nd time
    assert send_queue.qsize() == 1
    mock_mark_message_sent.assert_not_called()

    mock_distributed_send_message.reset_mock()
    fetch_and_send_message(send_queue, vendors)

    # will not retry a 3rd time
    assert send_queue.qsize() == 0
    mock_distributed_send_message.assert_not_called()

    # we retried after it failed the first time
    assert metrics.stats['message_retry_cnt'] == 2
    assert metrics.stats['sms_fail'] == 2