From 5986a650052b71a37bbffe1e609ba6eb8d6dce86 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Wed, 24 Feb 2021 14:38:06 +0000 Subject: [PATCH] Check international number for alpha: NO if true then use number to send SMS. This is not a catch all for international SMS, the rules are quite complex and still not completely understood. We are talking with our provider who maybe able to sort this out for us. But in the meantime, this should solve for the case that we understand. --- app/user/rest.py | 20 +++++++++++--------- requirements-app.txt | 2 +- tests/app/user/test_rest.py | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/app/user/rest.py b/app/user/rest.py index 32aa7b532..0f9c90ad3 100644 --- a/app/user/rest.py +++ b/app/user/rest.py @@ -4,7 +4,7 @@ from datetime import datetime from urllib.parse import urlencode from flask import (jsonify, request, Blueprint, current_app, abort) -from notifications_utils.recipients import is_uk_phone_number +from notifications_utils.recipients import is_uk_phone_number, use_numeric_sender from sqlalchemy.exc import IntegrityError from app.config import QueueNames @@ -105,10 +105,7 @@ def update_user_attribute(user_id): elif 'mobile_number' in update_dct: template = dao_get_template_by_id(current_app.config['TEAM_MEMBER_EDIT_MOBILE_TEMPLATE_ID']) recipient = user_to_update.mobile_number - if is_uk_phone_number(recipient): - reply_to = template.service.get_default_sms_sender() - else: - reply_to = current_app.config['NOTIFY_INTERNATIONAL_SMS_SENDER'] + reply_to = set_reply_to(recipient, template) else: return jsonify(data=user_to_update.serialize()), 200 service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID']) @@ -133,6 +130,14 @@ def update_user_attribute(user_id): return jsonify(data=user_to_update.serialize()), 200 +def set_reply_to(recipient, template): + if not is_uk_phone_number(recipient) and use_numeric_sender(recipient): + reply_to = current_app.config['NOTIFY_INTERNATIONAL_SMS_SENDER'] + else: + reply_to = template.service.get_default_sms_sender() + return reply_to + + @user_blueprint.route('//archive', methods=['POST']) def archive_user(user_id): user = get_user_by_id(user_id) @@ -271,10 +276,7 @@ def create_2fa_code(template_id, user_to_send_to, secret_code, recipient, person create_user_code(user_to_send_to, secret_code, template.template_type) reply_to = None if template.template_type == SMS_TYPE: - if is_uk_phone_number(recipient): - reply_to = template.service.get_default_sms_sender() - else: - reply_to = current_app.config['NOTIFY_INTERNATIONAL_SMS_SENDER'] + reply_to = set_reply_to(recipient, template) elif template.template_type == EMAIL_TYPE: reply_to = template.service.get_default_reply_to_email_address() saved_notification = persist_notification( diff --git a/requirements-app.txt b/requirements-app.txt index 8d99c0d8f..80933ce40 100644 --- a/requirements-app.txt +++ b/requirements-app.txt @@ -33,7 +33,7 @@ notifications-python-client==5.7.1 # PaaS awscli-cwlogs==1.4.6 -git+https://github.com/alphagov/notifications-utils.git@43.9.0#egg=notifications-utils==43.9.0 +git+https://github.com/alphagov/notifications-utils.git@43.9.1#egg=notifications-utils==43.9.1 # gds-metrics requires prometheseus 0.2.0, override that requirement as 0.7.1 brings significant performance gains prometheus-client==0.9.0 diff --git a/tests/app/user/test_rest.py b/tests/app/user/test_rest.py index 64ad80d15..7c17f9e83 100644 --- a/tests/app/user/test_rest.py +++ b/tests/app/user/test_rest.py @@ -315,7 +315,7 @@ def test_post_user_attribute_with_updated_by_sends_notification_to_international ): updater = create_user(name="Service Manago") update_dict = { - 'mobile_number': '601117224412', + 'mobile_number': '+601117224412', 'updated_by': str(updater.id) } auth_header = create_authorization_header()