Merge pull request #772 from alphagov/fix-v2-uri

Fix uri validation in v2 responses
This commit is contained in:
imdadahad
2016-12-16 15:53:26 +00:00
committed by GitHub
5 changed files with 169 additions and 81 deletions

View File

@@ -42,4 +42,27 @@ def build_error_message(errors):
def __format_message(e):
return e.message.replace("'", "") if not e.cause else "{} {}".format(e.path[0], e.cause.message)
def get_path(e):
error_path = None
try:
error_path = e.path.popleft()
# no need to catch IndexError exception explicity as
# error_path is None if e.path has no items
finally:
return error_path
def get_error_message(e):
error_message = None
try:
error_message = e.cause.message
except AttributeError:
error_message = e.message
finally:
return error_message.replace("'", '')
path = get_path(e)
message = get_error_message(e)
if path:
return "{} {}".format(path, message)
else:
return "{}".format(message)

View File

@@ -10,7 +10,7 @@ template = {
"properties": {
"id": uuid,
"version": {"type": "integer"},
"uri": {"type": "string"}
"uri": {"type": "string", "format": "uri"}
},
"required": ["id", "version", "uri"]
}
@@ -163,7 +163,7 @@ post_sms_response = {
"id": uuid,
"reference": {"type": ["string", "null"]},
"content": sms_content,
"uri": {"type": "string"},
"uri": {"type": "string", "format": "uri"},
"template": template
},
"required": ["id", "content", "uri", "template"]
@@ -206,7 +206,7 @@ post_email_response = {
"id": uuid,
"reference": {"type": ["string", "null"]},
"content": email_content,
"uri": {"type": "string"},
"uri": {"type": "string", "format": "uri"},
"template": template
},
"required": ["id", "content", "uri", "template"]
@@ -218,7 +218,7 @@ def create_post_sms_response_from_notification(notification, body, from_number,
"reference": notification.client_reference,
"content": {'body': body,
'from_number': from_number},
"uri": "{}/v2/notifications/{}".format(url_root, str(notification.id)),
"uri": "{}v2/notifications/{}".format(url_root, str(notification.id)),
"template": __create_template_from_notification(notification=notification, url_root=url_root)
}
@@ -232,7 +232,7 @@ def create_post_email_response_from_notification(notification, content, subject,
"body": content,
"subject": subject
},
"uri": "{}/v2/notifications/{}".format(url_root, str(notification.id)),
"uri": "{}v2/notifications/{}".format(url_root, str(notification.id)),
"template": __create_template_from_notification(notification=notification, url_root=url_root)
}
@@ -241,5 +241,5 @@ def __create_template_from_notification(notification, url_root):
return {
"id": notification.template_id,
"version": notification.template_version,
"uri": "{}/v2/templates/{}".format(url_root, str(notification.template_id))
"uri": "{}v2/templates/{}".format(url_root, str(notification.template_id))
}