Skip to content

Commit

Permalink
Refactored tests and get template endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kentsanggds committed Mar 14, 2017
1 parent 9e4b1b2 commit a596a1b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
9 changes: 4 additions & 5 deletions app/v2/template/get_template.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,21 +12,19 @@


@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
34 changes: 17 additions & 17 deletions tests/app/v2/template/test_get_template.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
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]


@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 ''
Expand All @@ -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
Expand All @@ -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),
Expand Down

0 comments on commit a596a1b

Please sign in to comment.