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:
Rebecca Law
2016-10-31 12:22:26 +00:00
parent 8cf2fc72a8
commit fc298367c5
7 changed files with 120 additions and 42 deletions

View File

@@ -25,12 +25,12 @@ def create_content_for_notification(template, personalisation):
def check_placeholders(template_object):
if template_object.missing_data:
message = 'Template missing personalisation: {}'.format(", ".join(template_object.missing_data))
raise BadRequestError(message=message)
raise BadRequestError(fields=[{'template': message}], message=message)
if template_object.additional_data:
message = 'Template personalisation not needed for template: {}'.format(
", ".join(template_object.additional_data))
raise BadRequestError(message=message)
raise BadRequestError(fields=[{'template': message}], message=message)
def persist_notification(template_id,

View File

@@ -16,14 +16,15 @@ def check_service_message_limit(key_type, service):
def check_template_is_for_notification_type(notification_type, template_type):
if notification_type != template_type:
raise BadRequestError(
message="{0} template is not suitable for {1} notification".format(template_type,
notification_type))
message = "{0} template is not suitable for {1} notification".format(template_type,
notification_type)
raise BadRequestError(fields=[{'template': message}], message=message)
def check_template_is_active(template):
if template.archived:
raise BadRequestError(message="Template has been deleted")
raise BadRequestError(fields=[{'template': 'Template has been deleted'}],
message="Template has been deleted")
def service_can_send_to_recipient(send_to, key_type, service):

View File

@@ -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

View File

@@ -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', {}))