- Showing stats for today
- {% if include_from_test_key %}
- Including test keys (change)
- {% else %}
- Excluding test keys (change)
- {% endif %}
-
+
From 21582cadb38a1b4d624db961a1a4fd24c6940df0 Mon Sep 17 00:00:00 2001
From: Rebecca Law
Date: Tue, 3 Jan 2017 16:14:25 +0000
Subject: [PATCH 4/5] Get the BooleanField working in the form.
---
app/main/forms.py | 5 ++-
app/main/views/platform_admin.py | 21 ++++------
tests/app/main/views/test_platform_admin.py | 44 ++++++++-------------
3 files changed, 26 insertions(+), 44 deletions(-)
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]
From 8b89b7f9112ad978a683107880866a5acfb987a1 Mon Sep 17 00:00:00 2001
From: Rebecca Law
Date: Wed, 4 Jan 2017 13:02:01 +0000
Subject: [PATCH 5/5] Remove print statements.
---
app/notify_client/service_api_client.py | 1 -
tests/app/main/views/test_platform_admin.py | 1 -
2 files changed, 2 deletions(-)
diff --git a/app/notify_client/service_api_client.py b/app/notify_client/service_api_client.py
index a4f0ef9d2..e974890db 100644
--- a/app/notify_client/service_api_client.py
+++ b/app/notify_client/service_api_client.py
@@ -60,7 +60,6 @@ class ServiceAPIClient(NotifyAdminAPIClient):
"""
Retrieve a list of services.
"""
- print(params_dict)
return self.get('/service', params=params_dict)
def get_active_services(self, params_dict=None):
diff --git a/tests/app/main/views/test_platform_admin.py b/tests/app/main/views/test_platform_admin.py
index e7654fcb3..0ac1aafea 100644
--- a/tests/app/main/views/test_platform_admin.py
+++ b/tests/app/main/views/test_platform_admin.py
@@ -99,7 +99,6 @@ 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=include_from_test_key))