diff --git a/app/schemas.py b/app/schemas.py index 016cfadb7..b3aa0ac49 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -6,7 +6,11 @@ from . import models from app.dao.permissions_dao import permission_dao from marshmallow import (post_load, ValidationError, validates, validates_schema) from marshmallow_sqlalchemy import field_for -from utils.recipients import validate_email_address, InvalidEmailError, validate_phone_number, InvalidPhoneError +from utils.recipients import ( + validate_email_address, InvalidEmailError, + validate_phone_number, InvalidPhoneError, + format_phone_number +) # TODO I think marshmallow provides a better integration and error handling. @@ -130,6 +134,13 @@ class SmsNotificationSchema(NotificationSchema): except InvalidPhoneError as error: raise ValidationError('Invalid phone number: {}'.format(error)) + @post_load + def format_phone_number(self, item): + item['to'] = format_phone_number(validate_phone_number( + item['to']) + ) + return item + class EmailNotificationSchema(NotificationSchema): to = fields.Str(required=True) diff --git a/tests/app/notifications/test_rest.py b/tests/app/notifications/test_rest.py index 967a74d39..5d8ed0b73 100644 --- a/tests/app/notifications/test_rest.py +++ b/tests/app/notifications/test_rest.py @@ -504,7 +504,7 @@ def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker mocker.patch('app.encryption.encrypt', return_value="something_encrypted") data = { - 'to': '+447700900855', + 'to': '07700 900 855', 'template': sample_template.id } @@ -521,6 +521,7 @@ def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker headers=[('Content-Type', 'application/json'), auth_header]) notification_id = json.loads(response.data)['notification_id'] + assert app.encryption.encrypt.call_args[0][0]['to'] == '+447700900855' app.celery.tasks.send_sms.apply_async.assert_called_once_with( (str(sample_template.service_id), notification_id,