From 03397f416e9b23d6941c694ddfdc3e73d40dd6fb Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Wed, 20 Sep 2017 16:02:15 +0100 Subject: [PATCH] move status mapping logic to the api client also added tests :angel: --- app/main/views/api_keys.py | 11 +------- app/notify_client/notification_api_client.py | 11 ++++++++ tests/__init__.py | 6 +++-- .../notify_client/test_notification_client.py | 25 +++++++++++++++++++ 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/app/main/views/api_keys.py b/app/main/views/api_keys.py index ca7329d9d..68a93f8d2 100644 --- a/app/main/views/api_keys.py +++ b/app/main/views/api_keys.py @@ -13,18 +13,9 @@ from app.notify_client.api_key_api_client import KEY_TYPE_NORMAL, KEY_TYPE_TEST, def api_integration(service_id): return render_template( 'views/api/index.html', - api_notifications=map_letters_to_accepted(notification_api_client.get_notifications_for_service( - service_id=service_id, - include_jobs=False, - include_from_test_key=True - )) + api_notifications=notification_api_client.get_api_notifications_for_service(service_id) ) -def map_letters_to_accepted(notifications): - for notification in notifications['notifications']: - if notification['notification_type'] == 'letter' and notification['status'] in ('created', 'sending'): - notification['status'] = 'accepted' - return notifications @main.route("/services//api/documentation") @login_required diff --git a/app/notify_client/notification_api_client.py b/app/notify_client/notification_api_client.py index f39ff33d0..cec5346cc 100644 --- a/app/notify_client/notification_api_client.py +++ b/app/notify_client/notification_api_client.py @@ -66,3 +66,14 @@ class NotificationApiClient(NotifyAdminAPIClient): def get_notification(self, service_id, notification_id): return self.get(url='/service/{}/notifications/{}'.format(service_id, notification_id)) + + def get_api_notifications_for_service(self, service_id): + ret = self.get_notifications_for_service(service_id, include_jobs=False, include_from_test_key=True) + return self.map_letters_to_accepted(ret) + + @staticmethod + def map_letters_to_accepted(notifications): + for notification in notifications['notifications']: + if notification['notification_type'] == 'letter' and notification['status'] in ('created', 'sending'): + notification['status'] = 'accepted' + return notifications diff --git a/tests/__init__.py b/tests/__init__.py index 629fedb6d..301b6dc0d 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -266,6 +266,7 @@ def notification_json( 'service': service_id, 'template_version': template['version'], 'personalisation': personalisation or {}, + 'notification_type': 'sms', } for i in range(rows)], 'total': rows, 'page_size': 50, @@ -281,7 +282,8 @@ def single_notification_json( status=None, sent_at=None, created_at=None, - updated_at=None + updated_at=None, + notification_type='sms' ): if template is None: template = template_json(service_id, str(generate_uuid())) @@ -310,7 +312,7 @@ def single_notification_json( 'id': '29441662-17ce-4ffe-9502-fcaed73b2826', 'template': template, 'job_row_number': 0, - 'notification_type': 'sms', + 'notification_type': notification_type, 'api_key': None, 'job': job_payload, 'sent_by': 'mmg' diff --git a/tests/app/notify_client/test_notification_client.py b/tests/app/notify_client/test_notification_client.py index 4f9750a05..4f7ddd5da 100644 --- a/tests/app/notify_client/test_notification_client.py +++ b/tests/app/notify_client/test_notification_client.py @@ -1,6 +1,11 @@ +import uuid + import pytest + from app.notify_client.notification_api_client import NotificationApiClient +from tests import single_notification_json, notification_json + @pytest.mark.parametrize("arguments,expected_call", [ ( @@ -55,3 +60,23 @@ def test_get_notification(mocker): mock_get.assert_called_once_with( url='/service/foo/notifications/bar' ) + + +def test_get_api_notifications_changes_letter_statuses(mocker): + service_id = str(uuid.uuid4()) + sms_notification = single_notification_json(service_id, notification_type='sms', status='created') + email_notification = single_notification_json(service_id, notification_type='email', status='created') + letter_notification = single_notification_json(service_id, notification_type='letter', status='created') + notis = notification_json(service_id=service_id, rows=0) + notis['notifications'] = [sms_notification, email_notification, letter_notification] + + mock_post = mocker.patch('app.notify_client.notification_api_client.NotificationApiClient.get', return_value=notis) + + ret = NotificationApiClient().get_api_notifications_for_service(service_id) + + assert ret['notifications'][0]['notification_type'] == 'sms' + assert ret['notifications'][1]['notification_type'] == 'email' + assert ret['notifications'][2]['notification_type'] == 'letter' + assert ret['notifications'][0]['status'] == 'created' + assert ret['notifications'][1]['status'] == 'created' + assert ret['notifications'][2]['status'] == 'accepted'