From 5ef9b426bb4179c0bcce211257c0d876e9cc9955 Mon Sep 17 00:00:00 2001 From: Ken Tsang Date: Fri, 26 Jan 2018 14:08:01 +0000 Subject: [PATCH] Update letter notification processing for letter pdfs in research mode - calls create fake response file to allow functional tests to run and trigger update of status to delivered only on preview and development so that FT response files don't pollute staging and live buckets --- app/v2/notifications/post_notifications.py | 24 +++- .../test_post_letter_notifications.py | 124 +++++++++++++++++- 2 files changed, 141 insertions(+), 7 deletions(-) diff --git a/app/v2/notifications/post_notifications.py b/app/v2/notifications/post_notifications.py index bcb2b4b7e..a8c9893b7 100644 --- a/app/v2/notifications/post_notifications.py +++ b/app/v2/notifications/post_notifications.py @@ -6,6 +6,7 @@ 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.models import ( SMS_TYPE, EMAIL_TYPE, @@ -14,9 +15,11 @@ from app.models import ( KEY_TYPE_TEST, KEY_TYPE_TEAM, NOTIFICATION_CREATED, - NOTIFICATION_SENDING + NOTIFICATION_SENDING, + NOTIFICATION_DELIVERED ) from app.celery.letters_pdf_tasks import create_letters_pdf +from app.celery.research_mode_tasks import create_fake_letter_response_file from app.celery.tasks import update_letter_notifications_to_sent_to_dvla from app.notifications.process_notifications import ( persist_notification, @@ -171,7 +174,7 @@ def process_letter_notification(*, letter_data, api_key, template, reply_to_text if api_key.key_type == KEY_TYPE_TEAM: raise BadRequestError(message='Cannot send letters with a team api key', status_code=403) - if api_key.service.restricted and api_key.key_type != KEY_TYPE_TEST: + 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) @@ -191,10 +194,19 @@ def process_letter_notification(*, letter_data, api_key, template, reply_to_text ) if api_key.service.has_permission('letters_as_pdf'): - create_letters_pdf.apply_async( - [str(notification.id)], - queue=QueueNames.CREATE_LETTERS_PDF - ) + if should_send: + create_letters_pdf.apply_async( + [str(notification.id)], + queue=QueueNames.CREATE_LETTERS_PDF + ) + elif (api_key.service.research_mode and + 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) 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 cda66f1c9..82728fb33 100644 --- a/tests/app/v2/notifications/test_post_letter_notifications.py +++ b/tests/app/v2/notifications/test_post_letter_notifications.py @@ -13,7 +13,7 @@ from app.models import KEY_TYPE_TEAM from app.models import KEY_TYPE_TEST from app.models import LETTER_TYPE from app.models import Notification -from app.models import NOTIFICATION_SENDING +from app.models import NOTIFICATION_SENDING, NOTIFICATION_DELIVERED from app.models import SMS_TYPE from app.schema_validation import validate from app.v2.errors import RateLimitError @@ -21,6 +21,7 @@ from app.v2.notifications.notification_schemas import post_letter_response from tests import create_authorization_header from tests.app.db import create_service, create_template, create_letter_contact +from tests.conftest import set_config_values test_address = { 'address_line_1': 'test 1', @@ -105,6 +106,127 @@ def test_post_letter_notification_for_letters_as_pdf_calls_celery_task(client, s fake_task.assert_called_once_with([str(notification.id)], queue=QueueNames.CREATE_LETTERS_PDF) +@pytest.mark.parametrize('env', [ + 'development', + 'preview', +]) +def test_post_letter_notification_for_letters_as_pdf_calls_create_fake_response_in_research_and_test_key_correct_env( + notify_api, client, sample_letter_template, mocker, env): + service_permissions_dao.dao_add_service_permission(sample_letter_template.service.id, 'letters_as_pdf') + sample_letter_template.service.research_mode = True + + data = { + 'template_id': str(sample_letter_template.id), + 'personalisation': { + 'address_line_1': 'Her Royal Highness Queen Elizabeth II', + 'address_line_2': 'Buckingham Palace', + 'address_line_3': 'London', + 'postcode': 'SW1 1AA', + 'name': 'Lizzie' + }, + 'reference': 'foo' + } + + fake_update_letter_noti_to_sent = mocker.patch( + 'app.celery.tasks.update_letter_notifications_to_sent_to_dvla.apply_async') + fake_create_letter_task = mocker.patch('app.celery.letters_pdf_tasks.create_letters_pdf.apply_async') + fake_create_dvla_response_task = mocker.patch( + 'app.celery.research_mode_tasks.create_fake_letter_response_file.apply_async') + + with set_config_values(notify_api, { + 'NOTIFY_ENVIRONMENT': env + }): + letter_request(client, data, service_id=sample_letter_template.service_id, key_type=KEY_TYPE_TEST) + + notification = Notification.query.one() + + assert fake_update_letter_noti_to_sent.called + assert not fake_create_letter_task.called + fake_create_dvla_response_task.assert_called_once_with((notification.reference,), queue=QueueNames.RESEARCH_MODE) + + +@pytest.mark.parametrize('env', [ + 'staging', + 'live', +]) +def test_post_letter_noti_for_letters_as_pdf_sets_status_delivered_in_research_and_test_key_incorrect_env( + notify_api, client, sample_letter_template, mocker, env): + service_permissions_dao.dao_add_service_permission(sample_letter_template.service.id, 'letters_as_pdf') + sample_letter_template.service.research_mode = True + + data = { + 'template_id': str(sample_letter_template.id), + 'personalisation': { + 'address_line_1': 'Her Royal Highness Queen Elizabeth II', + 'address_line_2': 'Buckingham Palace', + 'address_line_3': 'London', + 'postcode': 'SW1 1AA', + 'name': 'Lizzie' + }, + 'reference': 'foo' + } + + fake_update_letter_noti_to_sent = mocker.patch( + 'app.celery.tasks.update_letter_notifications_to_sent_to_dvla.apply_async') + fake_create_letter_task = mocker.patch('app.celery.letters_pdf_tasks.create_letters_pdf.apply_async') + fake_create_dvla_response_task = mocker.patch( + 'app.celery.research_mode_tasks.create_fake_letter_response_file.apply_async') + + with set_config_values(notify_api, { + 'NOTIFY_ENVIRONMENT': env + }): + letter_request(client, data, service_id=sample_letter_template.service_id, key_type=KEY_TYPE_TEST) + + notification = Notification.query.one() + + assert fake_update_letter_noti_to_sent.called + assert not fake_create_letter_task.called + assert not fake_create_dvla_response_task.called + assert notification.status == NOTIFICATION_DELIVERED + + +@pytest.mark.parametrize('env', [ + 'development', + 'preview', + 'staging', + 'live', +]) +def test_post_letter_noti_for_letters_as_pdf_sets_status_to_delivered_using_test_key_and_not_research_all_env( + notify_api, client, sample_letter_template, mocker, env): + service_permissions_dao.dao_add_service_permission(sample_letter_template.service.id, 'letters_as_pdf') + sample_letter_template.service.research_mode = False + + data = { + 'template_id': str(sample_letter_template.id), + 'personalisation': { + 'address_line_1': 'Her Royal Highness Queen Elizabeth II', + 'address_line_2': 'Buckingham Palace', + 'address_line_3': 'London', + 'postcode': 'SW1 1AA', + 'name': 'Lizzie' + }, + 'reference': 'foo' + } + + fake_update_letter_noti_to_sent = mocker.patch( + 'app.celery.tasks.update_letter_notifications_to_sent_to_dvla.apply_async') + fake_create_letter_task = mocker.patch('app.celery.letters_pdf_tasks.create_letters_pdf.apply_async') + fake_create_dvla_response_task = mocker.patch( + 'app.celery.research_mode_tasks.create_fake_letter_response_file.apply_async') + + with set_config_values(notify_api, { + 'NOTIFY_ENVIRONMENT': env + }): + letter_request(client, data, service_id=sample_letter_template.service_id, key_type=KEY_TYPE_TEST) + + notification = Notification.query.one() + + assert fake_update_letter_noti_to_sent.called + assert not fake_create_letter_task.called + assert not fake_create_dvla_response_task.called + assert notification.status == NOTIFICATION_DELIVERED + + def test_post_letter_notification_returns_400_and_missing_template( client, sample_service_full_permissions