From d5d079a150f245894da9103821c3e44730d4aa3c Mon Sep 17 00:00:00 2001 From: Jenny Duckett Date: Fri, 9 Dec 2016 12:10:42 +0000 Subject: [PATCH] Add optional queue param to send_notification_to_queue We want to use this function for sending internal notifications to a different queue. --- app/notifications/process_notifications.py | 25 +++++++++-------- .../test_process_notification.py | 28 +++++++++++-------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/app/notifications/process_notifications.py b/app/notifications/process_notifications.py index 549b9d595..06c4207eb 100644 --- a/app/notifications/process_notifications.py +++ b/app/notifications/process_notifications.py @@ -70,19 +70,22 @@ def persist_notification(template_id, return notification -def send_notification_to_queue(notification, research_mode): - try: - research_mode = research_mode or notification.key_type == KEY_TYPE_TEST +def send_notification_to_queue(notification, research_mode, queue=None): + if research_mode or notification.key_type == KEY_TYPE_TEST: + queue = 'research-mode' + elif not queue: if notification.notification_type == SMS_TYPE: - provider_tasks.deliver_sms.apply_async( - [str(notification.id)], - queue='send-sms' if not research_mode else 'research-mode' - ) + queue = 'send-sms' if notification.notification_type == EMAIL_TYPE: - provider_tasks.deliver_email.apply_async( - [str(notification.id)], - queue='send-email' if not research_mode else 'research-mode' - ) + queue = 'send-email' + + if notification.notification_type == SMS_TYPE: + deliver_task = provider_tasks.deliver_sms + if notification.notification_type == EMAIL_TYPE: + deliver_task = provider_tasks.deliver_email + + try: + deliver_task.apply_async([str(notification.id)], queue=queue) except Exception as e: current_app.logger.exception(e) dao_delete_notifications_and_history_by_id(notification.id) diff --git a/tests/app/notifications/test_process_notification.py b/tests/app/notifications/test_process_notification.py index 9a12fdf57..16b7ee0f2 100644 --- a/tests/app/notifications/test_process_notification.py +++ b/tests/app/notifications/test_process_notification.py @@ -135,24 +135,28 @@ def test_persist_notification_with_optionals(sample_job, sample_api_key, mocker) assert persisted_notification.reference is None -@pytest.mark.parametrize('research_mode, queue, notification_type, key_type', - [(True, 'research-mode', 'sms', 'normal'), - (True, 'research-mode', 'email', 'normal'), - (True, 'research-mode', 'email', 'team'), - (False, 'send-sms', 'sms', 'normal'), - (False, 'send-email', 'email', 'normal'), - (False, 'send-sms', 'sms', 'team'), - (False, 'research-mode', 'sms', 'test')]) +@pytest.mark.parametrize('research_mode, requested_queue, expected_queue, notification_type, key_type', + [(True, None, 'research-mode', 'sms', 'normal'), + (True, None, 'research-mode', 'email', 'normal'), + (True, None, 'research-mode', 'email', 'team'), + (False, None, 'send-sms', 'sms', 'normal'), + (False, None, 'send-email', 'email', 'normal'), + (False, None, 'send-sms', 'sms', 'team'), + (False, None, 'research-mode', 'sms', 'test'), + (True, 'notify', 'research-mode', 'email', 'normal'), + (False, 'notify', 'notify', 'sms', 'normal'), + (False, 'notify', 'notify', 'email', 'normal'), + (False, 'notify', 'research-mode', 'sms', 'test')]) def test_send_notification_to_queue(notify_db, notify_db_session, - research_mode, notification_type, - queue, key_type, mocker): + research_mode, requested_queue, expected_queue, + notification_type, key_type, mocker): mocked = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(notification_type)) template = sample_template(notify_db, notify_db_session) if notification_type == 'sms' \ else sample_email_template(notify_db, notify_db_session) notification = sample_notification(notify_db, notify_db_session, template=template, key_type=key_type) - send_notification_to_queue(notification=notification, research_mode=research_mode) + send_notification_to_queue(notification=notification, research_mode=research_mode, queue=requested_queue) - mocked.assert_called_once_with([str(notification.id)], queue=queue) + mocked.assert_called_once_with([str(notification.id)], queue=expected_queue) def test_send_notification_to_queue_throws_exception_deletes_notification(sample_notification, mocker):