mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 08:51:30 -05:00
Error handler for schema validation errors
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
from flask import jsonify, current_app
|
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 sqlalchemy.orm.exc import NoResultFound
|
||||||
|
|
||||||
from app.authentication.auth import AuthError
|
from app.authentication.auth import AuthError
|
||||||
@@ -35,6 +38,11 @@ def register_errors(blueprint):
|
|||||||
response = jsonify(error.to_dict_v2()), error.status_code
|
response = jsonify(error.to_dict_v2()), error.status_code
|
||||||
return response
|
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(NoResultFound)
|
||||||
@blueprint.errorhandler(DataError)
|
@blueprint.errorhandler(DataError)
|
||||||
def no_result_found(e):
|
def no_result_found(e):
|
||||||
|
|||||||
@@ -53,10 +53,9 @@ def test_post_sms_notification_returns_404_and_missing_template(notify_api, samp
|
|||||||
assert error_json['link'] == 'link to documentation'
|
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_request_context():
|
||||||
with notify_api.test_client() as client:
|
with notify_api.test_client() as client:
|
||||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
|
||||||
data = {
|
data = {
|
||||||
'phone_number': '+447700900855',
|
'phone_number': '+447700900855',
|
||||||
'template_id': str(sample_template.id)
|
'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))
|
error_resp = json.loads(response.get_data(as_text=True))
|
||||||
assert error_resp['code'] == 401
|
assert error_resp['code'] == 401
|
||||||
assert error_resp['message'] == {'token': ['Unauthorized, authentication token must be provided']}
|
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"]
|
||||||
|
|||||||
Reference in New Issue
Block a user