diff --git a/app/v2/templates/get_templates.py b/app/v2/templates/get_templates.py index 0fd2170d2..a5f4e6f0c 100644 --- a/app/v2/templates/get_templates.py +++ b/app/v2/templates/get_templates.py @@ -1,6 +1,4 @@ -import json - -from flask import jsonify, request, current_app, url_for +from flask import jsonify, request from jsonschema.exceptions import ValidationError from app import api_user @@ -12,27 +10,10 @@ from app.v2.templates.templates_schemas import get_all_template_request @v2_templates_blueprint.route("/", methods=['GET']) def get_templates(): - _data = request.args.to_dict() + validate(request.args.to_dict(), get_all_template_request) - data = validate(_data, get_all_template_request) - - templates = templates_dao.dao_get_all_templates_for_service( - api_user.service_id, - older_than=data.get('older_than'), - page_size=current_app.config.get('API_PAGE_SIZE')) - - def _build_links(templates): - _links = { - 'current': url_for(".get_templates", _external=True, **data), - } - - if len(templates): - next_query_params = dict(data, older_than=templates[-1].id) - _links['next'] = url_for(".get_templates", _external=True, **next_query_params) - - return _links + templates = templates_dao.dao_get_all_templates_for_service(api_user.service_id) return jsonify( - templates=[template.serialize() for template in templates], - links=_build_links(templates) + templates=[template.serialize() for template in templates] ), 200 diff --git a/tests/app/v2/templates/test_get_templates.py b/tests/app/v2/templates/test_get_templates.py index 7df477153..f7ddd1c8f 100644 --- a/tests/app/v2/templates/test_get_templates.py +++ b/tests/app/v2/templates/test_get_templates.py @@ -2,13 +2,12 @@ import pytest from flask import json -from app import DATETIME_FORMAT -from app.models import EMAIL_TYPE, SMS_TYPE, LETTER_TYPE, TEMPLATE_TYPES +from app.models import TEMPLATE_TYPES from tests import create_authorization_header from tests.app.db import create_template -def test_get_all_templates(client, sample_service): +def test_get_all_templates_returns_200(client, sample_service): num_templates = 3 templates = [] for i in range(num_templates): @@ -17,9 +16,10 @@ def test_get_all_templates(client, sample_service): auth_header = create_authorization_header(service_id=sample_service.id) - response = client.get(path='/v2/templates/?', + response = client.get(path='/v2/templates/', 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)) @@ -35,7 +35,7 @@ def test_get_all_templates(client, sample_service): @pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES) -def test_get_all_templates_for_type(client, sample_service, tmp_type): +def test_get_all_templates_for_valid_type_returns_200(client, sample_service, tmp_type): num_templates = 3 templates = [] for i in range(num_templates): @@ -46,6 +46,7 @@ def test_get_all_templates_for_type(client, sample_service, tmp_type): response = client.get(path='/v2/templates/?type={}'.format(tmp_type), 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)) @@ -60,58 +61,25 @@ def test_get_all_templates_for_type(client, sample_service, tmp_type): assert json_response['templates'][reverse_index]['type'] == templates[i].template_type -@pytest.mark.parametrize("tmp_type", [EMAIL_TYPE, SMS_TYPE]) -def test_get_all_templates_older_than_parameter(client, sample_service, tmp_type): - num_templates = 5 - templates = [] - for i in range(num_templates): - template = create_template(sample_service, template_type=tmp_type) - templates.append(template) - - num_templates_older = 3 - - # only get the first #num_templates_older templates - older_than_id = templates[num_templates_older].id - +def test_get_all_templates_for_invalid_type_returns_400(client, sample_service): auth_header = create_authorization_header(service_id=sample_service.id) - response = client.get(path='/v2/templates/?type={}&older_than={}'.format(tmp_type, older_than_id), + invalid_type = 'coconut' + + response = client.get(path='/v2/templates/?type={}'.format(invalid_type), headers=[('Content-Type', 'application/json'), auth_header]) - assert response.status_code == 200 + assert response.status_code == 400 assert response.headers['Content-type'] == 'application/json' json_response = json.loads(response.get_data(as_text=True)) - assert len(json_response['templates']) == num_templates_older - - # need to reverse index as get all templates returns list sorted by descending date - for i in range(num_templates_older): - reverse_index = num_templates_older - 1 - i - assert json_response['templates'][reverse_index]['id'] == str(templates[i].id) - assert json_response['templates'][reverse_index]['body'] == templates[i].content - assert json_response['templates'][reverse_index]['type'] == templates[i].template_type - - assert str(older_than_id) in json_response['links']['current'] - assert str(templates[0].id) in json_response['links']['next'] - - -@pytest.mark.parametrize("tmp_type", [EMAIL_TYPE, SMS_TYPE]) -def test_get_all_templates_none_existent_older_than_parameter(client, sample_service, tmp_type, fake_uuid): - num_templates = 2 - templates = [] - for i in range(num_templates): - template = create_template(sample_service, template_type=tmp_type) - templates.append(template) - - auth_header = create_authorization_header(service_id=sample_service.id) - - response = client.get(path='/v2/templates/?type={}&older_than={}'.format(tmp_type, fake_uuid), - 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)) - - assert len(json_response['templates']) == 0 + assert json_response == { + 'status_code': 400, + 'errors': [ + { + 'message': 'type coconut is not one of [sms, email, letter]', + 'error': 'ValidationError' + } + ] + }