mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 17:31:14 -05:00
Check that the request payload data is valid json.
By adding `force=True` to request.get_json() the mime type is ignore. If the data is not valid json the method will return a `BadRequestError` we catch that and throw our own error with a clear error message "Invalid JSON supplied in POST data". If the json is valid return the json data or an empty dict if None is passed in. This PR improves the error messages if the json is invalid, previously, the error message was "None object type" message which is not very helpful.
This commit is contained in:
@@ -931,4 +931,7 @@ def test_post_email_notification_when_data_is_empty_returns_400(
|
||||
)
|
||||
error_msg = json.loads(response.get_data(as_text=True))["errors"][0]["message"]
|
||||
assert response.status_code == 400
|
||||
assert error_msg == 'Request body is empty.'
|
||||
if notification_type == 'sms':
|
||||
assert error_msg == 'phone_number is a required property'
|
||||
else:
|
||||
assert error_msg == 'email_address is a required property'
|
||||
|
||||
@@ -154,13 +154,40 @@ def test_post_template_with_non_existent_template_id_returns_404(client, fake_uu
|
||||
}
|
||||
|
||||
|
||||
def test_post_template_without_content_header(client, sample_template):
|
||||
def test_post_template_returns_200_without_personalisation(client, sample_template):
|
||||
response = client.post(
|
||||
path='/v2/template/{}/preview'.format(sample_template.id),
|
||||
data=None,
|
||||
headers=[('Content-Type', 'application/json'),
|
||||
create_authorization_header(service_id=sample_template.service_id)]
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_post_template_returns_200_without_personalisation_and_missing_content_header(client, sample_template):
|
||||
response = client.post(
|
||||
path='/v2/template/{}/preview'.format(sample_template.id),
|
||||
data=None,
|
||||
headers=[create_authorization_header(service_id=sample_template.service_id)]
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_post_template_returns_200_without_personalisation_as_valid_json_and_missing_content_header(
|
||||
client, sample_template
|
||||
):
|
||||
response = client.post(
|
||||
path='/v2/template/{}/preview'.format(sample_template.id),
|
||||
data=json.dumps(None),
|
||||
headers=[create_authorization_header(service_id=sample_template.service_id)]
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_post_template_returns_200_with_valid_json_and_missing_content_header(client, sample_template):
|
||||
response = client.post(
|
||||
path='/v2/template/{}/preview'.format(sample_template.id),
|
||||
data=json.dumps(valid_personalisation),
|
||||
headers=[create_authorization_header(service_id=sample_template.service_id)]
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
assert json_response['errors'][0]['message'] == 'Content-Type header is not set to application/json.'
|
||||
assert response.status_code == 200
|
||||
|
||||
Reference in New Issue
Block a user