From a358f3cb3a60e90c241bcc71a63f602acf7ee5d6 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Mon, 31 Oct 2016 15:43:11 +0000 Subject: [PATCH] Error handler for schema validation errors --- app/v2/errors.py | 10 ++++++- .../notifications/test_post_notifications.py | 26 +++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/app/v2/errors.py b/app/v2/errors.py index 7fe378419..f6fa7e911 100644 --- a/app/v2/errors.py +++ b/app/v2/errors.py @@ -1,5 +1,8 @@ +import json + from flask import jsonify, current_app -from sqlalchemy.exc import SQLAlchemyError, DataError +from jsonschema import ValidationError +from sqlalchemy.exc import DataError from sqlalchemy.orm.exc import NoResultFound from app.authentication.auth import AuthError @@ -35,6 +38,11 @@ def register_errors(blueprint): response = jsonify(error.to_dict_v2()), error.status_code return response + @blueprint.errorhandler(ValidationError) + def validation_error(error): + current_app.logger.exception(error) + return jsonify(json.loads(error.message)), 400 + @blueprint.errorhandler(NoResultFound) @blueprint.errorhandler(DataError) def no_result_found(e): diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index 8bb633629..7eb20121c 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -53,10 +53,9 @@ def test_post_sms_notification_returns_404_and_missing_template(notify_api, samp assert error_json['link'] == 'link to documentation' -def test_post_sms_notification_returns_403_and_well_formed_auth_error(notify_api, sample_template, mocker): +def test_post_sms_notification_returns_403_and_well_formed_auth_error(notify_api, sample_template): with notify_api.test_request_context(): with notify_api.test_client() as client: - mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') data = { 'phone_number': '+447700900855', 'template_id': str(sample_template.id) @@ -72,3 +71,26 @@ def test_post_sms_notification_returns_403_and_well_formed_auth_error(notify_api error_resp = json.loads(response.get_data(as_text=True)) assert error_resp['code'] == 401 assert error_resp['message'] == {'token': ['Unauthorized, authentication token must be provided']} + + +def test_post_sms_notification_returns_400_and_for_schema_problems(notify_api, sample_template): + with notify_api.test_request_context(): + with notify_api.test_client() as client: + data = { + 'phone_number': '+447700900855', + 'template': str(sample_template.id) + } + auth_header = create_authorization_header(service_id=sample_template.service_id) + + response = client.post( + path='/v2/notifications/sms', + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header]) + + assert response.status_code == 400 + assert response.headers['Content-type'] == 'application/json' + error_resp = json.loads(response.get_data(as_text=True)) + assert error_resp['code'] == '1001' + assert error_resp['message'] == 'Validation error occurred - POST v2/notifications/sms' + assert error_resp['link'] == "link to error documentation (not yet implemented)" + assert error_resp['fields'] == ["'template_id' is a required property"]