From adcd798d88ad98340619daddc6a42f8f7edf93be Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Fri, 15 May 2020 17:50:30 +0100 Subject: [PATCH] Catch error when organisation domain already in use Up till now, when adding new organisation domain, if it was already in use, we didn't handle the 400 we got back from API. This PR adds handling for that error. --- app/main/views/organisations.py | 27 +++++++++++----- .../views/organisations/test_organisation.py | 32 +++++++++++++++++++ 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/app/main/views/organisations.py b/app/main/views/organisations.py index 7b656bbfa..01c0224d4 100644 --- a/app/main/views/organisations.py +++ b/app/main/views/organisations.py @@ -448,13 +448,24 @@ def edit_organisation_domains(org_id): form = OrganisationDomainsForm() if form.validate_on_submit(): - organisations_client.update_organisation( - org_id, - domains=list(OrderedDict.fromkeys( - domain.lower() - for domain in filter(None, form.domains.data) - )), - ) + try: + organisations_client.update_organisation( + org_id, + domains=list(OrderedDict.fromkeys( + domain.lower() + for domain in filter(None, form.domains.data) + )), + ) + except HTTPError as e: + error_message = "Domain already exists" + if e.status_code == 400 and error_message in e.message: + flash("This domain is already in use", "error") + return render_template( + 'views/organisations/organisation/settings/edit-domains.html', + form=form, + ) + else: + raise e return redirect(url_for('.organisation_settings', org_id=org_id)) form.populate(current_organisation.domains) @@ -483,7 +494,7 @@ def confirm_edit_organisation_name(org_id): except HTTPError as e: error_msg = "Organisation name already exists" if e.status_code == 400 and error_msg in e.message: - # Redirect the user back to the change service name screen + # Redirect the user back to the change organisation name screen flash('This organisation name is already in use', 'error') return redirect(url_for('main.edit_organisation_name', org_id=org_id)) else: diff --git a/tests/app/main/views/organisations/test_organisation.py b/tests/app/main/views/organisations/test_organisation.py index 6569e4acf..dbc8409e6 100644 --- a/tests/app/main/views/organisations/test_organisation.py +++ b/tests/app/main/views/organisations/test_organisation.py @@ -991,6 +991,38 @@ def test_update_organisation_domains( ) +def test_update_organisation_domains_when_domain_already_exists( + mocker, + client_request, + fake_uuid, + organisation_one, + mock_get_organisation, +): + user = create_platform_admin_user() + client_request.login(user) + + mocker.patch('app.organisations_client.update_organisation', side_effect=HTTPError( + response=Mock( + status_code=400, + json={'result': 'error', 'message': 'Domain already exists'} + ), + message="Domain already exists") + ) + + response = client_request.post( + 'main.edit_organisation_domains', + org_id=ORGANISATION_ID, + _data={ + 'domains': [ + 'example.gov.uk', + ] + }, + _expected_status=200, + ) + + assert response.find("div", class_="banner-dangerous").text.strip() == "This domain is already in use" + + def test_update_organisation_name( platform_admin_client, organisation_one,