Show error when user cannot be archived

A user can't be archived if they are the only member of their service
with `manage_settings` permission. `notifications-api` returns a `400`
and an error message if that is the case, however this PR to remove the
`400` error handler
https://github.com/alphagov/notifications-admin/pull/3320 stopped the
error message from showing. This meant that instead of seeing a message
about why a user couldn't be archived, we would just show a `500` error
page instead. This change checks the response from `notifications-api`
and shows an error banner with a message if the user can't be archived.
This commit is contained in:
Katie Smith
2020-04-21 17:40:47 +01:00
parent f91aba212d
commit ffdcda02bf
2 changed files with 42 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ import pytest
from bs4 import BeautifulSoup
from flask import url_for
from lxml import html
from notifications_python_client.errors import HTTPError
from tests import user_json
from tests.conftest import normalize_spaces
@@ -221,6 +222,39 @@ def test_archive_user_posts_to_user_client(
assert mock_events.called
def test_archive_user_shows_error_message_if_user_cannot_be_archived(
platform_admin_client,
api_user_active,
mocker,
mock_get_non_empty_organisations_and_services_for_user,
):
mocker.patch(
'app.user_api_client.post',
side_effect=HTTPError(
response=mocker.Mock(
status_code=400,
json={'result': 'error',
'message': 'User cant be removed from a service - check all services have another '
'team member with manage_settings'}
),
message='User cant be removed from a service - check all services have another team member '
'with manage_settings'
)
)
response = platform_admin_client.post(
url_for('main.archive_user', user_id=api_user_active['id']),
follow_redirects=True
)
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert normalize_spaces(page.find('h1').text) == 'Platform admin user'
assert normalize_spaces(
page.select_one('.banner-dangerous').text
) == 'User cant be removed from a service - check all services have another team member with manage_settings'
def test_archive_user_does_not_create_event_if_user_client_raises_exception(
platform_admin_client,
api_user_active,