move status mapping logic to the api client

also added tests 👼
This commit is contained in:
Leo Hemsted
2017-09-20 16:02:15 +01:00
parent f8fab35ee7
commit 03397f416e
4 changed files with 41 additions and 12 deletions

View File

@@ -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/<service_id>/api/documentation")
@login_required

View File

@@ -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

View File

@@ -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'

View File

@@ -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'