From 29a962060f21d1b8263cd55ed2541f8722e273ad Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Mon, 11 Sep 2017 14:16:04 +0100 Subject: [PATCH] Return delivery estimate for letter notifications MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit > For get all or get one letter the response needs to be updated so that > it looks similar to admin app. > > status: created|sending --> received letter > new column: `estimated delivery date`: derived from created at date. > (see how the admin app is doing it) > > NOTE: > At the moment we only have 2 statuses for a letter created and > sending, but we will want to have other internal statuses that make > sense to the Notify team but not our services. When we know those > statuses the status map will be updated at that point. – https://www.pivotaltracker.com/story/show/150512525 This commit implements the date (not status) part of this story. --- app/models.py | 5 ++++ tests/app/conftest.py | 12 ++++---- .../notifications/test_get_notifications.py | 28 ++++++++++++++++++- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/app/models.py b/app/models.py index f689cc3c4..8d1437986 100644 --- a/app/models.py +++ b/app/models.py @@ -16,6 +16,7 @@ from notifications_utils.recipients import ( InvalidPhoneError, InvalidEmailError ) +from notifications_utils.letter_timings import get_letter_timings from app.encryption import ( hashpw, @@ -1029,6 +1030,10 @@ class Notification(db.Model): serialized['line_5'] = self.personalisation.get('address_line_5') serialized['line_6'] = self.personalisation.get('address_line_6') serialized['postcode'] = self.personalisation['postcode'] + serialized['estimated_delivery'] = \ + get_letter_timings(serialized['created_at'])\ + .earliest_delivery\ + .strftime(DATETIME_FORMAT) return serialized diff --git a/tests/app/conftest.py b/tests/app/conftest.py index f23bc6200..0acfb3c39 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -568,12 +568,12 @@ def sample_notification( @pytest.fixture def sample_letter_notification(sample_letter_template): address = { - 'addressline1': 'A1', - 'addressline2': 'A2', - 'addressline3': 'A3', - 'addressline4': 'A4', - 'addressline5': 'A5', - 'addressline6': 'A6', + 'address_line_1': 'A1', + 'address_line_2': 'A2', + 'address_line_3': 'A3', + 'address_line_4': 'A4', + 'address_line_5': 'A5', + 'address_line_6': 'A6', 'postcode': 'A_POST' } return create_notification(sample_letter_template, personalisation=address) diff --git a/tests/app/v2/notifications/test_get_notifications.py b/tests/app/v2/notifications/test_get_notifications.py index ff487275c..351b2c82f 100644 --- a/tests/app/v2/notifications/test_get_notifications.py +++ b/tests/app/v2/notifications/test_get_notifications.py @@ -1,4 +1,4 @@ - +import datetime import pytest from flask import json @@ -207,6 +207,32 @@ def test_get_notification_by_id_invalid_id(client, sample_notification, id): } +@pytest.mark.parametrize('created_at_month, estimated_delivery', [ + ( + 12, '2000-12-06T16:00:00.000000Z', # 4pm GMT in winter + ), + ( + 6, '2000-06-05T15:00:00.000000Z', # 4pm BST in summer + ), +]) +def test_get_notification_adds_delivery_estimate_for_letters( + client, + sample_letter_notification, + created_at_month, + estimated_delivery, +): + sample_letter_notification.created_at = datetime.date(2000, created_at_month, 1) + auth_header = create_authorization_header(service_id=sample_letter_notification.service_id) + response = client.get( + path='/v2/notifications/{}'.format(sample_letter_notification.id), + headers=[('Content-Type', 'application/json'), auth_header] + ) + + json_response = json.loads(response.get_data(as_text=True)) + assert response.status_code == 200 + assert json_response['estimated_delivery'] == estimated_delivery + + def test_get_all_notifications_returns_200(client, sample_template): notifications = [create_notification(template=sample_template) for _ in range(2)] notification = notifications[-1]