mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-24 01:11:38 -05:00
Catch itegrity errors and return 400.
When creating or updating an organisation an itegrity error is raise if the name is already used. This change adds a new error handler for the organisation to catch the named unique index and return a 400 with a sensible message. We have an other error handler for unique service names which was caught in the error handler for all blueprints. A new error handler for the service_blueprint has been created for catch those specific unique constraints. This is a nice way to encapulate the specific errors for a specific blueprint.
This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
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
|
||||
@@ -62,17 +59,16 @@ def test_post_create_organisation_existing_name_raises_400(admin_request, sample
|
||||
'active': True
|
||||
}
|
||||
|
||||
with pytest.raises(InvalidRequest) as e:
|
||||
admin_request.post(
|
||||
'organisation.create_organisation',
|
||||
_data=data,
|
||||
_expected_status=400
|
||||
)
|
||||
response = admin_request.post(
|
||||
'organisation.create_organisation',
|
||||
_data=data,
|
||||
_expected_status=400
|
||||
)
|
||||
|
||||
organisation = Organisation.query.all()
|
||||
|
||||
assert len(organisation) == 1
|
||||
assert 'Organisation name already exists' in str(e.value)
|
||||
assert response['message'] == 'Organisation name already exists'
|
||||
|
||||
|
||||
def test_post_create_organisation_with_missing_name_gives_validation_error(admin_request, notify_db_session):
|
||||
@@ -114,22 +110,21 @@ def test_post_update_organisation_updates_fields(admin_request, notify_db_sessio
|
||||
|
||||
|
||||
def test_post_update_organisation_raises_400_on_existing_org_name(
|
||||
admin_request, notify_db_session, sample_organisation):
|
||||
admin_request, sample_organisation):
|
||||
org = create_organisation()
|
||||
data = {
|
||||
'name': sample_organisation.name,
|
||||
'active': False
|
||||
}
|
||||
|
||||
with pytest.raises(expected_exception=InvalidRequest) as e:
|
||||
admin_request.post(
|
||||
'organisation.update_organisation',
|
||||
_data=data,
|
||||
organisation_id=org.id,
|
||||
_expected_status=400
|
||||
)
|
||||
response = admin_request.post(
|
||||
'organisation.update_organisation',
|
||||
_data=data,
|
||||
organisation_id=org.id,
|
||||
_expected_status=400
|
||||
)
|
||||
|
||||
assert 'Organisation name already exists' in str(e.value)
|
||||
assert response['message'] == 'Organisation name already exists'
|
||||
|
||||
|
||||
def test_post_update_organisation_gives_404_status_if_org_does_not_exist(admin_request, notify_db_session):
|
||||
|
||||
Reference in New Issue
Block a user