mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 23:41:17 -05:00
Refactored tests and get template endpoint
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import uuid
|
||||
|
||||
from flask import jsonify, request
|
||||
from jsonschema.exceptions import ValidationError
|
||||
from werkzeug.exceptions import abort
|
||||
|
||||
from app import api_user
|
||||
@@ -11,21 +12,19 @@ from app.v2.template.template_schemas import get_template_by_id_request
|
||||
|
||||
|
||||
@template_blueprint.route("/<template_id>", methods=['GET'])
|
||||
@template_blueprint.route("/<template_id>/version/<version>", methods=['GET'])
|
||||
@template_blueprint.route("/<template_id>/version/<int:version>", methods=['GET'])
|
||||
def get_template_by_id(template_id, version=None):
|
||||
try:
|
||||
casted_id = uuid.UUID(template_id)
|
||||
|
||||
_data = {}
|
||||
_data['id'] = template_id
|
||||
if version:
|
||||
_data['version'] = int(version)
|
||||
_data['version'] = version
|
||||
|
||||
data = validate(_data, get_template_by_id_request)
|
||||
except ValueError or AttributeError:
|
||||
abort(404)
|
||||
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(
|
||||
casted_id, api_user.service_id, data.get('version'))
|
||||
template_id, api_user.service_id, data.get('version'))
|
||||
|
||||
return jsonify(template.serialize()), 200
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
import pytest
|
||||
import uuid
|
||||
|
||||
from flask import json
|
||||
|
||||
from app import DATETIME_FORMAT
|
||||
from app.models import EMAIL_TYPE, SMS_TYPE, LETTER_TYPE
|
||||
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'
|
||||
from tests.app.db import create_template
|
||||
|
||||
template_types = [EMAIL_TYPE, SMS_TYPE, LETTER_TYPE]
|
||||
valid_version_params = [None, 1]
|
||||
@@ -16,8 +14,8 @@ 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)
|
||||
def test_get_email_template_by_id_returns_200(client, sample_service, tmp_type, version):
|
||||
template = create_template(sample_service, template_type=tmp_type)
|
||||
auth_header = create_authorization_header(service_id=sample_service.id)
|
||||
|
||||
version_path = '/version/{}'.format(version) if version else ''
|
||||
@@ -44,12 +42,12 @@ def test_get_email_template_by_id_returns_200(client, notify_db, notify_db_sessi
|
||||
assert json_response == expected_response
|
||||
|
||||
|
||||
def test_get_template_with_invalid_template_id_returns_404(client, sample_service):
|
||||
def test_get_template_with_non_existent_template_id_returns_404(client, sample_service):
|
||||
auth_header = create_authorization_header(service_id=sample_service.id)
|
||||
|
||||
invalid_template_id = 'some_other_id'
|
||||
random_template_id = str(uuid.uuid4())
|
||||
|
||||
response = client.get(path='/v2/template/{}'.format(invalid_template_id),
|
||||
response = client.get(path='/v2/template/{}'.format(random_template_id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
assert response.status_code == 404
|
||||
@@ -58,20 +56,22 @@ def test_get_template_with_invalid_template_id_returns_404(client, sample_servic
|
||||
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"
|
||||
"errors": [
|
||||
{
|
||||
"error": "NoResultFound",
|
||||
"message": "No result found"
|
||||
}
|
||||
],
|
||||
"status_code": 404
|
||||
}
|
||||
|
||||
|
||||
@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)
|
||||
def test_get_template_with_non_existent_version_returns_404(client, sample_service, tmp_type):
|
||||
template = create_template(sample_service, 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),
|
||||
|
||||
Reference in New Issue
Block a user