mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 09:51:11 -05:00
V2 schemas for post sms notifications, post_sms_request and post_sms_response
This commit is contained in:
26
app/schema_validation/__init__.py
Normal file
26
app/schema_validation/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import json
|
||||||
|
from jsonschema import Draft4Validator, ValidationError
|
||||||
|
|
||||||
|
|
||||||
|
def validate(json_to_validate, schema):
|
||||||
|
validator = Draft4Validator(schema)
|
||||||
|
errors = list(validator.iter_errors(json_to_validate))
|
||||||
|
if errors.__len__() > 0:
|
||||||
|
raise ValidationError(build_error_message(errors, schema))
|
||||||
|
return json_to_validate
|
||||||
|
|
||||||
|
|
||||||
|
def build_error_message(errors, schema):
|
||||||
|
fields = []
|
||||||
|
for e in errors:
|
||||||
|
field = "'{}' {}".format(e.path[0], e.schema.get('validationMessage')) if e.schema.get(
|
||||||
|
'validationMessage') else e.message
|
||||||
|
fields.append(field)
|
||||||
|
message = {
|
||||||
|
"code": "1001",
|
||||||
|
"message": "Validation error occurred - {}".format(schema['title']),
|
||||||
|
"link": "link to error documentation (not yet implemented)",
|
||||||
|
"fields": fields
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.dumps(message)
|
||||||
20
app/schema_validation/definitions.py
Normal file
20
app/schema_validation/definitions.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
"""
|
||||||
|
Definitions are intended for schema definitions that are not likely to change from version to version.
|
||||||
|
If the definition is specific to a version put it in a definition file in the version package
|
||||||
|
"""
|
||||||
|
|
||||||
|
uuid = {
|
||||||
|
"type": "string",
|
||||||
|
"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$",
|
||||||
|
"validationMessage": "not a valid UUID",
|
||||||
|
"code": "1001", # yet to be implemented
|
||||||
|
"link": "link to our error documentation not yet implemented"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
personalisation = {
|
||||||
|
"type": "object",
|
||||||
|
"validationMessage": "should contain key value pairs",
|
||||||
|
"code": "1001", # yet to be implemented
|
||||||
|
"link": "link to our error documentation not yet implemented"
|
||||||
|
}
|
||||||
56
app/v2/notifications/notification_schemas.py
Normal file
56
app/v2/notifications/notification_schemas.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
from app.schema_validation.definitions import (uuid, personalisation)
|
||||||
|
|
||||||
|
post_sms_request = {
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"description": "POST sms notification schema",
|
||||||
|
"type": "object",
|
||||||
|
"title": "POST v2/notifications/sms",
|
||||||
|
"properties": {
|
||||||
|
"reference": {"type": "string"},
|
||||||
|
"phone_number": {"type": "string", "format": "sms"},
|
||||||
|
"template_id": uuid,
|
||||||
|
"personalisation": personalisation
|
||||||
|
},
|
||||||
|
"required": ["phone_number", "template_id"]
|
||||||
|
}
|
||||||
|
|
||||||
|
content = {
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"description": "POST sms notification response schema",
|
||||||
|
"type": "object",
|
||||||
|
"title": "notification content",
|
||||||
|
"properties": {
|
||||||
|
"body": {"type": "string"},
|
||||||
|
"from_number": {"type": "string"}
|
||||||
|
},
|
||||||
|
"required": ["body"]
|
||||||
|
}
|
||||||
|
|
||||||
|
# this may belong in a templates module
|
||||||
|
template = {
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"description": "POST sms notification response schema",
|
||||||
|
"type": "object",
|
||||||
|
"title": "notification content",
|
||||||
|
"properties": {
|
||||||
|
"id": uuid,
|
||||||
|
"version": {"type": "integer"},
|
||||||
|
"uri": {"type": "string"}
|
||||||
|
},
|
||||||
|
"required": ["id", "version", "uri"]
|
||||||
|
}
|
||||||
|
|
||||||
|
post_sms_response = {
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"description": "POST sms notification response schema",
|
||||||
|
"type": "object",
|
||||||
|
"title": "response v2/notifications/sms",
|
||||||
|
"properties": {
|
||||||
|
"id": uuid,
|
||||||
|
"reference": {"type": "string"},
|
||||||
|
"content": content,
|
||||||
|
"uri": {"type": "string"},
|
||||||
|
"template": template
|
||||||
|
},
|
||||||
|
"required": ["id", "content", "uri", "template"]
|
||||||
|
}
|
||||||
0
tests/app/v2/__init__.py
Normal file
0
tests/app/v2/__init__.py
Normal file
0
tests/app/v2/notifications/__init__.py
Normal file
0
tests/app/v2/notifications/__init__.py
Normal file
91
tests/app/v2/notifications/test_notification_schemas.py
Normal file
91
tests/app/v2/notifications/test_notification_schemas.py
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
import uuid
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from flask import json
|
||||||
|
from jsonschema import ValidationError
|
||||||
|
|
||||||
|
from app.v2.notifications.notification_schemas import post_sms_request, post_sms_response
|
||||||
|
from app.schema_validation import validate
|
||||||
|
|
||||||
|
valid_json = {"phone_number": "07515111111",
|
||||||
|
"template_id": str(uuid.uuid4())
|
||||||
|
}
|
||||||
|
valid_json_with_optionals = {
|
||||||
|
"phone_number": "07515111111",
|
||||||
|
"template_id": str(uuid.uuid4()),
|
||||||
|
"reference": "reference from caller",
|
||||||
|
"personalisation": {"key": "value"}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("input", [valid_json, valid_json_with_optionals])
|
||||||
|
def test_post_sms_schema_is_valid(input):
|
||||||
|
validate(input, post_sms_request)
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_sms_json_schema_bad_uuid_and_missing_phone_number():
|
||||||
|
j = {"template_id": "notUUID"}
|
||||||
|
try:
|
||||||
|
validate(j, post_sms_request)
|
||||||
|
except ValidationError as e:
|
||||||
|
error = json.loads(e.message)
|
||||||
|
assert "POST v2/notifications/sms" in error['message']
|
||||||
|
assert len(error.get('fields')) == 2
|
||||||
|
assert "phone_number" in e.message
|
||||||
|
assert "template_id" in e.message
|
||||||
|
assert error.get('code') == '1001'
|
||||||
|
assert error.get('link', None) is not None
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_sms_schema_with_personalisation_that_is_not_a_dict():
|
||||||
|
j = {
|
||||||
|
"phone_number": "07515111111",
|
||||||
|
"template_id": str(uuid.uuid4()),
|
||||||
|
"reference": "reference from caller",
|
||||||
|
"personalisation": "not_a_dict"
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
validate(j, post_sms_request)
|
||||||
|
except ValidationError as e:
|
||||||
|
error = json.loads(e.message)
|
||||||
|
assert "POST v2/notifications/sms" in error['message']
|
||||||
|
assert len(error.get('fields')) == 1
|
||||||
|
assert "personalisation" in e.message
|
||||||
|
assert error.get('code') == '1001'
|
||||||
|
assert error.get('link', None) is not None
|
||||||
|
|
||||||
|
|
||||||
|
valid_response = {
|
||||||
|
"id": str(uuid.uuid4()),
|
||||||
|
"content": {"body": "contents of message",
|
||||||
|
"from_number": "46045"},
|
||||||
|
"uri": "/v2/notifications/id",
|
||||||
|
"template": {"id": str(uuid.uuid4()),
|
||||||
|
"version": 1,
|
||||||
|
"uri": "/v2/template/id"}
|
||||||
|
}
|
||||||
|
|
||||||
|
valid_response_with_optionals = {
|
||||||
|
"id": str(uuid.uuid4()),
|
||||||
|
"reference": "reference_from_service",
|
||||||
|
"content": {"body": "contents of message",
|
||||||
|
"from_number": "46045"},
|
||||||
|
"uri": "/v2/notifications/id",
|
||||||
|
"template": {"id": str(uuid.uuid4()),
|
||||||
|
"version": 1,
|
||||||
|
"uri": "/v2/template/id"}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('input', [valid_response])
|
||||||
|
def test_post_sms_response_schema_is_valid(input):
|
||||||
|
validate(input, post_sms_response)
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_sms_response_schema_missing_uri():
|
||||||
|
j = valid_response
|
||||||
|
del j["uri"]
|
||||||
|
try:
|
||||||
|
validate(j, post_sms_response)
|
||||||
|
except ValidationError as e:
|
||||||
|
assert 'uri' in e.message
|
||||||
Reference in New Issue
Block a user