diff --git a/app/v2/notifications/post_notifications.py b/app/v2/notifications/post_notifications.py index 5466be234..bcb2b4b7e 100644 --- a/app/v2/notifications/post_notifications.py +++ b/app/v2/notifications/post_notifications.py @@ -2,6 +2,8 @@ import functools from flask import request, jsonify, current_app, abort +from notifications_utils.recipients import try_validate_and_format_phone_number + from app import api_user, authenticated_service from app.config import QueueNames from app.models import ( @@ -208,9 +210,13 @@ def get_reply_to_text(notification_type, form, template): elif notification_type == SMS_TYPE: service_sms_sender_id = form.get("sms_sender_id", None) - reply_to = check_service_sms_sender_id( + sms_sender_id = check_service_sms_sender_id( str(authenticated_service.id), service_sms_sender_id, notification_type - ) or template.get_reply_to_text() + ) + if sms_sender_id: + reply_to = try_validate_and_format_phone_number(sms_sender_id) + else: + reply_to = template.get_reply_to_text() elif notification_type == LETTER_TYPE: reply_to = template.get_reply_to_text() diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index cba00ff9b..df669d258 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -98,6 +98,34 @@ def test_post_sms_notification_uses_inbound_number_as_sender(client, notify_db_s mocked.assert_called_once_with([str(notification_id)], queue='send-sms-tasks') +def test_post_sms_notification_uses_inbound_number_reply_to_as_sender(client, notify_db_session, mocker): + service = create_service_with_inbound_number(inbound_number='07123123123') + + template = create_template(service=service, content="Hello (( Name))\nYour thing is due soon") + mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') + data = { + 'phone_number': '+447700900855', + 'template_id': str(template.id), + 'personalisation': {' Name': 'Jo'} + } + auth_header = create_authorization_header(service_id=service.id) + + response = client.post( + path='/v2/notifications/sms', + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header]) + assert response.status_code == 201 + resp_json = json.loads(response.get_data(as_text=True)) + assert validate(resp_json, post_sms_response) == resp_json + notifications = Notification.query.all() + assert len(notifications) == 1 + notification_id = notifications[0].id + assert resp_json['id'] == str(notification_id) + assert resp_json['content']['from_number'] == '447123123123' + assert notifications[0].reply_to_text == '447123123123' + mocked.assert_called_once_with([str(notification_id)], queue='send-sms-tasks') + + def test_post_sms_notification_returns_201_with_sms_sender_id( client, sample_template_with_placeholders, mocker ): @@ -125,6 +153,33 @@ def test_post_sms_notification_returns_201_with_sms_sender_id( mocked.assert_called_once_with([resp_json['id']], queue='send-sms-tasks') +def test_post_sms_notification_uses_sms_sender_id_reply_to( + client, sample_template_with_placeholders, mocker +): + sms_sender = create_service_sms_sender(service=sample_template_with_placeholders.service, sms_sender='07123123123') + mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') + data = { + 'phone_number': '+447700900855', + 'template_id': str(sample_template_with_placeholders.id), + 'personalisation': {' Name': 'Jo'}, + 'sms_sender_id': str(sms_sender.id) + } + auth_header = create_authorization_header(service_id=sample_template_with_placeholders.service_id) + + response = client.post( + path='/v2/notifications/sms', + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header]) + assert response.status_code == 201 + resp_json = json.loads(response.get_data(as_text=True)) + assert validate(resp_json, post_sms_response) == resp_json + assert resp_json['content']['from_number'] == '447123123123' + notifications = Notification.query.all() + assert len(notifications) == 1 + assert notifications[0].reply_to_text == '447123123123' + mocked.assert_called_once_with([resp_json['id']], queue='send-sms-tasks') + + def test_notification_reply_to_text_is_original_value_if_sender_is_changed_after_post_notification( client, sample_template, mocker ):