Organisation services API endpoints

This commit is contained in:
Ken Tsang
2018-02-10 01:37:17 +00:00
parent 60f96ab598
commit 44f9143d08
6 changed files with 154 additions and 0 deletions

View File

@@ -1,3 +1,5 @@
from app.schema_validation.definitions import uuid
post_create_organisation_schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST organisation schema",
@@ -19,3 +21,13 @@ post_update_organisation_schema = {
},
"required": []
}
post_link_service_to_organisation_schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST link service to organisation schema",
"type": "object",
"properties": {
"service_id": uuid
},
"required": ["service_id"]
}

View File

@@ -4,15 +4,20 @@ from app.dao.organisation_dao import (
dao_create_organisation,
dao_get_organisations,
dao_get_organisation_by_id,
dao_get_organisation_services,
dao_update_organisation,
dao_add_service_to_organisation,
)
from app.dao.services_dao import dao_fetch_service_by_id
from app.errors import register_errors, InvalidRequest
from app.models import Organisation
from app.organisation.organisation_schema import (
post_create_organisation_schema,
post_update_organisation_schema,
post_link_service_to_organisation_schema,
)
from app.schema_validation import validate
from app.schemas import service_schema
organisation_blueprint = Blueprint('organisation', __name__)
register_errors(organisation_blueprint)
@@ -55,3 +60,21 @@ def update_organisation(organisation_id):
return '', 204
else:
raise InvalidRequest("Organisation not found", 404)
@organisation_blueprint.route('/<uuid:organisation_id>/service', methods=['POST'])
def link_service_to_organisation(organisation_id):
data = request.get_json()
validate(data, post_link_service_to_organisation_schema)
service = dao_fetch_service_by_id(data['service_id'])
service.organisation = None
dao_add_service_to_organisation(service, organisation_id)
return '', 204
@organisation_blueprint.route('/<uuid:organisation_id>/services', methods=['GET'])
def get_organisation_services(organisation_id):
services = dao_get_organisation_services(organisation_id)
return jsonify([service_schema.dump(s).data for s in services])

View File

@@ -204,6 +204,7 @@ class ServiceSchema(BaseSchema):
dvla_organisation = field_for(models.Service, 'dvla_organisation')
permissions = fields.Method("service_permissions")
email_branding = field_for(models.Service, 'email_branding')
organisation = field_for(models.Service, 'organisation')
override_flag = False
reply_to_email_address = fields.Method(method_name="get_reply_to_email_address")
sms_sender = fields.Method(method_name="get_sms_sender")

View File

@@ -17,6 +17,7 @@ from app.dao.api_key_dao import (
get_unsigned_secret,
expire_api_key)
from app.dao.inbound_numbers_dao import dao_allocate_number_for_service
from app.dao.organisation_dao import dao_get_organisation_by_service_id
from app.dao.service_sms_sender_dao import (
dao_add_sms_sender_for_service,
dao_update_service_sms_sender,
@@ -664,6 +665,12 @@ def get_service_sms_senders_for_service(service_id):
return jsonify([sms_sender.serialize() for sms_sender in sms_senders]), 200
@service_blueprint.route('/<uuid:service_id>/organisation', methods=['GET'])
def get_organisation_for_service(service_id):
organisation = dao_get_organisation_by_service_id(service_id=service_id)
return jsonify(organisation.serialize() if organisation else {}), 200
@service_blueprint.route('/unique', methods=["GET"])
def is_service_name_unique():
name, email_from = check_request_args(request)

View File

@@ -1,4 +1,5 @@
from app.models import Organisation
from app.dao.organisation_dao import dao_add_service_to_organisation
from tests.app.db import create_organisation
@@ -103,3 +104,95 @@ def test_post_update_organisation_gives_404_status_if_org_does_not_exist(admin_r
organisation = Organisation.query.all()
assert not organisation
def test_post_link_service_to_organisation(admin_request, sample_service, sample_organisation):
data = {
'service_id': str(sample_service.id)
}
admin_request.post(
'organisation.link_service_to_organisation',
_data=data,
organisation_id=sample_organisation.id,
_expected_status=204
)
assert len(sample_organisation.services) == 1
def test_post_link_service_to_another_org(
admin_request, sample_service, sample_organisation):
data = {
'service_id': str(sample_service.id)
}
admin_request.post(
'organisation.link_service_to_organisation',
_data=data,
organisation_id=sample_organisation.id,
_expected_status=204
)
assert len(sample_organisation.services) == 1
new_org = create_organisation()
admin_request.post(
'organisation.link_service_to_organisation',
_data=data,
organisation_id=new_org.id,
_expected_status=204
)
assert not sample_organisation.services
assert len(new_org.services) == 1
def test_post_link_service_to_organisation_nonexistent_organisation(
admin_request, sample_service, fake_uuid):
data = {
'service_id': str(sample_service.id)
}
admin_request.post(
'organisation.link_service_to_organisation',
_data=data,
organisation_id=fake_uuid,
_expected_status=404
)
def test_post_link_service_to_organisation_nonexistent_service(
admin_request, sample_organisation, fake_uuid):
data = {
'service_id': fake_uuid
}
admin_request.post(
'organisation.link_service_to_organisation',
_data=data,
organisation_id=str(sample_organisation.id),
_expected_status=404
)
def test_post_link_service_to_organisation_missing_payload(
admin_request, sample_organisation, fake_uuid):
admin_request.post(
'organisation.link_service_to_organisation',
organisation_id=str(sample_organisation.id),
_expected_status=400
)
def test_rest_get_organisation_services(
admin_request, sample_organisation, sample_service):
dao_add_service_to_organisation(sample_service, sample_organisation.id)
response = admin_request.get(
'organisation.get_organisation_services',
organisation_id=str(sample_organisation.id),
_expected_status=200
)
assert len(response) == 1
assert response[0]['id'] == str(sample_service.id)
assert response[0]['name'] == sample_service.name

View File

@@ -9,6 +9,7 @@ from flask import url_for, current_app
from freezegun import freeze_time
from app.celery.scheduled_tasks import daily_stats_template_usage_by_month
from app.dao.organisation_dao import dao_add_service_to_organisation
from app.dao.services_dao import dao_remove_user_from_service
from app.dao.templates_dao import dao_redact_template
from app.dao.users_dao import save_model_user
@@ -2765,6 +2766,23 @@ def test_get_service_sms_senders_for_service_returns_empty_list_when_service_doe
assert json.loads(response.get_data(as_text=True)) == []
def test_get_organisation_for_service_id(admin_request, sample_service, sample_organisation):
dao_add_service_to_organisation(sample_service, sample_organisation.id)
response = admin_request.get(
'service.get_organisation_for_service',
service_id=sample_service.id
)
assert response == sample_organisation.serialize()
def test_get_organisation_for_service_id_return_empty_dict_if_service_not_in_organisation(admin_request, fake_uuid):
response = admin_request.get(
'service.get_organisation_for_service',
service_id=fake_uuid
)
assert response == {}
def test_get_platform_stats(client, notify_db_session):
service_1 = create_service(service_name='Service 1')
service_2 = create_service(service_name='Service 2')