From 52f76207720b2f384750758fdb44e8f142835909 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Mon, 9 Sep 2019 12:44:28 +0100 Subject: [PATCH] create pdfs for test templated letters previously, we didn't create templated letters, and just marked them as delivered straight away. However, we may need to return PDFs for these letters, so we should create them the same as live letters. Then update the functions so that they know where to look for these letters. --- app/celery/letters_pdf_tasks.py | 5 ++-- app/letters/utils.py | 5 ++-- app/v2/notifications/post_notifications.py | 30 ++++++++++--------- .../test_post_letter_notifications.py | 12 ++++---- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/app/celery/letters_pdf_tasks.py b/app/celery/letters_pdf_tasks.py index 7d659c876..3a9cac2fa 100644 --- a/app/celery/letters_pdf_tasks.py +++ b/app/celery/letters_pdf_tasks.py @@ -67,8 +67,9 @@ def create_letters_pdf(self, notification_id): upload_letter_pdf(notification, pdf_data) - notification.billable_units = billable_units - dao_update_notification(notification) + if notification.key_type != KEY_TYPE_TEST: + notification.billable_units = billable_units + dao_update_notification(notification) current_app.logger.info( 'Letter notification reference {reference}: billable units set to {billable_units}'.format( diff --git a/app/letters/utils.py b/app/letters/utils.py index e23542081..10e4310eb 100644 --- a/app/letters/utils.py +++ b/app/letters/utils.py @@ -50,11 +50,10 @@ def get_letter_pdf_filename(reference, crown, is_scan_letter=False, postage=SECO def get_bucket_name_and_prefix_for_notification(notification): - is_test_letter = notification.key_type == KEY_TYPE_TEST and notification.template.is_precompiled_letter folder = '' if notification.status == NOTIFICATION_VALIDATION_FAILED: bucket_name = current_app.config['INVALID_PDF_BUCKET_NAME'] - elif is_test_letter: + elif notification.key_type == KEY_TYPE_TEST: bucket_name = current_app.config['TEST_LETTERS_BUCKET_NAME'] else: bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME'] @@ -90,6 +89,8 @@ def upload_letter_pdf(notification, pdf_data, precompiled=False): if precompiled: bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME'] + elif notification.key_type == KEY_TYPE_TEST: + bucket_name = current_app.config['TEST_LETTERS_BUCKET_NAME'] else: bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME'] diff --git a/app/v2/notifications/post_notifications.py b/app/v2/notifications/post_notifications.py index 3d06c16d7..90801cb8a 100644 --- a/app/v2/notifications/post_notifications.py +++ b/app/v2/notifications/post_notifications.py @@ -259,10 +259,11 @@ def process_letter_notification(*, letter_data, api_key, template, reply_to_text template=template, reply_to_text=reply_to_text) - should_send = not (api_key.key_type == KEY_TYPE_TEST) + test_key = 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 + status = NOTIFICATION_CREATED if not test_key else NOTIFICATION_SENDING + queue = QueueNames.CREATE_LETTERS_PDF if not test_key else QueueNames.RESEARCH_MODE notification = create_letter_notification(letter_data=letter_data, template=template, @@ -270,18 +271,19 @@ def process_letter_notification(*, letter_data, api_key, template, reply_to_text status=status, reply_to_text=reply_to_text) - if should_send: - create_letters_pdf.apply_async( - [str(notification.id)], - queue=QueueNames.CREATE_LETTERS_PDF - ) - elif current_app.config['NOTIFY_ENVIRONMENT'] in ['preview', 'development']: - create_fake_letter_response_file.apply_async( - (notification.reference,), - queue=QueueNames.RESEARCH_MODE - ) - else: - update_notification_status_by_reference(notification.reference, NOTIFICATION_DELIVERED) + create_letters_pdf.apply_async( + [str(notification.id)], + queue=queue + ) + + if test_key: + if current_app.config['NOTIFY_ENVIRONMENT'] in ['preview', 'development']: + create_fake_letter_response_file.apply_async( + (notification.reference,), + queue=queue + ) + else: + update_notification_status_by_reference(notification.reference, NOTIFICATION_DELIVERED) return notification diff --git a/tests/app/v2/notifications/test_post_letter_notifications.py b/tests/app/v2/notifications/test_post_letter_notifications.py index f950135af..f891314f6 100644 --- a/tests/app/v2/notifications/test_post_letter_notifications.py +++ b/tests/app/v2/notifications/test_post_letter_notifications.py @@ -122,7 +122,7 @@ def test_post_letter_notification_sets_postage( 'staging', 'live', ]) -def test_post_letter_notification_with_test_key_set_status_to_delivered( +def test_post_letter_notification_with_test_key_creates_pdf_and_sets_status_to_delivered( notify_api, client, sample_letter_template, mocker, env): data = { @@ -148,7 +148,7 @@ def test_post_letter_notification_with_test_key_set_status_to_delivered( notification = Notification.query.one() - assert not fake_create_letter_task.called + fake_create_letter_task.assert_called_once_with([str(notification.id)], queue='research-mode-tasks') assert not fake_create_dvla_response_task.called assert notification.status == NOTIFICATION_DELIVERED @@ -157,7 +157,7 @@ def test_post_letter_notification_with_test_key_set_status_to_delivered( 'development', 'preview', ]) -def test_post_letter_notification_with_test_key_sets_status_to_sending_and_sends_fake_response_file( +def test_post_letter_notification_with_test_key_creates_pdf_and_sets_status_to_sending_and_sends_fake_response_file( notify_api, client, sample_letter_template, mocker, env): data = { @@ -183,7 +183,7 @@ def test_post_letter_notification_with_test_key_sets_status_to_sending_and_sends notification = Notification.query.one() - assert not fake_create_letter_task.called + fake_create_letter_task.assert_called_once_with([str(notification.id)], queue='research-mode-tasks') assert fake_create_dvla_response_task.called assert notification.status == NOTIFICATION_SENDING @@ -357,7 +357,7 @@ def test_post_letter_notification_doesnt_send_in_trial(client, sample_trial_lett {'error': 'BadRequestError', 'message': 'Cannot send letters when service is in trial mode'}] -def test_post_letter_notification_is_delivered_if_in_trial_mode_and_using_test_key( +def test_post_letter_notification_is_delivered_but_still_creates_pdf_if_in_trial_mode_and_using_test_key( client, sample_trial_letter_template, mocker @@ -373,7 +373,7 @@ def test_post_letter_notification_is_delivered_if_in_trial_mode_and_using_test_k notification = Notification.query.one() assert notification.status == NOTIFICATION_DELIVERED - assert not fake_create_letter_task.called + fake_create_letter_task.assert_called_once_with([str(notification.id)], queue='research-mode-tasks') def test_post_letter_notification_is_delivered_and_has_pdf_uploaded_to_test_letters_bucket_using_test_key(