diff --git a/app/delivery/send_to_providers.py b/app/delivery/send_to_providers.py index 6cbd00e2d..e0202c897 100644 --- a/app/delivery/send_to_providers.py +++ b/app/delivery/send_to_providers.py @@ -8,7 +8,10 @@ from notifications_utils.recipients import ( from notifications_utils.template import HTMLEmailTemplate, PlainTextEmailTemplate, SMSMessageTemplate from app import clients, statsd_client, create_uuid -from app.dao.notifications_dao import dao_update_notification +from app.dao.notifications_dao import ( + dao_update_notification, + dao_get_notification_email_reply_for_notification +) from app.dao.provider_details_dao import ( get_provider_details_by_notification_type, dao_toggle_sms_provider @@ -110,13 +113,18 @@ def send_email_to_provider(notification): from_address = '"{}" <{}@{}>'.format(service.name, service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']) + email_reply_to = dao_get_notification_email_reply_for_notification(notification.id) + + if not email_reply_to: + email_reply_to = service.get_default_reply_to_email_address() + reference = provider.send_email( from_address, notification.to, plain_text_email.subject, body=str(plain_text_email), html_body=str(html_email), - reply_to_address=service.get_default_reply_to_email_address(), + reply_to_address=email_reply_to, ) notification.reference = reference update_notification(notification, provider) diff --git a/tests/app/db.py b/tests/app/db.py index dfa3f6a07..20eb3b91e 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -1,34 +1,33 @@ from datetime import datetime import uuid -from app import db, create_random_identifier +from app import db from app.dao.jobs_dao import dao_create_job from app.dao.service_inbound_api_dao import save_service_inbound_api from app.models import ( ApiKey, - EMAIL_TYPE, - SMS_TYPE, - KEY_TYPE_NORMAL, - Service, - User, - Template, - MonthlyBilling, - Notification, - ScheduledNotification, - ServicePermission, - Rate, - Job, InboundSms, InboundNumber, + Job, + MonthlyBilling, + Notification, + NotificationEmailReplyTo, Organisation, + Rate, + Service, + ServiceEmailReplyTo, + ServiceInboundApi, + ServiceLetterContact, + ScheduledNotification, + ServicePermission, + ServiceSmsSender, + Template, + User, EMAIL_TYPE, - LETTER_TYPE, SMS_TYPE, INBOUND_SMS_TYPE, - KEY_TYPE_NORMAL, - ServiceInboundApi, - ServiceEmailReplyTo, - ServiceLetterContact, ServiceSmsSender) + KEY_TYPE_NORMAL +) from app.dao.users_dao import save_model_user from app.dao.notifications_dao import dao_create_notification, dao_created_scheduled_notification from app.dao.templates_dao import dao_create_template @@ -384,3 +383,21 @@ def create_letter_contact( db.session.commit() return letter_content + + +def create_reply_to_email_for_notification( + notification_id, + service, + email_address, + is_default=True +): + reply_to = create_reply_to_email(service, email_address, is_default) + + notification_email_reply_to = NotificationEmailReplyTo( + notification_id=str(notification_id), + service_email_reply_to_id=str(reply_to.id) + ) + db.session.add(notification_email_reply_to) + db.session.commit() + + return reply_to diff --git a/tests/app/delivery/test_send_to_providers.py b/tests/app/delivery/test_send_to_providers.py index 30ca1e876..99fffc002 100644 --- a/tests/app/delivery/test_send_to_providers.py +++ b/tests/app/delivery/test_send_to_providers.py @@ -21,10 +21,17 @@ from app.models import ( BRANDING_ORG, BRANDING_GOVUK, BRANDING_BOTH, - BRANDING_ORG_BANNER) + BRANDING_ORG_BANNER +) -from tests.app.db import create_service, create_template, create_notification, create_inbound_number, \ - create_reply_to_email +from tests.app.db import ( + create_service, + create_template, + create_notification, + create_inbound_number, + create_reply_to_email, + create_reply_to_email_for_notification +) def test_should_return_highest_priority_active_provider(restore_provider_details): @@ -697,3 +704,65 @@ def test_should_use_inbound_number_as_sender_if_set( reference=str(notification.id), sender=inbound_number.number ) + + +def test_send_email_to_provider_get_linked_email_reply_to_default_is_false( + sample_service, + sample_email_template, + mocker): + mocker.patch('app.aws_ses_client.send_email', return_value='reference') + mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks') + + db_notification = create_notification(template=sample_email_template) + create_reply_to_email(service=sample_service, email_address='foo@bar.com') + + reply_to = create_reply_to_email_for_notification( + db_notification.id, + sample_service, + "test@test.com", + is_default=False + ) + + send_to_providers.send_email_to_provider( + db_notification, + ) + + app.aws_ses_client.send_email.assert_called_once_with( + ANY, + ANY, + ANY, + body=ANY, + html_body=ANY, + reply_to_address=reply_to.email_address + ) + + +def test_send_email_to_provider_get_linked_email_reply_to_create_service_email_after_notification_mapping( + sample_service, + sample_email_template, + mocker): + mocker.patch('app.aws_ses_client.send_email', return_value='reference') + mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks') + + db_notification = create_notification(template=sample_email_template) + + reply_to = create_reply_to_email_for_notification( + db_notification.id, + sample_service, + "test@test.com" + ) + + create_reply_to_email(service=sample_service, email_address='foo@bar.com', is_default=False) + + send_to_providers.send_email_to_provider( + db_notification, + ) + + app.aws_ses_client.send_email.assert_called_once_with( + ANY, + ANY, + ANY, + body=ANY, + html_body=ANY, + reply_to_address=reply_to.email_address + )