From 30fe41fd43589b8e850b2c74d62c4be84747acf1 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Thu, 1 Nov 2018 17:00:44 +0000 Subject: [PATCH] Pass sender_id argument to tasks Started passing `sender_id` to the `save_email`, `save_sms` and `process_job` tasks, with a default value of `None`. If `sender_id` is provided, the `save_email` and `save_sms` tasks will use it to determine the reply-to email address or the SMS sender for the notifications in the job. The `process_job` task will start using the value in another commit. --- app/celery/tasks.py | 24 ++++++++++--- tests/app/celery/test_tasks.py | 62 +++++++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/app/celery/tasks.py b/app/celery/tasks.py index a6371c6be..a7713e668 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -45,7 +45,9 @@ from app.dao.notifications_dao import ( dao_get_notification_by_reference, ) from app.dao.provider_details_dao import get_current_provider +from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id from app.dao.service_inbound_api_dao import get_service_inbound_api_for_service +from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id from app.dao.services_dao import dao_fetch_service_by_id, fetch_todays_total_message_count from app.dao.templates_dao import dao_get_template_by_id from app.exceptions import DVLAException, NotificationTechnicalFailureException @@ -75,7 +77,7 @@ from app.utils import convert_utc_to_bst @notify_celery.task(name="process-job") @statsd(namespace="tasks") -def process_job(job_id): +def process_job(job_id, sender_id=None): start = datetime.utcnow() job = dao_get_job_by_id(job_id) @@ -181,11 +183,17 @@ def __sending_limits_for_job_exceeded(service, job, job_id): def save_sms(self, service_id, notification_id, - encrypted_notification): + encrypted_notification, + sender_id=None): notification = encryption.decrypt(encrypted_notification) service = dao_fetch_service_by_id(service_id) template = dao_get_template_by_id(notification['template'], version=notification['template_version']) + if sender_id: + reply_to_text = dao_get_service_sms_senders_by_id(service_id, sender_id).sms_sender + else: + reply_to_text = template.get_reply_to_text() + if not service_allowed_to_send_to(notification['to'], service, KEY_TYPE_NORMAL): current_app.logger.debug( "SMS {} failed as restricted service".format(notification_id) @@ -206,7 +214,7 @@ def save_sms(self, job_id=notification.get('job', None), job_row_number=notification.get('row_number', None), notification_id=notification_id, - reply_to_text=template.get_reply_to_text() + reply_to_text=reply_to_text ) provider_tasks.deliver_sms.apply_async( @@ -230,12 +238,18 @@ def save_sms(self, def save_email(self, service_id, notification_id, - encrypted_notification): + encrypted_notification, + sender_id=None): notification = encryption.decrypt(encrypted_notification) service = dao_fetch_service_by_id(service_id) template = dao_get_template_by_id(notification['template'], version=notification['template_version']) + if sender_id: + reply_to_text = dao_get_reply_to_by_id(service_id, sender_id).email_address + else: + reply_to_text = template.get_reply_to_text() + if not service_allowed_to_send_to(notification['to'], service, KEY_TYPE_NORMAL): current_app.logger.info("Email {} failed as restricted service".format(notification_id)) return @@ -254,7 +268,7 @@ def save_email(self, job_id=notification.get('job', None), job_row_number=notification.get('row_number', None), notification_id=notification_id, - reply_to_text=template.get_reply_to_text() + reply_to_text=reply_to_text ) provider_tasks.deliver_email.apply_async( diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index 71880f1d2..0348c6438 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -30,7 +30,7 @@ from app.celery.tasks import ( process_returned_letters_list, ) from app.config import QueueNames -from app.dao import jobs_dao, services_dao +from app.dao import jobs_dao, services_dao, service_email_reply_to_dao, service_sms_sender_dao from app.models import ( Job, Notification, @@ -739,6 +739,46 @@ def test_should_use_email_template_subject_placeholders(sample_email_template_wi ) +def test_save_email_uses_the_reply_to_text_when_provided(sample_email_template, mocker): + notification = _notification_json(sample_email_template, "my_email@my_email.com") + mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') + + service = sample_email_template.service + notification_id = uuid.uuid4() + service_email_reply_to_dao.add_reply_to_email_address_for_service(service.id, 'default@example.com', True) + other_email_reply_to = service_email_reply_to_dao.add_reply_to_email_address_for_service( + service.id, 'other@example.com', False) + + save_email( + sample_email_template.service_id, + notification_id, + encryption.encrypt(notification), + sender_id=other_email_reply_to.id, + ) + persisted_notification = Notification.query.one() + assert persisted_notification.notification_type == 'email' + assert persisted_notification.reply_to_text == 'other@example.com' + + +def test_save_email_uses_the_default_reply_to_text_if_sender_id_is_none(sample_email_template, mocker): + notification = _notification_json(sample_email_template, "my_email@my_email.com") + mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') + + service = sample_email_template.service + notification_id = uuid.uuid4() + service_email_reply_to_dao.add_reply_to_email_address_for_service(service.id, 'default@example.com', True) + + save_email( + sample_email_template.service_id, + notification_id, + encryption.encrypt(notification), + sender_id=None, + ) + persisted_notification = Notification.query.one() + assert persisted_notification.notification_type == 'email' + assert persisted_notification.reply_to_text == 'default@example.com' + + def test_should_use_email_template_and_persist_without_personalisation(sample_email_template, mocker): notification = _notification_json(sample_email_template, "my_email@my_email.com") mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') @@ -1031,6 +1071,26 @@ def test_save_sms_uses_sms_sender_reply_to_text(mocker, notify_db_session): assert persisted_notification.reply_to_text == '447123123123' +def test_save_sms_uses_non_default_sms_sender_reply_to_text_if_provided(mocker, notify_db_session): + service = create_service_with_defined_sms_sender(sms_sender_value='07123123123') + template = create_template(service=service) + new_sender = service_sms_sender_dao.dao_add_sms_sender_for_service(service.id, 'new-sender', False) + + notification = _notification_json(template, to="07700 900205") + mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') + + notification_id = uuid.uuid4() + save_sms( + service.id, + notification_id, + encryption.encrypt(notification), + sender_id=new_sender.id, + ) + + persisted_notification = Notification.query.one() + assert persisted_notification.reply_to_text == 'new-sender' + + @pytest.mark.parametrize('env', ['staging', 'live']) def test_save_letter_sets_delivered_letters_as_pdf_permission_in_research_mode_in_staging_live( notify_api, mocker, notify_db_session, sample_letter_job, env):