2019-06-12 12:09:26 +01:00
|
|
|
|
import uuid
|
|
|
|
|
|
from itertools import repeat
|
|
|
|
|
|
|
2018-03-13 09:29:09 +00:00
|
|
|
|
import pytest
|
|
|
|
|
|
from flask import url_for
|
2018-04-25 14:12:58 +01:00
|
|
|
|
|
2019-12-19 11:19:28 +00:00
|
|
|
|
from tests.conftest import SERVICE_ONE_ID, SERVICE_TWO_ID, normalize_spaces
|
2018-03-13 09:29:09 +00:00
|
|
|
|
|
2019-06-12 12:09:26 +01:00
|
|
|
|
OS1, OS2, OS3, S1, S2, S3 = repeat(uuid.uuid4(), 6)
|
|
|
|
|
|
|
2018-03-13 09:29:09 +00:00
|
|
|
|
SAMPLE_DATA = {
|
|
|
|
|
|
'organisations': [
|
|
|
|
|
|
{
|
|
|
|
|
|
'name': 'org_1',
|
|
|
|
|
|
'id': 'o1',
|
2019-06-17 16:09:58 +01:00
|
|
|
|
'count_of_live_services': 1,
|
2018-03-13 09:29:09 +00:00
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
'name': 'org_2',
|
|
|
|
|
|
'id': 'o2',
|
2019-06-17 16:09:58 +01:00
|
|
|
|
'count_of_live_services': 2,
|
2018-03-13 09:29:09 +00:00
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
'name': 'org_3',
|
|
|
|
|
|
'id': 'o3',
|
2019-06-17 16:09:58 +01:00
|
|
|
|
'count_of_live_services': 0,
|
2018-03-13 09:29:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
],
|
2019-06-12 12:09:26 +01:00
|
|
|
|
'services': [
|
|
|
|
|
|
{
|
|
|
|
|
|
'name': 'org_service_1',
|
|
|
|
|
|
'id': OS1,
|
|
|
|
|
|
'restricted': False,
|
|
|
|
|
|
'organisation': 'o1',
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
'name': 'org_service_2',
|
|
|
|
|
|
'id': OS2,
|
|
|
|
|
|
'restricted': False,
|
|
|
|
|
|
'organisation': 'o1',
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
'name': 'org_service_3',
|
|
|
|
|
|
'id': OS3,
|
|
|
|
|
|
'restricted': True,
|
|
|
|
|
|
'organisation': 'o1',
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
'name': 'service_1',
|
|
|
|
|
|
'id': S1,
|
|
|
|
|
|
'restricted': False,
|
|
|
|
|
|
'organisation': None,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
'name': 'service_2',
|
|
|
|
|
|
'id': S2,
|
|
|
|
|
|
'restricted': False,
|
|
|
|
|
|
'organisation': None,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
'name': 'service_3',
|
|
|
|
|
|
'id': S3,
|
|
|
|
|
|
'restricted': True,
|
|
|
|
|
|
'organisation': None,
|
|
|
|
|
|
},
|
2018-03-13 09:29:09 +00:00
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
|
def mock_get_orgs_and_services(mocker):
|
|
|
|
|
|
return mocker.patch(
|
|
|
|
|
|
'app.user_api_client.get_organisations_and_services_for_user',
|
|
|
|
|
|
return_value=SAMPLE_DATA
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_choose_account_should_show_choose_accounts_page(
|
|
|
|
|
|
client_request,
|
2019-06-12 12:09:26 +01:00
|
|
|
|
mock_get_non_empty_organisations_and_services_for_user,
|
2019-06-07 09:58:09 +01:00
|
|
|
|
mock_get_organisation,
|
2018-03-13 09:29:09 +00:00
|
|
|
|
):
|
|
|
|
|
|
resp = client_request.get('main.choose_account')
|
2019-10-22 20:01:30 +01:00
|
|
|
|
page = resp.find('main', {'id': 'main-content'})
|
2018-03-13 09:29:09 +00:00
|
|
|
|
|
2018-03-15 11:02:05 +00:00
|
|
|
|
assert normalize_spaces(page.h1.text) == 'Choose service'
|
2019-06-07 10:07:14 +01:00
|
|
|
|
outer_list_items = page.select('nav ul')[0].select('li')
|
2021-10-26 15:06:43 +01:00
|
|
|
|
headings = page.select('main h2')
|
2019-06-12 12:09:26 +01:00
|
|
|
|
|
2021-03-16 15:34:35 +00:00
|
|
|
|
assert len(outer_list_items) == 8
|
2018-03-13 09:29:09 +00:00
|
|
|
|
|
2021-10-26 15:06:43 +01:00
|
|
|
|
assert normalize_spaces(headings[0].text) == 'Live services'
|
|
|
|
|
|
|
2018-03-13 09:29:09 +00:00
|
|
|
|
# first org
|
2019-06-07 09:58:09 +01:00
|
|
|
|
assert outer_list_items[0].a.text == 'Org 1'
|
2018-03-13 09:29:09 +00:00
|
|
|
|
assert outer_list_items[0].a['href'] == url_for('.organisation_dashboard', org_id='o1')
|
2019-06-07 09:58:09 +01:00
|
|
|
|
assert normalize_spaces(outer_list_items[0].select_one('.browse-list-hint').text) == (
|
2019-06-17 16:09:58 +01:00
|
|
|
|
'1 live service'
|
2019-06-07 09:58:09 +01:00
|
|
|
|
)
|
2018-03-13 09:29:09 +00:00
|
|
|
|
|
|
|
|
|
|
# second org
|
2019-06-07 09:58:09 +01:00
|
|
|
|
assert outer_list_items[1].a.text == 'Org 2'
|
2018-03-13 09:29:09 +00:00
|
|
|
|
assert outer_list_items[1].a['href'] == url_for('.organisation_dashboard', org_id='o2')
|
2019-06-07 09:58:09 +01:00
|
|
|
|
assert normalize_spaces(outer_list_items[1].select_one('.browse-list-hint').text) == (
|
2019-06-17 16:09:58 +01:00
|
|
|
|
'2 live services'
|
2019-06-07 09:58:09 +01:00
|
|
|
|
)
|
2018-03-13 09:29:09 +00:00
|
|
|
|
|
|
|
|
|
|
# third org
|
2019-06-07 09:58:09 +01:00
|
|
|
|
assert outer_list_items[2].a.text == 'Org 3'
|
2018-03-13 09:29:09 +00:00
|
|
|
|
assert outer_list_items[2].a['href'] == url_for('.organisation_dashboard', org_id='o3')
|
2019-06-07 09:58:09 +01:00
|
|
|
|
assert normalize_spaces(outer_list_items[2].select_one('.browse-list-hint').text) == (
|
|
|
|
|
|
'0 live services'
|
|
|
|
|
|
)
|
2018-03-13 09:29:09 +00:00
|
|
|
|
|
2021-03-16 15:34:35 +00:00
|
|
|
|
# live services
|
2019-06-12 12:09:26 +01:00
|
|
|
|
assert outer_list_items[3].a.text == 'Service 1'
|
|
|
|
|
|
assert outer_list_items[3].a['href'] == url_for('.service_dashboard', service_id=SERVICE_TWO_ID)
|
2021-03-16 15:34:35 +00:00
|
|
|
|
assert outer_list_items[4].a.text == 'Service 2'
|
|
|
|
|
|
assert outer_list_items[4].a['href'] == url_for('.service_dashboard', service_id=SERVICE_TWO_ID)
|
|
|
|
|
|
assert outer_list_items[5].a.text == 'service one'
|
|
|
|
|
|
assert outer_list_items[5].a['href'] == url_for('.service_dashboard', service_id='12345')
|
|
|
|
|
|
assert outer_list_items[6].a.text == 'service one (org 2)'
|
|
|
|
|
|
assert outer_list_items[6].a['href'] == url_for('.service_dashboard', service_id='12345')
|
|
|
|
|
|
assert outer_list_items[7].a.text == 'service two (org 2)'
|
|
|
|
|
|
assert outer_list_items[7].a['href'] == url_for('.service_dashboard', service_id='67890')
|
|
|
|
|
|
|
2021-10-26 15:06:43 +01:00
|
|
|
|
assert normalize_spaces(headings[1].text) == 'Trial mode services'
|
|
|
|
|
|
|
2021-03-16 15:34:35 +00:00
|
|
|
|
# trial services
|
2019-06-07 10:07:14 +01:00
|
|
|
|
trial_services_list_items = page.select('nav ul')[1].select('li')
|
2019-06-12 12:09:26 +01:00
|
|
|
|
assert len(trial_services_list_items) == 3
|
|
|
|
|
|
assert trial_services_list_items[0].a.text == 'service three'
|
|
|
|
|
|
assert trial_services_list_items[0].a['href'] == url_for('.service_dashboard', service_id='abcde')
|
|
|
|
|
|
assert trial_services_list_items[1].a.text == 'service three'
|
|
|
|
|
|
assert trial_services_list_items[1].a['href'] == url_for('.service_dashboard', service_id='abcde')
|
|
|
|
|
|
|
2019-06-17 16:09:58 +01:00
|
|
|
|
assert mock_get_organisation.call_args_list == []
|
2018-03-13 09:29:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_choose_account_should_show_choose_accounts_page_if_no_services(
|
|
|
|
|
|
client_request,
|
2019-06-07 09:58:09 +01:00
|
|
|
|
mock_get_orgs_and_services,
|
|
|
|
|
|
mock_get_organisation,
|
|
|
|
|
|
mock_get_organisation_services,
|
2018-03-13 09:29:09 +00:00
|
|
|
|
):
|
|
|
|
|
|
mock_get_orgs_and_services.return_value = {
|
|
|
|
|
|
'organisations': [],
|
2019-06-12 12:09:26 +01:00
|
|
|
|
'services': []
|
2018-03-13 09:29:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
resp = client_request.get('main.choose_account')
|
2019-10-22 20:01:30 +01:00
|
|
|
|
page = resp.find('main', {'id': 'main-content'})
|
2018-03-13 09:29:09 +00:00
|
|
|
|
|
2020-02-17 15:36:46 +00:00
|
|
|
|
links = page.find_all('a')
|
2018-03-13 09:29:09 +00:00
|
|
|
|
assert len(links) == 1
|
|
|
|
|
|
add_service_link = links[0]
|
2018-03-15 11:02:05 +00:00
|
|
|
|
assert normalize_spaces(page.h1.text) == 'Choose service'
|
2019-04-30 11:32:36 +01:00
|
|
|
|
assert normalize_spaces(add_service_link.text) == 'Add a new service'
|
2021-10-26 15:06:43 +01:00
|
|
|
|
assert not page.select('main h2')
|
2018-03-13 09:29:09 +00:00
|
|
|
|
assert add_service_link['href'] == url_for('main.add_service')
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-10-26 15:06:43 +01:00
|
|
|
|
@pytest.mark.parametrize('orgs_and_services, expected_headings', (
|
|
|
|
|
|
({
|
|
|
|
|
|
'organisations': [],
|
|
|
|
|
|
'services': []
|
|
|
|
|
|
}, [
|
|
|
|
|
|
'Platform admin',
|
|
|
|
|
|
]),
|
|
|
|
|
|
(SAMPLE_DATA, [
|
|
|
|
|
|
'Platform admin',
|
|
|
|
|
|
'Live services',
|
|
|
|
|
|
'Trial mode services',
|
|
|
|
|
|
]),
|
|
|
|
|
|
({
|
|
|
|
|
|
'organisations': [],
|
|
|
|
|
|
'services': [{
|
|
|
|
|
|
'name': 'Live service',
|
|
|
|
|
|
'id': OS2,
|
|
|
|
|
|
'restricted': False,
|
|
|
|
|
|
'organisation': None,
|
|
|
|
|
|
}],
|
|
|
|
|
|
}, [
|
|
|
|
|
|
'Platform admin',
|
|
|
|
|
|
'Live services',
|
|
|
|
|
|
]),
|
|
|
|
|
|
({
|
|
|
|
|
|
'organisations': [],
|
|
|
|
|
|
'services': [{
|
|
|
|
|
|
'name': 'Trial service',
|
|
|
|
|
|
'id': OS2,
|
|
|
|
|
|
'restricted': True,
|
|
|
|
|
|
'organisation': None,
|
|
|
|
|
|
}],
|
|
|
|
|
|
}, [
|
|
|
|
|
|
'Platform admin',
|
|
|
|
|
|
'Trial mode services',
|
|
|
|
|
|
]),
|
|
|
|
|
|
))
|
2019-06-04 17:15:31 +01:00
|
|
|
|
def test_choose_account_should_should_organisations_link_for_platform_admin(
|
|
|
|
|
|
client_request,
|
|
|
|
|
|
platform_admin_user,
|
2019-07-08 12:00:40 +01:00
|
|
|
|
mock_get_organisations,
|
2019-06-04 17:15:31 +01:00
|
|
|
|
mock_get_orgs_and_services,
|
|
|
|
|
|
mock_get_organisation_services,
|
2019-07-08 12:00:40 +01:00
|
|
|
|
mock_get_service_and_organisation_counts,
|
2021-10-26 15:06:43 +01:00
|
|
|
|
orgs_and_services,
|
|
|
|
|
|
expected_headings,
|
2019-06-04 17:15:31 +01:00
|
|
|
|
):
|
2021-10-26 15:06:43 +01:00
|
|
|
|
mock_get_orgs_and_services.return_value = orgs_and_services
|
2019-06-04 17:15:31 +01:00
|
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
|
|
|
|
|
|
|
page = client_request.get('main.choose_account')
|
|
|
|
|
|
|
2019-07-08 12:00:40 +01:00
|
|
|
|
first_item = page.select_one('.browse-list-item')
|
|
|
|
|
|
first_link = first_item.select_one('a')
|
|
|
|
|
|
first_hint = first_item.select_one('.browse-list-hint')
|
2019-06-04 17:15:31 +01:00
|
|
|
|
assert first_link.text == 'All organisations'
|
|
|
|
|
|
assert first_link['href'] == url_for('main.organisations')
|
2019-07-08 12:00:40 +01:00
|
|
|
|
assert normalize_spaces(first_hint.text) == '3 organisations, 9,999 live services'
|
2019-06-04 17:15:31 +01:00
|
|
|
|
|
2021-10-26 15:06:43 +01:00
|
|
|
|
assert [
|
|
|
|
|
|
normalize_spaces(h2.text) for h2 in page.select('main h2')
|
|
|
|
|
|
] == expected_headings
|
|
|
|
|
|
|
2019-06-04 17:15:31 +01:00
|
|
|
|
|
2018-03-13 09:29:09 +00:00
|
|
|
|
def test_choose_account_should_show_back_to_service_link(
|
|
|
|
|
|
client_request,
|
2019-06-07 09:58:09 +01:00
|
|
|
|
mock_get_orgs_and_services,
|
|
|
|
|
|
mock_get_organisation,
|
|
|
|
|
|
mock_get_organisation_services,
|
2018-03-13 09:29:09 +00:00
|
|
|
|
):
|
|
|
|
|
|
resp = client_request.get('main.choose_account')
|
|
|
|
|
|
|
2019-10-22 20:01:30 +01:00
|
|
|
|
service_navigation = resp.find('div', {'class': 'navigation-service'})
|
|
|
|
|
|
back_to_service_link = service_navigation.a
|
2018-03-13 09:29:09 +00:00
|
|
|
|
|
|
|
|
|
|
assert back_to_service_link['href'] == url_for('main.show_accounts_or_dashboard')
|
|
|
|
|
|
assert back_to_service_link.text == 'Back to service one'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_choose_account_should_not_show_back_to_service_link_if_no_service_in_session(
|
|
|
|
|
|
client_request,
|
2019-06-07 09:58:09 +01:00
|
|
|
|
mock_get_orgs_and_services,
|
|
|
|
|
|
mock_get_organisation,
|
|
|
|
|
|
mock_get_organisation_services,
|
2018-03-13 09:29:09 +00:00
|
|
|
|
):
|
2022-01-04 15:40:42 +00:00
|
|
|
|
with client_request.session_transaction() as session:
|
2018-03-13 09:29:09 +00:00
|
|
|
|
session['service_id'] = None
|
|
|
|
|
|
page = client_request.get('main.choose_account')
|
|
|
|
|
|
|
|
|
|
|
|
assert len(page.select('.navigation-service a')) == 0
|
2018-05-09 10:48:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_choose_account_should_not_show_back_to_service_link_if_not_signed_in(
|
2022-01-04 15:40:42 +00:00
|
|
|
|
client_request,
|
2018-05-09 10:48:38 +01:00
|
|
|
|
mock_get_service,
|
|
|
|
|
|
):
|
2022-01-04 15:40:42 +00:00
|
|
|
|
client_request.logout()
|
|
|
|
|
|
|
|
|
|
|
|
with client_request.session_transaction() as session:
|
2018-05-09 10:48:38 +01:00
|
|
|
|
session['service_id'] = SERVICE_ONE_ID
|
2022-01-04 15:40:42 +00:00
|
|
|
|
page = client_request.get('main.sign_in')
|
2018-05-09 10:48:38 +01:00
|
|
|
|
|
|
|
|
|
|
assert page.select_one('h1').text == 'Sign in' # We’re not signed in
|
|
|
|
|
|
assert page.select_one('.navigation-service a') is None
|
2019-01-15 17:31:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
2019-05-30 10:56:19 +01:00
|
|
|
|
@pytest.mark.parametrize('active', (
|
|
|
|
|
|
False,
|
|
|
|
|
|
pytest.param(True, marks=pytest.mark.xfail(raises=AssertionError)),
|
|
|
|
|
|
))
|
|
|
|
|
|
def test_choose_account_should_not_show_back_to_service_link_if_service_archived(
|
|
|
|
|
|
client_request,
|
|
|
|
|
|
service_one,
|
|
|
|
|
|
mock_get_orgs_and_services,
|
2019-06-07 09:58:09 +01:00
|
|
|
|
mock_get_organisation,
|
|
|
|
|
|
mock_get_organisation_services,
|
2019-05-30 10:56:19 +01:00
|
|
|
|
active,
|
|
|
|
|
|
):
|
|
|
|
|
|
service_one['active'] = active
|
|
|
|
|
|
with client_request.session_transaction() as session:
|
|
|
|
|
|
session['service_id'] = service_one['id']
|
|
|
|
|
|
page = client_request.get('main.choose_account')
|
|
|
|
|
|
|
|
|
|
|
|
assert normalize_spaces(page.select_one('h1').text) == 'Choose service'
|
|
|
|
|
|
assert page.select_one('.navigation-service a') is None
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-12-19 11:19:28 +00:00
|
|
|
|
def test_should_not_show_back_to_service_if_user_doesnt_belong_to_service(
|
|
|
|
|
|
client_request,
|
|
|
|
|
|
fake_uuid,
|
|
|
|
|
|
mock_get_service,
|
|
|
|
|
|
service_two,
|
|
|
|
|
|
):
|
|
|
|
|
|
mock_get_service.return_value = service_two
|
|
|
|
|
|
expected_page_text = (
|
2019-01-15 17:31:55 +00:00
|
|
|
|
# Page has no ‘back to’ link
|
2019-12-04 12:01:26 +00:00
|
|
|
|
'You’re not allowed to see this page '
|
2019-11-12 17:06:06 +00:00
|
|
|
|
'To check your permissions, speak to a member of your team who can manage settings, team and usage.'
|
2019-12-19 11:19:28 +00:00
|
|
|
|
)
|
|
|
|
|
|
page = client_request.get(
|
|
|
|
|
|
'main.view_template',
|
|
|
|
|
|
service_id=mock_get_service.return_value['id'],
|
|
|
|
|
|
template_id=fake_uuid,
|
|
|
|
|
|
_expected_status=403,
|
|
|
|
|
|
_test_page_title=False,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert normalize_spaces(
|
|
|
|
|
|
page.select_one('header + .govuk-width-container').text
|
|
|
|
|
|
).startswith(
|
|
|
|
|
|
normalize_spaces(expected_page_text)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_show_back_to_service_if_user_belongs_to_service(
|
2019-01-15 17:31:55 +00:00
|
|
|
|
client_request,
|
|
|
|
|
|
fake_uuid,
|
|
|
|
|
|
mock_get_service,
|
|
|
|
|
|
mock_get_service_template,
|
2019-12-19 11:19:28 +00:00
|
|
|
|
service_one,
|
2019-01-15 17:31:55 +00:00
|
|
|
|
):
|
2019-12-19 11:19:28 +00:00
|
|
|
|
mock_get_service.return_value = service_one
|
|
|
|
|
|
expected_page_text = (
|
|
|
|
|
|
'Test Service Switch service '
|
|
|
|
|
|
''
|
|
|
|
|
|
'Dashboard '
|
|
|
|
|
|
'Templates '
|
2020-03-12 10:37:59 +00:00
|
|
|
|
'Uploads '
|
2019-12-19 11:19:28 +00:00
|
|
|
|
'Team members'
|
|
|
|
|
|
)
|
2019-01-15 17:31:55 +00:00
|
|
|
|
|
|
|
|
|
|
page = client_request.get(
|
|
|
|
|
|
'main.view_template',
|
|
|
|
|
|
service_id=mock_get_service.return_value['id'],
|
|
|
|
|
|
template_id=fake_uuid,
|
2019-04-30 16:32:20 +01:00
|
|
|
|
_test_page_title=False,
|
2019-01-15 17:31:55 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert normalize_spaces(
|
2019-10-22 20:01:30 +01:00
|
|
|
|
page.select_one('header + .govuk-width-container').text
|
2019-01-15 17:31:55 +00:00
|
|
|
|
).startswith(
|
2019-12-19 11:19:28 +00:00
|
|
|
|
normalize_spaces(expected_page_text)
|
2019-01-15 17:31:55 +00:00
|
|
|
|
)
|