2017-03-14 15:25:36 +00:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from flask import json
|
|
|
|
|
|
2017-03-24 10:23:47 +00:00
|
|
|
from app.models import EMAIL_TYPE, SMS_TYPE, TEMPLATE_TYPES
|
2017-03-14 15:25:36 +00:00
|
|
|
from app.v2.template.template_schemas import (
|
|
|
|
|
get_template_by_id_response,
|
2017-03-17 13:30:06 +00:00
|
|
|
get_template_by_id_request,
|
|
|
|
|
post_template_preview_request,
|
2017-03-28 10:41:25 +01:00
|
|
|
post_template_preview_response
|
2017-03-14 15:25:36 +00:00
|
|
|
)
|
|
|
|
|
from app.schema_validation import validate
|
|
|
|
|
from jsonschema.exceptions import ValidationError
|
|
|
|
|
|
|
|
|
|
|
2017-03-17 13:30:06 +00:00
|
|
|
valid_json_get_response = {
|
2017-03-14 15:25:36 +00:00
|
|
|
'id': str(uuid.uuid4()),
|
2017-03-17 13:30:06 +00:00
|
|
|
'type': SMS_TYPE,
|
2017-03-14 15:25:36 +00:00
|
|
|
'created_at': '2017-01-10T18:25:43.511Z',
|
2017-03-15 15:48:28 +00:00
|
|
|
'updated_at': None,
|
2017-03-14 15:25:36 +00:00
|
|
|
'version': 1,
|
|
|
|
|
'created_by': 'someone@test.com',
|
2017-03-17 13:30:06 +00:00
|
|
|
'body': 'some body'
|
2017-03-14 15:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
2017-03-17 13:30:06 +00:00
|
|
|
valid_json_get_response_with_optionals = {
|
2017-03-14 15:25:36 +00:00
|
|
|
'id': str(uuid.uuid4()),
|
2017-03-17 13:30:06 +00:00
|
|
|
'type': EMAIL_TYPE,
|
2017-03-14 15:25:36 +00:00
|
|
|
'created_at': '2017-01-10T18:25:43.511Z',
|
2017-03-15 15:48:28 +00:00
|
|
|
'updated_at': None,
|
2017-03-14 15:25:36 +00:00
|
|
|
'version': 1,
|
|
|
|
|
'created_by': 'someone',
|
2017-03-17 13:30:06 +00:00
|
|
|
'body': 'some body',
|
2017-03-14 15:25:36 +00:00
|
|
|
'subject': "some subject"
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-17 13:30:06 +00:00
|
|
|
valid_request_args = [{"id": str(uuid.uuid4()), "version": 1}, {"id": str(uuid.uuid4())}]
|
2017-03-14 15:25:36 +00:00
|
|
|
|
|
|
|
|
invalid_request_args = [
|
|
|
|
|
({"id": str(uuid.uuid4()), "version": "test"}, ["version test is not of type integer, null"]),
|
|
|
|
|
({"id": str(uuid.uuid4()), "version": 0}, ["version 0 is less than the minimum of 1"]),
|
|
|
|
|
({"version": 1}, ["id is a required property"]),
|
|
|
|
|
({"id": "invalid_uuid"}, ["id is not a valid UUID"]),
|
2017-03-14 17:53:49 +00:00
|
|
|
({"id": "invalid_uuid", "version": 0}, ["version 0 is less than the minimum of 1", "id is not a valid UUID"])
|
2017-03-14 15:25:36 +00:00
|
|
|
]
|
|
|
|
|
|
2017-03-17 13:30:06 +00:00
|
|
|
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"]),
|
|
|
|
|
({"personalisation": "invalid_personalisation"},
|
2017-03-24 10:23:47 +00:00
|
|
|
["id is a required property", "personalisation should contain key value pairs"])
|
2017-03-17 13:30:06 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
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'
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-14 15:25:36 +00:00
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("args", valid_request_args)
|
2017-03-14 17:53:49 +00:00
|
|
|
def test_get_template_request_schema_against_valid_args_is_valid(args):
|
2017-03-14 15:25:36 +00:00
|
|
|
assert validate(args, get_template_by_id_request) == args
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("args,error_message", invalid_request_args)
|
|
|
|
|
def test_get_template_request_schema_against_invalid_args_is_invalid(args, error_message):
|
|
|
|
|
with pytest.raises(ValidationError) as e:
|
|
|
|
|
validate(args, get_template_by_id_request)
|
|
|
|
|
errors = json.loads(str(e.value))
|
|
|
|
|
|
|
|
|
|
assert errors['status_code'] == 400
|
|
|
|
|
|
|
|
|
|
for error in errors['errors']:
|
|
|
|
|
assert error['message'] in error_message
|
|
|
|
|
|
|
|
|
|
|
2017-03-17 13:30:06 +00:00
|
|
|
@pytest.mark.parametrize("template_type", TEMPLATE_TYPES)
|
2017-03-20 13:45:29 +00:00
|
|
|
@pytest.mark.parametrize("response", [valid_json_get_response, valid_json_get_response_with_optionals])
|
2017-03-15 15:48:28 +00:00
|
|
|
@pytest.mark.parametrize("updated_datetime", [None, '2017-01-11T18:25:43.511Z'])
|
2017-03-20 13:45:29 +00:00
|
|
|
def test_get_template_response_schema_is_valid(response, template_type, updated_datetime):
|
2017-03-15 15:48:28 +00:00
|
|
|
if updated_datetime:
|
2017-03-20 13:45:29 +00:00
|
|
|
response['updated_at'] = updated_datetime
|
2017-03-17 13:30:06 +00:00
|
|
|
|
2017-03-20 13:45:29 +00:00
|
|
|
response['type'] = template_type
|
2017-03-17 13:30:06 +00:00
|
|
|
|
2017-03-20 13:45:29 +00:00
|
|
|
assert validate(response, get_template_by_id_response) == response
|
2017-03-17 13:30:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_post_template_preview_against_valid_args_is_valid():
|
|
|
|
|
assert validate(valid_json_post_args, post_template_preview_request) == valid_json_post_args
|
|
|
|
|
|
|
|
|
|
|
2017-03-24 10:23:47 +00:00
|
|
|
@pytest.mark.parametrize("args,error_messages", invalid_json_post_args)
|
|
|
|
|
def test_post_template_preview_against_invalid_args_is_invalid(args, error_messages):
|
2017-03-17 13:30:06 +00:00
|
|
|
with pytest.raises(ValidationError) as e:
|
|
|
|
|
validate(args, post_template_preview_request)
|
|
|
|
|
errors = json.loads(str(e.value))
|
|
|
|
|
|
|
|
|
|
assert errors['status_code'] == 400
|
2017-03-24 10:23:47 +00:00
|
|
|
assert len(errors['errors']) == len(error_messages)
|
2017-03-17 13:30:06 +00:00
|
|
|
for error in errors['errors']:
|
2017-03-24 10:23:47 +00:00
|
|
|
assert error['message'] in error_messages
|
2017-03-17 13:30:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("template_type", TEMPLATE_TYPES)
|
2017-03-20 13:45:29 +00:00
|
|
|
@pytest.mark.parametrize("response", [valid_json_post_response, valid_json_post_response_with_optionals])
|
|
|
|
|
def test_post_template_preview_response_schema_is_valid(response, template_type):
|
|
|
|
|
response['type'] = template_type
|
2017-03-15 15:48:28 +00:00
|
|
|
|
2017-03-20 13:45:29 +00:00
|
|
|
assert validate(response, post_template_preview_response) == response
|