Merge pull request #687 from alphagov/optionally-get-test-notifications

Optionally get notifications created with a test key
This commit is contained in:
Chris Hill-Scott
2016-09-26 08:40:54 +01:00
committed by GitHub
5 changed files with 55 additions and 28 deletions

View File

@@ -228,14 +228,17 @@ def get_notifications(filter_dict=None):
@statsd(namespace="dao")
def get_notifications_for_service(service_id,
filter_dict=None,
page=1,
page_size=None,
limit_days=None,
key_type=None,
personalisation=False,
include_jobs=False):
def get_notifications_for_service(
service_id,
filter_dict=None,
page=1,
page_size=None,
limit_days=None,
key_type=None,
personalisation=False,
include_jobs=False,
include_from_test_key=False
):
if page_size is None:
page_size = current_app.config['PAGE_SIZE']
@@ -250,7 +253,7 @@ def get_notifications_for_service(service_id,
if key_type is not None:
filters.append(Notification.key_type == key_type)
else:
elif not include_from_test_key:
filters.append(Notification.key_type != KEY_TYPE_TEST)
query = Notification.query.filter(*filters)

View File

@@ -422,6 +422,7 @@ class NotificationsFilterSchema(ma.Schema):
page_size = fields.Int(required=False)
limit_days = fields.Int(required=False)
include_jobs = fields.Boolean(required=False)
include_from_test_key = fields.Boolean(required=False)
@pre_load
def handle_multidict(self, in_data):

View File

@@ -214,6 +214,7 @@ def get_all_notifications_for_service(service_id):
page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE')
limit_days = data.get('limit_days')
include_jobs = data.get('include_jobs', True)
include_from_test_key = data.get('include_from_test_key', False)
pagination = notifications_dao.get_notifications_for_service(
service_id,
@@ -221,7 +222,9 @@ def get_all_notifications_for_service(service_id):
page=page,
page_size=page_size,
limit_days=limit_days,
include_jobs=include_jobs)
include_jobs=include_jobs,
include_from_test_key=include_from_test_key
)
kwargs = request.args.to_dict()
kwargs['service_id'] = service_id
return jsonify(

View File

@@ -1000,11 +1000,15 @@ def test_get_notifications_created_by_api_or_csv_are_returned_correctly_excludin
all_notifications = Notification.query.all()
assert len(all_notifications) == 4
# returns all API derived notifications
# returns all real API derived notifications
all_notifications = get_notifications_for_service(sample_service.id).items
assert len(all_notifications) == 2
# all notifications including jobs
# returns all API derived notifications, including those created with test key
all_notifications = get_notifications_for_service(sample_service.id, include_from_test_key=True).items
assert len(all_notifications) == 3
# all real notifications including jobs
all_notifications = get_notifications_for_service(sample_service.id, limit_days=1, include_jobs=True).items
assert len(all_notifications) == 3

View File

@@ -16,6 +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
def test_get_service_list(notify_api, service_factory):
@@ -1037,26 +1038,41 @@ def test_get_all_notifications_for_service_in_order(notify_api, notify_db, notif
assert response.status_code == 200
@pytest.mark.parametrize(
'include_from_test_key, expected_count_of_notifications',
[
(False, 2),
(True, 3)
]
)
def test_get_all_notifications_for_service_including_ones_made_by_jobs(
notify_api,
notify_db,
notify_db_session,
sample_service):
with notify_api.test_request_context(), notify_api.test_client() as client:
with_job = sample_notification_with_job(notify_db, notify_db_session, service=sample_service)
without_job = create_sample_notification(notify_db, notify_db_session, service=sample_service)
client,
notify_db,
notify_db_session,
sample_service,
include_from_test_key,
expected_count_of_notifications
):
with_job = sample_notification_with_job(notify_db, notify_db_session, service=sample_service)
without_job = create_sample_notification(notify_db, notify_db_session, service=sample_service)
from_test_api_key = create_sample_notification(
notify_db, notify_db_session, service=sample_service, key_type=KEY_TYPE_TEST
)
auth_header = create_authorization_header()
auth_header = create_authorization_header()
response = client.get(
path='/service/{}/notifications'.format(sample_service.id),
headers=[auth_header])
response = client.get(
path='/service/{}/notifications?include_from_test_key={}'.format(
sample_service.id, include_from_test_key
),
headers=[auth_header]
)
resp = json.loads(response.get_data(as_text=True))
assert len(resp['notifications']) == 2
assert resp['notifications'][0]['to'] == with_job.to
assert resp['notifications'][1]['to'] == without_job.to
assert response.status_code == 200
resp = json.loads(response.get_data(as_text=True))
assert len(resp['notifications']) == expected_count_of_notifications
assert resp['notifications'][0]['to'] == with_job.to
assert resp['notifications'][1]['to'] == without_job.to
assert response.status_code == 200
def test_get_only_api_created_notifications_for_service(