Merge pull request #1244 from alphagov/return-letter-delivery-estimate

Return delivery estimate for letter notifications
This commit is contained in:
Chris Hill-Scott
2017-09-18 11:09:51 +01:00
committed by GitHub
3 changed files with 63 additions and 7 deletions

View File

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

View File

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

View File

@@ -1,4 +1,4 @@
import datetime
import pytest
from flask import json
@@ -9,6 +9,11 @@ from tests.app.db import (
create_template,
create_service)
from tests.app.conftest import (
sample_notification,
sample_email_notification,
)
@pytest.mark.parametrize('billable_units, provider', [
(1, 'mmg'),
@@ -207,6 +212,52 @@ 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
@pytest.mark.parametrize('notification_mock', [
sample_notification,
sample_email_notification,
])
def test_get_notification_doesnt_have_delivery_estimate_for_non_letters(
client,
notify_db,
notify_db_session,
notification_mock,
):
mocked_notification = notification_mock(notify_db, notify_db_session)
auth_header = create_authorization_header(service_id=mocked_notification.service_id)
response = client.get(
path='/v2/notifications/{}'.format(mocked_notification.id),
headers=[('Content-Type', 'application/json'), auth_header]
)
assert response.status_code == 200
assert 'estimated_delivery' not in json.loads(response.get_data(as_text=True))
def test_get_all_notifications_returns_200(client, sample_template):
notifications = [create_notification(template=sample_template) for _ in range(2)]
notification = notifications[-1]