From 30e371fa4cfda1f2ce61daa2d2f8feaa1daca640 Mon Sep 17 00:00:00 2001 From: Ken Tsang Date: Tue, 13 Mar 2018 14:08:01 +0000 Subject: [PATCH] Set precompiled letters to pending virus check initially --- app/v2/notifications/post_notifications.py | 14 +++++++++----- .../v2/notifications/test_post_notifications.py | 7 ++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/v2/notifications/post_notifications.py b/app/v2/notifications/post_notifications.py index b1fd958e6..42e7180c0 100644 --- a/app/v2/notifications/post_notifications.py +++ b/app/v2/notifications/post_notifications.py @@ -25,7 +25,8 @@ from app.models import ( KEY_TYPE_TEAM, NOTIFICATION_CREATED, NOTIFICATION_SENDING, - NOTIFICATION_DELIVERED + NOTIFICATION_DELIVERED, + NOTIFICATION_PENDING_VIRUS_CHECK, ) from app.celery.letters_pdf_tasks import create_letters_pdf from app.celery.research_mode_tasks import create_fake_letter_response_file @@ -220,8 +221,15 @@ def process_letter_notification(*, letter_data, api_key, template, reply_to_text if not api_key.service.research_mode and api_key.service.restricted and api_key.key_type != KEY_TYPE_TEST: raise BadRequestError(message='Cannot send letters when service is in trial mode', status_code=403) + should_send = not (api_key.service.research_mode or api_key.key_type == KEY_TYPE_TEST) + + # if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up + status = NOTIFICATION_CREATED if should_send else NOTIFICATION_SENDING + if precompiled: try: + if should_send: + status = NOTIFICATION_PENDING_VIRUS_CHECK letter_content = base64.b64decode(letter_data['content']) pages = pdf_page_count(io.BytesIO(letter_content)) except ValueError: @@ -230,10 +238,6 @@ def process_letter_notification(*, letter_data, api_key, template, reply_to_text current_app.logger.exception(msg='Invalid PDF received') raise BadRequestError(message='Letter content is not a valid PDF', status_code=400) - should_send = not (api_key.service.research_mode or api_key.key_type == KEY_TYPE_TEST) - - # if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up - status = NOTIFICATION_CREATED if should_send else NOTIFICATION_SENDING notification = create_letter_notification(letter_data=letter_data, template=template, api_key=api_key, diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index e21b4ca68..eadb5dd88 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -7,8 +7,10 @@ from freezegun import freeze_time from app.dao.service_sms_sender_dao import dao_update_service_sms_sender from app.models import ( ScheduledNotification, - SCHEDULE_NOTIFICATIONS, EMAIL_TYPE, + NOTIFICATION_CREATED, + NOTIFICATION_PENDING_VIRUS_CHECK, + SCHEDULE_NOTIFICATIONS, SMS_TYPE ) from flask import json, current_app @@ -55,6 +57,7 @@ def test_post_sms_notification_returns_201(client, sample_template_with_placehol assert validate(resp_json, post_sms_response) == resp_json notifications = Notification.query.all() assert len(notifications) == 1 + assert notifications[0].status == NOTIFICATION_CREATED notification_id = notifications[0].id assert resp_json['id'] == str(notification_id) assert resp_json['reference'] == reference @@ -306,6 +309,7 @@ def test_post_email_notification_returns_201(client, sample_email_template_with_ resp_json = json.loads(response.get_data(as_text=True)) assert validate(resp_json, post_email_response) == resp_json notification = Notification.query.one() + assert notification.status == NOTIFICATION_CREATED assert resp_json['id'] == str(notification.id) assert resp_json['reference'] == reference assert notification.reference is None @@ -756,6 +760,7 @@ def test_post_precompiled_letter_notification_returns_201(client, notify_user, m notification = Notification.query.first() assert notification.billable_units == 3 + assert notification.status == NOTIFICATION_PENDING_VIRUS_CHECK resp_json = json.loads(response.get_data(as_text=True))