mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-05-05 16:38:59 -04:00
move status mapping logic to the api client
also added tests 👼
This commit is contained in:
@@ -13,18 +13,9 @@ from app.notify_client.api_key_api_client import KEY_TYPE_NORMAL, KEY_TYPE_TEST,
|
|||||||
def api_integration(service_id):
|
def api_integration(service_id):
|
||||||
return render_template(
|
return render_template(
|
||||||
'views/api/index.html',
|
'views/api/index.html',
|
||||||
api_notifications=map_letters_to_accepted(notification_api_client.get_notifications_for_service(
|
api_notifications=notification_api_client.get_api_notifications_for_service(service_id)
|
||||||
service_id=service_id,
|
|
||||||
include_jobs=False,
|
|
||||||
include_from_test_key=True
|
|
||||||
))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
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")
|
@main.route("/services/<service_id>/api/documentation")
|
||||||
@login_required
|
@login_required
|
||||||
|
|||||||
@@ -66,3 +66,14 @@ class NotificationApiClient(NotifyAdminAPIClient):
|
|||||||
|
|
||||||
def get_notification(self, service_id, notification_id):
|
def get_notification(self, service_id, notification_id):
|
||||||
return self.get(url='/service/{}/notifications/{}'.format(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
|
||||||
|
|||||||
@@ -266,6 +266,7 @@ def notification_json(
|
|||||||
'service': service_id,
|
'service': service_id,
|
||||||
'template_version': template['version'],
|
'template_version': template['version'],
|
||||||
'personalisation': personalisation or {},
|
'personalisation': personalisation or {},
|
||||||
|
'notification_type': 'sms',
|
||||||
} for i in range(rows)],
|
} for i in range(rows)],
|
||||||
'total': rows,
|
'total': rows,
|
||||||
'page_size': 50,
|
'page_size': 50,
|
||||||
@@ -281,7 +282,8 @@ def single_notification_json(
|
|||||||
status=None,
|
status=None,
|
||||||
sent_at=None,
|
sent_at=None,
|
||||||
created_at=None,
|
created_at=None,
|
||||||
updated_at=None
|
updated_at=None,
|
||||||
|
notification_type='sms'
|
||||||
):
|
):
|
||||||
if template is None:
|
if template is None:
|
||||||
template = template_json(service_id, str(generate_uuid()))
|
template = template_json(service_id, str(generate_uuid()))
|
||||||
@@ -310,7 +312,7 @@ def single_notification_json(
|
|||||||
'id': '29441662-17ce-4ffe-9502-fcaed73b2826',
|
'id': '29441662-17ce-4ffe-9502-fcaed73b2826',
|
||||||
'template': template,
|
'template': template,
|
||||||
'job_row_number': 0,
|
'job_row_number': 0,
|
||||||
'notification_type': 'sms',
|
'notification_type': notification_type,
|
||||||
'api_key': None,
|
'api_key': None,
|
||||||
'job': job_payload,
|
'job': job_payload,
|
||||||
'sent_by': 'mmg'
|
'sent_by': 'mmg'
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
|
import uuid
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from app.notify_client.notification_api_client import NotificationApiClient
|
from app.notify_client.notification_api_client import NotificationApiClient
|
||||||
|
|
||||||
|
from tests import single_notification_json, notification_json
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("arguments,expected_call", [
|
@pytest.mark.parametrize("arguments,expected_call", [
|
||||||
(
|
(
|
||||||
@@ -55,3 +60,23 @@ def test_get_notification(mocker):
|
|||||||
mock_get.assert_called_once_with(
|
mock_get.assert_called_once_with(
|
||||||
url='/service/foo/notifications/bar'
|
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'
|
||||||
|
|||||||
Reference in New Issue
Block a user