Added schemas + tests & updated get template tests

This commit is contained in:
Ken Tsang
2017-03-17 13:30:06 +00:00
parent 5fcc80c7be
commit fad67e1bdd
2 changed files with 107 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
from app.models import TEMPLATE_TYPES from app.models import TEMPLATE_TYPES
from app.schema_validation.definitions import uuid from app.schema_validation.definitions import uuid, personalisation
get_template_by_id_request = { get_template_by_id_request = {
@@ -39,3 +39,30 @@ get_template_by_id_response = {
}, },
"required": ["id", "type", "created_at", "updated_at", "version", "created_by", "body"] "required": ["id", "type", "created_at", "updated_at", "version", "created_by", "body"]
} }
post_template_preview_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST template schema",
"type": "object",
"title": "POST v2/template/{id}/preview",
"properties": {
"id": uuid,
"personalisation": personalisation
},
"required": ["id", "personalisation"]
}
post_template_preview_response = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST template preview schema response",
"type": "object",
"title": "reponse v2/template/{id}/preview",
"properties": {
"id": uuid,
"type": {"enum": TEMPLATE_TYPES},
"version": {"type": "integer"},
"body": {"type": "string"},
"subject": {"type": ["string", "null"]}
},
"required": ["id", "type", "version", "body"]
}

View File

@@ -3,37 +3,39 @@ import uuid
import pytest import pytest
from flask import json from flask import json
from app.models import EMAIL_TYPE, SMS_TYPE, LETTER_TYPE, TEMPLATE_TYPES
from app.v2.template.template_schemas import ( from app.v2.template.template_schemas import (
get_template_by_id_response, get_template_by_id_response,
get_template_by_id_request get_template_by_id_request,
post_template_preview_request,
post_template_preview_response
) )
from app.schema_validation import validate from app.schema_validation import validate
from jsonschema.exceptions import ValidationError from jsonschema.exceptions import ValidationError
valid_json = { valid_json_get_response = {
'id': str(uuid.uuid4()), 'id': str(uuid.uuid4()),
'type': 'email', 'type': SMS_TYPE,
'created_at': '2017-01-10T18:25:43.511Z', 'created_at': '2017-01-10T18:25:43.511Z',
'updated_at': None, 'updated_at': None,
'version': 1, 'version': 1,
'created_by': 'someone@test.com', 'created_by': 'someone@test.com',
'body': "some body" 'body': 'some body'
} }
valid_json_with_optionals = { valid_json_get_response_with_optionals = {
'id': str(uuid.uuid4()), 'id': str(uuid.uuid4()),
'type': 'email', 'type': EMAIL_TYPE,
'created_at': '2017-01-10T18:25:43.511Z', 'created_at': '2017-01-10T18:25:43.511Z',
'updated_at': None, 'updated_at': None,
'version': 1, 'version': 1,
'created_by': 'someone', 'created_by': 'someone',
'body': "some body", 'body': 'some body',
'subject': "some subject" 'subject': "some subject"
} }
valid_request_args = [ valid_request_args = [{"id": str(uuid.uuid4()), "version": 1}, {"id": str(uuid.uuid4())}]
{"id": str(uuid.uuid4()), "version": 1}, {"id": str(uuid.uuid4())}]
invalid_request_args = [ invalid_request_args = [
({"id": str(uuid.uuid4()), "version": "test"}, ["version test is not of type integer, null"]), ({"id": str(uuid.uuid4()), "version": "test"}, ["version test is not of type integer, null"]),
@@ -43,6 +45,39 @@ invalid_request_args = [
({"id": "invalid_uuid", "version": 0}, ["version 0 is less than the minimum of 1", "id is not a valid UUID"]) ({"id": "invalid_uuid", "version": 0}, ["version 0 is less than the minimum of 1", "id is not a valid UUID"])
] ]
valid_json_post_args = {
"id": str(uuid.uuid4()),
"personalisation": {"key": "value"}
}
invalid_json_post_args = [
({"id": "invalid_uuid", "personalisation": {"key": "value"}}, ["id is not a valid UUID"]),
({"id": str(uuid.uuid4()), "personalisation": "invalid_personalisation"},
["personalisation should contain key value pairs"]),
({"id": str(uuid.uuid4())}, ["personalisation is a required property"]),
({"personalisation": "invalid_personalisation"},
["id is a required property",
"personalisation is a required property",
"personalisation should contain key value pairs"])
]
valid_json_post_response = {
'id': str(uuid.uuid4()),
'type': 'email',
'version': 1,
'created_by': 'someone@test.com',
'body': 'some body'
}
valid_json_post_response_with_optionals = {
'id': str(uuid.uuid4()),
'type': 'email',
'version': 1,
'created_by': 'someone@test.com',
'body': "some body",
'subject': 'some subject'
}
@pytest.mark.parametrize("args", valid_request_args) @pytest.mark.parametrize("args", valid_request_args)
def test_get_template_request_schema_against_valid_args_is_valid(args): def test_get_template_request_schema_against_valid_args_is_valid(args):
@@ -61,10 +96,41 @@ def test_get_template_request_schema_against_invalid_args_is_invalid(args, error
assert error['message'] in error_message assert error['message'] in error_message
@pytest.mark.parametrize("response", [valid_json, valid_json_with_optionals]) @pytest.mark.parametrize("template_type", TEMPLATE_TYPES)
@pytest.mark.parametrize("updated_datetime", [None, '2017-01-11T18:25:43.511Z']) @pytest.mark.parametrize("updated_datetime", [None, '2017-01-11T18:25:43.511Z'])
def test_get_template_response_schema_is_valid(response, updated_datetime): def test_get_template_response_schema_is_valid(template_type, updated_datetime):
if updated_datetime: if updated_datetime:
response['updated_at'] = updated_datetime valid_json_get_response['updated_at'] = updated_datetime
assert validate(response, get_template_by_id_response) == response valid_json_get_response['type'] = template_type
if template_type != EMAIL_TYPE:
valid_json_get_response['subject'] = None
assert validate(valid_json_get_response, get_template_by_id_response) == valid_json_get_response
def test_post_template_preview_against_valid_args_is_valid():
assert validate(valid_json_post_args, post_template_preview_request) == valid_json_post_args
@pytest.mark.parametrize("args,error_message", invalid_json_post_args)
def test_post_template_preview_against_invalid_args_is_invalid(args, error_message):
with pytest.raises(ValidationError) as e:
validate(args, post_template_preview_request)
errors = json.loads(str(e.value))
assert errors['status_code'] == 400
for error in errors['errors']:
assert error['message'] in error_message
@pytest.mark.parametrize("template_type", TEMPLATE_TYPES)
def test_post_template_preview_response_schema_is_valid(template_type):
valid_json_post_response['type'] = template_type
if template_type != EMAIL_TYPE:
valid_json_post_response['subject'] = None
assert validate(valid_json_post_response, post_template_preview_response) == valid_json_post_response