mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Add schemas, endpoints and supporting tests
This commit is contained in:
0
tests/app/v2/template/__init__.py
Normal file
0
tests/app/v2/template/__init__.py
Normal file
93
tests/app/v2/template/test_get_template.py
Normal file
93
tests/app/v2/template/test_get_template.py
Normal file
@@ -0,0 +1,93 @@
|
||||
import pytest
|
||||
|
||||
from flask import json
|
||||
|
||||
from app import DATETIME_FORMAT
|
||||
from tests import create_authorization_header
|
||||
from tests.app.conftest import sample_template as create_sample_template
|
||||
|
||||
EMAIL_TYPE = 'email'
|
||||
SMS_TYPE = 'sms'
|
||||
LETTER_TYPE = 'letter'
|
||||
|
||||
template_types = [EMAIL_TYPE, SMS_TYPE, LETTER_TYPE]
|
||||
valid_version_params = [None, 1]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("tmp_type", template_types)
|
||||
@pytest.mark.parametrize("version", valid_version_params)
|
||||
def test_get_email_template_by_id_returns_200(client, notify_db, notify_db_session, sample_service, tmp_type, version):
|
||||
template = create_sample_template(notify_db, notify_db_session, template_type=tmp_type)
|
||||
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
|
||||
|
||||
|
||||
def test_get_template_with_invalid_template_id_returns_404(client, sample_service):
|
||||
auth_header = create_authorization_header(service_id=sample_service.id)
|
||||
|
||||
invalid_template_id = 'some_other_id'
|
||||
|
||||
response = client.get(path='/v2/template/{}'.format(invalid_template_id),
|
||||
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 == {
|
||||
"message": "The requested URL was not found on the server. "
|
||||
"If you entered the URL manually please check your spelling and try again.",
|
||||
"result": "error"
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("tmp_type", template_types)
|
||||
def test_get_template_with_invalid_version_returns_404(client, notify_db, notify_db_session, sample_service, tmp_type):
|
||||
template = create_sample_template(
|
||||
notify_db, notify_db_session, template_type=tmp_type)
|
||||
|
||||
auth_header = create_authorization_header(service_id=sample_service.id)
|
||||
|
||||
# test with version number beyond latest version
|
||||
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
|
||||
}
|
||||
67
tests/app/v2/template/test_template_schemas.py
Normal file
67
tests/app/v2/template/test_template_schemas.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
from flask import json
|
||||
|
||||
from app.v2.template.template_schemas import (
|
||||
get_template_by_id_response,
|
||||
get_template_by_id_request
|
||||
)
|
||||
from app.schema_validation import validate
|
||||
from jsonschema.exceptions import ValidationError
|
||||
|
||||
|
||||
valid_json = {
|
||||
'id': str(uuid.uuid4()),
|
||||
'type': 'email',
|
||||
'created_at': '2017-01-10T18:25:43.511Z',
|
||||
'updated_at': '2017-04-23T18:25:43.511Z',
|
||||
'version': 1,
|
||||
'created_by': 'someone@test.com',
|
||||
'body': "some body"
|
||||
}
|
||||
|
||||
valid_json_with_optionals = {
|
||||
'id': str(uuid.uuid4()),
|
||||
'type': 'email',
|
||||
'created_at': '2017-01-10T18:25:43.511Z',
|
||||
'updated_at': '2017-04-23T18:25:43.511Z',
|
||||
'version': 1,
|
||||
'created_by': 'someone',
|
||||
'body': "some body",
|
||||
'subject': "some subject"
|
||||
}
|
||||
|
||||
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"]),
|
||||
({"id": str(uuid.uuid4()), "version": 0}, ["version 0 is less than the minimum of 1"]),
|
||||
({"version": 1}, ["id is a required property"]),
|
||||
({"id": "invalid_uuid"}, ["id is not a valid UUID"]),
|
||||
({"id": "invalid_uuid", "version": 0}, ["version 0 is less than the minimum of 1",
|
||||
"id is not a valid UUID"])
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("args", valid_request_args)
|
||||
def test_get_template_request_schema__against_valid_args_is_valid(args):
|
||||
assert validate(args, get_template_by_id_request) == args
|
||||
|
||||
|
||||
@pytest.mark.parametrize("args,error_message", invalid_request_args)
|
||||
def test_get_template_request_schema_against_invalid_args_is_invalid(args, error_message):
|
||||
with pytest.raises(ValidationError) as e:
|
||||
validate(args, get_template_by_id_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("response", [valid_json, valid_json_with_optionals])
|
||||
def test_get_template_response_schema_is_valid(response):
|
||||
assert validate(response, get_template_by_id_response) == response
|
||||
Reference in New Issue
Block a user