mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-10 01:55:41 -04:00
Store phone number as the user entered it
It’s confusing to the user to have their phone number played back to them in a format that they didn’t enter it. We’ve seen multiple times that people enter 0781… and then don’t recognise their own phone number when it’s played back as +44781… The API can handle phone numbers in any format as of https://github.com/alphagov/notifications-api/pull/134 So there is no need to reformat the user’s phone number before storing it now.
This commit is contained in:
@@ -37,19 +37,10 @@ class UKMobileNumber(TelField):
|
|||||||
|
|
||||||
def pre_validate(self, form):
|
def pre_validate(self, form):
|
||||||
try:
|
try:
|
||||||
self.data = validate_phone_number(self.data)
|
validate_phone_number(self.data)
|
||||||
except InvalidPhoneError as e:
|
except InvalidPhoneError as e:
|
||||||
raise ValidationError(e.message)
|
raise ValidationError(e.message)
|
||||||
|
|
||||||
def post_validate(self, form, validation_stopped):
|
|
||||||
|
|
||||||
if len(self.data) != 9:
|
|
||||||
return
|
|
||||||
# TODO implement in the render field method.
|
|
||||||
# API's require no spaces in the number
|
|
||||||
# self.data = '+44 7{} {} {}'.format(*re.findall('...', self.data))
|
|
||||||
self.data = format_phone_number(self.data)
|
|
||||||
|
|
||||||
|
|
||||||
def mobile_number():
|
def mobile_number():
|
||||||
return UKMobileNumber('Mobile phone number',
|
return UKMobileNumber('Mobile phone number',
|
||||||
|
|||||||
@@ -1,75 +0,0 @@
|
|||||||
import pytest
|
|
||||||
from wtforms import Form
|
|
||||||
from app.main.forms import UKMobileNumber
|
|
||||||
|
|
||||||
|
|
||||||
class FormExample(Form):
|
|
||||||
phone_number = UKMobileNumber()
|
|
||||||
|
|
||||||
invalid_phone_numbers = sum([
|
|
||||||
[
|
|
||||||
(phone_number, error) for phone_number in group
|
|
||||||
] for error, group in [
|
|
||||||
('Too many digits', (
|
|
||||||
'0712345678910',
|
|
||||||
'0044712345678910',
|
|
||||||
'0044712345678910',
|
|
||||||
'+44 (0)7123 456 789 10',
|
|
||||||
)),
|
|
||||||
('Not enough digits', (
|
|
||||||
'0712345678',
|
|
||||||
'004471234567',
|
|
||||||
'00447123456',
|
|
||||||
'+44 (0)7123 456 78',
|
|
||||||
)),
|
|
||||||
('Must be a UK mobile number (eg 07700 900460)', (
|
|
||||||
'08081 570364',
|
|
||||||
'+44 8081 570364',
|
|
||||||
'0117 496 0860',
|
|
||||||
'+44 117 496 0860',
|
|
||||||
'020 7946 0991',
|
|
||||||
'+44 20 7946 0991',
|
|
||||||
'71234567890',
|
|
||||||
)),
|
|
||||||
('Must not contain letters or symbols', (
|
|
||||||
'07890x32109',
|
|
||||||
'07123 456789...',
|
|
||||||
'07123 ☟☜⬇⬆☞☝',
|
|
||||||
'07123☟☜⬇⬆☞☝',
|
|
||||||
'07";DROP TABLE;"',
|
|
||||||
'+44 07ab cde fgh',
|
|
||||||
))
|
|
||||||
]
|
|
||||||
], [])
|
|
||||||
|
|
||||||
valid_phone_numbers = [
|
|
||||||
'07123456789',
|
|
||||||
'07123 456789',
|
|
||||||
'07123-456-789',
|
|
||||||
'00447123456789',
|
|
||||||
'00 44 7123456789',
|
|
||||||
'+447123456789',
|
|
||||||
'+44 7123 456 789',
|
|
||||||
'+44 (0)7123 456 789'
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("phone_number", valid_phone_numbers)
|
|
||||||
def test_phone_number_accepts_valid_values(phone_number):
|
|
||||||
form = FormExample(phone_number=phone_number)
|
|
||||||
form.validate()
|
|
||||||
assert form.errors == {}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("phone_number, error_message", invalid_phone_numbers)
|
|
||||||
def test_phone_number_rejects_invalid_values(phone_number, error_message):
|
|
||||||
form = FormExample(phone_number=phone_number)
|
|
||||||
form.validate()
|
|
||||||
assert form.phone_number.errors[0] == error_message
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("phone_number", valid_phone_numbers)
|
|
||||||
def test_phone_number_outputs_in_correct_format(phone_number):
|
|
||||||
form = FormExample(phone_number=phone_number)
|
|
||||||
form.validate()
|
|
||||||
assert form.phone_number.data == '+447123456789'
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
from app.utils import (
|
|
||||||
validate_phone_number,
|
|
||||||
InvalidPhoneError
|
|
||||||
)
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
valid_phone_numbers = [
|
|
||||||
'07123456789',
|
|
||||||
'07123 456789',
|
|
||||||
'07123-456-789',
|
|
||||||
'00447123456789',
|
|
||||||
'00 44 7123456789',
|
|
||||||
'+447123456789',
|
|
||||||
'+44 7123 456 789',
|
|
||||||
'+44 (0)7123 456 789'
|
|
||||||
]
|
|
||||||
|
|
||||||
invalid_phone_numbers = sum([
|
|
||||||
[
|
|
||||||
(phone_number, error) for phone_number in group
|
|
||||||
] for error, group in [
|
|
||||||
('Too many digits', (
|
|
||||||
'0712345678910',
|
|
||||||
'0044712345678910',
|
|
||||||
'0044712345678910',
|
|
||||||
'+44 (0)7123 456 789 10',
|
|
||||||
)),
|
|
||||||
('Not enough digits', (
|
|
||||||
'0712345678',
|
|
||||||
'004471234567',
|
|
||||||
'00447123456',
|
|
||||||
'+44 (0)7123 456 78',
|
|
||||||
)),
|
|
||||||
('Must be a UK mobile number (eg 07700 900460)', (
|
|
||||||
'08081 570364',
|
|
||||||
'+44 8081 570364',
|
|
||||||
'0117 496 0860',
|
|
||||||
'+44 117 496 0860',
|
|
||||||
'020 7946 0991',
|
|
||||||
'+44 20 7946 0991',
|
|
||||||
'71234567890',
|
|
||||||
)),
|
|
||||||
('Must not contain letters or symbols', (
|
|
||||||
'07890x32109',
|
|
||||||
'07123 456789...',
|
|
||||||
'07123 ☟☜⬇⬆☞☝',
|
|
||||||
'07123☟☜⬇⬆☞☝',
|
|
||||||
'07";DROP TABLE;"',
|
|
||||||
'+44 07ab cde fgh',
|
|
||||||
))
|
|
||||||
]
|
|
||||||
], [])
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("phone_number", valid_phone_numbers)
|
|
||||||
def test_phone_number_accepts_valid_values(phone_number):
|
|
||||||
try:
|
|
||||||
validate_phone_number(phone_number)
|
|
||||||
except InvalidPhoneError:
|
|
||||||
pytest.fail('Unexpected InvalidPhoneError')
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("phone_number, error_message", invalid_phone_numbers)
|
|
||||||
def test_phone_number_rejects_invalid_values(phone_number, error_message):
|
|
||||||
with pytest.raises(InvalidPhoneError) as e:
|
|
||||||
validate_phone_number(phone_number)
|
|
||||||
assert error_message == str(e.value)
|
|
||||||
Reference in New Issue
Block a user