mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-05 10:42:41 -05:00
Updated test_validators to test the contents of the error messages.
Added some tests to the test_post_notifications. Added a errorhandler for AuthErrors. This endpoint is not being used anywhere, however there is some common code being used in the v1 post endpoint. The only thing that may be affected is the error response, hopefully they are the same.
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
from flask import jsonify, current_app
|
||||
from sqlalchemy.exc import SQLAlchemyError, DataError
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from app.authentication.auth import AuthError
|
||||
from app.errors import InvalidRequest
|
||||
|
||||
|
||||
@@ -15,11 +19,11 @@ class TooManyRequestsError(InvalidRequest):
|
||||
|
||||
class BadRequestError(InvalidRequest):
|
||||
status_code = 400
|
||||
code = "10400"
|
||||
code = 10400
|
||||
link = "link to documentation"
|
||||
message = "An error occurred"
|
||||
|
||||
def __init__(self, fields=None, message=None):
|
||||
def __init__(self, fields=[], message=None):
|
||||
self.fields = fields
|
||||
self.message = message if message else self.message
|
||||
|
||||
@@ -31,7 +35,20 @@ def register_errors(blueprint):
|
||||
response = jsonify(error.to_dict_v2()), error.status_code
|
||||
return response
|
||||
|
||||
@blueprint.errorhandler(NoResultFound)
|
||||
@blueprint.errorhandler(DataError)
|
||||
def no_result_found(e):
|
||||
current_app.logger.exception(e)
|
||||
return jsonify(message="No result found"), 404
|
||||
|
||||
@blueprint.errorhandler(AuthError)
|
||||
def auth_error(error):
|
||||
return jsonify(status_code=error.code,
|
||||
message=error.message,
|
||||
code=error.code,
|
||||
link='link to docs'), error.code
|
||||
|
||||
@blueprint.errorhandler(Exception)
|
||||
def authentication_error(error):
|
||||
# v2 error format - NOT this
|
||||
return jsonify(result='error', message=error.message), error.code
|
||||
def internal_server_error(error):
|
||||
current_app.logger.exception(error)
|
||||
return jsonify(message='Internal server error'), 500
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
from flask import request, jsonify
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from app import api_user
|
||||
from app.dao import services_dao, templates_dao
|
||||
from app.models import SMS_TYPE
|
||||
@@ -11,6 +13,7 @@ from app.notifications.validators import (check_service_message_limit,
|
||||
service_can_send_to_recipient,
|
||||
check_sms_content_char_count)
|
||||
from app.schema_validation import validate
|
||||
from app.v2.errors import BadRequestError
|
||||
from app.v2.notifications import notification_blueprint
|
||||
from app.v2.notifications.notification_schemas import (post_sms_request,
|
||||
create_post_sms_response_from_notification)
|
||||
@@ -52,8 +55,14 @@ def post_email_notification():
|
||||
|
||||
|
||||
def __validate_template(form, service):
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=form['template_id'],
|
||||
service_id=service.id)
|
||||
try:
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=form['template_id'],
|
||||
service_id=service.id)
|
||||
except NoResultFound:
|
||||
message = 'Template not found'
|
||||
raise BadRequestError(message=message,
|
||||
fields=[{'template': message}])
|
||||
|
||||
check_template_is_for_notification_type(SMS_TYPE, template.template_type)
|
||||
check_template_is_active(template)
|
||||
template_with_content = create_content_for_notification(template, form.get('personalisation', {}))
|
||||
|
||||
Reference in New Issue
Block a user