First commit

This commit is contained in:
Ken Tsang
2017-03-28 10:41:25 +01:00
parent f5da3574b5
commit 89e244ccd2
10 changed files with 361 additions and 102 deletions

View File

@@ -1,10 +1,9 @@
import pytest
import uuid
from flask import json
from app import DATETIME_FORMAT
from app.models import EMAIL_TYPE, SMS_TYPE, LETTER_TYPE, TEMPLATE_TYPES
from app.models import EMAIL_TYPE, TEMPLATE_TYPES
from tests import create_authorization_header
from tests.app.db import create_template

View File

@@ -8,9 +8,7 @@ from app.v2.template.template_schemas import (
get_template_by_id_response,
get_template_by_id_request,
post_template_preview_request,
post_template_preview_response,
get_all_template_request,
get_all_template_response
post_template_preview_response
)
from app.schema_validation import validate
from jsonschema.exceptions import ValidationError
@@ -77,74 +75,6 @@ valid_json_post_response_with_optionals = {
'subject': 'some subject'
}
valid_json_get_all_response = [
{
'links': {'self': 'http://some.path', 'next': 'http://some.other.path'},
"templates": [
{"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"},
{"id": str(uuid.uuid4()), "version": 2, "uri": "http://template/id"}
]
},
{
'links': {'self': 'http://some.path'},
"templates": [{"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"}]
},
{
'links': {'self': 'http://some.path'},
"templates": []
}
]
invalid_json_get_all_response = [
({
'links': {'self': 'invalid_uri'},
"templates": [
{"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"}
]
}, ['links invalid_uri is not a valid URI.']),
({
'links': {'self': 'http://some.path'},
"templates": [
{"id": 'invalid_id', "version": 1, "uri": "http://template/id"}
]
}, ['templates is not a valid UUID']),
({
'links': {'self': 'http://some.path'},
"templates": [
{"id": str(uuid.uuid4()), "version": 'invalid_version', "uri": "http://template/id"}
]
}, ['templates invalid_version is not of type integer']),
({
'links': {'self': 'http://some.path'},
"templates": [
{"id": str(uuid.uuid4()), "version": 1, "uri": "invalid_uri"}
]
}, ['templates invalid_uri is not a valid URI.']),
({
'links': {'self': 'http://some.path'}
}, ['templates is a required property']),
({
'links': {'next': 'http://some.other.path'},
"templates": [{"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"}]
}, ['links self is a required property']),
({
'links': {'self': 'http://some.path', 'next': 'http://some.other.path'},
"templates": [{"version": 1, "uri": "http://template/id"}]
}, ['templates id is a required property']),
({
'links': {'self': 'http://some.path', 'next': 'http://some.other.path'},
"templates": [{"id": str(uuid.uuid4()), "uri": "http://template/id"}]
}, ['templates version is a required property']),
({
'links': {'self': 'http://some.path', 'next': 'http://some.other.path'},
"templates": [{"id": str(uuid.uuid4()), "version": 1}]
}, ['templates uri is a required property']),
({
'links': {'self': 'http://some.path', 'next': 'http://some.other.path'},
"templates": [{"version": 1}]
}, ['templates id is a required property', 'templates uri is a required property']),
]
@pytest.mark.parametrize("args", valid_request_args)
def test_get_template_request_schema_against_valid_args_is_valid(args):
@@ -197,26 +127,3 @@ def test_post_template_preview_response_schema_is_valid(response, template_type)
response['type'] = template_type
assert validate(response, post_template_preview_response) == response
@pytest.mark.parametrize("template_type", TEMPLATE_TYPES)
def test_get_all_template_request_schema_against_valid_args_is_valid(template_type):
data = {'type': template_type}
assert validate(data, get_all_template_request) == data
@pytest.mark.parametrize("response", valid_json_get_all_response)
def test_valid_get_all_templates_response_schema_is_valid(response):
assert validate(response, get_all_template_response) == response
@pytest.mark.parametrize("response,error_messages", invalid_json_get_all_response)
def test_invalid_get_all_templates_response_schema_is_invalid(response, error_messages):
with pytest.raises(ValidationError) as e:
validate(response, get_all_template_response)
errors = json.loads(str(e.value))
assert errors['status_code'] == 400
assert len(errors['errors']) == len(error_messages)
for error in errors['errors']:
assert error['message'] in error_messages

View File

@@ -0,0 +1,117 @@
import pytest
from flask import json
from app import DATETIME_FORMAT
from app.models import EMAIL_TYPE, SMS_TYPE, LETTER_TYPE, TEMPLATE_TYPES
from tests import create_authorization_header
from tests.app.db import create_template
def test_get_all_templates(client, sample_service):
num_templates = 3
templates = []
for i in range(num_templates):
for tmp_type in TEMPLATE_TYPES:
templates.append(create_template(sample_service, template_type=tmp_type))
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.get(path='/v2/templates/?',
headers=[('Content-Type', 'application/json'), auth_header])
assert response.headers['Content-type'] == 'application/json'
json_response = json.loads(response.get_data(as_text=True))
assert len(json_response['templates']) == num_templates * len(TEMPLATE_TYPES)
# need to reverse index as get all templates returns list sorted by descending date
for i in range(len(json_response['templates'])):
reverse_index = len(json_response['templates']) - 1 - i
assert json_response['templates'][reverse_index]['id'] == str(templates[i].id)
assert json_response['templates'][reverse_index]['body'] == templates[i].content
assert json_response['templates'][reverse_index]['type'] == templates[i].template_type
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
def test_get_all_templates_for_type(client, sample_service, tmp_type):
num_templates = 3
templates = []
for i in range(num_templates):
templates.append(create_template(sample_service, template_type=tmp_type))
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.get(path='/v2/templates/?type={}'.format(tmp_type),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.headers['Content-type'] == 'application/json'
json_response = json.loads(response.get_data(as_text=True))
assert len(json_response['templates']) == num_templates
# need to reverse index as get all templates returns list sorted by descending date
for i in range(len(json_response['templates'])):
reverse_index = len(json_response['templates']) - 1 - i
assert json_response['templates'][reverse_index]['id'] == str(templates[i].id)
assert json_response['templates'][reverse_index]['body'] == templates[i].content
assert json_response['templates'][reverse_index]['type'] == templates[i].template_type
@pytest.mark.parametrize("tmp_type", [EMAIL_TYPE, SMS_TYPE])
def test_get_all_templates_older_than_parameter(client, sample_service, tmp_type):
num_templates = 5
templates = []
for i in range(num_templates):
template = create_template(sample_service, template_type=tmp_type)
templates.append(template)
num_templates_older = 3
# only get the first #num_templates_older templates
older_than_id = templates[num_templates_older].id
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.get(path='/v2/templates/?type={}&older_than={}'.format(tmp_type, older_than_id),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 200
assert response.headers['Content-type'] == 'application/json'
json_response = json.loads(response.get_data(as_text=True))
assert len(json_response['templates']) == num_templates_older
# need to reverse index as get all templates returns list sorted by descending date
for i in range(num_templates_older):
reverse_index = num_templates_older - 1 - i
assert json_response['templates'][reverse_index]['id'] == str(templates[i].id)
assert json_response['templates'][reverse_index]['body'] == templates[i].content
assert json_response['templates'][reverse_index]['type'] == templates[i].template_type
assert str(older_than_id) in json_response['links']['current']
assert str(templates[0].id) in json_response['links']['next']
@pytest.mark.parametrize("tmp_type", [EMAIL_TYPE, SMS_TYPE])
def test_get_all_templates_none_existent_older_than_parameter(client, sample_service, tmp_type, fake_uuid):
num_templates = 2
templates = []
for i in range(num_templates):
template = create_template(sample_service, template_type=tmp_type)
templates.append(template)
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.get(path='/v2/templates/?type={}&older_than={}'.format(tmp_type, fake_uuid),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 200
assert response.headers['Content-type'] == 'application/json'
json_response = json.loads(response.get_data(as_text=True))
assert len(json_response['templates']) == 0

View File

@@ -0,0 +1,142 @@
import uuid
import pytest
from flask import json
from app.models import EMAIL_TYPE, SMS_TYPE, TEMPLATE_TYPES
from app.v2.templates.templates_schemas import (
get_all_template_request,
get_all_template_response
)
from app.schema_validation import validate
from jsonschema.exceptions import ValidationError
valid_json_get_all_response = [
{
'links': {'current': 'http://some.path', 'next': 'http://some.other.path'},
"templates": [
{"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"},
{"id": str(uuid.uuid4()), "version": 2, "uri": "http://template/id"}
]
},
{
'links': {'current': 'http://some.path'},
"templates": [{"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"}]
},
{
'links': {'current': 'http://some.path'},
"templates": []
}
]
invalid_json_get_all_response = [
({
'links': {'current': 'invalid_uri'},
"templates": [
{"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"}
]
}, ['links invalid_uri is not a valid URI.']),
({
'links': {'current': 'http://some.path'},
"templates": [
{"id": 'invalid_id', "version": 1, "uri": "http://template/id"}
]
}, ['templates is not a valid UUID']),
({
'links': {'current': 'http://some.path'},
"templates": [
{"id": str(uuid.uuid4()), "version": 'invalid_version', "uri": "http://template/id"}
]
}, ['templates invalid_version is not of type integer']),
({
'links': {'current': 'http://some.path'},
"templates": [
{"id": str(uuid.uuid4()), "version": 1, "uri": "invalid_uri"}
]
}, ['templates invalid_uri is not a valid URI.']),
({
'links': {'current': 'http://some.path'}
}, ['templates is a required property']),
({
'links': {'next': 'http://some.other.path'},
"templates": [{"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"}]
}, ['links current is a required property']),
({
'links': {'current': 'http://some.path', 'next': 'http://some.other.path'},
"templates": [{"version": 1, "uri": "http://template/id"}]
}, ['templates id is a required property']),
({
'links': {'current': 'http://some.path', 'next': 'http://some.other.path'},
"templates": [{"id": str(uuid.uuid4()), "uri": "http://template/id"}]
}, ['templates version is a required property']),
({
'links': {'current': 'http://some.path', 'next': 'http://some.other.path'},
"templates": [{"id": str(uuid.uuid4()), "version": 1}]
}, ['templates uri is a required property']),
({
'links': {'current': 'http://some.path', 'next': 'http://some.other.path'},
"templates": [{"version": 1}]
}, ['templates id is a required property', 'templates uri is a required property']),
]
@pytest.mark.parametrize("template_type", TEMPLATE_TYPES)
def test_get_all_template_request_schema_against_no_args_is_valid(template_type):
data = {}
assert validate(data, get_all_template_request) == data
@pytest.mark.parametrize("template_type", TEMPLATE_TYPES)
def test_get_all_template_request_schema_against_valid_args_is_valid(template_type):
data = {'type': template_type}
assert validate(data, get_all_template_request) == data
@pytest.mark.parametrize("template_type", TEMPLATE_TYPES)
def test_get_all_template_request_schema_against_valid_args_with_optional_is_valid(template_type, fake_uuid):
data = {'type': template_type, 'older_than': fake_uuid}
assert validate(data, get_all_template_request) == data
@pytest.mark.parametrize("template_type", TEMPLATE_TYPES)
def test_get_all_template_request_schema_against_invalid_args_is_invalid(template_type):
data = {'type': 'unknown'}
with pytest.raises(ValidationError) as e:
validate(data, get_all_template_request)
errors = json.loads(str(e.value))
assert errors['status_code'] == 400
assert len(errors['errors']) == 1
assert errors['errors'][0]['message'] == 'type unknown is not one of [sms, email, letter]'
@pytest.mark.parametrize("template_type", TEMPLATE_TYPES)
def test_get_all_template_request_schema_against_invalid_args_with_optional_is_invalid(template_type):
data = {'type': template_type, 'older_than': 'invalid_uuid'}
with pytest.raises(ValidationError) as e:
validate(data, get_all_template_request)
errors = json.loads(str(e.value))
assert errors['status_code'] == 400
assert len(errors['errors']) == 1
assert errors['errors'][0]['message'] == 'older_than is not a valid UUID'
@pytest.mark.parametrize("response", valid_json_get_all_response)
def test_valid_get_all_templates_response_schema_is_valid(response):
assert validate(response, get_all_template_response) == response
@pytest.mark.parametrize("response,error_messages", invalid_json_get_all_response)
def test_invalid_get_all_templates_response_schema_is_invalid(response, error_messages):
with pytest.raises(ValidationError) as e:
validate(response, get_all_template_response)
errors = json.loads(str(e.value))
assert errors['status_code'] == 400
assert len(errors['errors']) == len(error_messages)
for error in errors['errors']:
assert error['message'] in error_messages