Refactored to use template response as template

This commit is contained in:
Ken Tsang
2017-04-04 14:04:56 +01:00
parent d290a2e0ad
commit a5e514c356
5 changed files with 149 additions and 75 deletions

View File

@@ -1,6 +1,5 @@
from app.models import SMS_TYPE, TEMPLATE_TYPES
from app.schema_validation.definitions import uuid, personalisation
from app.v2.template_schema import template
get_template_by_id_request = {
@@ -68,51 +67,6 @@ post_template_preview_response = {
"required": ["id", "type", "version", "body"]
}
get_all_template_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "request schema for parameters allowed when getting all templates",
"type": "object",
"properties": {
"type": {"enum": TEMPLATE_TYPES},
},
"required": ["type"],
"additionalProperties": False,
}
get_all_template_response = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "GET response schema when getting all templates",
"type": "object",
"properties": {
"links": {
"type": "object",
"properties": {
"self": {
"type": "string",
"format": "uri"
},
"next": {
"type": "string",
"format": "uri"
}
},
"additionalProperties": False,
"required": ["self"],
},
"templates": {
"type": "array",
"items": {
"type": "object",
"$ref": "#/definitions/template"
}
}
},
"required": ["links", "templates"],
"definitions": {
"template": template
}
}
def create_post_template_preview_response(template, template_object):
subject = template_object.subject if template.template_type != SMS_TYPE else None

View File

@@ -1,15 +0,0 @@
from app.schema_validation.definitions import uuid
# this may belong in a templates module
template = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "template schema",
"type": "object",
"title": "notification content",
"properties": {
"id": uuid,
"version": {"type": "integer"},
"uri": {"type": "string", "format": "uri"}
},
"required": ["id", "version", "uri"]
}

View File

@@ -14,6 +14,8 @@ def get_templates():
templates = templates_dao.dao_get_all_templates_for_service(api_user.service_id)
print(templates)
return jsonify(
templates=[template.serialize() for template in templates]
), 200

View File

@@ -1,5 +1,5 @@
from app.models import TEMPLATE_TYPES
from app.v2.template_schema import template
from app.v2.template.template_schemas import get_template_by_id_response as template
get_all_template_request = {

View File

@@ -15,12 +15,38 @@ from jsonschema.exceptions import ValidationError
valid_json_get_all_response = [
{
"templates": [
{"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"},
{"id": str(uuid.uuid4()), "version": 2, "uri": "http://template/id"}
{
'id': str(uuid.uuid4()),
'type': SMS_TYPE,
'created_at': '2017-01-10T18:25:43.511Z',
'updated_at': None,
'version': 1,
'created_by': 'someone@test.com',
'body': 'some body'
},
{
'id': str(uuid.uuid4()),
'type': EMAIL_TYPE,
'created_at': '2017-02-10T18:25:43.511Z',
'updated_at': None,
'version': 2,
'created_by': 'someone@test.com',
'body': 'some body'
}
]
},
{
"templates": [{"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"}]
"templates": [
{
'id': str(uuid.uuid4()),
'type': SMS_TYPE,
'created_at': '2017-02-10T18:25:43.511Z',
'updated_at': None,
'version': 2,
'created_by': 'someone@test.com',
'body': 'some body'
}
]
},
{
"templates": []
@@ -30,32 +56,139 @@ valid_json_get_all_response = [
invalid_json_get_all_response = [
({
"templates": [
{"id": 'invalid_id', "version": 1, "uri": "http://template/id"}
{
'id': 'invalid_id',
'type': SMS_TYPE,
'created_at': '2017-02-10T18:25:43.511Z',
'updated_at': None,
'version': 1,
'created_by': 'someone@test.com',
'body': 'some body'
}
]
}, ['templates is not a valid UUID']),
({
"templates": [
{"id": str(uuid.uuid4()), "version": 'invalid_version', "uri": "http://template/id"}
{
'id': str(uuid.uuid4()),
'type': SMS_TYPE,
'created_at': '2017-02-10T18:25:43.511Z',
'updated_at': None,
'version': 'invalid_version',
'created_by': 'someone@test.com',
'body': 'some body'
}
]
}, ['templates invalid_version is not of type integer']),
({
"templates": [
{"id": str(uuid.uuid4()), "version": 1, "uri": "invalid_uri"}
{
'id': str(uuid.uuid4()),
'type': SMS_TYPE,
'created_at': 'invalid_created_at',
'updated_at': None,
'version': 1,
'created_by': 'someone@test.com',
'body': 'some body'
}
]
}, ['templates invalid_uri is not a valid URI.']),
}, ['templates invalid_created_at is not a date-time']),
({}, ['templates is a required property']),
({
"templates": [{"version": 1, "uri": "http://template/id"}]
"templates": [
{
'type': SMS_TYPE,
'created_at': '2017-02-10T18:25:43.511Z',
'updated_at': None,
'version': 1,
'created_by': 'someone@test.com',
'body': 'some body'
}
]
}, ['templates id is a required property']),
({
"templates": [{"id": str(uuid.uuid4()), "uri": "http://template/id"}]
"templates": [
{
'id': str(uuid.uuid4()),
'created_at': '2017-02-10T18:25:43.511Z',
'updated_at': None,
'version': 1,
'created_by': 'someone@test.com',
'body': 'some body'
}
]
}, ['templates type is a required property']),
({
"templates": [
{
'id': str(uuid.uuid4()),
'type': SMS_TYPE,
'updated_at': None,
'version': 1,
'created_by': 'someone@test.com',
'body': 'some body'
}
]
}, ['templates created_at is a required property']),
({
"templates": [
{
'id': str(uuid.uuid4()),
'type': SMS_TYPE,
'created_at': '2017-02-10T18:25:43.511Z',
'version': 1,
'created_by': 'someone@test.com',
'body': 'some body'
}
]
}, ['templates updated_at is a required property']),
({
"templates": [
{
'id': str(uuid.uuid4()),
'type': SMS_TYPE,
'created_at': '2017-02-10T18:25:43.511Z',
'updated_at': None,
'created_by': 'someone@test.com',
'body': 'some body'
}
]
}, ['templates version is a required property']),
({
"templates": [{"id": str(uuid.uuid4()), "version": 1}]
}, ['templates uri is a required property']),
"templates": [
{
'id': str(uuid.uuid4()),
'type': SMS_TYPE,
'created_at': '2017-02-10T18:25:43.511Z',
'updated_at': None,
'version': 1,
'body': 'some body'
}
]
}, ['templates created_by is a required property']),
({
"templates": [{"version": 1}]
}, ['templates id is a required property', 'templates uri is a required property']),
"templates": [
{
'id': str(uuid.uuid4()),
'type': SMS_TYPE,
'created_at': '2017-02-10T18:25:43.511Z',
'updated_at': None,
'version': 1,
'created_by': 'someone@test.com'
}
]
}, ['templates body is a required property']),
({
"templates": [
{
'type': SMS_TYPE,
'created_at': '2017-02-10T18:25:43.511Z',
'updated_at': None,
'created_by': 'someone@test.com',
'body': 'some body'
}
]
}, ['templates id is a required property', 'templates version is a required property']),
]