mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-04 19:08:26 -04:00
add tests for choose_accounts
This commit is contained in:
@@ -1,40 +1,104 @@
|
|||||||
|
import pytest
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from flask import url_for
|
from flask import url_for
|
||||||
|
from tests.conftest import normalize_spaces
|
||||||
|
|
||||||
|
|
||||||
def test_should_show_choose_accounts_page(
|
SAMPLE_DATA = {
|
||||||
logged_in_client,
|
'organisations': [
|
||||||
mock_login,
|
{
|
||||||
mock_get_user,
|
'name': 'org_1',
|
||||||
api_user_active,
|
'id': 'o1',
|
||||||
mock_get_services,
|
'services': [
|
||||||
|
{'name': 'org_service_1', 'id': 'os1'},
|
||||||
|
{'name': 'org_service_2', 'id': 'os2'},
|
||||||
|
{'name': 'org_service_3', 'id': 'os3'},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'name': 'org_2',
|
||||||
|
'id': 'o2',
|
||||||
|
'services': [
|
||||||
|
{'name': 'org_service_4', 'id': 'os4'},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'name': 'org_3',
|
||||||
|
'id': 'o3',
|
||||||
|
'services': []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'services_without_organisations': [
|
||||||
|
{'name': 'service_1', 'id': 's1'},
|
||||||
|
{'name': 'service_2', 'id': 's2'},
|
||||||
|
{'name': 'service_3', 'id': 's3'},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@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,
|
||||||
|
mock_get_orgs_and_services
|
||||||
):
|
):
|
||||||
response = logged_in_client.get(url_for('main.choose_account'))
|
resp = client_request.get('main.choose_account')
|
||||||
|
page = resp.find('div', {'id': 'content'}).main
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert normalize_spaces(page.h1.text) == 'Choose account'
|
||||||
resp_data = response.get_data(as_text=True)
|
|
||||||
assert 'Choose service' in resp_data
|
|
||||||
services = mock_get_services.side_effect()
|
|
||||||
assert mock_get_services.called
|
|
||||||
assert services['data'][0]['name'] in resp_data
|
|
||||||
assert services['data'][1]['name'] in resp_data
|
|
||||||
|
|
||||||
|
|
||||||
def test_should_show_choose_accounts_page_if_no_services(
|
def test_choose_account_should_show_choose_accounts_page_if_no_services(
|
||||||
logged_in_client,
|
client_request,
|
||||||
mock_login,
|
mock_get_orgs_and_services
|
||||||
api_user_active,
|
|
||||||
):
|
):
|
||||||
# if users last service has been archived there'll be no services
|
mock_get_orgs_and_services.return_value = {
|
||||||
# mock_login already patches get_services to return no data
|
'organisations': [],
|
||||||
response = logged_in_client.get(url_for('main.choose_account'))
|
'services_without_organisations': []
|
||||||
assert response.status_code == 200
|
}
|
||||||
resp_data = response.get_data(as_text=True)
|
resp = client_request.get('main.choose_account')
|
||||||
assert 'Choose service' in resp_data
|
page = resp.find('div', {'id': 'content'}).main
|
||||||
assert 'Add a new service' in resp_data
|
|
||||||
|
links = page.findAll('a')
|
||||||
|
assert len(links) == 1
|
||||||
|
add_service_link = links[0]
|
||||||
|
assert normalize_spaces(page.h1.text) == 'Choose account'
|
||||||
|
assert normalize_spaces(add_service_link.text) == 'Add a new service…'
|
||||||
|
assert add_service_link['href'] == url_for('main.add_service')
|
||||||
|
|
||||||
|
|
||||||
def test_redirect_if_only_one_service(
|
def test_choose_account_should_show_back_to_service_link(
|
||||||
|
client_request,
|
||||||
|
mock_get_orgs_and_services
|
||||||
|
):
|
||||||
|
resp = client_request.get('main.choose_account')
|
||||||
|
|
||||||
|
page = resp.find('div', {'id': 'content'})
|
||||||
|
back_to_service_link = page.find('div', {'class': 'navigation-service'}).a
|
||||||
|
|
||||||
|
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,
|
||||||
|
client_request,
|
||||||
|
mock_get_orgs_and_services
|
||||||
|
):
|
||||||
|
with client.session_transaction() as session:
|
||||||
|
session['service_id'] = None
|
||||||
|
page = client_request.get('main.choose_account')
|
||||||
|
|
||||||
|
assert len(page.select('.navigation-service a')) == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_show_accounts_or_dashboard_redirects_if_only_one_service(
|
||||||
logged_in_client,
|
logged_in_client,
|
||||||
mock_login,
|
mock_login,
|
||||||
api_user_active,
|
api_user_active,
|
||||||
@@ -47,7 +111,7 @@ def test_redirect_if_only_one_service(
|
|||||||
assert response.location == url_for('main.service_dashboard', service_id=service['id'], _external=True)
|
assert response.location == url_for('main.service_dashboard', service_id=service['id'], _external=True)
|
||||||
|
|
||||||
|
|
||||||
def test_redirect_if_multiple_services(
|
def test_show_accounts_or_dashboard_redirects_if_multiple_services(
|
||||||
logged_in_client,
|
logged_in_client,
|
||||||
mock_login,
|
mock_login,
|
||||||
api_user_active,
|
api_user_active,
|
||||||
@@ -58,12 +122,10 @@ def test_redirect_if_multiple_services(
|
|||||||
assert response.location == url_for('main.choose_account', _external=True)
|
assert response.location == url_for('main.choose_account', _external=True)
|
||||||
|
|
||||||
|
|
||||||
def test_redirect_if_service_in_session(
|
def test_show_accounts_or_dashboard_redirects_if_service_in_session(
|
||||||
logged_in_client,
|
logged_in_client,
|
||||||
mock_login,
|
mock_login,
|
||||||
api_user_active,
|
api_user_active,
|
||||||
mock_get_services,
|
|
||||||
mock_get_service,
|
|
||||||
):
|
):
|
||||||
with logged_in_client.session_transaction() as session:
|
with logged_in_client.session_transaction() as session:
|
||||||
session['service_id'] = '147ad62a-2951-4fa1-9ca0-093cd1a52c52'
|
session['service_id'] = '147ad62a-2951-4fa1-9ca0-093cd1a52c52'
|
||||||
@@ -77,36 +139,10 @@ def test_redirect_if_service_in_session(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_should_redirect_if_not_logged_in(
|
def test_show_accounts_or_dashboard_redirects_if_not_logged_in(
|
||||||
logged_in_client,
|
logged_in_client,
|
||||||
app_
|
app_
|
||||||
):
|
):
|
||||||
response = logged_in_client.get(url_for('main.show_accounts_or_dashboard'))
|
response = logged_in_client.get(url_for('main.show_accounts_or_dashboard'))
|
||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
assert url_for('main.index', _external=True) in response.location
|
assert url_for('main.index', _external=True) in response.location
|
||||||
|
|
||||||
|
|
||||||
def test_should_show_back_to_service_link(
|
|
||||||
logged_in_client
|
|
||||||
):
|
|
||||||
response = logged_in_client.get(url_for('main.choose_account'))
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
|
||||||
assert page.select('.navigation-service a')[0]['href'] == (
|
|
||||||
url_for('main.show_accounts_or_dashboard')
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_should_not_show_back_to_service_link_if_no_service_in_session(
|
|
||||||
client,
|
|
||||||
api_user_active,
|
|
||||||
mock_get_user,
|
|
||||||
mock_get_services_with_no_services,
|
|
||||||
):
|
|
||||||
client.login(api_user_active)
|
|
||||||
response = client.get(url_for('main.choose_account'))
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
|
||||||
assert len(page.select('.navigation-service a')) == 0
|
|
||||||
|
|||||||
Reference in New Issue
Block a user