Throw error if postage parameter for precompiled POST request incorrect

This commit is contained in:
Pea Tyczynska
2019-01-10 16:04:06 +00:00
parent 56bae2b077
commit 5a1094b6fd
3 changed files with 28 additions and 1 deletions

View File

@@ -29,6 +29,13 @@ def validate(json_to_validate, schema):
validate_email_address(instance)
return True
@format_checker.checks('postage', raises=ValidationError)
def validate_schema_postage(instance):
if isinstance(instance, str):
if instance not in ["first", "second"]:
raise ValidationError("invalid. It must be either first or second.")
return True
@format_checker.checks('datetime_within_next_day', raises=ValidationError)
def validate_schema_date_with_hour(instance):
if isinstance(instance, str):

View File

@@ -240,7 +240,7 @@ post_precompiled_letter_request = {
"properties": {
"reference": {"type": "string"},
"content": {"type": "string"},
"postage": {"type": "string"}
"postage": {"type": "string", "format": "postage"}
},
"required": ["reference", "content"],
"additionalProperties": False

View File

@@ -511,3 +511,23 @@ def test_post_precompiled_letter_notification_returns_201(
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json == {'id': str(notification.id), 'reference': 'letter-reference'}
def test_post_letter_notification_throws_error_for_invalid_postage(client, notify_user, mocker):
sample_service = create_service(service_permissions=['letter', 'precompiled_letter'])
data = {
"reference": "letter-reference",
"content": "bGV0dGVyLWNvbnRlbnQ=",
"postage": "space unicorn"
}
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.post(
path="v2/notifications/letter",
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 400, response.get_data(as_text=True)
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['errors'][0]['message'] == "postage invalid. It must be either first or second."
assert not Notification.query.first()