2019-11-04 11:07:55 +00:00
|
|
|
|
import uuid
|
|
|
|
|
|
|
2019-05-22 14:05:19 +01:00
|
|
|
|
import pytest
|
2018-07-06 11:10:14 +01:00
|
|
|
|
from flask import url_for
|
2020-04-21 17:40:47 +01:00
|
|
|
|
from notifications_python_client.errors import HTTPError
|
2018-07-06 11:10:14 +01:00
|
|
|
|
|
2018-07-06 16:16:21 +01:00
|
|
|
|
from tests import user_json
|
2019-06-05 11:25:27 +01:00
|
|
|
|
from tests.conftest import normalize_spaces
|
2018-07-06 11:10:14 +01:00
|
|
|
|
|
2018-07-10 17:24:20 +01:00
|
|
|
|
|
2018-07-16 15:04:03 +01:00
|
|
|
|
def test_find_users_by_email_page_loads_correctly(client_request, platform_admin_user):
|
|
|
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
|
document = client_request.get('main.find_users_by_email')
|
2018-07-06 11:10:14 +01:00
|
|
|
|
|
2018-07-16 15:04:03 +01:00
|
|
|
|
assert document.h1.text.strip() == 'Find users by email'
|
|
|
|
|
|
assert len(document.find_all('input', {'type': 'search'})) > 0
|
2018-07-06 16:16:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_find_users_by_email_displays_users_found(
|
2018-07-16 15:04:03 +01:00
|
|
|
|
client_request,
|
2018-07-10 17:24:20 +01:00
|
|
|
|
platform_admin_user,
|
|
|
|
|
|
mocker
|
2018-07-06 16:16:21 +01:00
|
|
|
|
):
|
2018-07-16 15:04:03 +01:00
|
|
|
|
client_request.login(platform_admin_user)
|
2018-07-10 17:24:20 +01:00
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.user_api_client.find_users_by_full_or_partial_email',
|
|
|
|
|
|
return_value={"data": [user_json()]},
|
|
|
|
|
|
autospec=True,
|
|
|
|
|
|
)
|
2018-07-16 15:04:03 +01:00
|
|
|
|
document = client_request.post(
|
|
|
|
|
|
'main.find_users_by_email',
|
|
|
|
|
|
_data={"search": "twilight.sparkle"},
|
|
|
|
|
|
_expected_status=200
|
|
|
|
|
|
)
|
2018-07-06 16:16:21 +01:00
|
|
|
|
|
2018-07-16 15:04:03 +01:00
|
|
|
|
assert any(element.text.strip() == 'test@gov.uk' for element in document.find_all(
|
|
|
|
|
|
'a', {'class': 'browse-list-link'}, href=True)
|
|
|
|
|
|
)
|
|
|
|
|
|
assert any(element.text.strip() == 'Test User' for element in document.find_all('p', {'class': 'browse-list-hint'}))
|
|
|
|
|
|
|
|
|
|
|
|
assert document.find('a', {'class': 'browse-list-link'}).text.strip() == 'test@gov.uk'
|
|
|
|
|
|
assert document.find('p', {'class': 'browse-list-hint'}).text.strip() == 'Test User'
|
2018-07-10 17:24:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_find_users_by_email_displays_multiple_users(
|
2018-07-16 15:04:03 +01:00
|
|
|
|
client_request,
|
2018-07-10 17:24:20 +01:00
|
|
|
|
platform_admin_user,
|
|
|
|
|
|
mocker
|
|
|
|
|
|
):
|
2018-07-16 15:04:03 +01:00
|
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.user_api_client.find_users_by_full_or_partial_email',
|
|
|
|
|
|
return_value={"data": [user_json(name="Apple Jack"), user_json(name="Apple Bloom")]},
|
|
|
|
|
|
autospec=True,
|
|
|
|
|
|
)
|
|
|
|
|
|
document = client_request.post('main.find_users_by_email', _data={"search": "apple"}, _expected_status=200)
|
2018-07-10 17:24:20 +01:00
|
|
|
|
|
2018-07-16 15:04:03 +01:00
|
|
|
|
assert any(
|
|
|
|
|
|
element.text.strip() == 'Apple Jack' for element in document.find_all('p', {'class': 'browse-list-hint'})
|
|
|
|
|
|
)
|
|
|
|
|
|
assert any(
|
|
|
|
|
|
element.text.strip() == 'Apple Bloom' for element in document.find_all('p', {'class': 'browse-list-hint'})
|
|
|
|
|
|
)
|
2018-07-10 17:24:20 +01:00
|
|
|
|
|
2018-07-06 16:16:21 +01:00
|
|
|
|
|
|
|
|
|
|
def test_find_users_by_email_displays_message_if_no_users_found(
|
2018-07-16 15:04:03 +01:00
|
|
|
|
client_request,
|
2018-07-10 17:24:20 +01:00
|
|
|
|
platform_admin_user,
|
|
|
|
|
|
mocker
|
2018-07-06 16:16:21 +01:00
|
|
|
|
):
|
2018-07-16 15:04:03 +01:00
|
|
|
|
client_request.login(platform_admin_user)
|
2018-07-06 16:16:21 +01:00
|
|
|
|
mocker.patch('app.user_api_client.find_users_by_full_or_partial_email', return_value={"data": []}, autospec=True)
|
2018-07-16 15:04:03 +01:00
|
|
|
|
document = client_request.post(
|
|
|
|
|
|
'main.find_users_by_email', _data={"search": "twilight.sparkle"}, _expected_status=200
|
|
|
|
|
|
)
|
2018-07-06 16:16:21 +01:00
|
|
|
|
|
2018-07-16 15:04:03 +01:00
|
|
|
|
assert document.find('p', {'class': 'browse-list-hint'}).text.strip() == 'No users found.'
|
2018-07-10 16:33:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_find_users_by_email_validates_against_empty_search_submission(
|
2018-07-16 15:04:03 +01:00
|
|
|
|
client_request,
|
2018-07-10 17:24:20 +01:00
|
|
|
|
platform_admin_user,
|
|
|
|
|
|
mocker
|
2018-07-10 16:33:13 +01:00
|
|
|
|
):
|
2018-07-16 15:04:03 +01:00
|
|
|
|
client_request.login(platform_admin_user)
|
2019-08-16 11:20:36 +01:00
|
|
|
|
document = client_request.post('main.find_users_by_email', _data={"search": ""}, _expected_status=200)
|
2018-07-10 16:33:13 +01:00
|
|
|
|
|
2020-08-07 10:16:24 +01:00
|
|
|
|
expected_message = "Error: You need to enter full or partial email address to search by."
|
|
|
|
|
|
assert document.find('span', {'class': 'govuk-error-message'}).text.strip() == expected_message
|
2018-07-10 17:24:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_user_information_page_shows_information_about_user(
|
2022-01-04 10:56:25 +00:00
|
|
|
|
client_request,
|
2018-07-10 17:24:20 +01:00
|
|
|
|
platform_admin_user,
|
2019-11-04 11:07:55 +00:00
|
|
|
|
mocker,
|
|
|
|
|
|
fake_uuid,
|
2018-07-10 17:24:20 +01:00
|
|
|
|
):
|
2022-01-04 10:56:25 +00:00
|
|
|
|
client_request.login(platform_admin_user)
|
2019-11-04 11:07:55 +00:00
|
|
|
|
user_service_one = uuid.uuid4()
|
|
|
|
|
|
user_service_two = uuid.uuid4()
|
2018-07-10 17:24:20 +01:00
|
|
|
|
mocker.patch('app.user_api_client.get_user', side_effect=[
|
|
|
|
|
|
platform_admin_user,
|
2019-11-04 11:07:55 +00:00
|
|
|
|
user_json(name="Apple Bloom", services=[user_service_one, user_service_two])
|
2018-07-16 16:34:10 +01:00
|
|
|
|
], autospec=True)
|
|
|
|
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.user_api_client.get_organisations_and_services_for_user',
|
2019-06-12 12:09:26 +01:00
|
|
|
|
return_value={'organisations': [], 'services': [
|
2019-11-04 11:07:55 +00:00
|
|
|
|
{"id": user_service_one, "name": "Fresh Orchard Juice", "restricted": True},
|
|
|
|
|
|
{"id": user_service_two, "name": "Nature Therapy", "restricted": False},
|
2018-07-16 16:34:10 +01:00
|
|
|
|
]},
|
|
|
|
|
|
autospec=True
|
|
|
|
|
|
)
|
2022-01-04 10:56:25 +00:00
|
|
|
|
page = client_request.get('main.user_information', user_id=fake_uuid)
|
2018-07-10 17:24:20 +01:00
|
|
|
|
|
2022-01-04 10:56:25 +00:00
|
|
|
|
assert normalize_spaces(page.select_one('h1').text) == 'Apple Bloom'
|
2018-07-10 17:24:20 +01:00
|
|
|
|
|
2022-01-04 10:56:25 +00:00
|
|
|
|
assert [
|
|
|
|
|
|
normalize_spaces(p.text) for p in page.select('main p')
|
|
|
|
|
|
] == [
|
|
|
|
|
|
'test@gov.uk',
|
|
|
|
|
|
'+447700900986',
|
|
|
|
|
|
'Last logged in just now',
|
|
|
|
|
|
]
|
2018-07-10 17:24:20 +01:00
|
|
|
|
|
2022-01-04 10:56:25 +00:00
|
|
|
|
assert '0 failed login attempts' not in page.text
|
2018-07-10 17:24:20 +01:00
|
|
|
|
|
2022-01-04 10:56:25 +00:00
|
|
|
|
assert [
|
|
|
|
|
|
normalize_spaces(h2.text) for h2 in page.select('main h2')
|
|
|
|
|
|
] == [
|
|
|
|
|
|
'Live services',
|
|
|
|
|
|
'Trial mode services',
|
|
|
|
|
|
'Last login',
|
|
|
|
|
|
]
|
2019-06-12 09:50:11 +01:00
|
|
|
|
|
2022-01-04 10:56:25 +00:00
|
|
|
|
assert [
|
|
|
|
|
|
normalize_spaces(a.text) for a in page.select('main li a')
|
|
|
|
|
|
] == [
|
|
|
|
|
|
'Nature Therapy',
|
|
|
|
|
|
'Fresh Orchard Juice',
|
|
|
|
|
|
]
|
2018-07-10 17:24:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_user_information_page_displays_if_there_are_failed_login_attempts(
|
2022-01-04 10:56:25 +00:00
|
|
|
|
client_request,
|
2018-07-10 17:24:20 +01:00
|
|
|
|
platform_admin_user,
|
2019-11-04 11:07:55 +00:00
|
|
|
|
mocker,
|
|
|
|
|
|
fake_uuid,
|
2018-07-10 17:24:20 +01:00
|
|
|
|
):
|
2022-01-04 10:56:25 +00:00
|
|
|
|
client_request.login(platform_admin_user)
|
2018-07-10 17:24:20 +01:00
|
|
|
|
mocker.patch('app.user_api_client.get_user', side_effect=[
|
|
|
|
|
|
platform_admin_user,
|
2019-05-23 15:27:35 +01:00
|
|
|
|
user_json(name="Apple Bloom", failed_login_count=2)
|
2018-07-10 17:24:20 +01:00
|
|
|
|
], autospec=True)
|
2018-07-16 16:34:10 +01:00
|
|
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.user_api_client.get_organisations_and_services_for_user',
|
2019-11-04 11:07:55 +00:00
|
|
|
|
return_value={'organisations': [], 'services': []},
|
2018-07-16 16:34:10 +01:00
|
|
|
|
autospec=True
|
|
|
|
|
|
)
|
2022-01-04 10:56:25 +00:00
|
|
|
|
page = client_request.get('main.user_information', user_id=fake_uuid)
|
2018-07-10 17:24:20 +01:00
|
|
|
|
|
2022-01-04 10:56:25 +00:00
|
|
|
|
assert normalize_spaces(page.select('main p')[-1].text) == (
|
|
|
|
|
|
'2 failed login attempts'
|
|
|
|
|
|
)
|
2019-05-22 11:38:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
2019-06-05 11:25:27 +01:00
|
|
|
|
def test_user_information_page_shows_archive_link_for_active_users(
|
2021-12-30 16:13:49 +00:00
|
|
|
|
client_request,
|
|
|
|
|
|
platform_admin_user,
|
2019-06-05 11:25:27 +01:00
|
|
|
|
api_user_active,
|
|
|
|
|
|
mock_get_organisations_and_services_for_user,
|
|
|
|
|
|
):
|
2021-12-30 16:13:49 +00:00
|
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
|
page = client_request.get(
|
|
|
|
|
|
'main.user_information', user_id=api_user_active['id']
|
2019-06-05 11:25:27 +01:00
|
|
|
|
)
|
|
|
|
|
|
archive_url = url_for('main.archive_user', user_id=api_user_active['id'])
|
|
|
|
|
|
|
|
|
|
|
|
link = page.find('a', {'href': archive_url})
|
|
|
|
|
|
assert normalize_spaces(link.text) == 'Archive user'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_user_information_page_does_not_show_archive_link_for_inactive_users(
|
|
|
|
|
|
mocker,
|
2022-01-04 10:56:25 +00:00
|
|
|
|
client_request,
|
2019-06-05 11:25:27 +01:00
|
|
|
|
platform_admin_user,
|
|
|
|
|
|
mock_get_organisations_and_services_for_user,
|
|
|
|
|
|
):
|
2022-01-04 10:56:25 +00:00
|
|
|
|
inactive_user_id = uuid.uuid4()
|
|
|
|
|
|
inactive_user = user_json(id_=inactive_user_id, state='inactive')
|
|
|
|
|
|
client_request.login(platform_admin_user)
|
2019-06-05 11:25:27 +01:00
|
|
|
|
mocker.patch('app.user_api_client.get_user', side_effect=[platform_admin_user, inactive_user], autospec=True)
|
|
|
|
|
|
|
2022-01-04 10:56:25 +00:00
|
|
|
|
page = client_request.get(
|
|
|
|
|
|
'main.user_information', user_id=inactive_user_id
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
archive_url = url_for('main.archive_user', user_id=inactive_user_id)
|
2019-06-05 11:25:27 +01:00
|
|
|
|
assert not page.find('a', {'href': archive_url})
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-22 11:38:47 +01:00
|
|
|
|
def test_archive_user_prompts_for_confirmation(
|
2021-12-30 16:13:49 +00:00
|
|
|
|
client_request,
|
|
|
|
|
|
platform_admin_user,
|
2019-05-22 11:38:47 +01:00
|
|
|
|
api_user_active,
|
|
|
|
|
|
mock_get_organisations_and_services_for_user,
|
|
|
|
|
|
):
|
2021-12-30 16:13:49 +00:00
|
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
|
page = client_request.get(
|
|
|
|
|
|
'main.archive_user', user_id=api_user_active['id']
|
2019-05-22 11:38:47 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
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(
|
2021-12-30 16:13:49 +00:00
|
|
|
|
client_request,
|
|
|
|
|
|
platform_admin_user,
|
2019-05-22 11:38:47 +01:00
|
|
|
|
api_user_active,
|
|
|
|
|
|
mocker,
|
2019-05-22 14:05:19 +01:00
|
|
|
|
mock_events,
|
2019-05-22 11:38:47 +01:00
|
|
|
|
):
|
|
|
|
|
|
mock_user_client = mocker.patch('app.user_api_client.post')
|
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
|
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,
|
|
|
|
|
|
),
|
2019-05-22 11:38:47 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
2019-06-05 11:25:27 +01:00
|
|
|
|
mock_user_client.assert_called_once_with('/user/{}/archive'.format(api_user_active['id']), data=None)
|
2019-05-22 14:05:19 +01:00
|
|
|
|
|
|
|
|
|
|
assert mock_events.called
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-21 17:40:47 +01:00
|
|
|
|
def test_archive_user_shows_error_message_if_user_cannot_be_archived(
|
2021-12-30 16:13:49 +00:00
|
|
|
|
client_request,
|
|
|
|
|
|
platform_admin_user,
|
2020-04-21 17:40:47 +01:00
|
|
|
|
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 can’t be removed from a service - check all services have another '
|
|
|
|
|
|
'team member with manage_settings'}
|
|
|
|
|
|
),
|
|
|
|
|
|
message='User can’t be removed from a service - check all services have another team member '
|
|
|
|
|
|
'with manage_settings'
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
|
'main.archive_user',
|
|
|
|
|
|
user_id=api_user_active['id'],
|
|
|
|
|
|
_follow_redirects=True,
|
2020-04-21 17:40:47 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert normalize_spaces(page.find('h1').text) == 'Platform admin user'
|
|
|
|
|
|
assert normalize_spaces(
|
|
|
|
|
|
page.select_one('.banner-dangerous').text
|
|
|
|
|
|
) == 'User can’t be removed from a service - check all services have another team member with manage_settings'
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-22 16:31:59 +01:00
|
|
|
|
def test_archive_user_does_not_create_event_if_user_client_raises_unexpected_exception(
|
2021-12-30 16:13:49 +00:00
|
|
|
|
client_request,
|
|
|
|
|
|
platform_admin_user,
|
2019-05-22 14:05:19 +01:00
|
|
|
|
api_user_active,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
mock_events,
|
|
|
|
|
|
):
|
|
|
|
|
|
with pytest.raises(Exception):
|
2021-12-30 16:13:49 +00:00
|
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
|
client_request.post(
|
|
|
|
|
|
'main.archive_user', user_id=api_user_active.id,
|
2019-05-22 14:05:19 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
2020-04-22 16:31:59 +01:00
|
|
|
|
assert not mock_events.called
|