mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-19 05:59:44 -04:00
Merge pull request #3796 from alphagov/handle-duplicate-org
Handle exception is org name already exists
This commit is contained in:
@@ -62,10 +62,17 @@ def add_organisation():
|
||||
form = NewOrganisationForm()
|
||||
|
||||
if form.validate_on_submit():
|
||||
return redirect(url_for(
|
||||
'.organisation_settings',
|
||||
org_id=Organisation.create_from_form(form).id,
|
||||
))
|
||||
try:
|
||||
return redirect(url_for(
|
||||
'.organisation_settings',
|
||||
org_id=Organisation.create_from_form(form).id,
|
||||
))
|
||||
except HTTPError as e:
|
||||
msg = 'Organisation name already exists'
|
||||
if e.status_code == 400 and msg in e.message:
|
||||
form.name.errors.append("This organisation name is already in use")
|
||||
else:
|
||||
raise e
|
||||
|
||||
return render_template(
|
||||
'views/organisations/add-organisation.html',
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from unittest.mock import ANY, Mock
|
||||
|
||||
import pytest
|
||||
from bs4 import BeautifulSoup
|
||||
from flask import url_for
|
||||
@@ -102,7 +100,7 @@ def test_page_to_create_new_organisation(
|
||||
('radio', 'organisation_type', 'other'),
|
||||
('radio', 'crown_status', 'crown'),
|
||||
('radio', 'crown_status', 'non-crown'),
|
||||
('hidden', 'csrf_token', ANY),
|
||||
('hidden', 'csrf_token', mocker.ANY),
|
||||
]
|
||||
|
||||
|
||||
@@ -194,6 +192,37 @@ def test_create_new_organisation_fails_with_incorrect_input(
|
||||
assert error_message in page.select_one('.govuk-error-message').text
|
||||
|
||||
|
||||
def test_create_new_organisation_fails_with_duplicate_name(
|
||||
client_request,
|
||||
platform_admin_user,
|
||||
mocker,
|
||||
):
|
||||
def _create(**_kwargs):
|
||||
json_mock = mocker.Mock(return_value={'message': 'Organisation name already exists'})
|
||||
resp_mock = mocker.Mock(status_code=400, json=json_mock)
|
||||
http_error = HTTPError(response=resp_mock, message="Default message")
|
||||
raise http_error
|
||||
|
||||
mocker.patch(
|
||||
'app.organisations_client.create_organisation',
|
||||
side_effect=_create
|
||||
)
|
||||
|
||||
client_request.login(platform_admin_user)
|
||||
page = client_request.post(
|
||||
'.add_organisation',
|
||||
_data={
|
||||
'name': 'Existing org',
|
||||
'organisation_type': 'local',
|
||||
'crown_status': 'non-crown',
|
||||
},
|
||||
_expected_status=200,
|
||||
)
|
||||
|
||||
error_message = 'This organisation name is already in use'
|
||||
assert error_message in page.select_one('.govuk-error-message').text
|
||||
|
||||
|
||||
@pytest.mark.parametrize('organisation_type, organisation, expected_status', (
|
||||
('nhs_gp', None, 200),
|
||||
('central', None, 403),
|
||||
@@ -1048,7 +1077,7 @@ def test_update_organisation_domains_when_domain_already_exists(
|
||||
client_request.login(user)
|
||||
|
||||
mocker.patch('app.organisations_client.update_organisation', side_effect=HTTPError(
|
||||
response=Mock(
|
||||
response=mocker.Mock(
|
||||
status_code=400,
|
||||
json={'result': 'error', 'message': 'Domain already exists'}
|
||||
),
|
||||
@@ -1198,7 +1227,7 @@ def test_confirm_update_organisation_with_name_already_in_use(
|
||||
mocker.patch(
|
||||
'app.organisations_client.update_organisation_name',
|
||||
side_effect=HTTPError(
|
||||
response=Mock(
|
||||
response=mocker.Mock(
|
||||
status_code=400,
|
||||
json={'result': 'error', 'message': 'Organisation name already exists'}
|
||||
),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import pytest
|
||||
from flask import session, url_for
|
||||
from notifications_python_client.errors import HTTPError
|
||||
|
||||
from app.utils import is_gov_user
|
||||
from tests import organisation_json
|
||||
@@ -309,9 +310,20 @@ def test_add_service_fails_if_service_name_fails_validation(
|
||||
|
||||
def test_should_return_form_errors_with_duplicate_service_name_regardless_of_case(
|
||||
client_request,
|
||||
mock_create_duplicate_service,
|
||||
mock_get_organisation_by_domain,
|
||||
mocker,
|
||||
):
|
||||
def _create(**_kwargs):
|
||||
json_mock = mocker.Mock(return_value={'message': {'name': ["Duplicate service name"]}})
|
||||
resp_mock = mocker.Mock(status_code=400, json=json_mock)
|
||||
http_error = HTTPError(response=resp_mock, message="Default message")
|
||||
raise http_error
|
||||
|
||||
mocker.patch(
|
||||
'app.service_api_client.create_service',
|
||||
side_effect=_create
|
||||
)
|
||||
|
||||
page = client_request.post(
|
||||
'main.add_service',
|
||||
_data={
|
||||
|
||||
@@ -587,25 +587,6 @@ def mock_create_service(mocker):
|
||||
'app.service_api_client.create_service', side_effect=_create)
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_create_duplicate_service(mocker):
|
||||
def _create(
|
||||
service_name,
|
||||
organisation_type,
|
||||
message_limit,
|
||||
restricted,
|
||||
user_id,
|
||||
email_from,
|
||||
):
|
||||
json_mock = Mock(return_value={'message': {'name': ["Duplicate service name '{}'".format(service_name)]}})
|
||||
resp_mock = Mock(status_code=400, json=json_mock)
|
||||
http_error = HTTPError(response=resp_mock, message="Default message")
|
||||
raise http_error
|
||||
|
||||
return mocker.patch(
|
||||
'app.service_api_client.create_service', side_effect=_create)
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_update_service(mocker):
|
||||
def _update(service_id, **kwargs):
|
||||
|
||||
Reference in New Issue
Block a user