From 5327298371ddedb9ea3639c81ed83ef23efe7498 Mon Sep 17 00:00:00 2001 From: Alexey Bezhan Date: Fri, 23 Feb 2018 10:38:36 +0000 Subject: [PATCH] Add a view function for pre-compiled PDF letters Adds a separate view function that is registered under the same route as existing letter POST notification. --- app/v2/notifications/notification_schemas.py | 14 +++- app/v2/notifications/post_notifications.py | 71 ++++++++++++++++++- .../notifications/test_post_notifications.py | 30 ++++++++ 3 files changed, 113 insertions(+), 2 deletions(-) diff --git a/app/v2/notifications/notification_schemas.py b/app/v2/notifications/notification_schemas.py index 56b5512e5..506a54706 100644 --- a/app/v2/notifications/notification_schemas.py +++ b/app/v2/notifications/notification_schemas.py @@ -206,7 +206,6 @@ post_email_response = { "required": ["id", "content", "uri", "template"] } - post_letter_request = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "POST letter notification schema", @@ -221,6 +220,19 @@ post_letter_request = { "additionalProperties": False } +post_precompiled_letter_request = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "POST precompiled letter notification schema", + "type": "object", + "title": "POST v2/notifications/letter", + "properties": { + "reference": {"type": "string"}, + "content": {"type": "string"} + }, + "required": ["reference", "content"], + "additionalProperties": False +} + letter_content = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "Letter content for POST letter notification", diff --git a/app/v2/notifications/post_notifications.py b/app/v2/notifications/post_notifications.py index 1e802697d..891c2bb99 100644 --- a/app/v2/notifications/post_notifications.py +++ b/app/v2/notifications/post_notifications.py @@ -7,7 +7,10 @@ from notifications_utils.recipients import try_validate_and_format_phone_number from app import api_user, authenticated_service from app.config import QueueNames from app.dao.notifications_dao import update_notification_status_by_reference +from app.dao.templates_dao import dao_create_template +from app.dao.users_dao import get_user_by_id from app.models import ( + Template, SMS_TYPE, EMAIL_TYPE, LETTER_TYPE, @@ -44,7 +47,8 @@ from app.v2.notifications import v2_notification_blueprint from app.v2.notifications.notification_schemas import ( post_sms_request, post_email_request, - post_letter_request + post_letter_request, + post_precompiled_letter_request ) from app.v2.notifications.create_response import ( create_post_sms_response_from_notification, @@ -53,6 +57,47 @@ from app.v2.notifications.create_response import ( ) +@v2_notification_blueprint.route('/{}'.format(LETTER_TYPE), methods=['POST']) +def post_precompiled_letter_notification(): + if 'content' not in (request.get_json() or {}): + return post_notification(LETTER_TYPE) + + form = validate(request.get_json(), post_precompiled_letter_request) + + #check_service_has_permission(notification_type, authenticated_service.permissions) + + check_rate_limiting(authenticated_service, api_user) + + template = get_precompiled_letter_template(authenticated_service.id) + + form['personalisation'] = { + 'address_line_1': form['reference'] + } + + reply_to = get_reply_to_text(LETTER_TYPE, form, template) + + notification = process_letter_notification( + letter_data=form, + api_key=api_user, + template=template, + reply_to_text=reply_to + ) + + create_resp_partial = functools.partial( + create_post_letter_response_from_notification, + subject=template.subject, + ) + + resp = create_resp_partial( + notification=notification, + content=None, + url_root=request.url_root, + scheduled_for=None, + ) + + return jsonify(resp), 201 + + @v2_notification_blueprint.route('/', methods=['POST']) def post_notification(notification_type): if notification_type == EMAIL_TYPE: @@ -222,3 +267,27 @@ def get_reply_to_text(notification_type, form, template): reply_to = template.get_reply_to_text() return reply_to + + +def get_precompiled_letter_template(service_id): + template = Template.query.filter_by( + service_id=service_id, + template_type=LETTER_TYPE, + hidden=True + ).first() + if template is not None: + return template + + template = Template( + name='Pre-compiled PDF letter', + created_by=get_user_by_id(api_user.created_by_id), + service_id=service_id, + template_type=LETTER_TYPE, + hidden=True, + subject='Pre-compiled PDF', + content='', + ) + + dao_create_template(template) + + return template diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index 7ce825ac7..d44c72708 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -1,4 +1,5 @@ import uuid +from unittest.mock import ANY import pytest from freezegun import freeze_time @@ -694,3 +695,32 @@ def test_post_email_notification_with_invalid_reply_to_id_returns_400(client, sa assert 'email_reply_to_id {} does not exist in database for service id {}'. \ format(fake_uuid, sample_email_template.service_id) in resp_json['errors'][0]['message'] assert 'BadRequestError' in resp_json['errors'][0]['error'] + + +def test_post_precompiled_letter_notification_returns_201(client, sample_service, mocker): + mocker.patch('app.celery.letters_pdf_tasks.create_letters_pdf.apply_async') + data = { + "reference": "letter-reference", + "content": "abcdefgh" + } + auth_header = create_authorization_header(service_id=sample_service.id) + response = client.post( + path="v2/notifications/letter", + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header]) + + assert response.status_code == 201, response.get_data(as_text=True) + + resp_json = json.loads(response.get_data(as_text=True)) + assert resp_json == { + 'content': {'body': None, 'subject': 'Pre-compiled PDF'}, + 'id': ANY, + 'reference': 'letter-reference', + 'scheduled_for': None, + 'template': { + 'id': ANY, + 'uri': ANY, + 'version': 1 + }, + 'uri': ANY + }