mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 18:31:13 -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:
@@ -20,8 +20,8 @@ from tests.app.conftest import (
|
||||
sample_email_template as create_sample_email_template,
|
||||
sample_template as create_sample_template,
|
||||
sample_service_whitelist as create_sample_service_whitelist,
|
||||
sample_api_key as create_sample_api_key
|
||||
)
|
||||
sample_api_key as create_sample_api_key,
|
||||
sample_service)
|
||||
|
||||
from app.models import Template
|
||||
from app.errors import InvalidRequest
|
||||
@@ -1046,3 +1046,81 @@ def test_send_notification_uses_priority_queue_when_template_is_marked_as_priori
|
||||
|
||||
assert response.status_code == 201
|
||||
mocked.assert_called_once_with([notification_id], queue='priority')
|
||||
|
||||
|
||||
def test_should_allow_store_original_number_on_sms_notification(notify_api, sample_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
data = {
|
||||
'to': '+(44) 7700-900 855',
|
||||
'template': str(sample_template.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(service_id=sample_template.service_id)
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/sms',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
response_data = json.loads(response.data)['data']
|
||||
notification_id = response_data['notification']['id']
|
||||
|
||||
mocked.assert_called_once_with([notification_id], queue='send-sms')
|
||||
assert response.status_code == 201
|
||||
assert notification_id
|
||||
notifications = Notification.query.all()
|
||||
assert len(notifications) == 1
|
||||
assert '+(44) 7700-900 855' == notifications[0].to
|
||||
|
||||
|
||||
def test_should_not_allow_international_number_on_sms_notification(notify_api, sample_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
data = {
|
||||
'to': '20-12-1234-1234',
|
||||
'template': str(sample_template.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(service_id=sample_template.service_id)
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/sms',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
assert not mocked.called
|
||||
assert response.status_code == 400
|
||||
error_json = json.loads(response.get_data(as_text=True))
|
||||
assert error_json['result'] == 'error'
|
||||
assert error_json['message']['to'][0] == 'Cannot send to international mobile numbers'
|
||||
|
||||
|
||||
def test_should_allow_international_number_on_sms_notification(notify_api, notify_db, notify_db_session, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
service = sample_service(notify_db, notify_db_session, can_send_international_sms=True)
|
||||
template = create_sample_template(notify_db, notify_db_session, service=service)
|
||||
|
||||
data = {
|
||||
'to': '20-12-1234-1234',
|
||||
'template': str(template.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(service_id=service.id)
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/sms',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
assert response.status_code == 201
|
||||
|
||||
@@ -2,12 +2,14 @@ import pytest
|
||||
from freezegun import freeze_time
|
||||
|
||||
import app
|
||||
from app.models import KEY_TYPE_NORMAL
|
||||
from app.notifications.validators import (
|
||||
check_service_message_limit,
|
||||
check_template_is_for_notification_type,
|
||||
check_template_is_active,
|
||||
service_can_send_to_recipient,
|
||||
check_sms_content_char_count
|
||||
check_sms_content_char_count,
|
||||
validate_and_format_recipient
|
||||
)
|
||||
from app.v2.errors import (
|
||||
BadRequestError,
|
||||
@@ -262,3 +264,19 @@ def test_check_sms_content_char_count_fails(char_count, notify_api):
|
||||
assert e.value.message == 'Content for template has a character count greater than the limit of {}'.format(
|
||||
notify_api.config['SMS_CHAR_COUNT_LIMIT'])
|
||||
assert e.value.fields == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type', ['test', 'normal'])
|
||||
def test_rejects_api_calls_with_international_numbers_if_service_does_not_allow_int_sms(sample_service, key_type):
|
||||
with pytest.raises(BadRequestError) as e:
|
||||
validate_and_format_recipient('20-12-1234-1234', key_type, sample_service, 'sms')
|
||||
assert e.value.status_code == 400
|
||||
assert e.value.message == 'Cannot send to international mobile numbers'
|
||||
assert e.value.fields == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type', ['test', 'normal'])
|
||||
def test_allows_api_calls_with_international_numbers_if_service_does_allow_int_sms(sample_service, key_type):
|
||||
sample_service.can_send_international_sms = True
|
||||
result = validate_and_format_recipient('20-12-1234-1234', key_type, sample_service, 'sms')
|
||||
assert result == '201212341234'
|
||||
|
||||
Reference in New Issue
Block a user