- Add validation methods for post notification.

- Use these validation methods in post_sms_notification and the version 1 of post_notification.
- Create a v2 error handlers.
- InvalidRequest has a to_dict method for private and v1 error responses and a to_dict_v2 method to create the v2 of the error responses.
- Each validation method has extensive unit tests, so the unit test for the endpoint do not need to check every error case, but check that the error handle formats the message correctly.
- The format of the error messages is still a work on progress.
- This version of the api could be deployed without causing a problem to the application.
- The new endpoing is still a work in progress and is not being used yet.
This commit is contained in:
Rebecca Law
2016-10-27 11:46:37 +01:00
parent 23a4f00e56
commit c2eecdae36
12 changed files with 328 additions and 101 deletions

View File

@@ -1,38 +1,40 @@
from flask import request
from app import api_user
from app.dao import services_dao, templates_dao
from app.models import SMS_TYPE
from app.notifications.process_notifications import create_content_for_notification
from app.notifications.validators import (check_service_message_limit,
check_template_is_for_notification_type,
check_template_is_active,
service_can_send_to_recipient,
check_sms_content_char_count)
from app.schema_validation import validate
from app.v2.notifications import notification_blueprint
from app.v2.notifications.notification_schemas import post_sms_request
@notification_blueprint.route('/sms', methods=['POST'])
def post_sms_notification():
# # get service
# service = services_dao.dao_fetch_service_by_id(api_user.service_id)
# # validate input against json schema (not marshmallow)
# form = validate(request.get_json(), post_sms_request)
#
# # following checks will be in a common function for all versions of the endpoint.
# # check service has not exceeded the sending limit
# check_service_message_limit(api_user.key_type, service)
# template = templates_dao.dao_get_template_by_id_and_service_id(
# template_id=form['template_id'],
# service_id=service.id
# )
# # check template is for sms
# check_template_is_for_notification_type(SMS_TYPE, template.template_type)
# # check template is not archived
# check_template_is_active(template)
# # check service is allowed to send
# service_can_send_to_recipient(form['phone_number'], api_user.key_type, service)
# # create body of message (create_template_object_for_notification)
# create_template_object_for_notification(template, form.get('personalisation', {}))
# # persist notification
# # send sms to provider queue for research mode queue
#
form = validate(request.get_json(), post_sms_request)
service = services_dao.dao_fetch_service_by_id(api_user.service_id)
# following checks will be in a common function for all versions of the endpoint.
# check service has not exceeded the sending limit
check_service_message_limit(api_user.key_type, service)
service_can_send_to_recipient(form['phone_number'], api_user.key_type, service, SMS_TYPE)
template = templates_dao.dao_get_template_by_id_and_service_id(
template_id=form['template_id'],
service_id=service.id)
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', {}))
check_sms_content_char_count(template_with_content.replaced_content_count)
# validate post form against post_sms_request schema
# validate service
# validate template
# create content
# persist notification
# send notification to queue
# send sms to provider queue for research mode queue
# return post_sms_response schema
return "post_sms_response schema", 201