From 26f50af6e9bbdefb2daedc9b885c23cc7fef3d56 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 30 Aug 2017 10:55:18 +0100 Subject: [PATCH] Let whitelist and user have int. phone numbers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On the frontend, we’re letting users register with international phone numbers. So we shouldn’t block users from doing this on the API side. Same thing for the whitelist, where we’re also allowing international phone numbers now. --- app/models.py | 2 +- app/schemas.py | 2 +- tests/app/dao/test_users_dao.py | 9 +++++++-- tests/app/service/test_service_whitelist.py | 5 +++-- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/app/models.py b/app/models.py index e32456f68..89e4d56b8 100644 --- a/app/models.py +++ b/app/models.py @@ -318,7 +318,7 @@ class ServiceWhitelist(db.Model): try: if recipient_type == MOBILE_TYPE: - validate_phone_number(recipient) + validate_phone_number(recipient, international=True) instance.recipient = recipient elif recipient_type == EMAIL_TYPE: validate_email_address(recipient) diff --git a/app/schemas.py b/app/schemas.py index af76eef6e..5f81821d4 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -130,7 +130,7 @@ class UserUpdateAttributeSchema(BaseSchema): @validates('mobile_number') def validate_mobile_number(self, value): try: - validate_phone_number(value) + validate_phone_number(value, international=True) except InvalidPhoneError as error: raise ValidationError('Invalid phone number: {}'.format(error)) diff --git a/tests/app/dao/test_users_dao.py b/tests/app/dao/test_users_dao.py index c815eb66b..22e1670f3 100644 --- a/tests/app/dao/test_users_dao.py +++ b/tests/app/dao/test_users_dao.py @@ -23,19 +23,24 @@ from app.models import User, VerifyCode from tests.app.db import create_user -def test_create_user(notify_db_session): +@pytest.mark.parametrize('phone_number', [ + '+447700900986', + '+1-800-555-5555', +]) +def test_create_user(notify_db_session, phone_number): email = 'notify@digital.cabinet-office.gov.uk' data = { 'name': 'Test User', 'email_address': email, 'password': 'password', - 'mobile_number': '+447700900986' + 'mobile_number': phone_number } user = User(**data) save_model_user(user) assert User.query.count() == 1 assert User.query.first().email_address == email assert User.query.first().id == user.id + assert User.query.first().mobile_number == phone_number assert not user.platform_admin diff --git a/tests/app/service/test_service_whitelist.py b/tests/app/service/test_service_whitelist.py index 23f6a3aa2..5edbe8e28 100644 --- a/tests/app/service/test_service_whitelist.py +++ b/tests/app/service/test_service_whitelist.py @@ -24,14 +24,15 @@ def test_get_whitelist_returns_data(client, sample_service_whitelist): def test_get_whitelist_separates_emails_and_phones(client, sample_service): dao_add_and_commit_whitelisted_contacts([ ServiceWhitelist.from_string(sample_service.id, EMAIL_TYPE, 'service@example.com'), - ServiceWhitelist.from_string(sample_service.id, MOBILE_TYPE, '07123456789') + ServiceWhitelist.from_string(sample_service.id, MOBILE_TYPE, '07123456789'), + ServiceWhitelist.from_string(sample_service.id, MOBILE_TYPE, '+1800-555-555'), ]) response = client.get('service/{}/whitelist'.format(sample_service.id), headers=[create_authorization_header()]) assert response.status_code == 200 assert json.loads(response.get_data(as_text=True)) == { 'email_addresses': ['service@example.com'], - 'phone_numbers': ['07123456789'] + 'phone_numbers': ['+1800-555-555', '07123456789'] }