- 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,6 +1,9 @@
from flask import current_app
from app.dao import services_dao
from app.errors import InvalidRequest
from app.models import KEY_TYPE_TEST
from app.models import KEY_TYPE_TEST, KEY_TYPE_TEAM
from app.service.utils import service_allowed_to_send_to
from app.v2.errors import TooManyRequestsError, BadRequestError
def check_service_message_limit(key_type, service):
@@ -8,18 +11,43 @@ def check_service_message_limit(key_type, service):
service.restricted)):
service_stats = services_dao.fetch_todays_total_message_count(service.id)
if service_stats >= service.message_limit:
error = 'Exceeded send limits ({}) for today'.format(service.message_limit)
raise InvalidRequest(error, status_code=429)
raise TooManyRequestsError(service.message_limit)
def check_template_is_for_notification_type(notification_type, template_type):
if notification_type != template_type:
raise InvalidRequest("{0} template is not suitable for {1} notification".format(template_type,
notification_type),
status_code=400)
raise BadRequestError(
message="{0} template is not suitable for {1} notification".format(template_type,
notification_type),
fields=[{"template": "{0} template is not suitable for {1} notification".format(template_type,
notification_type)}])
def check_template_is_active(template):
if template.archived:
raise InvalidRequest('Template has been deleted', status_code=400)
raise BadRequestError(fields=[{"template": "has been deleted"}],
message="Template has been deleted")
def service_can_send_to_recipient(send_to, key_type, service, recipient_type):
if not service_allowed_to_send_to(send_to, service, key_type):
if key_type == KEY_TYPE_TEAM:
message = 'Cant send to this recipient using a team-only API key'
else:
message = (
'Cant send to this recipient when service is in trial mode '
' see https://www.notifications.service.gov.uk/trial-mode'
)
raise BadRequestError(
fields={recipient_type: [message]}
)
def check_sms_content_char_count(content_count):
char_count_limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
if (
content_count > char_count_limit
):
message = 'Content has a character count greater than the limit of {}'.format(char_count_limit)
errors = {'content': [message]}
raise BadRequestError(fields=errors)