diff --git a/app/main/forms.py b/app/main/forms.py index fe83ae0b4..45b40abb5 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -517,7 +517,8 @@ class Whitelist(Form): label="Mobile numbers" ) + class DateFilterForm(Form): start_date = DateField("Start Date", [validators.optional()]) - end_date = StringField("End Date", [validators.optional()]) - include_from_test_key = BooleanField("Include test keys", default='checked') + end_date = DateField("End Date", [validators.optional()]) + include_from_test_key = BooleanField("Include test keys", default="checked", false_values={"N"}) diff --git a/app/main/views/platform_admin.py b/app/main/views/platform_admin.py index a91a111a5..462320c26 100644 --- a/app/main/views/platform_admin.py +++ b/app/main/views/platform_admin.py @@ -19,26 +19,19 @@ from app.statistics_utils import get_formatted_percentage @user_has_permissions(admin_override=True) def platform_admin(): form = DateFilterForm(request.args) - include_from_test_key = form.include_from_test_key.data - start_date = form.start_date.data - end_date = form.end_date.data - # specifically DO get inactive services - api_args = {'detailed': True} - if not include_from_test_key: - api_args['include_from_test_key'] = False + api_args = {'detailed': True, # specifically DO get inactive services + 'include_from_test_key': form.include_from_test_key.data + } - if start_date: - # For now the start and end date are only set as query params. The previous commit added a form - # but I could get the validation right, not did the page look good. - # This is just an intermediate pass at returning the data. - api_args['start_date'] = start_date - api_args['end_date'] = end_date or datetime.utcnow().date() + if form.start_date.data: + api_args['start_date'] = form.start_date.data + api_args['end_date'] = form.end_date.data or datetime.utcnow().date() services = service_api_client.get_services(api_args)['data'] return render_template( 'views/platform-admin.html', - include_from_test_key=include_from_test_key, + include_from_test_key=form.include_from_test_key.data, form=form, **get_statistics(sorted( services, diff --git a/tests/app/main/views/test_platform_admin.py b/tests/app/main/views/test_platform_admin.py index efa700b30..e7654fcb3 100644 --- a/tests/app/main/views/test_platform_admin.py +++ b/tests/app/main/views/test_platform_admin.py @@ -1,3 +1,5 @@ +import datetime + from flask import url_for import pytest from bs4 import BeautifulSoup @@ -55,7 +57,7 @@ def test_should_show_research_and_restricted_mode( response = client.get(url_for('main.platform_admin')) assert response.status_code == 200 - mock_get_detailed_services.assert_called_once_with({'detailed': True}) + mock_get_detailed_services.assert_called_once_with({'detailed': True, 'include_from_test_key': True}) page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') # get first column in second row, which contains flags as text. table_body = page.find_all('table')[table_index].find_all('tbody')[0] @@ -78,20 +80,17 @@ def test_should_render_platform_admin_page( assert response.status_code == 200 resp_data = response.get_data(as_text=True) assert 'Platform admin' in resp_data - assert 'Showing stats for today' in resp_data assert 'Live services' in resp_data assert 'Trial mode services' in resp_data - mock_get_detailed_services.assert_called_once_with({'detailed': True}) + mock_get_detailed_services.assert_called_once_with({'detailed': True, 'include_from_test_key': True}) -@pytest.mark.parametrize('include_from_test_key, expected_text, unexpected_text, api_args', [ - (True, 'Including test keys', 'Excluding test keys', {'detailed': True}), - (False, 'Excluding test keys', 'Including test keys', {'detailed': True, 'include_from_test_key': False}) +@pytest.mark.parametrize('include_from_test_key, api_args', [ + ("Y", {'detailed': True, 'include_from_test_key': True}), + ("N", {'detailed': True, 'include_from_test_key': False}) ]) def test_platform_admin_toggle_including_from_test_key( include_from_test_key, - expected_text, - unexpected_text, api_args, app_, platform_admin_user, @@ -100,24 +99,12 @@ def test_platform_admin_toggle_including_from_test_key( ): with app_.test_request_context(): with app_.test_client() as client: + print(include_from_test_key) mock_get_user(mocker, user=platform_admin_user) client.login(platform_admin_user) - response = client.get(url_for('main.platform_admin', include_from_test_key=str(include_from_test_key))) + response = client.get(url_for('main.platform_admin', include_from_test_key=include_from_test_key)) assert response.status_code == 200 - resp_data = response.get_data(as_text=True) - assert expected_text in resp_data - assert unexpected_text not in resp_data - - page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') - change_link = page.find('a', text='change') - assert change_link['href'] - query_param = 'include_from_test_key=False' - if include_from_test_key: - assert query_param in change_link['href'] - else: - assert query_param not in change_link['href'] - mock_get_detailed_services.assert_called_once_with(api_args) @@ -131,16 +118,17 @@ def test_platform_admin_with_date_filter( with app_.test_client() as client: mock_get_user(mocker, user=platform_admin_user) client.login(platform_admin_user) - response = client.get(url_for('main.platform_admin', start_date='2016-12-20', end_date='2012-12-28')) + response = client.get(url_for('main.platform_admin', start_date='2016-12-20', end_date='2016-12-28')) assert response.status_code == 200 resp_data = response.get_data(as_text=True) assert 'Platform admin' in resp_data - assert 'Showing stats for today' in resp_data assert 'Live services' in resp_data assert 'Trial mode services' in resp_data - mock_get_detailed_services.assert_called_once_with({'detailed': True, - 'start_date': '2016-12-20', 'end_date': '2012-12-28'}) + mock_get_detailed_services.assert_called_once_with({'include_from_test_key': False, + 'start_date': datetime.date(2016, 12, 20), + 'end_date': datetime.date(2016, 12, 28), + 'detailed': True}) def test_create_global_stats_sets_failure_rates(fake_uuid): @@ -254,7 +242,7 @@ def test_should_show_email_and_sms_stats_for_all_service_types( response = client.get(url_for('main.platform_admin')) assert response.status_code == 200 - mock_get_detailed_services.assert_called_once_with({'detailed': True}) + mock_get_detailed_services.assert_called_once_with({'detailed': True, 'include_from_test_key': True}) page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') table_body = page.find_all('table')[table_index].find_all('tbody')[0] @@ -300,7 +288,7 @@ def test_should_show_archived_services_last( response = client.get(url_for('main.platform_admin')) assert response.status_code == 200 - mock_get_detailed_services.assert_called_once_with({'detailed': True}) + mock_get_detailed_services.assert_called_once_with({'detailed': True, 'include_from_test_key': True}) page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') table_body = page.find_all('table')[table_index].find_all('tbody')[0]