mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 18:01:08 -05:00
Add get all templates schema
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
from app.models import SMS_TYPE, 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
|
||||||
|
from app.v2.template_schema import template
|
||||||
|
|
||||||
|
|
||||||
get_template_by_id_request = {
|
get_template_by_id_request = {
|
||||||
@@ -67,6 +68,51 @@ post_template_preview_response = {
|
|||||||
"required": ["id", "type", "version", "body"]
|
"required": ["id", "type", "version", "body"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get_all_template_request = {
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"description": "request schema for parameters allowed when getting all templates",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"type": {"enum": TEMPLATE_TYPES},
|
||||||
|
},
|
||||||
|
"required": ["type"],
|
||||||
|
"additionalProperties": False,
|
||||||
|
}
|
||||||
|
|
||||||
|
get_all_template_response = {
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"description": "GET response schema when getting all templates",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"links": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"self": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "uri"
|
||||||
|
},
|
||||||
|
"next": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "uri"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additionalProperties": False,
|
||||||
|
"required": ["self"],
|
||||||
|
},
|
||||||
|
"templates": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "object",
|
||||||
|
"$ref": "#/definitions/template"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["links", "templates"],
|
||||||
|
"definitions": {
|
||||||
|
"template": template
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def create_post_template_preview_response(template, template_object):
|
def create_post_template_preview_response(template, template_object):
|
||||||
subject = template_object.subject if template.template_type != SMS_TYPE else None
|
subject = template_object.subject if template.template_type != SMS_TYPE else None
|
||||||
|
|||||||
15
app/v2/template_schema.py
Normal file
15
app/v2/template_schema.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
from app.schema_validation.definitions import uuid
|
||||||
|
|
||||||
|
# this may belong in a templates module
|
||||||
|
template = {
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"description": "template schema",
|
||||||
|
"type": "object",
|
||||||
|
"title": "notification content",
|
||||||
|
"properties": {
|
||||||
|
"id": uuid,
|
||||||
|
"version": {"type": "integer"},
|
||||||
|
"uri": {"type": "string", "format": "uri"}
|
||||||
|
},
|
||||||
|
"required": ["id", "version", "uri"]
|
||||||
|
}
|
||||||
@@ -3,12 +3,14 @@ import uuid
|
|||||||
import pytest
|
import pytest
|
||||||
from flask import json
|
from flask import json
|
||||||
|
|
||||||
from app.models import EMAIL_TYPE, SMS_TYPE, LETTER_TYPE, TEMPLATE_TYPES
|
from app.models import EMAIL_TYPE, SMS_TYPE, TEMPLATE_TYPES
|
||||||
from app.v2.template.template_schemas import (
|
from app.v2.template.template_schemas import (
|
||||||
get_template_by_id_response,
|
get_template_by_id_response,
|
||||||
get_template_by_id_request,
|
get_template_by_id_request,
|
||||||
post_template_preview_request,
|
post_template_preview_request,
|
||||||
post_template_preview_response
|
post_template_preview_response,
|
||||||
|
get_all_template_request,
|
||||||
|
get_all_template_response
|
||||||
)
|
)
|
||||||
from app.schema_validation import validate
|
from app.schema_validation import validate
|
||||||
from jsonschema.exceptions import ValidationError
|
from jsonschema.exceptions import ValidationError
|
||||||
@@ -55,9 +57,7 @@ invalid_json_post_args = [
|
|||||||
({"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"]),
|
||||||
({"personalisation": "invalid_personalisation"},
|
({"personalisation": "invalid_personalisation"},
|
||||||
["id is a required property",
|
["id is a required property", "personalisation should contain key value pairs"])
|
||||||
"personalisation is a required property",
|
|
||||||
"personalisation should contain key value pairs"])
|
|
||||||
]
|
]
|
||||||
|
|
||||||
valid_json_post_response = {
|
valid_json_post_response = {
|
||||||
@@ -77,6 +77,74 @@ valid_json_post_response_with_optionals = {
|
|||||||
'subject': 'some subject'
|
'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)
|
@pytest.mark.parametrize("args", valid_request_args)
|
||||||
def test_get_template_request_schema_against_valid_args_is_valid(args):
|
def test_get_template_request_schema_against_valid_args_is_valid(args):
|
||||||
@@ -111,16 +179,16 @@ def test_post_template_preview_against_valid_args_is_valid():
|
|||||||
assert validate(valid_json_post_args, post_template_preview_request) == valid_json_post_args
|
assert validate(valid_json_post_args, post_template_preview_request) == valid_json_post_args
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("args,error_message", invalid_json_post_args)
|
@pytest.mark.parametrize("args,error_messages", invalid_json_post_args)
|
||||||
def test_post_template_preview_against_invalid_args_is_invalid(args, error_message):
|
def test_post_template_preview_against_invalid_args_is_invalid(args, error_messages):
|
||||||
with pytest.raises(ValidationError) as e:
|
with pytest.raises(ValidationError) as e:
|
||||||
validate(args, post_template_preview_request)
|
validate(args, post_template_preview_request)
|
||||||
errors = json.loads(str(e.value))
|
errors = json.loads(str(e.value))
|
||||||
|
|
||||||
assert errors['status_code'] == 400
|
assert errors['status_code'] == 400
|
||||||
|
assert len(errors['errors']) == len(error_messages)
|
||||||
for error in errors['errors']:
|
for error in errors['errors']:
|
||||||
assert error['message'] in error_message
|
assert error['message'] in error_messages
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("template_type", TEMPLATE_TYPES)
|
@pytest.mark.parametrize("template_type", TEMPLATE_TYPES)
|
||||||
@@ -129,3 +197,26 @@ def test_post_template_preview_response_schema_is_valid(response, template_type)
|
|||||||
response['type'] = template_type
|
response['type'] = template_type
|
||||||
|
|
||||||
assert validate(response, post_template_preview_response) == response
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user