mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 00:41:35 -05:00
Add check org name is unique endpoint
This commit is contained in:
@@ -107,3 +107,26 @@ def get_organisation_users(organisation_id):
|
|||||||
|
|
||||||
result = user_schema.dump(org_users, many=True)
|
result = user_schema.dump(org_users, many=True)
|
||||||
return jsonify(data=result.data)
|
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
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from app.models import Organisation
|
from app.models import Organisation
|
||||||
from app.dao.organisation_dao import dao_add_service_to_organisation, dao_add_user_to_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
|
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 len(response['data']) == 2
|
||||||
assert response['data'][0]['id'] == str(first.id)
|
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"]
|
||||||
|
|||||||
Reference in New Issue
Block a user