diff --git a/app/invite/rest.py b/app/invite/rest.py index 8105b171f..0553b3a07 100644 --- a/app/invite/rest.py +++ b/app/invite/rest.py @@ -42,7 +42,8 @@ def create_invited_user(service_id): }, notification_type=EMAIL_TYPE, api_key_id=None, - key_type=KEY_TYPE_NORMAL + key_type=KEY_TYPE_NORMAL, + reply_to_text=service.get_default_reply_to_email_address() ) send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY) diff --git a/app/notifications/rest.py b/app/notifications/rest.py index 5a640b0e7..db33143e9 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -119,7 +119,7 @@ def send_notification(notification_type): if notification_type == SMS_TYPE: _service_can_send_internationally(authenticated_service, notification_form['to']) - + reply_to = get_reply_to_text(notification_type) # Do not persist or send notification to the queue if it is a simulated recipient simulated = simulated_recipient(notification_form['to'], notification_type) notification_model = persist_notification(template_id=template.id, @@ -130,7 +130,9 @@ def send_notification(notification_type): notification_type=notification_type, api_key_id=api_user.id, key_type=api_user.key_type, - simulated=simulated) + simulated=simulated, + reply_to_text=reply_to + ) if not simulated: queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None send_notification_to_queue(notification=notification_model, @@ -148,6 +150,16 @@ def send_notification(notification_type): ), 201 +def get_reply_to_text(notification_type): + if notification_type == EMAIL_TYPE: + return authenticated_service.get_default_reply_to_email_address() + if notification_type == SMS_TYPE: + return authenticated_service.get_default_sms_sender() + if notification_type == LETTER_TYPE: + return authenticated_service.get_default_letter_contact() + + + def get_notification_return_data(notification_id, notification, template): output = { 'body': str(template), diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 948ba20cb..611baf810 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -29,7 +29,8 @@ from app.models import ( ServiceWhitelist, KEY_TYPE_NORMAL, KEY_TYPE_TEST, KEY_TYPE_TEAM, MOBILE_TYPE, EMAIL_TYPE, INBOUND_SMS_TYPE, SMS_TYPE, LETTER_TYPE, NOTIFICATION_STATUS_TYPES_COMPLETED, - SERVICE_PERMISSION_TYPES) + SERVICE_PERMISSION_TYPES, ServiceEmailReplyTo +) from app.dao.users_dao import (create_user_code, create_secret_code) from app.dao.services_dao import (dao_create_service, dao_add_user_to_service) from app.dao.templates_dao import dao_create_template @@ -985,17 +986,26 @@ def notify_service(notify_db, notify_db_session): user = create_user() service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID']) if not service: + service = Service( + name='Notify Service', + message_limit=1000, + restricted=False, + email_from='notify.service', + created_by=user, + prefix_sms=False, + ) + dao_create_service(service=service, service_id=current_app.config['NOTIFY_SERVICE_ID'], user=user) + data = { - 'id': current_app.config['NOTIFY_SERVICE_ID'], - 'name': 'Notify Service', - 'message_limit': 1000, - 'active': True, - 'restricted': False, - 'email_from': 'notify.service', - 'created_by': user, + 'service': service, + 'email_address': "notify@gov.uk", + 'is_default': True, } - service = Service(**data) - db.session.add(service) + reply_to = ServiceEmailReplyTo(**data) + + db.session.add(reply_to) + db.session.commit() + return service, user diff --git a/tests/app/invite/test_invite_rest.py b/tests/app/invite/test_invite_rest.py index 2d1b52b02..fbfa83b58 100644 --- a/tests/app/invite/test_invite_rest.py +++ b/tests/app/invite/test_invite_rest.py @@ -33,6 +33,7 @@ def test_create_invited_user(admin_request, sample_service, mocker, invitation_e assert json_resp['data']['id'] notification = Notification.query.first() + assert notification.reply_to_text == "notify@gov.uk" mocked.assert_called_once_with([(str(notification.id))], queue="notify-internal-tasks") diff --git a/tests/app/notifications/rest/test_send_notification.py b/tests/app/notifications/rest/test_send_notification.py index c15052acd..5238d547a 100644 --- a/tests/app/notifications/rest/test_send_notification.py +++ b/tests/app/notifications/rest/test_send_notification.py @@ -31,6 +31,7 @@ from tests.app.conftest import ( from app.models import Template from app.errors import InvalidRequest +from tests.app.db import create_service, create_reply_to_email @pytest.mark.parametrize('template_type', @@ -1219,3 +1220,34 @@ def test_should_throw_exception_if_notification_type_is_invalid(client, sample_s headers=[('Content-Type', 'application/json'), auth_header]) assert response.status_code == 400 assert json.loads(response.get_data(as_text=True))["message"] == err_msg + + +@pytest.mark.parametrize("notification_type, recipient", + [("sms", '07700 900 855'), + ("email", "test@gov.uk") + ] + ) +def test_post_notification_should_set_reply_to_text(client, notify_db, notify_db_session, mocker, notification_type, + recipient): + mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(notification_type)) + service = create_service() + template = create_sample_template(notify_db=notify_db, notify_db_session=notify_db_session, + service=service, template_type=notification_type) + expected_reply_to = current_app.config['FROM_NUMBER'] + if notification_type == EMAIL_TYPE: + expected_reply_to = 'reply_to@gov.uk' + create_reply_to_email(service=service, email_address=expected_reply_to, is_default=True) + + data = { + 'to': recipient, + 'template': str(template.id) + } + response = client.post("/notifications/{}".format(notification_type), + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), + create_authorization_header(service_id=service.id)] + ) + assert response.status_code == 201 + notifications = Notification.query.all() + assert len(notifications) == 1 + assert notifications[0].reply_to_text == expected_reply_to