From d4d733f9971df17614a145f6c0e6451d95b11d1e Mon Sep 17 00:00:00 2001 From: Imdad Ahad Date: Wed, 7 Dec 2016 14:08:22 +0000 Subject: [PATCH 1/2] Add dao method to get notification by reference with tests --- app/dao/notifications_dao.py | 9 +++++++++ tests/app/conftest.py | 6 ++++-- tests/app/dao/test_notification_dao.py | 13 ++++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 802800068..c3e3e4d33 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -248,6 +248,15 @@ def get_notification_by_id(notification_id): return Notification.query.filter_by(id=notification_id).first() +@statsd(namespace="dao") +def get_notification_by_reference(service_id, reference, key_type): + filter_dict = {'service_id': service_id, 'client_reference': reference} + if key_type: + filter_dict['key_type'] = key_type + + return Notification.query.filter_by(**filter_dict).options(joinedload('template_history')).one() + + def get_notifications(filter_dict=None): return _filter_query(Notification.query, filter_dict=filter_dict) diff --git a/tests/app/conftest.py b/tests/app/conftest.py index a21584cd3..c6cc1a024 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -411,7 +411,8 @@ def sample_notification(notify_db, personalisation=None, api_key_id=None, key_type=KEY_TYPE_NORMAL, - sent_by=None): + sent_by=None, + client_reference=None): if created_at is None: created_at = datetime.utcnow() if service is None: @@ -445,7 +446,8 @@ def sample_notification(notify_db, 'api_key_id': api_key_id, 'key_type': key_type, 'sent_by': sent_by, - 'updated_at': created_at if status in NOTIFICATION_STATUS_TYPES_COMPLETED else None + 'updated_at': created_at if status in NOTIFICATION_STATUS_TYPES_COMPLETED else None, + 'client_reference': client_reference } if job_row_number: data['job_row_number'] = job_row_number diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index fe768631f..77db3456e 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -30,6 +30,7 @@ from app.dao.notifications_dao import ( dao_update_notification, delete_notifications_created_more_than_a_week_ago, get_notification_by_id, + get_notification_by_reference, get_notification_for_job, get_notification_billable_unit_count_per_month, get_notification_with_personalisation, @@ -616,7 +617,7 @@ def test_save_notification_with_no_job(sample_template, mmg_provider): assert notification_from_db.status == 'created' -def test_get_notification(sample_notification): +def test_get_notification_by_id(sample_notification): notification_from_db = get_notification_with_personalisation( sample_notification.service.id, sample_notification.id, @@ -625,6 +626,16 @@ def test_get_notification(sample_notification): assert sample_notification == notification_from_db +def test_get_notification_by_reference(notify_db, notify_db_session): + notification = sample_notification(notify_db, notify_db_session, client_reference="some-client-ref") + notification_from_db = get_notification_by_reference( + notification.service.id, + notification.client_reference, + key_type=None + ) + assert notification == notification_from_db + + def test_save_notification_no_job_id(sample_template, mmg_provider): assert Notification.query.count() == 0 data = _notification_json(sample_template) From 620a90fd7c45f494fe0425f01d7c4e687e7b6a0a Mon Sep 17 00:00:00 2001 From: Imdad Ahad Date: Wed, 7 Dec 2016 14:09:56 +0000 Subject: [PATCH 2/2] Make get_notifications return single notification by reference --- app/v2/notifications/get_notifications.py | 13 ++++++- app/v2/notifications/notification_schemas.py | 1 + .../notifications/test_get_notifications.py | 34 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/app/v2/notifications/get_notifications.py b/app/v2/notifications/get_notifications.py index 1e664fb75..a7ce90665 100644 --- a/app/v2/notifications/get_notifications.py +++ b/app/v2/notifications/get_notifications.py @@ -18,12 +18,23 @@ def get_notification_by_id(id): @notification_blueprint.route("", methods=['GET']) def get_notifications(): _data = request.args.to_dict(flat=False) + + # flat=False makes everything a list, but we only ever allow one value for "older_than" if 'older_than' in _data: - # flat=False makes everything a list, but we only ever allow one value for "older_than" _data['older_than'] = _data['older_than'][0] + # and client reference + if 'client_reference' in _data: + _data['client_reference'] = _data['client_reference'][0] + data = validate(_data, get_notifications_request) + if data.get('client_reference'): + notification = notifications_dao.get_notification_by_reference( + str(api_user.service_id), data.get('client_reference'), key_type=None + ) + return jsonify(notification.serialize()), 200 + paginated_notifications = notifications_dao.get_notifications_for_service( str(api_user.service_id), filter_dict=data, diff --git a/app/v2/notifications/notification_schemas.py b/app/v2/notifications/notification_schemas.py index 016cb7503..b206d2873 100644 --- a/app/v2/notifications/notification_schemas.py +++ b/app/v2/notifications/notification_schemas.py @@ -78,6 +78,7 @@ get_notifications_request = { "description": "schema for query parameters allowed when getting list of notifications", "type": "object", "properties": { + "client_reference": {"type": "string"}, "status": { "type": "array", "items": { diff --git a/tests/app/v2/notifications/test_get_notifications.py b/tests/app/v2/notifications/test_get_notifications.py index eecf58b0a..9eac5a269 100644 --- a/tests/app/v2/notifications/test_get_notifications.py +++ b/tests/app/v2/notifications/test_get_notifications.py @@ -60,6 +60,40 @@ def test_get_notification_by_id_returns_200( assert json_response == expected_response +def test_get_notification_by_reference_returns_200(client, notify_db, notify_db_session): + sample_notification_with_reference = create_sample_notification( + notify_db, notify_db_session, client_reference='some-client-reference') + + auth_header = create_authorization_header(service_id=sample_notification_with_reference.service_id) + response = client.get( + path='/v2/notifications?client_reference={}'.format(sample_notification_with_reference.client_reference), + headers=[('Content-Type', 'application/json'), auth_header]) + + assert response.status_code == 200 + assert response.headers['Content-type'] == 'application/json' + + +def test_get_notification_by_reference_nonexistent_reference_returns_400(client, sample_notification): + auth_header = create_authorization_header(service_id=sample_notification.service_id) + response = client.get( + path='/v2/notifications?client_reference={}'.format(sample_notification.client_reference), + headers=[('Content-Type', 'application/json'), auth_header]) + + assert response.status_code == 404 + assert response.headers['Content-type'] == 'application/json' + + json_response = json.loads(response.get_data(as_text=True)) + assert json_response == { + "errors": [ + { + "error": "NoResultFound", + "message": "No result found" + } + ], + "status_code": 404 + } + + def test_get_notification_by_id_nonexistent_id(client, sample_notification): auth_header = create_authorization_header(service_id=sample_notification.service_id) response = client.get(