diff --git a/app/v2/notifications/post_notifications.py b/app/v2/notifications/post_notifications.py index 178893218..9e5f6163a 100644 --- a/app/v2/notifications/post_notifications.py +++ b/app/v2/notifications/post_notifications.py @@ -3,6 +3,7 @@ import functools import io import math +import werkzeug from flask import request, jsonify, current_app, abort from notifications_utils.pdf import pdf_page_count, PdfReadError from notifications_utils.recipients import try_validate_and_format_phone_number @@ -104,12 +105,17 @@ def post_precompiled_letter_notification(): @v2_notification_blueprint.route('/', methods=['POST']) def post_notification(notification_type): + try: + request_json = request.get_json() + except werkzeug.exceptions.BadRequest as e: + raise BadRequestError(message=e.description, status_code=400) + if notification_type == EMAIL_TYPE: - form = validate(request.get_json(), post_email_request) + form = validate(request_json, post_email_request) elif notification_type == SMS_TYPE: - form = validate(request.get_json(), post_sms_request) + form = validate(request_json, post_sms_request) elif notification_type == LETTER_TYPE: - form = validate(request.get_json(), post_letter_request) + form = validate(request_json, post_letter_request) else: abort(404) diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index ce2ae1ec0..707ab2a04 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -839,3 +839,12 @@ def test_post_notification_without_document_upload_permission(client, notify_db, headers=[('Content-Type', 'application/json'), auth_header]) assert response.status_code == 400, response.get_data(as_text=True) + + +def test_post_notification_returns_400_when_get_json_throws_exception(client, sample_email_template, rmock, mocker): + auth_header = create_authorization_header(service_id=sample_email_template.service_id) + response = client.post( + path="v2/notifications/email", + data="[", + headers=[('Content-Type', 'application/json'), auth_header]) + assert response.status_code == 400