mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-18 06:11:49 -05:00
Throw error if postage parameter for precompiled POST request incorrect
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user