From d2649aebc8daa0646e9372094dfc86e72c9ff069 Mon Sep 17 00:00:00 2001 From: Jenny Duckett Date: Fri, 2 Dec 2016 17:40:12 +0000 Subject: [PATCH] Add include_from_test_key parameter to /service We want to be able to toggle the numbers on the platform admin page between including and excluding notifications sent using test keys, so that we can see both real use of the platform and all load on it. This parameter defaults to True, which is the existing behaviour. --- app/service/rest.py | 7 ++++--- tests/app/service/test_rest.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/app/service/rest.py b/app/service/rest.py index 75c68fe1f..1c1e9b77c 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -62,11 +62,12 @@ def get_services(): only_active = request.args.get('only_active') == 'True' detailed = request.args.get('detailed') == 'True' user_id = request.args.get('user_id', None) + include_from_test_key = request.args.get('include_from_test_key', 'True') != 'False' if user_id: services = dao_fetch_all_services_by_user(user_id, only_active) elif detailed: - return jsonify(data=get_detailed_services(only_active)) + return jsonify(data=get_detailed_services(only_active, include_from_test_key=include_from_test_key)) else: services = dao_fetch_all_services(only_active) data = service_schema.dump(services, many=True).data @@ -268,9 +269,9 @@ def get_detailed_service(service_id, today_only=False): return detailed_service_schema.dump(service).data -def get_detailed_services(only_active=False): +def get_detailed_services(only_active=False, include_from_test_key=True): services = {service.id: service for service in dao_fetch_all_services(only_active)} - stats = dao_fetch_todays_stats_for_all_services() + stats = dao_fetch_todays_stats_for_all_services(include_from_test_key=include_from_test_key) for service_id, rows in itertools.groupby(stats, lambda x: x.service_id): services[service_id].statistics = statistics.format_statistics(rows) diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 3b3bdc744..3641982d1 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -16,7 +16,7 @@ from tests.app.conftest import ( sample_user as create_sample_user, sample_notification as create_sample_notification, sample_notification_with_job) -from app.models import KEY_TYPE_TEST +from app.models import KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST def test_get_service_list(notify_api, service_factory): @@ -1251,7 +1251,8 @@ def test_get_weekly_notification_stats(notify_api, notify_db, notify_db_session) def test_get_services_with_detailed_flag(notify_api, notify_db, notify_db_session): notifications = [ create_sample_notification(notify_db, notify_db_session), - create_sample_notification(notify_db, notify_db_session) + create_sample_notification(notify_db, notify_db_session), + create_sample_notification(notify_db, notify_db_session, key_type=KEY_TYPE_TEST) ] with notify_api.test_request_context(), notify_api.test_client() as client: resp = client.get( @@ -1264,6 +1265,29 @@ def test_get_services_with_detailed_flag(notify_api, notify_db, notify_db_sessio assert len(data) == 1 assert data[0]['name'] == 'Sample service' assert data[0]['id'] == str(notifications[0].service_id) + assert data[0]['statistics'] == { + 'email': {'delivered': 0, 'failed': 0, 'requested': 0}, + 'sms': {'delivered': 0, 'failed': 0, 'requested': 3} + } + + +def test_get_services_with_detailed_flag_excluding_from_test_key(notify_api, notify_db, notify_db_session): + notifications = [ + create_sample_notification(notify_db, notify_db_session, key_type=KEY_TYPE_NORMAL), + create_sample_notification(notify_db, notify_db_session, key_type=KEY_TYPE_TEAM), + create_sample_notification(notify_db, notify_db_session, key_type=KEY_TYPE_TEST), + create_sample_notification(notify_db, notify_db_session, key_type=KEY_TYPE_TEST), + create_sample_notification(notify_db, notify_db_session, key_type=KEY_TYPE_TEST) + ] + with notify_api.test_request_context(), notify_api.test_client() as client: + resp = client.get( + '/service?detailed=True&include_from_test_key=False', + headers=[create_authorization_header()] + ) + + assert resp.status_code == 200 + data = json.loads(resp.get_data(as_text=True))['data'] + assert len(data) == 1 assert data[0]['statistics'] == { 'email': {'delivered': 0, 'failed': 0, 'requested': 0}, 'sms': {'delivered': 0, 'failed': 0, 'requested': 2}