Skip to content

Commit

Permalink
Merge pull request #1228 from GSA/wrong_phone_numbers2
Browse files Browse the repository at this point in the history
raise exception if phone number doesn't match
  • Loading branch information
ccostino authored Aug 1, 2024
2 parents eab4dfa + 635c170 commit b3e56cb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
16 changes: 16 additions & 0 deletions app/delivery/send_to_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from app.dao.email_branding_dao import dao_get_email_branding_by_id
from app.dao.notifications_dao import dao_update_notification
from app.dao.provider_details_dao import get_provider_details_by_notification_type
from app.dao.service_sms_sender_dao import dao_get_sms_senders_by_service_id
from app.enums import BrandType, KeyType, NotificationStatus, NotificationType
from app.exceptions import NotificationTechnicalFailureException
from app.serialised_models import SerialisedService, SerialisedTemplate
Expand Down Expand Up @@ -101,6 +102,13 @@ def send_sms_to_provider(notification):
raise Exception(
f"The recipient for (Service ID: {si}; Job ID: {ji}; Job Row Number {jrn} was not found."
)

sender_numbers = get_sender_numbers(notification)
if notification.reply_to_text not in sender_numbers:
raise ValueError(
f"{notification.reply_to_text} not in {sender_numbers} #notify-admin-1701"
)

send_sms_kwargs = {
"to": recipient,
"content": str(template),
Expand Down Expand Up @@ -130,6 +138,14 @@ def send_sms_to_provider(notification):
return message_id


def get_sender_numbers(notification):
possible_senders = dao_get_sms_senders_by_service_id(notification.service_id)
sender_numbers = []
for possible_sender in possible_senders:
sender_numbers.append(possible_sender.sms_sender)
return sender_numbers


def send_email_to_provider(notification):
# Someone needs an email, possibly new registration
recipient = redis_store.get(f"email-address-{notification.id}")
Expand Down
21 changes: 19 additions & 2 deletions tests/app/delivery/test_send_to_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ def test_should_send_sms_with_downgraded_content(notify_db_session, mocker):
# é, o, and u are in GSM.
# ī, grapes, tabs, zero width space and ellipsis are not
# ó isn't in GSM, but it is in the welsh alphabet so will still be sent
mocker.patch(
"app.delivery.send_to_providers.get_sender_numbers", return_value=["testing"]
)
msg = "a é ī o u 🍇 foo\tbar\u200bbaz((misc))…"
placeholder = "∆∆∆abc"
gsm_message = "?ódz Housing Service: a é i o u ? foo barbaz???abc..."
Expand All @@ -327,6 +330,7 @@ def test_should_send_sms_with_downgraded_content(notify_db_session, mocker):
template=template,
)
db_notification.personalisation = {"misc": placeholder}
db_notification.reply_to_text = 'testing'

mocker.patch("app.aws_sns_client.send_sms")

Expand Down Expand Up @@ -609,11 +613,16 @@ def __update_notification(notification_to_update, research_mode, expected_status
def test_should_update_billable_units_and_status_according_to_research_mode_and_key_type(
sample_template, mocker, research_mode, key_type, billable_units, expected_status
):

mocker.patch(
"app.delivery.send_to_providers.get_sender_numbers", return_value=["testing"]
)
notification = create_notification(
template=sample_template,
billable_units=0,
status=NotificationStatus.CREATED,
key_type=key_type,
reply_to_text='testing',
)
mocker.patch("app.aws_sns_client.send_sms")
mocker.patch(
Expand Down Expand Up @@ -771,9 +780,13 @@ def test_send_email_to_provider_uses_reply_to_from_notification(


def test_send_sms_to_provider_should_use_normalised_to(mocker, client, sample_template):

mocker.patch(
"app.delivery.send_to_providers.get_sender_numbers", return_value=["testing"]
)
send_mock = mocker.patch("app.aws_sns_client.send_sms")
notification = create_notification(
template=sample_template, to_field="+12028675309", normalised_to="2028675309"
template=sample_template, to_field="+12028675309", normalised_to="2028675309", reply_to_text='testing'
)

mock_s3 = mocker.patch("app.delivery.send_to_providers.get_phone_number_from_s3")
Expand Down Expand Up @@ -826,6 +839,10 @@ def test_send_email_to_provider_should_user_normalised_to(
def test_send_sms_to_provider_should_return_template_if_found_in_redis(
mocker, client, sample_template
):

mocker.patch(
"app.delivery.send_to_providers.get_sender_numbers", return_value=["testing"]
)
from app.schemas import service_schema, template_schema

service_dict = service_schema.dump(sample_template.service)
Expand All @@ -845,7 +862,7 @@ def test_send_sms_to_provider_should_return_template_if_found_in_redis(

send_mock = mocker.patch("app.aws_sns_client.send_sms")
notification = create_notification(
template=sample_template, to_field="+447700900855", normalised_to="447700900855"
template=sample_template, to_field="+447700900855", normalised_to="447700900855", reply_to_text='testing'
)

mock_s3 = mocker.patch("app.delivery.send_to_providers.get_phone_number_from_s3")
Expand Down

0 comments on commit b3e56cb

Please sign in to comment.