mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:15:19 -05:00
Merge pull request #892 from alphagov/v2-preview-subject-personalisation
V2 preview subject personalisation
This commit is contained in:
@@ -1,11 +1,9 @@
|
|||||||
import uuid
|
|
||||||
|
|
||||||
from flask import jsonify, request
|
from flask import jsonify, request
|
||||||
from jsonschema.exceptions import ValidationError
|
from jsonschema.exceptions import ValidationError
|
||||||
from werkzeug.exceptions import abort
|
|
||||||
|
|
||||||
from app import api_user
|
from app import api_user
|
||||||
from app.dao import templates_dao
|
from app.dao import templates_dao
|
||||||
|
from app.models import SMS_TYPE
|
||||||
from app.schema_validation import validate
|
from app.schema_validation import validate
|
||||||
from app.utils import get_template_instance
|
from app.utils import get_template_instance
|
||||||
from app.v2.errors import BadRequestError
|
from app.v2.errors import BadRequestError
|
||||||
@@ -16,6 +14,9 @@ from app.v2.template.template_schemas import post_template_preview_request, crea
|
|||||||
@v2_template_blueprint.route("/<template_id>/preview", methods=['POST'])
|
@v2_template_blueprint.route("/<template_id>/preview", methods=['POST'])
|
||||||
def post_template_preview(template_id):
|
def post_template_preview(template_id):
|
||||||
_data = request.get_json()
|
_data = request.get_json()
|
||||||
|
if _data is None:
|
||||||
|
_data = {}
|
||||||
|
|
||||||
_data['id'] = template_id
|
_data['id'] = template_id
|
||||||
|
|
||||||
data = validate(_data, post_template_preview_request)
|
data = validate(_data, post_template_preview_request)
|
||||||
@@ -29,8 +30,7 @@ def post_template_preview(template_id):
|
|||||||
check_placeholders(template_object)
|
check_placeholders(template_object)
|
||||||
|
|
||||||
resp = create_post_template_preview_response(template=template,
|
resp = create_post_template_preview_response(template=template,
|
||||||
body=str(template_object),
|
template_object=template_object)
|
||||||
url_root=request.url_root)
|
|
||||||
|
|
||||||
return jsonify(resp), 200
|
return jsonify(resp), 200
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from app.models import TEMPLATE_TYPES
|
from app.models import SMS_TYPE, TEMPLATE_TYPES
|
||||||
from app.schema_validation.definitions import uuid, personalisation
|
from app.schema_validation.definitions import uuid, personalisation
|
||||||
|
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ post_template_preview_request = {
|
|||||||
"id": uuid,
|
"id": uuid,
|
||||||
"personalisation": personalisation
|
"personalisation": personalisation
|
||||||
},
|
},
|
||||||
"required": ["id", "personalisation"]
|
"required": ["id"]
|
||||||
}
|
}
|
||||||
|
|
||||||
post_template_preview_response = {
|
post_template_preview_response = {
|
||||||
@@ -68,12 +68,13 @@ post_template_preview_response = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def create_post_template_preview_response(template, body, url_root):
|
def create_post_template_preview_response(template, template_object):
|
||||||
|
subject = template_object.subject if template.template_type != SMS_TYPE else None
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"id": template.id,
|
"id": template.id,
|
||||||
"type": template.template_type,
|
"type": template.template_type,
|
||||||
"version": template.version,
|
"version": template.version,
|
||||||
"body": body,
|
"body": str(template_object),
|
||||||
"subject": template.subject,
|
"subject": subject
|
||||||
"uri": "{}v2/template/{}/preview".format(url_root, template.id)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ def create_service(user=None, service_name="Sample service", service_id=None):
|
|||||||
def create_template(
|
def create_template(
|
||||||
service,
|
service,
|
||||||
template_type=SMS_TYPE,
|
template_type=SMS_TYPE,
|
||||||
|
subject='Template subject',
|
||||||
content='Dear Sir/Madam, Hello. Yours Truly, The Government.',
|
content='Dear Sir/Madam, Hello. Yours Truly, The Government.',
|
||||||
template_id=None
|
template_id=None
|
||||||
):
|
):
|
||||||
@@ -50,7 +51,7 @@ def create_template(
|
|||||||
'created_by': service.created_by,
|
'created_by': service.created_by,
|
||||||
}
|
}
|
||||||
if template_type != SMS_TYPE:
|
if template_type != SMS_TYPE:
|
||||||
data['subject'] = 'Template subject'
|
data['subject'] = subject
|
||||||
template = Template(**data)
|
template = Template(**data)
|
||||||
dao_create_template(template)
|
dao_create_template(template)
|
||||||
return template
|
return template
|
||||||
|
|||||||
@@ -1,29 +1,62 @@
|
|||||||
import pytest
|
import pytest
|
||||||
import uuid
|
|
||||||
|
|
||||||
from flask import json
|
from flask import json
|
||||||
|
|
||||||
from app.models import TEMPLATE_TYPES
|
from app.models import SMS_TYPE, TEMPLATE_TYPES
|
||||||
from tests import create_authorization_header
|
from tests import create_authorization_header
|
||||||
from tests.app.db import create_template
|
from tests.app.db import create_template
|
||||||
|
|
||||||
valid_data = {
|
valid_personalisation = {
|
||||||
'personalisation': {'Name': 'Jo'}
|
'personalisation': {'Name': 'Jo'}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
valid_post = [
|
||||||
|
(
|
||||||
|
"Some subject",
|
||||||
|
"Some content",
|
||||||
|
None,
|
||||||
|
"Some subject",
|
||||||
|
"Some content"
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"Some subject",
|
||||||
|
"Dear ((Name)), Hello. Yours Truly, The Government.",
|
||||||
|
valid_personalisation,
|
||||||
|
"Some subject",
|
||||||
|
"Dear Jo, Hello. Yours Truly, The Government."
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"Message for ((Name))",
|
||||||
|
"Dear ((Name)), Hello. Yours Truly, The Government.",
|
||||||
|
valid_personalisation,
|
||||||
|
"Message for Jo",
|
||||||
|
"Dear Jo, Hello. Yours Truly, The Government."
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"Message for ((Name))",
|
||||||
|
"Some content",
|
||||||
|
valid_personalisation,
|
||||||
|
"Message for Jo",
|
||||||
|
"Some content"
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
|
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
|
||||||
def test_valid_post_template_returns_200(client, sample_service, tmp_type):
|
@pytest.mark.parametrize("subject,content,post_data,expected_subject,expected_content", valid_post)
|
||||||
|
def test_valid_post_template_returns_200(
|
||||||
|
client, sample_service, tmp_type, subject, content, post_data, expected_subject, expected_content):
|
||||||
template = create_template(
|
template = create_template(
|
||||||
sample_service,
|
sample_service,
|
||||||
template_type=tmp_type,
|
template_type=tmp_type,
|
||||||
content='Dear ((Name)), Hello. Yours Truly, The Government.')
|
subject=subject,
|
||||||
|
content=content)
|
||||||
|
|
||||||
auth_header = create_authorization_header(service_id=sample_service.id)
|
auth_header = create_authorization_header(service_id=sample_service.id)
|
||||||
|
|
||||||
response = client.post(
|
response = client.post(
|
||||||
path='/v2/template/{}/preview'.format(template.id),
|
path='/v2/template/{}/preview'.format(template.id),
|
||||||
data=json.dumps(valid_data),
|
data=json.dumps(post_data),
|
||||||
headers=[('Content-Type', 'application/json'), auth_header])
|
headers=[('Content-Type', 'application/json'), auth_header])
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
@@ -31,8 +64,9 @@ def test_valid_post_template_returns_200(client, sample_service, tmp_type):
|
|||||||
resp_json = json.loads(response.get_data(as_text=True))
|
resp_json = json.loads(response.get_data(as_text=True))
|
||||||
|
|
||||||
assert resp_json['id'] == str(template.id)
|
assert resp_json['id'] == str(template.id)
|
||||||
assert 'v2/template/{}/preview'.format(template.id) in resp_json['uri']
|
if tmp_type != SMS_TYPE:
|
||||||
assert 'Dear {}'.format(valid_data['personalisation']['Name']) in resp_json['body']
|
assert expected_subject in resp_json['subject']
|
||||||
|
assert expected_content in resp_json['body']
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
|
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
|
||||||
@@ -46,7 +80,7 @@ def test_invalid_post_template_returns_400(client, sample_service, tmp_type):
|
|||||||
|
|
||||||
response = client.post(
|
response = client.post(
|
||||||
path='/v2/template/{}/preview'.format(template.id),
|
path='/v2/template/{}/preview'.format(template.id),
|
||||||
data=json.dumps(valid_data),
|
data=json.dumps(valid_personalisation),
|
||||||
headers=[('Content-Type', 'application/json'), auth_header])
|
headers=[('Content-Type', 'application/json'), auth_header])
|
||||||
|
|
||||||
assert response.status_code == 400
|
assert response.status_code == 400
|
||||||
@@ -62,7 +96,7 @@ def test_post_template_with_non_existent_template_id_returns_404(client, fake_uu
|
|||||||
|
|
||||||
response = client.post(
|
response = client.post(
|
||||||
path='/v2/template/{}/preview'.format(fake_uuid),
|
path='/v2/template/{}/preview'.format(fake_uuid),
|
||||||
data=json.dumps(valid_data),
|
data=json.dumps(valid_personalisation),
|
||||||
headers=[('Content-Type', 'application/json'), auth_header])
|
headers=[('Content-Type', 'application/json'), auth_header])
|
||||||
|
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
|
|||||||
@@ -54,7 +54,6 @@ invalid_json_post_args = [
|
|||||||
({"id": "invalid_uuid", "personalisation": {"key": "value"}}, ["id is not a valid UUID"]),
|
({"id": "invalid_uuid", "personalisation": {"key": "value"}}, ["id is not a valid UUID"]),
|
||||||
({"id": str(uuid.uuid4()), "personalisation": "invalid_personalisation"},
|
({"id": str(uuid.uuid4()), "personalisation": "invalid_personalisation"},
|
||||||
["personalisation should contain key value pairs"]),
|
["personalisation should contain key value pairs"]),
|
||||||
({"id": str(uuid.uuid4())}, ["personalisation is a required property"]),
|
|
||||||
({"personalisation": "invalid_personalisation"},
|
({"personalisation": "invalid_personalisation"},
|
||||||
["id is a required property",
|
["id is a required property",
|
||||||
"personalisation is a required property",
|
"personalisation is a required property",
|
||||||
|
|||||||
Reference in New Issue
Block a user