diff --git a/app/delivery/send_to_providers.py b/app/delivery/send_to_providers.py index 6cbd00e2d..6ef710075 100644 --- a/app/delivery/send_to_providers.py +++ b/app/delivery/send_to_providers.py @@ -8,7 +8,7 @@ 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 +110,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..a2b2b98c3 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -28,7 +28,7 @@ from app.models import ( KEY_TYPE_NORMAL, ServiceInboundApi, ServiceEmailReplyTo, - ServiceLetterContact, ServiceSmsSender) + ServiceLetterContact, ServiceSmsSender, NotificationEmailReplyTo) 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 +384,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..7b9c8cd8b 100644 --- a/tests/app/delivery/test_send_to_providers.py +++ b/tests/app/delivery/test_send_to_providers.py @@ -6,6 +6,7 @@ from unittest.mock import ANY, call import pytest from notifications_utils.recipients import validate_and_format_phone_number from flask import current_app +from requests_mock import mock import app from app import mmg_client, firetext_client @@ -21,10 +22,10 @@ from app.models import ( BRANDING_ORG, BRANDING_GOVUK, BRANDING_BOTH, - BRANDING_ORG_BANNER) + BRANDING_ORG_BANNER, NotificationEmailReplyTo) from tests.app.db import create_service, create_template, create_notification, create_inbound_number, \ - create_reply_to_email + create_reply_to_email, create_reply_to_email_for_notification def test_should_return_highest_priority_active_provider(restore_provider_details): @@ -697,3 +698,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 + )