mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 15:31:15 -05:00
Make sure we check if a service can send to number harmoniously
We were checking this separately in two places in the code. Now we will have this logic in one place, in validators. Also pull in utils version that recognises crown depenency numbers as international.
This commit is contained in:
@@ -15,7 +15,7 @@ from app.errors import (
|
||||
InvalidRequest
|
||||
)
|
||||
from app.models import (
|
||||
EMAIL_TYPE, INTERNATIONAL_SMS_TYPE, SMS_TYPE,
|
||||
EMAIL_TYPE, SMS_TYPE,
|
||||
KEY_TYPE_TEAM, PRIORITY,
|
||||
LETTER_TYPE)
|
||||
from app.notifications.process_notifications import (
|
||||
@@ -24,9 +24,10 @@ from app.notifications.process_notifications import (
|
||||
simulated_recipient
|
||||
)
|
||||
from app.notifications.validators import (
|
||||
check_if_service_can_send_to_number,
|
||||
check_rate_limiting,
|
||||
service_has_permission,
|
||||
validate_template
|
||||
validate_template,
|
||||
)
|
||||
from app.schemas import (
|
||||
email_notification_schema,
|
||||
@@ -38,7 +39,6 @@ from app.service.utils import service_allowed_to_send_to
|
||||
from app.utils import pagination_links, get_public_notify_type_text
|
||||
|
||||
from notifications_utils import SMS_CHAR_COUNT_LIMIT
|
||||
from notifications_utils.recipients import get_international_phone_info
|
||||
|
||||
notifications = Blueprint('notifications', __name__)
|
||||
|
||||
@@ -115,7 +115,7 @@ def send_notification(notification_type):
|
||||
)
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
_service_can_send_internationally(authenticated_service, notification_form['to'])
|
||||
check_if_service_can_send_to_number(authenticated_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,
|
||||
@@ -160,17 +160,6 @@ 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 international_phone_info.country_prefix != '44') and \
|
||||
INTERNATIONAL_SMS_TYPE not in [p.permission for p in service.permissions]:
|
||||
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:
|
||||
|
||||
@@ -120,11 +120,7 @@ def validate_and_format_recipient(send_to, key_type, service, notification_type,
|
||||
service_can_send_to_recipient(send_to, key_type, service, allow_whitelisted_recipients)
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
international_phone_info = get_international_phone_info(send_to)
|
||||
|
||||
if international_phone_info.international and \
|
||||
INTERNATIONAL_SMS_TYPE not in [p.permission for p in service.permissions]:
|
||||
raise BadRequestError(message="Cannot send to international mobile numbers")
|
||||
international_phone_info = check_if_service_can_send_to_number(service, send_to)
|
||||
|
||||
return validate_and_format_phone_number(
|
||||
number=send_to,
|
||||
@@ -134,6 +130,18 @@ def validate_and_format_recipient(send_to, key_type, service, notification_type,
|
||||
return validate_and_format_email_address(email_address=send_to)
|
||||
|
||||
|
||||
def check_if_service_can_send_to_number(service, number):
|
||||
international_phone_info = get_international_phone_info(number)
|
||||
|
||||
if (
|
||||
# if number is international and not a crown dependency
|
||||
international_phone_info.international and international_phone_info.country_prefix != '44'
|
||||
) and INTERNATIONAL_SMS_TYPE not in [p.permission for p in service.permissions]:
|
||||
raise BadRequestError(message="Cannot send to international mobile numbers")
|
||||
else:
|
||||
return international_phone_info
|
||||
|
||||
|
||||
def check_content_char_count(template_with_content):
|
||||
if template_with_content.is_message_too_long():
|
||||
message = f"Text messages cannot be longer than {SMS_CHAR_COUNT_LIMIT} characters. " \
|
||||
|
||||
Reference in New Issue
Block a user