From 4174c72f218fe305de18af58f3c203f0462907bf Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Wed, 20 Sep 2017 15:21:05 +0100 Subject: [PATCH] allow 'accepted' as a proxy for created + sending as well as 'failed' for the three failure types when querying the api --- app/models.py | 18 +++++++++--------- app/v2/notifications/notification_schemas.py | 4 ++-- tests/app/test_model.py | 15 +++++++++++---- .../v2/notifications/test_get_notifications.py | 2 +- .../notifications/test_notification_schemas.py | 4 ++-- 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/app/models.py b/app/models.py index e1a9f36fd..5dede96e9 100644 --- a/app/models.py +++ b/app/models.py @@ -1,3 +1,4 @@ +import itertools import time import uuid import datetime @@ -903,10 +904,10 @@ class Notification(db.Model): - > IN - ['failed', 'created'] + ['failed', 'created', 'accepted'] < OUT - ['technical-failure', 'temporary-failure', 'permanent-failure', 'created'] + ['technical-failure', 'temporary-failure', 'permanent-failure', 'created', 'sending'] :param status_or_statuses: a single status or list of statuses @@ -914,18 +915,17 @@ class Notification(db.Model): """ def _substitute_status_str(_status): - return NOTIFICATION_STATUS_TYPES_FAILED if _status == NOTIFICATION_FAILED else _status + return ( + NOTIFICATION_STATUS_TYPES_FAILED if _status == NOTIFICATION_FAILED else + [NOTIFICATION_CREATED, NOTIFICATION_SENDING] if _status == NOTIFICATION_STATUS_LETTER_ACCEPTED else + [_status] + ) def _substitute_status_seq(_statuses): - if NOTIFICATION_FAILED in _statuses: - _statuses = list(set( - NOTIFICATION_STATUS_TYPES_FAILED + [_s for _s in _statuses if _s != NOTIFICATION_FAILED] - )) - return _statuses + return list(set(itertools.chain.from_iterable(_substitute_status_str(status) for status in _statuses))) if isinstance(status_or_statuses, str): return _substitute_status_str(status_or_statuses) - return _substitute_status_seq(status_or_statuses) @property diff --git a/app/v2/notifications/notification_schemas.py b/app/v2/notifications/notification_schemas.py index a22ad863b..9ff028d1f 100644 --- a/app/v2/notifications/notification_schemas.py +++ b/app/v2/notifications/notification_schemas.py @@ -1,4 +1,4 @@ -from app.models import NOTIFICATION_STATUS_TYPES, TEMPLATE_TYPES +from app.models import NOTIFICATION_STATUS_TYPES, NOTIFICATION_STATUS_LETTER_ACCEPTED, TEMPLATE_TYPES from app.schema_validation.definitions import (uuid, personalisation, letter_personalisation) @@ -59,7 +59,7 @@ get_notifications_request = { "status": { "type": "array", "items": { - "enum": NOTIFICATION_STATUS_TYPES + "enum": NOTIFICATION_STATUS_TYPES + [NOTIFICATION_STATUS_LETTER_ACCEPTED] } }, "template_type": { diff --git a/tests/app/test_model.py b/tests/app/test_model.py index 0dd18eb56..a20e83001 100644 --- a/tests/app/test_model.py +++ b/tests/app/test_model.py @@ -10,10 +10,12 @@ from app.models import ( MOBILE_TYPE, EMAIL_TYPE, NOTIFICATION_CREATED, + NOTIFICATION_SENDING, NOTIFICATION_PENDING, NOTIFICATION_FAILED, NOTIFICATION_TECHNICAL_FAILURE, - NOTIFICATION_STATUS_TYPES_FAILED + NOTIFICATION_STATUS_TYPES_FAILED, + NOTIFICATION_STATUS_LETTER_ACCEPTED ) from tests.app.conftest import ( sample_template as create_sample_template, @@ -55,8 +57,9 @@ def test_should_not_build_service_whitelist_from_invalid_contact(recipient_type, @pytest.mark.parametrize('initial_statuses, expected_statuses', [ # passing in single statuses as strings (NOTIFICATION_FAILED, NOTIFICATION_STATUS_TYPES_FAILED), - (NOTIFICATION_CREATED, NOTIFICATION_CREATED), - (NOTIFICATION_TECHNICAL_FAILURE, NOTIFICATION_TECHNICAL_FAILURE), + (NOTIFICATION_STATUS_LETTER_ACCEPTED, [NOTIFICATION_SENDING, NOTIFICATION_CREATED]), + (NOTIFICATION_CREATED, [NOTIFICATION_CREATED]), + (NOTIFICATION_TECHNICAL_FAILURE, [NOTIFICATION_TECHNICAL_FAILURE]), # passing in lists containing single statuses ([NOTIFICATION_FAILED], NOTIFICATION_STATUS_TYPES_FAILED), ([NOTIFICATION_CREATED], [NOTIFICATION_CREATED]), @@ -65,13 +68,17 @@ def test_should_not_build_service_whitelist_from_invalid_contact(recipient_type, ([NOTIFICATION_FAILED, NOTIFICATION_CREATED], NOTIFICATION_STATUS_TYPES_FAILED + [NOTIFICATION_CREATED]), ([NOTIFICATION_CREATED, NOTIFICATION_PENDING], [NOTIFICATION_CREATED, NOTIFICATION_PENDING]), ([NOTIFICATION_CREATED, NOTIFICATION_TECHNICAL_FAILURE], [NOTIFICATION_CREATED, NOTIFICATION_TECHNICAL_FAILURE]), + ( + [NOTIFICATION_FAILED, NOTIFICATION_STATUS_LETTER_ACCEPTED], + NOTIFICATION_STATUS_TYPES_FAILED + [NOTIFICATION_SENDING, NOTIFICATION_CREATED] + ), # checking we don't end up with duplicates ( [NOTIFICATION_FAILED, NOTIFICATION_CREATED, NOTIFICATION_TECHNICAL_FAILURE], NOTIFICATION_STATUS_TYPES_FAILED + [NOTIFICATION_CREATED] ), ]) -def test_status_conversion_handles_failed_statuses(initial_statuses, expected_statuses): +def test_status_conversion(initial_statuses, expected_statuses): converted_statuses = Notification.substitute_status(initial_statuses) assert len(converted_statuses) == len(expected_statuses) assert set(converted_statuses) == set(expected_statuses) diff --git a/tests/app/v2/notifications/test_get_notifications.py b/tests/app/v2/notifications/test_get_notifications.py index 66133da75..926c8c1d8 100644 --- a/tests/app/v2/notifications/test_get_notifications.py +++ b/tests/app/v2/notifications/test_get_notifications.py @@ -393,7 +393,7 @@ def test_get_all_notifications_filter_by_status_invalid_status(client, sample_no assert json_response['status_code'] == 400 assert len(json_response['errors']) == 1 assert json_response['errors'][0]['message'] == "status elephant is not one of [created, sending, sent, " \ - "delivered, pending, failed, technical-failure, temporary-failure, permanent-failure]" + "delivered, pending, failed, technical-failure, temporary-failure, permanent-failure, accepted]" def test_get_all_notifications_filter_by_multiple_statuses(client, sample_template): diff --git a/tests/app/v2/notifications/test_notification_schemas.py b/tests/app/v2/notifications/test_notification_schemas.py index 9e3040194..c667def11 100644 --- a/tests/app/v2/notifications/test_notification_schemas.py +++ b/tests/app/v2/notifications/test_notification_schemas.py @@ -26,7 +26,7 @@ def test_get_notifications_request_invalid_statuses( ): partial_error_status = "is not one of " \ "[created, sending, sent, delivered, pending, failed, " \ - "technical-failure, temporary-failure, permanent-failure]" + "technical-failure, temporary-failure, permanent-failure, accepted]" with pytest.raises(ValidationError) as e: validate({'status': invalid_statuses + valid_statuses}, get_notifications_request) @@ -73,7 +73,7 @@ def test_get_notifications_request_invalid_statuses_and_template_types(): error_messages = [error['message'] for error in errors] for invalid_status in ["elephant", "giraffe"]: assert "status {} is not one of [created, sending, sent, delivered, " \ - "pending, failed, technical-failure, temporary-failure, permanent-failure]".format( + "pending, failed, technical-failure, temporary-failure, permanent-failure, accepted]".format( invalid_status ) in error_messages