From a94fa5472d21aab8916a80087363eda1a5a0cc73 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Tue, 13 Mar 2018 09:02:09 +0000 Subject: [PATCH] platform admins can go back to any service --- app/main/views/choose_account.py | 4 +- tests/app/main/views/test_choose_accounts.py | 44 +++++++++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/app/main/views/choose_account.py b/app/main/views/choose_account.py index 80fb4d143..9e846670e 100644 --- a/app/main/views/choose_account.py +++ b/app/main/views/choose_account.py @@ -26,11 +26,11 @@ def show_accounts_or_dashboard(): return redirect(url_for('.index')) service_id = session.get('service_id') - if any(service_id == x for x in current_user.services): + if service_id and (service_id in current_user.services or current_user.platform_admin): return redirect(url_for('.service_dashboard', service_id=service_id)) organisation_id = session.get('organisation_id') - if any(organisation_id == x for x in current_user.organisations): + if organisation_id and (organisation_id in current_user.organisations or current_user.platform_admin): return redirect(url_for('.organisation_dashboard', org_id=organisation_id)) if len(current_user.services) == 1 and not current_user.organisations: diff --git a/tests/app/main/views/test_choose_accounts.py b/tests/app/main/views/test_choose_accounts.py index 781b4456e..c6b645457 100644 --- a/tests/app/main/views/test_choose_accounts.py +++ b/tests/app/main/views/test_choose_accounts.py @@ -37,11 +37,12 @@ SAMPLE_DATA = { } -def user_with_orgs_and_services(num_orgs, num_services): +def user_with_orgs_and_services(num_orgs, num_services, platform_admin=False): return User(user_json( name='leo', organisations=['org{}'.format(i) for i in range(1, num_orgs + 1)], - services=['service{}'.format(i) for i in range(1, num_services + 1)] + services=['service{}'.format(i) for i in range(1, num_services + 1)], + platform_admin=platform_admin )) @@ -231,3 +232,42 @@ def test_show_accounts_or_dashboard_redirects_if_not_logged_in( response = client.get(url_for('main.show_accounts_or_dashboard')) assert response.status_code == 302 assert response.location == url_for('main.index', _external=True) + + +def test_show_accounts_or_dashboard_redirects_to_service_dashboard_if_platform_admin( + client, + mocker, + mock_get_service +): + client.login(user_with_orgs_and_services(num_orgs=1, num_services=1, platform_admin=True), mocker=mocker) + with client.session_transaction() as session: + session['service_id'] = 'service2' + session['organisation_id'] = None + + response = client.get(url_for('.show_accounts_or_dashboard')) + + assert response.status_code == 302 + assert response.location == url_for( + 'main.service_dashboard', + service_id='service2', + _external=True + ) + + +def test_show_accounts_or_dashboard_redirects_to_org_dashboard_if_platform_admin( + client, + mocker +): + client.login(user_with_orgs_and_services(num_orgs=1, num_services=1, platform_admin=True), mocker=mocker) + with client.session_transaction() as session: + session['service_id'] = None + session['organisation_id'] = 'org2' + + response = client.get(url_for('.show_accounts_or_dashboard')) + + assert response.status_code == 302 + assert response.location == url_for( + 'main.organisation_dashboard', + org_id='org2', + _external=True + )