mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 18:01:08 -05:00
Validate International phone numbers
- uses new utils methods to validate phone numbers - defaults to International=True on validation. This ensures the validator works on all numbers - Then check if the user can send this message to the number internationally if needed.
This commit is contained in:
@@ -4,6 +4,7 @@ from flask import current_app
|
||||
|
||||
from app import redis_store
|
||||
from app.celery import provider_tasks
|
||||
from notifications_utils.recipients import validate_and_format_phone_number
|
||||
from notifications_utils.clients import redis
|
||||
from app.dao.notifications_dao import dao_create_notification, dao_delete_notifications_and_history_by_id
|
||||
from app.models import SMS_TYPE, Notification, KEY_TYPE_TEST, EMAIL_TYPE
|
||||
@@ -98,6 +99,10 @@ def send_notification_to_queue(notification, research_mode, queue=None):
|
||||
|
||||
|
||||
def simulated_recipient(to_address, notification_type):
|
||||
return (to_address in current_app.config['SIMULATED_SMS_NUMBERS']
|
||||
if notification_type == SMS_TYPE
|
||||
else to_address in current_app.config['SIMULATED_EMAIL_ADDRESSES'])
|
||||
if notification_type == SMS_TYPE:
|
||||
formatted_simulated_numbers = [
|
||||
validate_and_format_phone_number(number) for number in current_app.config['SIMULATED_SMS_NUMBERS']
|
||||
]
|
||||
return to_address in formatted_simulated_numbers
|
||||
else:
|
||||
return to_address in current_app.config['SIMULATED_EMAIL_ADDRESSES']
|
||||
|
||||
@@ -2,8 +2,7 @@ from flask import (
|
||||
Blueprint,
|
||||
jsonify,
|
||||
request,
|
||||
current_app,
|
||||
json
|
||||
current_app
|
||||
)
|
||||
|
||||
from app import api_user
|
||||
@@ -14,10 +13,6 @@ from app.dao import (
|
||||
)
|
||||
from app.models import KEY_TYPE_TEAM, PRIORITY
|
||||
from app.models import SMS_TYPE
|
||||
from app.notifications.process_client_response import (
|
||||
validate_callback_data,
|
||||
process_sms_client_response
|
||||
)
|
||||
from app.notifications.process_notifications import (persist_notification,
|
||||
send_notification_to_queue,
|
||||
simulated_recipient)
|
||||
@@ -35,6 +30,8 @@ from app.schemas import (
|
||||
from app.service.utils import service_allowed_to_send_to
|
||||
from app.utils import pagination_links, get_template_instance
|
||||
|
||||
from notifications_utils.recipients import get_international_phone_info
|
||||
|
||||
notifications = Blueprint('notifications', __name__)
|
||||
|
||||
from app.errors import (
|
||||
@@ -104,13 +101,15 @@ def send_notification(notification_type):
|
||||
notification_form, errors = (
|
||||
sms_template_notification_schema if notification_type == SMS_TYPE else email_notification_schema
|
||||
).load(request.get_json())
|
||||
|
||||
if errors:
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
check_service_message_limit(api_user.key_type, service)
|
||||
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=notification_form['template'],
|
||||
service_id=service.id)
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(
|
||||
template_id=notification_form['template'],
|
||||
service_id=service.id)
|
||||
|
||||
check_template_is_for_notification_type(notification_type, template.template_type)
|
||||
check_template_is_active(template)
|
||||
@@ -118,12 +117,14 @@ def send_notification(notification_type):
|
||||
template_object = create_template_object_for_notification(template, notification_form.get('personalisation', {}))
|
||||
|
||||
_service_allowed_to_send_to(notification_form, service)
|
||||
if notification_type == SMS_TYPE:
|
||||
_service_can_send_internationally(service, notification_form['to'])
|
||||
|
||||
# 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,
|
||||
template_version=template.version,
|
||||
recipient=notification_form['to'],
|
||||
recipient=request.get_json()['to'],
|
||||
service=service,
|
||||
personalisation=notification_form.get('personalisation', None),
|
||||
notification_type=notification_type,
|
||||
@@ -160,6 +161,16 @@ def get_notification_return_data(notification_id, notification, template):
|
||||
return output
|
||||
|
||||
|
||||
def _service_can_send_internationally(service, number):
|
||||
international_phone_info = get_international_phone_info(number)
|
||||
|
||||
if international_phone_info.international and not service.can_send_international_sms:
|
||||
raise InvalidRequest(
|
||||
{'to': ["Cannot send to international mobile numbers"]},
|
||||
status_code=400
|
||||
)
|
||||
|
||||
|
||||
def _service_allowed_to_send_to(notification, service):
|
||||
if not service_allowed_to_send_to(notification['to'], service, api_user.key_type):
|
||||
if api_user.key_type == KEY_TYPE_TEAM:
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
from flask import current_app
|
||||
from notifications_utils.recipients import validate_and_format_phone_number, validate_and_format_email_address
|
||||
from notifications_utils.recipients import (
|
||||
validate_and_format_phone_number,
|
||||
validate_and_format_email_address,
|
||||
get_international_phone_info
|
||||
)
|
||||
|
||||
from app.dao import services_dao
|
||||
from app.models import KEY_TYPE_TEST, KEY_TYPE_TEAM, SMS_TYPE
|
||||
@@ -47,8 +51,17 @@ def service_can_send_to_recipient(send_to, key_type, service):
|
||||
|
||||
def validate_and_format_recipient(send_to, key_type, service, notification_type):
|
||||
service_can_send_to_recipient(send_to, key_type, service)
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
return validate_and_format_phone_number(number=send_to)
|
||||
international_phone_info = get_international_phone_info(send_to)
|
||||
|
||||
if international_phone_info.international and not service.can_send_international_sms:
|
||||
raise BadRequestError(message="Cannot send to international mobile numbers")
|
||||
|
||||
return validate_and_format_phone_number(
|
||||
number=send_to,
|
||||
international=international_phone_info.international
|
||||
)
|
||||
else:
|
||||
return validate_and_format_email_address(email_address=send_to)
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ def validate(json_to_validate, schema):
|
||||
@format_checker.checks('phone_number', raises=InvalidPhoneError)
|
||||
def validate_schema_phone_number(instance):
|
||||
if instance is not None:
|
||||
validate_phone_number(instance)
|
||||
validate_phone_number(instance, international=True)
|
||||
return True
|
||||
|
||||
@format_checker.checks('email_address', raises=InvalidEmailError)
|
||||
|
||||
@@ -324,13 +324,13 @@ class SmsNotificationSchema(NotificationSchema):
|
||||
@validates('to')
|
||||
def validate_to(self, value):
|
||||
try:
|
||||
validate_phone_number(value)
|
||||
validate_phone_number(value, international=True)
|
||||
except InvalidPhoneError as error:
|
||||
raise ValidationError('Invalid phone number: {}'.format(error))
|
||||
|
||||
@post_load
|
||||
def format_phone_number(self, item):
|
||||
item['to'] = validate_and_format_phone_number(item['to'])
|
||||
item['to'] = validate_and_format_phone_number(item['to'], international=True)
|
||||
return item
|
||||
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ def post_notification(notification_type):
|
||||
form = validate(request.get_json(), post_email_request)
|
||||
else:
|
||||
form = validate(request.get_json(), post_sms_request)
|
||||
|
||||
service = services_dao.dao_fetch_service_by_id(api_user.service_id)
|
||||
check_service_message_limit(api_user.key_type, service)
|
||||
form_send_to = form['phone_number'] if notification_type == SMS_TYPE else form['email_address']
|
||||
@@ -41,7 +42,7 @@ def post_notification(notification_type):
|
||||
simulated = simulated_recipient(send_to, notification_type)
|
||||
notification = persist_notification(template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient=send_to,
|
||||
recipient=form_send_to,
|
||||
service=service,
|
||||
personalisation=form.get('personalisation', None),
|
||||
notification_type=notification_type,
|
||||
|
||||
Reference in New Issue
Block a user