Allow users to set postage per precompiled letter

This commit is contained in:
Pea Tyczynska
2019-01-09 17:49:19 +00:00
parent 1719f31909
commit 56bae2b077
4 changed files with 29 additions and 12 deletions

View File

@@ -20,6 +20,7 @@ def create_letter_notification(letter_data, template, api_key, status, reply_to_
client_reference=letter_data.get('reference'), client_reference=letter_data.get('reference'),
status=status, status=status,
reply_to_text=reply_to_text, reply_to_text=reply_to_text,
billable_units=billable_units billable_units=billable_units,
postage=letter_data.get('postage')
) )
return notification return notification

View File

@@ -75,7 +75,8 @@ def persist_notification(
created_by_id=None, created_by_id=None,
status=NOTIFICATION_CREATED, status=NOTIFICATION_CREATED,
reply_to_text=None, reply_to_text=None,
billable_units=None billable_units=None,
postage=None
): ):
notification_created_at = created_at or datetime.utcnow() notification_created_at = created_at or datetime.utcnow()
if not notification_id: if not notification_id:
@@ -112,11 +113,14 @@ def persist_notification(
elif notification_type == EMAIL_TYPE: elif notification_type == EMAIL_TYPE:
notification.normalised_to = format_email_address(notification.to) notification.normalised_to = format_email_address(notification.to)
elif notification_type == LETTER_TYPE: elif notification_type == LETTER_TYPE:
template = dao_get_template_by_id(template_id, template_version) if postage:
if service.has_permission(CHOOSE_POSTAGE) and template.postage: notification.postage = postage
notification.postage = template.postage
else: else:
notification.postage = service.postage template = dao_get_template_by_id(template_id, template_version)
if service.has_permission(CHOOSE_POSTAGE) and template.postage:
notification.postage = template.postage
else:
notification.postage = service.postage
# if simulated create a Notification model to return but do not persist the Notification to the dB # if simulated create a Notification model to return but do not persist the Notification to the dB
if not simulated: if not simulated:

View File

@@ -239,7 +239,8 @@ post_precompiled_letter_request = {
"title": "POST v2/notifications/letter", "title": "POST v2/notifications/letter",
"properties": { "properties": {
"reference": {"type": "string"}, "reference": {"type": "string"},
"content": {"type": "string"} "content": {"type": "string"},
"postage": {"type": "string"}
}, },
"required": ["reference", "content"], "required": ["reference", "content"],
"additionalProperties": False "additionalProperties": False

View File

@@ -469,16 +469,27 @@ def test_post_precompiled_letter_with_invalid_base64(client, notify_user, mocker
assert not Notification.query.first() assert not Notification.query.first()
@pytest.mark.parametrize('postage', ['first', 'second']) @pytest.mark.parametrize('service_postage, notification_postage, expected_postage', [
def test_post_precompiled_letter_notification_returns_201(client, notify_user, mocker, postage): ('second', 'second', 'second'),
('second', 'first', 'first'),
('second', None, 'second'),
('first', 'first', 'first'),
('first', 'second', 'second'),
('first', None, 'first'),
])
def test_post_precompiled_letter_notification_returns_201(
client, notify_user, mocker, service_postage, notification_postage, expected_postage
):
sample_service = create_service(service_permissions=['letter', 'precompiled_letter']) sample_service = create_service(service_permissions=['letter', 'precompiled_letter'])
sample_service.postage = postage sample_service.postage = service_postage
s3mock = mocker.patch('app.v2.notifications.post_notifications.upload_letter_pdf') s3mock = mocker.patch('app.v2.notifications.post_notifications.upload_letter_pdf')
mocker.patch('app.celery.letters_pdf_tasks.notify_celery.send_task') mocker.patch('app.celery.letters_pdf_tasks.notify_celery.send_task')
data = { data = {
"reference": "letter-reference", "reference": "letter-reference",
"content": "bGV0dGVyLWNvbnRlbnQ=" "content": "bGV0dGVyLWNvbnRlbnQ="
} }
if notification_postage:
data["postage"] = notification_postage
auth_header = create_authorization_header(service_id=sample_service.id) auth_header = create_authorization_header(service_id=sample_service.id)
response = client.post( response = client.post(
path="v2/notifications/letter", path="v2/notifications/letter",
@@ -493,10 +504,10 @@ def test_post_precompiled_letter_notification_returns_201(client, notify_user, m
assert notification.billable_units == 0 assert notification.billable_units == 0
assert notification.status == NOTIFICATION_PENDING_VIRUS_CHECK assert notification.status == NOTIFICATION_PENDING_VIRUS_CHECK
assert notification.postage == postage assert notification.postage == expected_postage
notification_history = NotificationHistory.query.one() notification_history = NotificationHistory.query.one()
assert notification_history.postage == postage assert notification_history.postage == expected_postage
resp_json = json.loads(response.get_data(as_text=True)) resp_json = json.loads(response.get_data(as_text=True))
assert resp_json == {'id': str(notification.id), 'reference': 'letter-reference'} assert resp_json == {'id': str(notification.id), 'reference': 'letter-reference'}