From a487293bf04e1706e7bfaebd077e7dd0950ea44c Mon Sep 17 00:00:00 2001 From: chrisw Date: Tue, 6 Mar 2018 12:49:46 +0000 Subject: [PATCH] Add check org name is unique endpoint --- app/organisation/rest.py | 23 +++++++ tests/app/organisation/test_rest.py | 99 +++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) diff --git a/app/organisation/rest.py b/app/organisation/rest.py index 3bf3972b4..04100ce2a 100644 --- a/app/organisation/rest.py +++ b/app/organisation/rest.py @@ -107,3 +107,26 @@ def get_organisation_users(organisation_id): result = user_schema.dump(org_users, many=True) return jsonify(data=result.data) + + +@organisation_blueprint.route('/unique', methods=["GET"]) +def is_organisation_name_unique(): + organisation_id, name = check_request_args(request) + + name_exists = Organisation.query.filter(Organisation.name.ilike(name)).first() + + result = (not name_exists) or str(name_exists.id) == organisation_id + return jsonify(result=result), 200 + + +def check_request_args(request): + org_id = request.args.get('org_id') + name = request.args.get('name', None) + errors = [] + if not org_id: + errors.append({'org_id': ["Can't be empty"]}) + if not name: + errors.append({'name': ["Can't be empty"]}) + if errors: + raise InvalidRequest(errors, status_code=400) + return org_id, name diff --git a/tests/app/organisation/test_rest.py b/tests/app/organisation/test_rest.py index 55db57330..dee403245 100644 --- a/tests/app/organisation/test_rest.py +++ b/tests/app/organisation/test_rest.py @@ -1,5 +1,7 @@ import uuid +import pytest + from app.models import Organisation from app.dao.organisation_dao import dao_add_service_to_organisation, dao_add_user_to_organisation from tests.app.db import create_organisation, create_service, create_user @@ -310,3 +312,100 @@ def test_get_organisation_users_returns_users_for_organisation(admin_request, sa assert len(response['data']) == 2 assert response['data'][0]['id'] == str(first.id) + + +def test_is_organisation_name_unique_returns_200_if_unique(admin_request, notify_db, notify_db_session): + organisation = create_organisation(name='unique') + + response = admin_request.get( + 'organisation.is_organisation_name_unique', + _expected_status=200, + org_id=organisation.id, + name='something' + ) + + assert response == {"result": True} + + +@pytest.mark.parametrize('name', ["UNIQUE", "Unique.", "**uniQUE**"]) +def test_is_organisation_name_unique_returns_200_and_name_capitalized_or_punctuation_added( + admin_request, + notify_db, + notify_db_session, + name +): + organisation = create_organisation(name='unique') + + response = admin_request.get( + 'organisation.is_organisation_name_unique', + _expected_status=200, + org_id=organisation.id, + name=name + ) + + assert response == {"result": True} + + +@pytest.mark.parametrize('name', ["UNIQUE", "Unique"]) +def test_is_organisation_name_unique_returns_200_and_false_with_same_name_and_different_case_of_other_organisation( + admin_request, + notify_db, + notify_db_session, + name +): + create_organisation(name='unique') + different_organisation_id = '111aa111-2222-bbbb-aaaa-111111111111' + + response = admin_request.get( + 'organisation.is_organisation_name_unique', + _expected_status=200, + org_id=different_organisation_id, + name=name + ) + + assert response == {"result": False} + + +def test_is_organisation_name_unique_returns_200_and_false_if_name_exists_for_a_different_organisation( + admin_request, + notify_db, + notify_db_session +): + create_organisation(name='existing name') + different_organisation_id = '111aa111-2222-bbbb-aaaa-111111111111' + + response = admin_request.get( + 'organisation.is_organisation_name_unique', + _expected_status=200, + org_id=different_organisation_id, + name='existing name' + ) + + assert response == {"result": False} + + +def test_is_organisation_name_unique_returns_200_and_true_if_name_exists_for_the_same_organisation( + admin_request, + notify_db, + notify_db_session +): + organisation = create_organisation(name='unique') + + response = admin_request.get( + 'organisation.is_organisation_name_unique', + _expected_status=200, + org_id=organisation.id, + name='unique' + ) + + assert response == {"result": True} + + +def test_is_organisation_name_unique_returns_400_when_name_does_not_exist(admin_request): + response = admin_request.get( + 'organisation.is_organisation_name_unique', + _expected_status=400 + ) + + assert response["message"][0]["org_id"] == ["Can't be empty"] + assert response["message"][1]["name"] == ["Can't be empty"]