From 3618a3967cfc267a7488a040766931025c834e5c Mon Sep 17 00:00:00 2001 From: Martyn Inglis Date: Wed, 28 Sep 2016 15:29:10 +0100 Subject: [PATCH] Creating notification tasks should write send tasks to the research-mode queue if research mode service. --- app/celery/tasks.py | 10 +++++-- tests/app/celery/test_tasks.py | 53 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/app/celery/tasks.py b/app/celery/tasks.py index c880cfd48..cf46a22e3 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -131,7 +131,10 @@ def send_sms(self, created_at, notification, notification_id, service.id, SMS_TYPE, api_key_id, key_type ) ) - provider_tasks.deliver_sms.apply_async((notification_id), queue='send-sms') + provider_tasks.deliver_sms.apply_async( + (notification_id), + queue='send-sms' if not service.research_mode else 'research-mode' + ) current_app.logger.info( "SMS {} created at {}".format(notification_id, created_at) @@ -170,7 +173,10 @@ def send_email(self, service_id, ) ) - provider_tasks.deliver_email.apply_async((notification_id), queue='send-email') + provider_tasks.deliver_email.apply_async( + (notification_id), + queue='send-email' if not service.research_mode else 'research-mode' + ) current_app.logger.info("Email {} created at {}".format(notification_id, created_at)) except SQLAlchemyError as e: diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index 61e531891..d17a18119 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -357,6 +357,32 @@ def test_should_send_template_to_correct_sms_task_and_persist(sample_template_wi assert persisted_notification.notification_type == 'sms' +def test_should_put_send_sms_task_in_research_mode_queue_if_research_mode_service(notify_db, notify_db_session, mocker): + service = sample_service(notify_db, notify_db_session) + service.research_mode = True + services_dao.dao_update_service(service) + + template = sample_template(notify_db, notify_db_session, service=service) + + notification = _notification_json(template, to="+447234123123") + + mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') + + notification_id = uuid.uuid4() + + send_sms( + template.service_id, + notification_id, + encryption.encrypt(notification), + datetime.utcnow().strftime(DATETIME_FORMAT) + ) + + provider_tasks.deliver_sms.apply_async.assert_called_once_with( + (notification_id), + queue="research-mode" + ) + + def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notify_db_session, mocker): user = sample_user(notify_db, notify_db_session, mobile_numnber="07700 900890") service = sample_service(notify_db, notify_db_session, user=user, restricted=True) @@ -491,6 +517,33 @@ def test_should_not_send_email_if_restricted_service_and_invalid_email_address(n Notification.query.filter_by(id=notification_id).one() +def test_should_put_send_email_task_in_research_mode_queue_if_research_mode_service(notify_db, notify_db_session, mocker): + service = sample_service(notify_db, notify_db_session) + service.research_mode = True + services_dao.dao_update_service(service) + + template = sample_email_template(notify_db, notify_db_session, service=service) + + notification = _notification_json(template, to="test@test.com") + + mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') + + notification_id = uuid.uuid4() + + send_email( + template.service_id, + notification_id, + encryption.encrypt(notification), + datetime.utcnow().strftime(DATETIME_FORMAT) + ) + + provider_tasks.deliver_email.apply_async.assert_called_once_with( + (notification_id), + queue="research-mode" + ) + + + def test_should_send_sms_template_to_and_persist_with_job_id(sample_job, sample_api_key, mocker): notification = _notification_json( sample_job.template,