diff --git a/app/organisation/rest.py b/app/organisation/rest.py index 68bbe9119..0bb5ff3ae 100644 --- a/app/organisation/rest.py +++ b/app/organisation/rest.py @@ -1,4 +1,4 @@ -from flask import Blueprint, current_app, jsonify, request +from flask import Blueprint, jsonify, request from sqlalchemy.exc import IntegrityError from app.dao.organisation_dao import ( @@ -29,11 +29,7 @@ def handle_integrity_error(exc): Handle integrity errors caused by the unique contraint on ix_organisation_name """ if 'ix_organisation_name' in str(exc): - current_app.logger.exception('Unique constraint ix_organisation_name triggered') - return jsonify( - result='error', - message='Organisation name already exists' - ), 400 + raise InvalidRequest('Organisation name already exists', 400) raise diff --git a/tests/app/organisation/test_rest.py b/tests/app/organisation/test_rest.py index 0bcd56381..a6fa6daaa 100644 --- a/tests/app/organisation/test_rest.py +++ b/tests/app/organisation/test_rest.py @@ -1,3 +1,6 @@ +import pytest + +from app.errors import InvalidRequest from app.models import Organisation from app.dao.organisation_dao import dao_add_service_to_organisation from tests.app.db import create_organisation, create_service @@ -59,16 +62,17 @@ def test_post_create_organisation_existing_name_raises_400(admin_request, sample 'active': True } - response = admin_request.post( - 'organisation.create_organisation', - _data=data, - _expected_status=400 - ) + with pytest.raises(InvalidRequest) as e: + admin_request.post( + 'organisation.create_organisation', + _data=data, + _expected_status=400 + ) organisation = Organisation.query.all() assert len(organisation) == 1 - assert response['message'] == 'Organisation name already exists' + assert 'Organisation name already exists' in str(e.value) def test_post_create_organisation_with_missing_name_gives_validation_error(admin_request, notify_db_session): @@ -117,14 +121,15 @@ def test_post_update_organisation_raises_400_on_existing_org_name( 'active': False } - response = admin_request.post( - 'organisation.update_organisation', - _data=data, - organisation_id=org.id, - _expected_status=400 - ) + with pytest.raises(expected_exception=InvalidRequest) as e: + admin_request.post( + 'organisation.update_organisation', + _data=data, + organisation_id=org.id, + _expected_status=400 + ) - assert response['message'] == 'Organisation name already exists' + assert 'Organisation name already exists' in str(e.value) def test_post_update_organisation_gives_404_status_if_org_does_not_exist(admin_request, notify_db_session):