Refactor tests to use admin_request

This commit is contained in:
Ken Tsang
2017-07-10 13:45:48 +01:00
parent 3b1f229384
commit af9091f207
3 changed files with 104 additions and 52 deletions

View File

@@ -1,11 +1,23 @@
post_organisation_schema = { post_create_organisation_schema = {
"$schema": "http://json-schema.org/draft-04/schema#", "$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST schema for getting organisation", "description": "POST schema for getting organisation",
"type": "object", "type": "object",
"properties": { "properties": {
"colour": {"type": ["string", "null"], "format": "string"}, "colour": {"type": ["string", "null"]},
"name": {"type": ["string", "null"], "minimum": 1}, "name": {"type": ["string", "null"]},
"logo": {"type": ["string", "null"], "minimum": 1} "logo": {"type": ["string", "null"]}
}, },
"required": ["logo"] "required": ["logo"]
} }
post_update_organisation_schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST schema for getting organisation",
"type": "object",
"properties": {
"colour": {"type": ["string", "null"]},
"name": {"type": ["string", "null"]},
"logo": {"type": ["string", "null"]}
},
"required": []
}

View File

@@ -2,12 +2,13 @@ from flask import Blueprint, jsonify, request
from app.dao.organisations_dao import ( from app.dao.organisations_dao import (
dao_create_organisation, dao_create_organisation,
dao_update_organisation,
dao_get_organisations, dao_get_organisations,
dao_get_organisation_by_id, dao_get_organisation_by_id,
) )
from app.errors import register_errors from app.errors import register_errors
from app.models import Organisation from app.models import Organisation
from app.organisation.organisation_schema import post_organisation_schema from app.organisation.organisation_schema import post_create_organisation_schema, post_update_organisation_schema
from app.schema_validation import validate from app.schema_validation import validate
organisation_blueprint = Blueprint('organisation', __name__) organisation_blueprint = Blueprint('organisation', __name__)
@@ -27,12 +28,26 @@ def get_organisation_by_id(org_id):
@organisation_blueprint.route('', methods=['POST']) @organisation_blueprint.route('', methods=['POST'])
def post_organisation(): def create_organisation():
data = request.get_json() data = request.get_json()
validate(data, post_organisation_schema) validate(data, post_create_organisation_schema)
organisation = Organisation(**data) organisation = Organisation(**data)
dao_create_organisation(organisation) dao_create_organisation(organisation)
return jsonify(data=organisation.serialize()), 201 return jsonify(data=organisation.serialize()), 201
@organisation_blueprint.route('/<uuid:organisation_id>', methods=['POST'])
def update_organisation(organisation_id):
fetched_organisation = dao_get_organisation_by_id(organisation_id)
data = request.get_json()
validate(data, post_update_organisation_schema)
for key in data.keys():
setattr(fetched_organisation, key, data[key])
return jsonify(data=fetched_organisation.serialize()), 200

View File

@@ -1,88 +1,113 @@
from flask import json from flask import json
import pytest
from app.models import Organisation from app.models import Organisation
from tests import create_authorization_header from tests import create_authorization_header
def test_get_organisations(notify_api, notify_db, notify_db_session): def test_get_organisations(admin_request, notify_db, notify_db_session):
org1 = Organisation(colour='#FFFFFF', logo='/path/image.png', name='Org1') org1 = Organisation(colour='#FFFFFF', logo='/path/image.png', name='Org1')
org2 = Organisation(colour='#000000', logo='/path/other.png', name='Org2') org2 = Organisation(colour='#000000', logo='/path/other.png', name='Org2')
notify_db.session.add_all([org1, org2]) notify_db.session.add_all([org1, org2])
notify_db.session.commit() notify_db.session.commit()
with notify_api.test_request_context(), notify_api.test_client() as client: organisations = admin_request.get(
auth_header = create_authorization_header() 'organisation.get_organisations'
response = client.get('/organisation', headers=[auth_header]) )['organisations']
assert response.status_code == 200
organisations = json.loads(response.get_data(as_text=True))['organisations']
assert len(organisations) == 2 assert len(organisations) == 2
assert {org['id'] for org in organisations} == {str(org1.id), str(org2.id)} assert {org['id'] for org in organisations} == {str(org1.id), str(org2.id)}
def test_get_organisation_by_id(notify_api, notify_db, notify_db_session): def test_get_organisation_by_id(admin_request, notify_db, notify_db_session):
org = Organisation(colour='#FFFFFF', logo='/path/image.png', name='My Org') org = Organisation(colour='#FFFFFF', logo='/path/image.png', name='My Org')
notify_db.session.add(org) notify_db.session.add(org)
notify_db.session.commit() notify_db.session.commit()
with notify_api.test_request_context(), notify_api.test_client() as client: response = admin_request.get(
auth_header = create_authorization_header() 'organisation.get_organisation_by_id',
response = client.get('/organisation/{}'.format(org.id), headers=[auth_header]) _expected_status=200,
org_id=org.id
)
assert response.status_code == 200 assert set(response['organisation'].keys()) == {'colour', 'logo', 'name', 'id'}
organisation = json.loads(response.get_data(as_text=True))['organisation'] assert response['organisation']['colour'] == '#FFFFFF'
assert set(organisation.keys()) == {'colour', 'logo', 'name', 'id'} assert response['organisation']['logo'] == '/path/image.png'
assert organisation['colour'] == '#FFFFFF' assert response['organisation']['name'] == 'My Org'
assert organisation['logo'] == '/path/image.png' assert response['organisation']['id'] == str(org.id)
assert organisation['name'] == 'My Org'
assert organisation['id'] == str(org.id)
def test_create_organisation(client, notify_db, notify_db_session): def test_post_create_organisation(admin_request, notify_db_session):
data = { data = {
'name': 'test organisation', 'name': 'test organisation',
'colour': '#0000ff', 'colour': '#0000ff',
'logo': '/images/test_x2.png' 'logo': '/images/test_x2.png'
} }
auth_header = create_authorization_header() response = admin_request.post(
'organisation.create_organisation',
response = client.post( _data=data,
'/organisation', _expected_status=201
headers=[('Content-Type', 'application/json'), auth_header],
data=json.dumps(data)
) )
assert response.status_code == 201 assert data['name'] == response['data']['name']
json_resp = json.loads(response.get_data(as_text=True)) assert data['colour'] == response['data']['colour']
assert data['name'] == json_resp['data']['name'] assert data['logo'] == response['data']['logo']
def test_create_organisation_without_logo_raises_error(client, notify_db, notify_db_session): def test_post_create_organisation_without_logo_raises_error(admin_request, notify_db_session):
data = { data = {
'name': 'test organisation', 'name': 'test organisation',
'colour': '#0000ff', 'colour': '#0000ff',
} }
auth_header = create_authorization_header() response = admin_request.post(
'organisation.create_organisation',
response = client.post( _data=data,
'/organisation', _expected_status=400
headers=[('Content-Type', 'application/json'), auth_header],
data=json.dumps(data)
) )
assert response.status_code == 400 assert response['errors'][0]['message'] == "logo is a required property"
json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['errors'][0]['message'] == "logo is a required property"
def test_create_organisation_without_name_or_colour_is_valid(client, notify_db, notify_db_session): def test_post_create_organisation_without_name_or_colour_is_valid(admin_request, notify_db_session):
data = { data = {
'logo': 'images/text_x2.png' 'logo': 'images/text_x2.png'
} }
auth_header = create_authorization_header() response = admin_request.post(
'organisation.create_organisation',
response = client.post( _data=data,
'/organisation', _expected_status=201
headers=[('Content-Type', 'application/json'), auth_header],
data=json.dumps(data)
) )
assert response.status_code == 201
assert response['data']['logo'] == data['logo']
assert response['data']['name'] is None
assert response['data']['colour'] is None
@pytest.mark.parametrize('data_update', [
({'name': 'test organisation 1'}),
({'logo': 'images/text_x3.png', 'colour': '#ffffff'})
])
def test_post_update_organisation_updates_field(admin_request, notify_db_session, data_update):
data = {
'name': 'test organisation',
'logo': 'images/text_x2.png'
}
response = admin_request.post(
'organisation.create_organisation',
_data=data,
_expected_status=201
)
org_id = response['data']['id']
response = admin_request.post(
'organisation.update_organisation',
_data=data_update,
organisation_id=org_id
)
organisations = Organisation.query.all()
assert len(organisations) == 1
assert str(organisations[0].id) == org_id
for key in data_update.keys():
assert getattr(organisations[0], key) == data_update[key]