Merge pull request #943 from alphagov/schema-validation

fix v2 schema phone/email validation when non-str passed in
This commit is contained in:
Leo Hemsted
2017-05-11 14:42:22 +01:00
committed by GitHub
2 changed files with 25 additions and 10 deletions

View File

@@ -10,13 +10,13 @@ def validate(json_to_validate, schema):
@format_checker.checks('phone_number', raises=InvalidPhoneError)
def validate_schema_phone_number(instance):
if instance is not None:
if isinstance(instance, str):
validate_phone_number(instance, international=True)
return True
@format_checker.checks('email_address', raises=InvalidEmailError)
def validate_schema_email_address(instance):
if instance is not None:
if isinstance(instance, str):
validate_email_address(instance)
return True

View File

@@ -131,10 +131,15 @@ def test_post_sms_schema_with_personalisation_that_is_not_a_dict():
assert len(error.keys()) == 2
@pytest.mark.parametrize('invalid_phone_number, err_msg',
[('08515111111', 'phone_number Not a UK mobile number'),
('07515111*11', 'phone_number Must not contain letters or symbols'),
('notaphoneumber', 'phone_number Must not contain letters or symbols')])
@pytest.mark.parametrize('invalid_phone_number, err_msg', [
('08515111111', 'phone_number Not a UK mobile number'),
('07515111*11', 'phone_number Must not contain letters or symbols'),
('notaphoneumber', 'phone_number Must not contain letters or symbols'),
(7700900001, 'phone_number 7700900001 is not of type string'),
(None, 'phone_number None is not of type string'),
([], 'phone_number [] is not of type string'),
({}, 'phone_number {} is not of type string'),
])
def test_post_sms_request_schema_invalid_phone_number(invalid_phone_number, err_msg):
j = {"phone_number": invalid_phone_number,
"template_id": str(uuid.uuid4())
@@ -213,12 +218,22 @@ def test_post_email_schema_bad_uuid_and_missing_email_address():
validate(j, post_email_request_schema)
def test_post_email_schema_invalid_email_address():
j = {"template_id": str(uuid.uuid4()),
"email_address": "notavalidemail@address"}
with pytest.raises(ValidationError):
@pytest.mark.parametrize('email_address, err_msg', [
('example', 'email_address Not a valid email address'),
(12345, 'email_address 12345 is not of type string'),
(None, 'email_address None is not of type string'),
([], 'email_address [] is not of type string'),
({}, 'email_address {} is not of type string'),
])
def test_post_email_schema_invalid_email_address(email_address, err_msg):
j = {"template_id": str(uuid.uuid4()), "email_address": email_address}
with pytest.raises(ValidationError) as e:
validate(j, post_email_request_schema)
errors = json.loads(str(e.value)).get('errors')
assert len(errors) == 1
assert {"error": "ValidationError", "message": err_msg} == errors[0]
def valid_email_response():
return {