2017-03-14 15:25:36 +00:00
|
|
|
import pytest
|
2017-03-14 17:51:30 +00:00
|
|
|
import uuid
|
2017-03-14 15:25:36 +00:00
|
|
|
|
|
|
|
|
from flask import json
|
|
|
|
|
|
|
|
|
|
from app import DATETIME_FORMAT
|
2017-03-17 13:32:06 +00:00
|
|
|
from app.models import EMAIL_TYPE, SMS_TYPE, LETTER_TYPE, TEMPLATE_TYPES
|
2017-03-14 15:25:36 +00:00
|
|
|
from tests import create_authorization_header
|
2017-03-14 17:51:30 +00:00
|
|
|
from tests.app.db import create_template
|
2017-03-14 15:25:36 +00:00
|
|
|
|
|
|
|
|
valid_version_params = [None, 1]
|
|
|
|
|
|
|
|
|
|
|
2017-03-17 13:32:06 +00:00
|
|
|
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
|
2017-03-14 15:25:36 +00:00
|
|
|
@pytest.mark.parametrize("version", valid_version_params)
|
2017-03-14 17:51:30 +00:00
|
|
|
def test_get_email_template_by_id_returns_200(client, sample_service, tmp_type, version):
|
|
|
|
|
template = create_template(sample_service, template_type=tmp_type)
|
2017-03-14 15:25:36 +00:00
|
|
|
auth_header = create_authorization_header(service_id=sample_service.id)
|
|
|
|
|
|
|
|
|
|
version_path = '/version/{}'.format(version) if version else ''
|
|
|
|
|
|
|
|
|
|
response = client.get(path='/v2/template/{}{}'.format(template.id, version_path),
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
|
|
expected_response = {
|
|
|
|
|
'id': '{}'.format(template.id),
|
|
|
|
|
'type': '{}'.format(template.template_type),
|
|
|
|
|
'created_at': template.created_at.strftime(DATETIME_FORMAT),
|
|
|
|
|
'updated_at': None,
|
|
|
|
|
'version': template.version,
|
|
|
|
|
'created_by': template.created_by.email_address,
|
|
|
|
|
'body': template.content,
|
|
|
|
|
"subject": template.subject if tmp_type == EMAIL_TYPE else None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert json_response == expected_response
|
|
|
|
|
|
|
|
|
|
|
2017-03-22 14:31:16 +00:00
|
|
|
def test_get_template_with_non_existent_template_id_returns_404(client, fake_uuid, sample_service):
|
2017-03-14 15:25:36 +00:00
|
|
|
auth_header = create_authorization_header(service_id=sample_service.id)
|
|
|
|
|
|
2017-03-22 14:31:16 +00:00
|
|
|
response = client.get(path='/v2/template/{}'.format(fake_uuid),
|
2017-03-14 15:25:36 +00:00
|
|
|
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 == {
|
2017-03-14 17:51:30 +00:00
|
|
|
"errors": [
|
|
|
|
|
{
|
|
|
|
|
"error": "NoResultFound",
|
|
|
|
|
"message": "No result found"
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"status_code": 404
|
2017-03-14 15:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-03-17 13:32:06 +00:00
|
|
|
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
|
2017-03-14 17:51:30 +00:00
|
|
|
def test_get_template_with_non_existent_version_returns_404(client, sample_service, tmp_type):
|
|
|
|
|
template = create_template(sample_service, template_type=tmp_type)
|
2017-03-14 15:25:36 +00:00
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=sample_service.id)
|
|
|
|
|
|
|
|
|
|
invalid_version = template.version + 1
|
|
|
|
|
|
|
|
|
|
response = client.get(path='/v2/template/{}/version/{}'.format(template.id, invalid_version),
|
|
|
|
|
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
|
|
|
|
|
}
|