mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-11 18:37:33 -04:00
Stop using platform_admin_client fixture
We have a `client_request` fixture which does a bunch of useful stuff like: - checking the status code of the response - returning a `BeautifulSoup` object For most tests of a platform admin view we used `platform_admin_client` instead. This is not as good because it returns a raw `Response` object and doesn’t do the additional checks. This commit converts all the tests using `platform_admin_client` to: use new `client_request` and log in as `platform_admin_user` before making any requests. This is also nice because it makes any test easy to parametrize with additional users, for example to test differences in behaviour dependant on being platform admin or not.
This commit is contained in:
@@ -157,14 +157,15 @@ def test_user_information_page_displays_if_there_are_failed_login_attempts(
|
||||
|
||||
|
||||
def test_user_information_page_shows_archive_link_for_active_users(
|
||||
platform_admin_client,
|
||||
client_request,
|
||||
platform_admin_user,
|
||||
api_user_active,
|
||||
mock_get_organisations_and_services_for_user,
|
||||
):
|
||||
response = platform_admin_client.get(
|
||||
url_for('main.user_information', user_id=api_user_active['id'])
|
||||
client_request.login(platform_admin_user)
|
||||
page = client_request.get(
|
||||
'main.user_information', user_id=api_user_active['id']
|
||||
)
|
||||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||
archive_url = url_for('main.archive_user', user_id=api_user_active['id'])
|
||||
|
||||
link = page.find('a', {'href': archive_url})
|
||||
@@ -190,40 +191,46 @@ def test_user_information_page_does_not_show_archive_link_for_inactive_users(
|
||||
|
||||
|
||||
def test_archive_user_prompts_for_confirmation(
|
||||
platform_admin_client,
|
||||
client_request,
|
||||
platform_admin_user,
|
||||
api_user_active,
|
||||
mock_get_organisations_and_services_for_user,
|
||||
):
|
||||
response = platform_admin_client.get(
|
||||
url_for('main.archive_user', user_id=api_user_active['id'])
|
||||
client_request.login(platform_admin_user)
|
||||
page = client_request.get(
|
||||
'main.archive_user', user_id=api_user_active['id']
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||
assert 'Are you sure you want to archive this user?' in page.find('div', class_='banner-dangerous').text
|
||||
|
||||
|
||||
def test_archive_user_posts_to_user_client(
|
||||
platform_admin_client,
|
||||
client_request,
|
||||
platform_admin_user,
|
||||
api_user_active,
|
||||
mocker,
|
||||
mock_events,
|
||||
):
|
||||
mock_user_client = mocker.patch('app.user_api_client.post')
|
||||
|
||||
response = platform_admin_client.post(
|
||||
url_for('main.archive_user', user_id=api_user_active['id'])
|
||||
client_request.login(platform_admin_user)
|
||||
client_request.post(
|
||||
'main.archive_user', user_id=api_user_active['id'],
|
||||
_expected_redirect=url_for(
|
||||
'main.user_information',
|
||||
user_id=api_user_active['id'],
|
||||
_external=True,
|
||||
),
|
||||
)
|
||||
|
||||
assert response.status_code == 302
|
||||
assert response.location == url_for('main.user_information', user_id=api_user_active['id'], _external=True)
|
||||
mock_user_client.assert_called_once_with('/user/{}/archive'.format(api_user_active['id']), data=None)
|
||||
|
||||
assert mock_events.called
|
||||
|
||||
|
||||
def test_archive_user_shows_error_message_if_user_cannot_be_archived(
|
||||
platform_admin_client,
|
||||
client_request,
|
||||
platform_admin_user,
|
||||
api_user_active,
|
||||
mocker,
|
||||
mock_get_non_empty_organisations_and_services_for_user,
|
||||
@@ -242,13 +249,13 @@ def test_archive_user_shows_error_message_if_user_cannot_be_archived(
|
||||
)
|
||||
)
|
||||
|
||||
response = platform_admin_client.post(
|
||||
url_for('main.archive_user', user_id=api_user_active['id']),
|
||||
follow_redirects=True
|
||||
client_request.login(platform_admin_user)
|
||||
page = client_request.post(
|
||||
'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
|
||||
@@ -256,14 +263,16 @@ def test_archive_user_shows_error_message_if_user_cannot_be_archived(
|
||||
|
||||
|
||||
def test_archive_user_does_not_create_event_if_user_client_raises_unexpected_exception(
|
||||
platform_admin_client,
|
||||
client_request,
|
||||
platform_admin_user,
|
||||
api_user_active,
|
||||
mocker,
|
||||
mock_events,
|
||||
):
|
||||
with pytest.raises(Exception):
|
||||
platform_admin_client.post(
|
||||
url_for('main.archive_user', user_id=api_user_active.id)
|
||||
client_request.login(platform_admin_user)
|
||||
client_request.post(
|
||||
'main.archive_user', user_id=api_user_active.id,
|
||||
)
|
||||
|
||||
assert not mock_events.called
|
||||
|
||||
Reference in New Issue
Block a user