Merge pull request #865 from alphagov/add-v2-template-preview

Add v2 template preview endpoint and schema
This commit is contained in:
kentsanggds
2017-03-28 18:40:32 +01:00
committed by GitHub
9 changed files with 258 additions and 39 deletions

View File

@@ -4,15 +4,14 @@ import uuid
from flask import json
from app import DATETIME_FORMAT
from app.models import EMAIL_TYPE, SMS_TYPE, LETTER_TYPE
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
template_types = [EMAIL_TYPE, SMS_TYPE, LETTER_TYPE]
valid_version_params = [None, 1]
@pytest.mark.parametrize("tmp_type", template_types)
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
@pytest.mark.parametrize("version", valid_version_params)
def test_get_email_template_by_id_returns_200(client, sample_service, tmp_type, version):
template = create_template(sample_service, template_type=tmp_type)
@@ -42,12 +41,10 @@ def test_get_email_template_by_id_returns_200(client, sample_service, tmp_type,
assert json_response == expected_response
def test_get_template_with_non_existent_template_id_returns_404(client, sample_service):
def test_get_template_with_non_existent_template_id_returns_404(client, fake_uuid, sample_service):
auth_header = create_authorization_header(service_id=sample_service.id)
random_template_id = str(uuid.uuid4())
response = client.get(path='/v2/template/{}'.format(random_template_id),
response = client.get(path='/v2/template/{}'.format(fake_uuid),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 404
@@ -66,7 +63,7 @@ def test_get_template_with_non_existent_template_id_returns_404(client, sample_s
}
@pytest.mark.parametrize("tmp_type", template_types)
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
def test_get_template_with_non_existent_version_returns_404(client, sample_service, tmp_type):
template = create_template(sample_service, template_type=tmp_type)

View File

@@ -0,0 +1,81 @@
import pytest
import uuid
from flask import json
from app.models import TEMPLATE_TYPES
from tests import create_authorization_header
from tests.app.db import create_template
valid_data = {
'personalisation': {'Name': 'Jo'}
}
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
def test_valid_post_template_returns_200(client, sample_service, tmp_type):
template = create_template(
sample_service,
template_type=tmp_type,
content='Dear ((Name)), Hello. Yours Truly, The Government.')
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.post(
path='/v2/template/{}/preview'.format(template.id),
data=json.dumps(valid_data),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['id'] == str(template.id)
assert 'v2/template/{}/preview'.format(template.id) in resp_json['uri']
assert 'Dear {}'.format(valid_data['personalisation']['Name']) in resp_json['body']
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
def test_invalid_post_template_returns_400(client, sample_service, tmp_type):
template = create_template(
sample_service,
template_type=tmp_type,
content='Dear ((Name)), Hello ((Missing)). Yours Truly, The Government.')
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.post(
path='/v2/template/{}/preview'.format(template.id),
data=json.dumps(valid_data),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 400
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['errors'][0]['error'] == 'BadRequestError'
assert 'Missing personalisation: Missing' in resp_json['errors'][0]['message']
def test_post_template_with_non_existent_template_id_returns_404(client, fake_uuid, sample_service):
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.post(
path='/v2/template/{}/preview'.format(fake_uuid),
data=json.dumps(valid_data),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 404
assert response.headers['Content-type'] == 'application/json'
json_response = json.loads(response.get_data(as_text=True))
assert json_response == {
"errors": [
{
"error": "NoResultFound",
"message": "No result found"
}
],
"status_code": 404
}

View File

@@ -3,37 +3,39 @@ import uuid
import pytest
from flask import json
from app.models import EMAIL_TYPE, SMS_TYPE, LETTER_TYPE, TEMPLATE_TYPES
from app.v2.template.template_schemas import (
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 jsonschema.exceptions import ValidationError
valid_json = {
valid_json_get_response = {
'id': str(uuid.uuid4()),
'type': 'email',
'type': SMS_TYPE,
'created_at': '2017-01-10T18:25:43.511Z',
'updated_at': None,
'version': 1,
'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()),
'type': 'email',
'type': EMAIL_TYPE,
'created_at': '2017-01-10T18:25:43.511Z',
'updated_at': None,
'version': 1,
'created_by': 'someone',
'body': "some body",
'body': 'some body',
'subject': "some subject"
}
valid_request_args = [
{"id": str(uuid.uuid4()), "version": 1}, {"id": str(uuid.uuid4())}]
valid_request_args = [{"id": str(uuid.uuid4()), "version": 1}, {"id": str(uuid.uuid4())}]
invalid_request_args = [
({"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"])
]
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)
def test_get_template_request_schema_against_valid_args_is_valid(args):
@@ -61,10 +96,37 @@ def test_get_template_request_schema_against_invalid_args_is_invalid(args, error
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("response", [valid_json_get_response, valid_json_get_response_with_optionals])
@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(response, template_type, updated_datetime):
if updated_datetime:
response['updated_at'] = updated_datetime
response['type'] = template_type
assert validate(response, get_template_by_id_response) == 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)
@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
assert validate(response, post_template_preview_response) == response