def test_handle_api_request_v0_send_with_mode(mocker): from iris.sender.rpc import handle_api_request from iris.sender.shared import send_queue # support expanding target mocker.patch('iris.sender.cache.RoleTargets.__call__', lambda _, role, target: [target]) mocker.patch('iris.bin.sender.set_target_contact') fake_mode_notification = {} fake_mode_notification.update(fake_notification) fake_mode_notification['mode'] = 'sms' mock_address = mocker.MagicMock() mock_socket = mocker.MagicMock() mock_socket.recv.return_value = msgpack.packb({ 'endpoint': 'v0/send', 'data': fake_mode_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_mode_notification['application'], fake_mode_notification['subject'])
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 mocker.patch('iris.bin.sender.db') mocker.patch('iris.bin.sender.send_message').return_value = 1 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 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_send_message, send_queue ) # drain out send queue while send_queue.qsize() > 0: send_queue.get() send_queue.put(fake_message) fetch_and_send_message() assert send_queue.qsize() == 0 mock_mark_message_sent.assert_called_once()
def test_no_valid_modes(mocker): def check_mark_message_sent(m): assert m['message_id'] == fake_message['message_id'] def mock_set_target_contact(message): return False mocker.patch('iris.bin.sender.db') mock_mark_message_no_contact = mocker.patch('iris.bin.sender.mark_message_has_no_contact') 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 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_send_message, send_queue ) # drain out send queue while send_queue.qsize() > 0: send_queue.get() send_queue.put(fake_message) fetch_and_send_message() assert send_queue.qsize() == 0 assert not mock_mark_message_sent.called mock_mark_message_no_contact.assert_called_once()
def test_fetch_and_prepare_message(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, send_queue) init_queue_with_item(message_queue, fake_message) init_queue_with_item(send_queue) fetch_and_prepare_message() assert message_queue.qsize() == 0 assert send_queue.qsize() == 1 m = send_queue.get() assert m['message_id'] == fake_message['message_id']
def test_handle_api_notification_request_invalid_message(mocker): from iris.sender.rpc import handle_api_notification_request 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')) from iris.bin.sender import send_queue # drain out send queue while send_queue.qsize() > 0: send_queue.get()