diff --git a/app/celery/letters_pdf_tasks.py b/app/celery/letters_pdf_tasks.py index dafb126b3..ad0c767c2 100644 --- a/app/celery/letters_pdf_tasks.py +++ b/app/celery/letters_pdf_tasks.py @@ -55,51 +55,6 @@ from app.models import ( ) -@notify_celery.task(bind=True, name="get-pdf-for-templated-letter", max_retries=15, default_retry_delay=300) -def get_pdf_for_templated_letter(self, notification_id): - try: - notification = get_notification_by_id(notification_id, _raise=True) - letter_filename = generate_letter_pdf_filename( - reference=notification.reference, - created_at=notification.created_at, - ignore_folder=notification.key_type == KEY_TYPE_TEST, - postage=notification.postage - ) - letter_data = { - 'letter_contact_block': notification.reply_to_text, - 'template': { - "subject": notification.template.subject, - "content": notification.template.content, - "template_type": notification.template.template_type - }, - 'values': notification.personalisation, - 'logo_filename': notification.service.letter_branding and notification.service.letter_branding.filename, - 'letter_filename': letter_filename, - "notification_id": str(notification_id), - 'key_type': notification.key_type - } - - encrypted_data = encryption.encrypt(letter_data) - - notify_celery.send_task( - name=TaskNames.CREATE_PDF_FOR_TEMPLATED_LETTER, - args=(encrypted_data,), - queue=QueueNames.SANITISE_LETTERS - ) - except Exception as e: - try: - current_app.logger.exception( - f"RETRY: calling create-letter-pdf task for notification {notification_id} failed" - ) - self.retry(exc=e, queue=QueueNames.RETRY) - except self.MaxRetriesExceededError: - message = f"RETRY FAILED: Max retries reached. " \ - f"The task create-letter-pdf failed for notification id {notification_id}. " \ - f"Notification has been updated to technical-failure" - update_notification_status_by_id(notification_id, NOTIFICATION_TECHNICAL_FAILURE) - raise NotificationTechnicalFailureException(message) - - @notify_celery.task(bind=True, name="update-billable-units-for-letter", max_retries=15, default_retry_delay=300) def update_billable_units_for_letter(self, notification_id, page_count): notification = get_notification_by_id(notification_id, _raise=True) diff --git a/app/celery/scheduled_tasks.py b/app/celery/scheduled_tasks.py index 1baac023d..b337f2397 100644 --- a/app/celery/scheduled_tasks.py +++ b/app/celery/scheduled_tasks.py @@ -9,7 +9,6 @@ from sqlalchemy.exc import SQLAlchemyError from app import notify_celery, zendesk_client from app.aws import s3 -from app.celery.letters_pdf_tasks import get_pdf_for_templated_letter from app.celery.tasks import ( get_recipient_csv_and_template_and_sender_id, process_incomplete_jobs, @@ -165,20 +164,6 @@ def replay_created_notifications(): for n in notifications_to_resend: send_notification_to_queue(notification=n, research_mode=n.service.research_mode) - # if the letter has not be send after an hour, then create a zendesk ticket - letters = letters_missing_from_sending_bucket(resend_created_notifications_older_than) - - if len(letters) > 0: - msg = "{} letters were created over an hour ago, " \ - "but do not have an updated_at timestamp or billable units. " \ - "\n Creating app.celery.letters_pdf_tasks.create_letters tasks to upload letter to S3 " \ - "and update notifications for the following notification ids: " \ - "\n {}".format(len(letters), [x.id for x in letters]) - - current_app.logger.info(msg) - for letter in letters: - get_pdf_for_templated_letter.apply_async([str(letter.id)], queue=QueueNames.CREATE_LETTERS_PDF) - @notify_celery.task(name='check-for-missing-rows-in-completed-jobs') def check_for_missing_rows_in_completed_jobs(): diff --git a/app/celery/tasks.py b/app/celery/tasks.py index dd37229f1..676e71904 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -385,12 +385,7 @@ def save_letter( status=status ) - if not service.research_mode: - letters_pdf_tasks.get_pdf_for_templated_letter.apply_async( - [str(saved_notification.id)], - queue=QueueNames.CREATE_LETTERS_PDF - ) - elif current_app.config['NOTIFY_ENVIRONMENT'] in ['preview', 'development']: + if current_app.config['NOTIFY_ENVIRONMENT'] in ['preview', 'development']: research_mode_tasks.create_fake_letter_response_file.apply_async( (saved_notification.reference,), queue=QueueNames.RESEARCH_MODE diff --git a/app/commands.py b/app/commands.py index 11439fb98..bf2c6353b 100644 --- a/app/commands.py +++ b/app/commands.py @@ -19,10 +19,7 @@ from sqlalchemy.orm.exc import NoResultFound from app import db from app.aws import s3 -from app.celery.letters_pdf_tasks import ( - get_pdf_for_templated_letter, - resanitise_pdf, -) +from app.celery.letters_pdf_tasks import resanitise_pdf from app.celery.tasks import process_row, record_daily_sorted_counts from app.config import QueueNames from app.dao.annual_billing_dao import ( @@ -162,14 +159,6 @@ def insert_inbound_numbers_from_file(file_name): db.session.commit() -@notify_command(name='replay-create-pdf-for-templated-letter') -@click.option('-n', '--notification_id', type=click.UUID, required=True, - help="Notification id of the letter that needs the get_pdf_for_templated_letter task replayed") -def replay_create_pdf_for_templated_letter(notification_id): - print("Create task to get_pdf_for_templated_letter for notification: {}".format(notification_id)) - get_pdf_for_templated_letter.apply_async([str(notification_id)], queue=QueueNames.CREATE_LETTERS_PDF) - - @notify_command(name='recreate-pdf-for-precompiled-or-uploaded-letter') @click.option('-n', '--notification_id', type=click.UUID, required=True, help="Notification ID of the precompiled or uploaded letter") diff --git a/app/notifications/process_notifications.py b/app/notifications/process_notifications.py index 1c8d090e0..b831de7bb 100644 --- a/app/notifications/process_notifications.py +++ b/app/notifications/process_notifications.py @@ -17,7 +17,6 @@ from notifications_utils.template import ( from app import redis_store from app.celery import provider_tasks -from app.celery.letters_pdf_tasks import get_pdf_for_templated_letter from app.config import QueueNames from app.dao.notifications_dao import ( dao_create_notification, @@ -194,10 +193,6 @@ def send_notification_to_queue_detached( if not queue: queue = QueueNames.SEND_EMAIL deliver_task = provider_tasks.deliver_email - if notification_type == LETTER_TYPE: - if not queue: - queue = QueueNames.CREATE_LETTERS_PDF - deliver_task = get_pdf_for_templated_letter try: deliver_task.apply_async([str(notification_id)], queue=queue) diff --git a/app/v2/notifications/post_notifications.py b/app/v2/notifications/post_notifications.py index 756c4d846..2e51e639c 100644 --- a/app/v2/notifications/post_notifications.py +++ b/app/v2/notifications/post_notifications.py @@ -16,7 +16,6 @@ from app import ( notify_celery, ) from app.celery.letters_pdf_tasks import ( - get_pdf_for_templated_letter, sanitise_letter, ) from app.celery.research_mode_tasks import create_fake_letter_response_file @@ -385,11 +384,6 @@ def process_letter_notification( postage=postage ) - get_pdf_for_templated_letter.apply_async( - [str(notification.id)], - queue=queue - ) - if test_key and current_app.config['NOTIFY_ENVIRONMENT'] in ['preview', 'development']: create_fake_letter_response_file.apply_async( (notification.reference,), diff --git a/tests/app/celery/test_scheduled_tasks.py b/tests/app/celery/test_scheduled_tasks.py index bae2e0c5b..181c4181c 100644 --- a/tests/app/celery/test_scheduled_tasks.py +++ b/tests/app/celery/test_scheduled_tasks.py @@ -257,28 +257,6 @@ def test_replay_created_notifications(notify_db_session, sample_service, mocker) queue="send-sms-tasks") -def test_replay_created_notifications_get_pdf_for_templated_letter_tasks_for_letters_not_ready_to_send( - sample_letter_template, mocker -): - mock_task = mocker.patch('app.celery.scheduled_tasks.get_pdf_for_templated_letter.apply_async') - create_notification(template=sample_letter_template, billable_units=0, - created_at=datetime.utcnow() - timedelta(hours=4)) - - create_notification(template=sample_letter_template, billable_units=0, - created_at=datetime.utcnow() - timedelta(minutes=20)) - notification_1 = create_notification(template=sample_letter_template, billable_units=0, - created_at=datetime.utcnow() - timedelta(hours=1, minutes=20)) - notification_2 = create_notification(template=sample_letter_template, billable_units=0, - created_at=datetime.utcnow() - timedelta(hours=5)) - - replay_created_notifications() - - calls = [call([str(notification_1.id)], queue=QueueNames.CREATE_LETTERS_PDF), - call([str(notification_2.id)], queue=QueueNames.CREATE_LETTERS_PDF), - ] - mock_task.assert_has_calls(calls, any_order=True) - - def test_check_job_status_task_does_not_raise_error(sample_template): create_job( template=sample_template, diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index 4b3fe9bdb..3a92122fb 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -935,278 +935,6 @@ def test_save_sms_does_not_send_duplicate_and_does_not_put_in_retry_queue(sample assert not retry.called -@pytest.mark.parametrize('personalisation, expected_to, expected_normalised', ( - ({ - 'addressline1': 'Foo', - 'addressline2': 'Bar', - 'addressline3': 'Baz', - 'addressline4': 'Wibble', - 'addressline5': 'Wobble', - 'addressline6': 'Wubble', - 'postcode': 'SE1 2SA', - }, ( - 'Foo\n' - 'Bar\n' - 'Baz\n' - 'Wibble\n' - 'Wobble\n' - 'Wubble\n' - 'SE1 2SA' - ), ( - 'foobarbazwibblewobblewubblese12sa' - )), - ({ - # The address isn’t normalised when we store it in the - # `personalisation` column, but is normalised for storing in the - # `to` column - 'addressline2': ' Foo ', - 'addressline4': 'Bar', - 'addressline6': 'se12sa', - }, ( - 'Foo\n' - 'Bar\n' - 'SE1 2SA' - ), ( - 'foobarse12sa' - )), -)) -def test_save_letter_saves_letter_to_database( - mocker, - notify_db_session, - personalisation, - expected_to, - expected_normalised, -): - service = create_service() - contact_block = create_letter_contact(service=service, contact_block="Address contact", is_default=True) - template = create_template(service=service, template_type=LETTER_TYPE, reply_to=contact_block.id) - job = create_job(template=template) - - mocker.patch('app.celery.tasks.create_random_identifier', return_value="this-is-random-in-real-life") - mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - - notification_json = _notification_json( - template=job.template, - to='This is ignored for letters', - personalisation=personalisation, - job_id=job.id, - row_number=1 - ) - notification_id = uuid.uuid4() - created_at = datetime.utcnow() - - save_letter( - job.service_id, - notification_id, - encryption.encrypt(notification_json), - ) - - notification_db = Notification.query.one() - assert notification_db.id == notification_id - assert notification_db.to == expected_to - assert notification_db.normalised_to == expected_normalised - assert notification_db.job_id == job.id - assert notification_db.template_id == job.template.id - assert notification_db.template_version == job.template.version - assert notification_db.status == 'created' - assert notification_db.created_at >= created_at - assert notification_db.notification_type == 'letter' - assert notification_db.sent_at is None - assert notification_db.sent_by is None - assert notification_db.personalisation == personalisation - assert notification_db.reference == "this-is-random-in-real-life" - assert notification_db.reply_to_text == contact_block.contact_block - - -@pytest.mark.parametrize('last_line_of_address, postage, expected_postage, expected_international', - [('SW1 1AA', 'first', 'first', False), - ('SW1 1AA', 'second', 'second', False), - ('New Zealand', 'second', 'rest-of-world', True), - ('France', 'first', 'europe', True)]) -def test_save_letter_saves_letter_to_database_with_correct_postage( - mocker, notify_db_session, last_line_of_address, postage, expected_postage, expected_international -): - service = create_service(service_permissions=[LETTER_TYPE]) - template = create_template(service=service, template_type=LETTER_TYPE, postage=postage) - letter_job = create_job(template=template) - - mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - notification_json = _notification_json( - template=letter_job.template, - to='Foo', - personalisation={'addressline1': 'Foo', 'addressline2': 'Bar', 'postcode': last_line_of_address}, - job_id=letter_job.id, - row_number=1 - ) - notification_id = uuid.uuid4() - save_letter( - letter_job.service_id, - notification_id, - encryption.encrypt(notification_json), - ) - - notification_db = Notification.query.one() - assert notification_db.id == notification_id - assert notification_db.postage == expected_postage - assert notification_db.international == expected_international - - -@pytest.mark.parametrize('reference_paceholder,', [None, 'ref2']) -def test_save_letter_saves_letter_to_database_with_correct_client_reference( - mocker, notify_db_session, reference_paceholder -): - service = create_service(service_permissions=[LETTER_TYPE]) - template = create_template(service=service, template_type=LETTER_TYPE) - letter_job = create_job(template=template) - - personalisation = {'addressline1': 'Foo', 'addressline2': 'Bar', 'postcode': 'SW1A 1AA'} - if reference_paceholder: - personalisation['reference'] = reference_paceholder - - mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - notification_json = _notification_json( - template=letter_job.template, - to='Foo', - personalisation=personalisation, - job_id=letter_job.id, - row_number=1 - ) - notification_id = uuid.uuid4() - save_letter( - letter_job.service_id, - notification_id, - encryption.encrypt(notification_json), - ) - - notification_db = Notification.query.one() - assert notification_db.id == notification_id - assert notification_db.client_reference == reference_paceholder - - -def test_save_letter_saves_letter_to_database_with_formatted_postcode(mocker, notify_db_session): - service = create_service(service_permissions=[LETTER_TYPE]) - template = create_template(service=service, template_type=LETTER_TYPE) - letter_job = create_job(template=template) - - mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - notification_json = _notification_json( - template=letter_job.template, - to='Foo', - personalisation={'addressline1': 'Foo', 'addressline2': 'Bar', 'postcode': 'se1 64sa'}, - job_id=letter_job.id, - row_number=1 - ) - notification_id = uuid.uuid4() - save_letter( - letter_job.service_id, - notification_id, - encryption.encrypt(notification_json), - ) - - notification_db = Notification.query.one() - assert notification_db.id == notification_id - assert notification_db.personalisation["postcode"] == "se1 64sa" - - -def test_save_letter_saves_letter_to_database_right_reply_to(mocker, notify_db_session): - service = create_service() - create_letter_contact(service=service, contact_block="Address contact", is_default=True) - template = create_template(service=service, template_type=LETTER_TYPE, reply_to=None) - job = create_job(template=template) - - mocker.patch('app.celery.tasks.create_random_identifier', return_value="this-is-random-in-real-life") - mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - - personalisation = { - 'addressline1': 'Foo', - 'addressline2': 'Bar', - 'addressline3': 'Baz', - 'addressline4': 'Wibble', - 'addressline5': 'Wobble', - 'addressline6': 'Wubble', - 'postcode': 'SE1 3WS', - } - notification_json = _notification_json( - template=job.template, - to='Foo', - personalisation=personalisation, - job_id=job.id, - row_number=1 - ) - notification_id = uuid.uuid4() - created_at = datetime.utcnow() - - save_letter( - job.service_id, - notification_id, - encryption.encrypt(notification_json), - ) - - notification_db = Notification.query.one() - assert notification_db.id == notification_id - assert notification_db.to == ( - 'Foo\n' - 'Bar\n' - 'Baz\n' - 'Wibble\n' - 'Wobble\n' - 'Wubble\n' - 'SE1 3WS' - ) - assert notification_db.job_id == job.id - assert notification_db.template_id == job.template.id - assert notification_db.template_version == job.template.version - assert notification_db.status == 'created' - assert notification_db.created_at >= created_at - assert notification_db.notification_type == 'letter' - assert notification_db.sent_at is None - assert notification_db.sent_by is None - assert notification_db.personalisation == personalisation - assert notification_db.reference == "this-is-random-in-real-life" - assert not notification_db.reply_to_text - - -def test_save_letter_uses_template_reply_to_text(mocker, notify_db_session): - service = create_service() - create_letter_contact(service=service, contact_block="Address contact", is_default=True) - template_contact = create_letter_contact( - service=service, - contact_block="Template address contact", - is_default=False - ) - template = create_template( - service=service, - template_type=LETTER_TYPE, - reply_to=template_contact.id - ) - - job = create_job(template=template) - - mocker.patch('app.celery.tasks.create_random_identifier', return_value="this-is-random-in-real-life") - mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - - personalisation = { - 'addressline1': 'Foo', - 'addressline2': 'Bar', - 'postcode': 'Flob', - } - notification_json = _notification_json( - template=job.template, - to='Foo', - personalisation=personalisation, - job_id=job.id, - row_number=1 - ) - - save_letter( - job.service_id, - uuid.uuid4(), - encryption.encrypt(notification_json), - ) - - notification_db = Notification.query.one() - assert notification_db.reply_to_text == "Template address contact" - def test_save_sms_uses_sms_sender_reply_to_text(mocker, notify_db_session): service = create_service_with_defined_sms_sender(sms_sender_value='07123123123') @@ -1246,115 +974,6 @@ def test_save_sms_uses_non_default_sms_sender_reply_to_text_if_provided(mocker, assert persisted_notification.reply_to_text == 'new-sender' -@pytest.mark.skip(reason="Needs updating for TTS: Remove mail") -@pytest.mark.parametrize('env', ['staging', 'live']) -def test_save_letter_sets_delivered_letters_as_pdf_permission_in_research_mode_in_staging_live( - notify_api, mocker, notify_db_session, sample_letter_job, env): - sample_letter_job.service.research_mode = True - sample_reference = "this-is-random-in-real-life" - mock_create_fake_letter_response_file = mocker.patch( - 'app.celery.research_mode_tasks.create_fake_letter_response_file.apply_async') - mocker.patch('app.celery.tasks.create_random_identifier', return_value=sample_reference) - - personalisation = { - 'addressline1': 'Foo', - 'addressline2': 'Bar', - 'postcode': 'Flob', - } - notification_json = _notification_json( - template=sample_letter_job.template, - to='Foo', - personalisation=personalisation, - job_id=sample_letter_job.id, - row_number=1 - ) - notification_id = uuid.uuid4() - - with set_config_values(notify_api, { - 'NOTIFY_ENVIRONMENT': env - }): - save_letter( - sample_letter_job.service_id, - notification_id, - encryption.encrypt(notification_json), - ) - - notification = Notification.query.filter(Notification.id == notification_id).one() - assert notification.status == 'delivered' - assert not mock_create_fake_letter_response_file.called - - -@pytest.mark.skip(reason="Needs updating for TTS: Remove mail") -@pytest.mark.parametrize('env', ['development', 'preview']) -def test_save_letter_calls_create_fake_response_for_letters_in_research_mode_on_development_preview( - notify_api, mocker, notify_db_session, sample_letter_job, env): - sample_letter_job.service.research_mode = True - sample_reference = "this-is-random-in-real-life" - mock_create_fake_letter_response_file = mocker.patch( - 'app.celery.research_mode_tasks.create_fake_letter_response_file.apply_async') - mocker.patch('app.celery.tasks.create_random_identifier', return_value=sample_reference) - - personalisation = { - 'addressline1': 'Foo', - 'addressline2': 'Bar', - 'postcode': 'Flob', - } - notification_json = _notification_json( - template=sample_letter_job.template, - to='Foo', - personalisation=personalisation, - job_id=sample_letter_job.id, - row_number=1 - ) - notification_id = uuid.uuid4() - - with set_config_values(notify_api, { - 'NOTIFY_ENVIRONMENT': env - }): - save_letter( - sample_letter_job.service_id, - notification_id, - encryption.encrypt(notification_json), - ) - - mock_create_fake_letter_response_file.assert_called_once_with( - (sample_reference,), - queue=QueueNames.RESEARCH_MODE - ) - - -@pytest.mark.skip(reason="Needs updating for TTS: Remove mail") -def test_save_letter_calls_get_pdf_for_templated_letter_task_not_in_research( - mocker, notify_db_session, sample_letter_job): - mock_create_letters_pdf = mocker.patch('app.celery.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - - personalisation = { - 'addressline1': 'Foo', - 'addressline2': 'Bar', - 'postcode': 'Flob', - } - notification_json = _notification_json( - template=sample_letter_job.template, - to='Foo', - personalisation=personalisation, - job_id=sample_letter_job.id, - row_number=1 - ) - notification_id = uuid.uuid4() - - save_letter( - sample_letter_job.service_id, - notification_id, - encryption.encrypt(notification_json), - ) - - assert mock_create_letters_pdf.called - mock_create_letters_pdf.assert_called_once_with( - [str(notification_id)], - queue=QueueNames.CREATE_LETTERS_PDF - ) - - def test_should_cancel_job_if_service_is_inactive(sample_service, sample_job, mocker): diff --git a/tests/app/notifications/test_process_notification.py b/tests/app/notifications/test_process_notification.py index df7e9a5e1..b0548bda1 100644 --- a/tests/app/notifications/test_process_notification.py +++ b/tests/app/notifications/test_process_notification.py @@ -251,11 +251,9 @@ def test_persist_notification_sets_daily_limit_cache_if_one_does_not_exists( (True, None, 'sms', 'normal', 'research-mode-tasks', 'provider_tasks.deliver_sms'), (True, None, 'email', 'normal', 'research-mode-tasks', 'provider_tasks.deliver_email'), (True, None, 'email', 'team', 'research-mode-tasks', 'provider_tasks.deliver_email'), - (True, None, 'letter', 'normal', 'research-mode-tasks', 'letters_pdf_tasks.get_pdf_for_templated_letter'), (False, None, 'sms', 'normal', 'send-sms-tasks', 'provider_tasks.deliver_sms'), (False, None, 'email', 'normal', 'send-email-tasks', 'provider_tasks.deliver_email'), (False, None, 'sms', 'team', 'send-sms-tasks', 'provider_tasks.deliver_sms'), - (False, None, 'letter', 'normal', 'create-letters-pdf-tasks', 'letters_pdf_tasks.get_pdf_for_templated_letter'), (False, None, 'sms', 'test', 'research-mode-tasks', 'provider_tasks.deliver_sms'), (True, 'notify-internal-tasks', 'email', 'normal', 'research-mode-tasks', 'provider_tasks.deliver_email'), (False, 'notify-internal-tasks', 'sms', 'normal', 'notify-internal-tasks', 'provider_tasks.deliver_sms'), diff --git a/tests/app/service/send_notification/test_send_notification.py b/tests/app/service/send_notification/test_send_notification.py index 2dc6f7ae5..ced70f647 100644 --- a/tests/app/service/send_notification/test_send_notification.py +++ b/tests/app/service/send_notification/test_send_notification.py @@ -1229,32 +1229,7 @@ def test_post_notification_should_set_reply_to_text(client, sample_service, mock assert notifications[0].reply_to_text == expected_reply_to -@pytest.mark.parametrize('last_line_of_address, expected_postage, expected_international', - [('France', 'europe', True), - ('Canada', 'rest-of-world', True), - ('SW1 1AA', 'second', False)]) -def test_send_notification_should_send_international_letters( - sample_letter_template, mocker, last_line_of_address, expected_postage, expected_international -): - deliver_mock = mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - data = { - 'template_id': sample_letter_template.id, - 'personalisation': { - 'address_line_1': 'Jane', - 'address_line_2': 'Rue Vert', - 'address_line_3': last_line_of_address - }, - 'to': 'Jane', - 'created_by': sample_letter_template.service.created_by_id - } - - notification_id = send_one_off_notification(sample_letter_template.service_id, data) - assert deliver_mock.called - notification = Notification.query.get(notification_id['id']) - assert notification.postage == expected_postage - assert notification.international == expected_international - - +@pytest.mark.skip(reason="Rewrite without letters") @pytest.mark.parametrize('reference_paceholder,', [None, 'ref2']) def test_send_notification_should_set_client_reference_from_placeholder( sample_letter_template, mocker, reference_paceholder diff --git a/tests/app/v2/notifications/test_post_letter_notifications.py b/tests/app/v2/notifications/test_post_letter_notifications.py deleted file mode 100644 index 4687f5ae2..000000000 --- a/tests/app/v2/notifications/test_post_letter_notifications.py +++ /dev/null @@ -1,740 +0,0 @@ -import uuid -from unittest.mock import ANY - -import pytest -from flask import json, url_for - -from app.config import QueueNames -from app.models import ( - EMAIL_TYPE, - INTERNATIONAL_LETTERS, - KEY_TYPE_NORMAL, - KEY_TYPE_TEAM, - KEY_TYPE_TEST, - LETTER_TYPE, - NOTIFICATION_CREATED, - NOTIFICATION_DELIVERED, - NOTIFICATION_PENDING_VIRUS_CHECK, - NOTIFICATION_SENDING, - SMS_TYPE, - Job, - Notification, -) -from app.notifications.process_letter_notifications import ( - create_letter_notification, -) -from app.schema_validation import validate -from app.v2.errors import RateLimitError -from app.v2.notifications.notification_schemas import post_letter_response -from tests import create_service_authorization_header -from tests.app.db import create_letter_contact, create_service, create_template -from tests.conftest import set_config_values - -test_address = { - 'address_line_1': 'test 1', - 'address_line_2': 'test 2', - 'postcode': 'test pc' -} - - -def letter_request(client, data, service_id, key_type=KEY_TYPE_NORMAL, _expected_status=201, precompiled=False): - if precompiled: - url = url_for('v2_notifications.post_precompiled_letter_notification') - else: - url = url_for('v2_notifications.post_notification', notification_type=LETTER_TYPE) - resp = client.post( - url, - data=json.dumps(data), - headers=[ - ('Content-Type', 'application/json'), - create_service_authorization_header(service_id=service_id, key_type=key_type) - ] - ) - json_resp = json.loads(resp.get_data(as_text=True)) - assert resp.status_code == _expected_status, json_resp - return json_resp - - -@pytest.mark.parametrize('reference', [None, 'reference_from_client']) -def test_post_letter_notification_returns_201(client, sample_letter_template, mocker, reference): - mock = mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - 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' - } - } - - if reference: - data.update({'reference': reference}) - - resp_json = letter_request(client, data, service_id=sample_letter_template.service_id) - - assert validate(resp_json, post_letter_response) == resp_json - assert Job.query.count() == 0 - notification = Notification.query.one() - assert notification.status == NOTIFICATION_CREATED - assert resp_json['id'] == str(notification.id) - assert resp_json['reference'] == reference - assert resp_json['content']['subject'] == sample_letter_template.subject - assert resp_json['content']['body'] == sample_letter_template.content - assert 'v2/notifications/{}'.format(notification.id) in resp_json['uri'] - assert resp_json['template']['id'] == str(sample_letter_template.id) - assert resp_json['template']['version'] == sample_letter_template.version - assert ( - 'services/{}/templates/{}'.format( - sample_letter_template.service_id, - sample_letter_template.id - ) in resp_json['template']['uri'] - ) - assert not resp_json['scheduled_for'] - assert not notification.reply_to_text - mock.assert_called_once_with([str(notification.id)], queue=QueueNames.CREATE_LETTERS_PDF) - - -def test_post_letter_notification_sets_postage( - client, notify_db_session, mocker -): - service = create_service(service_permissions=[LETTER_TYPE]) - template = create_template(service, template_type="letter", postage="first") - mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - data = { - 'template_id': str(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' - } - } - - resp_json = letter_request(client, data, service_id=service.id) - - assert validate(resp_json, post_letter_response) == resp_json - notification = Notification.query.one() - assert notification.postage == "first" - - -def test_post_letter_notification_formats_postcode( - client, notify_db_session, mocker -): - service = create_service(service_permissions=[LETTER_TYPE]) - template = create_template(service, template_type="letter") - mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - data = { - 'template_id': str(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' - } - } - - resp_json = letter_request(client, data, service_id=service.id) - - assert validate(resp_json, post_letter_response) == resp_json - notification = Notification.query.one() - # We store what the client gives us, and only reformat it when - # generating the PDF - assert notification.personalisation["postcode"] == ' Sw1 1aa ' - - -def test_post_letter_notification_stores_country( - client, notify_db_session, mocker -): - service = create_service(service_permissions=[LETTER_TYPE, INTERNATIONAL_LETTERS]) - template = create_template(service, template_type="letter") - mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - data = { - 'template_id': str(template.id), - 'personalisation': { - 'address_line_1': 'Kaiser Wilhelm II', - 'address_line_2': 'Kronprinzenpalais', - 'address_line_5': ' deutschland ', - } - } - - resp_json = letter_request(client, data, service_id=service.id) - - assert validate(resp_json, post_letter_response) == resp_json - notification = Notification.query.one() - # In the personalisation we store what the client gives us - assert notification.personalisation["address_line_1"] == 'Kaiser Wilhelm II' - assert notification.personalisation["address_line_2"] == 'Kronprinzenpalais' - assert notification.personalisation["address_line_5"] == ' deutschland ' - # In the to field we store the whole address with the canonical country - assert notification.to == ( - 'Kaiser Wilhelm II\n' - 'Kronprinzenpalais\n' - 'Germany' - ) - assert notification.postage == 'europe' - assert notification.international - - -def test_post_letter_notification_international_sets_rest_of_world( - client, notify_db_session, mocker -): - service = create_service(service_permissions=[LETTER_TYPE, INTERNATIONAL_LETTERS]) - template = create_template(service, template_type="letter") - mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - data = { - 'template_id': str(template.id), - 'personalisation': { - 'address_line_1': 'Prince Harry', - 'address_line_2': 'Toronto', - 'address_line_5': 'Canada', - } - } - - resp_json = letter_request(client, data, service_id=service.id) - - assert validate(resp_json, post_letter_response) == resp_json - notification = Notification.query.one() - - assert notification.postage == 'rest-of-world' - - -@pytest.mark.parametrize('permissions, personalisation, expected_error', ( - ( - [LETTER_TYPE], - { - 'address_line_1': 'Her Royal Highness Queen Elizabeth II', - 'address_line_2': 'Buckingham Palace', - 'address_line_3': 'London', - 'postcode': 'not a real postcode', - 'name': 'Lizzie' - }, - 'Must be a real UK postcode', - ), - ( - [LETTER_TYPE], - { - 'address_line_1': 'Her Royal Highness Queen Elizabeth II', - 'address_line_2': ']Buckingham Palace', - 'postcode': 'SW1A 1AA', - 'name': 'Lizzie' - }, - 'Address lines must not start with any of the following characters: @ ( ) = [ ] ” \\ / , < >', - ), - ( - [LETTER_TYPE, INTERNATIONAL_LETTERS], - { - 'address_line_1': 'Her Royal Highness Queen Elizabeth II', - 'address_line_2': 'Buckingham Palace', - 'address_line_3': 'London', - 'postcode': 'not a real postcode', - 'name': 'Lizzie' - }, - 'Last line of address must be a real UK postcode or another country', - ), -)) -def test_post_letter_notification_throws_error_for_bad_address( - client, notify_db_session, mocker, permissions, personalisation, expected_error -): - service = create_service(service_permissions=permissions) - template = create_template(service, template_type="letter", postage="first") - mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - data = { - 'template_id': str(template.id), - 'personalisation': personalisation - } - - error_json = letter_request(client, data, service_id=service.id, _expected_status=400) - - assert error_json['status_code'] == 400 - assert error_json['errors'] == [{ - 'error': 'ValidationError', - 'message': expected_error - }] - - -@pytest.mark.parametrize('env', [ - 'staging', - 'live', -]) -def test_post_letter_notification_with_test_key_creates_pdf_and_sets_status_to_delivered( - notify_api, client, sample_letter_template, mocker, env): - - 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_create_letter_task = mocker.patch('app.celery.letters_pdf_tasks.get_pdf_for_templated_letter.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() - - 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 - assert notification.updated_at is not None - - -@pytest.mark.parametrize('env', [ - 'development', - 'preview', -]) -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 = { - '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_create_letter_task = mocker.patch('app.celery.letters_pdf_tasks.get_pdf_for_templated_letter.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() - - 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 - - -def test_post_letter_notification_returns_400_and_missing_template( - client, - sample_service_full_permissions -): - data = { - 'template_id': str(uuid.uuid4()), - 'personalisation': test_address - } - - error_json = letter_request(client, data, service_id=sample_service_full_permissions.id, _expected_status=400) - - assert error_json['status_code'] == 400 - assert error_json['errors'] == [{'error': 'BadRequestError', 'message': 'Template not found'}] - - -def test_post_letter_notification_returns_400_for_empty_personalisation( - client, - sample_service_full_permissions, - sample_letter_template -): - data = { - 'template_id': str(sample_letter_template.id), - 'personalisation': {'address_line_1': '', 'address_line_2': '', 'postcode': ''} - } - - error_json = letter_request(client, data, service_id=sample_service_full_permissions.id, _expected_status=400) - - assert error_json['status_code'] == 400 - assert all([e['error'] == 'ValidationError' for e in error_json['errors']]) - assert set([e['message'] for e in error_json['errors']]) == { - 'Address must be at least 3 lines', - } - - -def test_post_notification_returns_400_for_missing_letter_contact_block_personalisation( - client, - sample_service, -): - letter_contact_block = create_letter_contact( - service=sample_service, contact_block='((contact block))', is_default=True - ) - template = create_template( - service=sample_service, - template_type='letter', - reply_to=letter_contact_block.id, - ) - data = { - 'template_id': str(template.id), - 'personalisation': { - 'address_line_1': 'Line 1', - 'address_line_2': 'Line 2', - 'postcode': 'SW1A 1AA', - }, - } - - error_json = letter_request( - client, - data, - service_id=sample_service.id, - _expected_status=400, - ) - - assert error_json['status_code'] == 400 - assert error_json['errors'] == [{ - 'error': 'BadRequestError', - 'message': 'Missing personalisation: contact block' - }] - - -def test_notification_returns_400_for_missing_template_field( - client, - sample_service_full_permissions -): - data = { - 'personalisation': test_address - } - - error_json = letter_request(client, data, service_id=sample_service_full_permissions.id, _expected_status=400) - - assert error_json['status_code'] == 400 - assert error_json['errors'] == [{ - 'error': 'ValidationError', - 'message': 'template_id is a required property' - }] - - -def test_notification_returns_400_if_address_doesnt_have_underscores( - client, - sample_letter_template -): - data = { - 'template_id': str(sample_letter_template.id), - 'personalisation': { - 'address line 1': 'Her Royal Highness Queen Elizabeth II', - 'address-line-2': 'Buckingham Palace', - 'postcode': 'SW1 1AA', - } - } - - error_json = letter_request(client, data, service_id=sample_letter_template.service_id, _expected_status=400) - - assert error_json['status_code'] == 400 - assert error_json['errors'] == [ - { - 'error': 'ValidationError', - 'message': 'Address must be at least 3 lines' - } - ] - - -def test_returns_a_429_limit_exceeded_if_rate_limit_exceeded( - client, - sample_letter_template, - mocker -): - persist_mock = mocker.patch('app.v2.notifications.post_notifications.persist_notification') - mocker.patch( - 'app.v2.notifications.post_notifications.check_rate_limiting', - side_effect=RateLimitError('LIMIT', 'INTERVAL', 'TYPE') - ) - - data = { - 'template_id': str(sample_letter_template.id), - 'personalisation': test_address - } - - error_json = letter_request(client, data, service_id=sample_letter_template.service_id, _expected_status=429) - - assert error_json['status_code'] == 429 - assert error_json['errors'] == [{ - 'error': 'RateLimitError', - 'message': 'Exceeded rate limit for key type TYPE of LIMIT requests per INTERVAL seconds' - }] - - assert not persist_mock.called - - -@pytest.mark.parametrize('service_args, expected_status, expected_message', [ - ( - {'service_permissions': [EMAIL_TYPE, SMS_TYPE]}, - 400, - 'Service is not allowed to send letters', - ), - ( - {'restricted': True}, - 403, - 'Cannot send letters when service is in trial mode', - ) -]) -def test_post_letter_notification_returns_403_if_not_allowed_to_send_notification( - client, - notify_db_session, - service_args, - expected_status, - expected_message, -): - service = create_service(**service_args) - template = create_template(service, template_type=LETTER_TYPE) - - data = { - 'template_id': str(template.id), - 'personalisation': test_address - } - - error_json = letter_request(client, data, service_id=service.id, _expected_status=expected_status) - assert error_json['status_code'] == expected_status - assert error_json['errors'] == [ - {'error': 'BadRequestError', 'message': expected_message} - ] - - -def test_post_letter_notification_doesnt_accept_team_key(client, sample_letter_template, mocker): - mocker.patch('app.celery.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - data = { - 'template_id': str(sample_letter_template.id), - 'personalisation': {'address_line_1': 'Foo', 'address_line_2': 'Bar', 'postcode': 'Baz'} - } - - error_json = letter_request( - client, - data, - sample_letter_template.service_id, - key_type=KEY_TYPE_TEAM, - _expected_status=403 - ) - - assert error_json['status_code'] == 403 - assert error_json['errors'] == [{'error': 'BadRequestError', 'message': 'Cannot send letters with a team api key'}] - - -def test_post_letter_notification_doesnt_send_in_trial(client, sample_trial_letter_template, mocker): - mocker.patch('app.celery.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - data = { - 'template_id': str(sample_trial_letter_template.id), - 'personalisation': {'address_line_1': 'Foo', 'address_line_2': 'Bar', 'postcode': 'Baz'} - } - - error_json = letter_request( - client, - data, - sample_trial_letter_template.service_id, - _expected_status=403 - ) - - assert error_json['status_code'] == 403 - assert error_json['errors'] == [ - {'error': 'BadRequestError', 'message': 'Cannot send letters when service is in trial mode'}] - - -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 -): - fake_create_letter_task = mocker.patch('app.celery.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - - data = { - "template_id": sample_trial_letter_template.id, - "personalisation": {'address_line_1': 'Foo', 'address_line_2': 'Bar', 'postcode': 'BA5 5AB'} - } - - letter_request(client, data=data, service_id=sample_trial_letter_template.service_id, key_type=KEY_TYPE_TEST) - - notification = Notification.query.one() - assert notification.status == NOTIFICATION_DELIVERED - 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( - client, - notify_user, - mocker -): - sample_letter_service = create_service(service_permissions=['letter']) - mocker.patch('app.celery.letters_pdf_tasks.notify_celery.send_task') - s3mock = mocker.patch('app.v2.notifications.post_notifications.upload_letter_pdf', return_value='test.pdf') - data = { - "reference": "letter-reference", - "content": "bGV0dGVyLWNvbnRlbnQ=" - } - letter_request( - client, - data=data, - service_id=str(sample_letter_service.id), - key_type=KEY_TYPE_TEST, - precompiled=True) - - notification = Notification.query.one() - assert notification.status == NOTIFICATION_PENDING_VIRUS_CHECK - s3mock.assert_called_once_with(ANY, b'letter-content', precompiled=True) - - -def test_post_letter_notification_ignores_reply_to_text_for_service( - client, notify_db_session, mocker -): - mocker.patch('app.celery.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - - service = create_service(service_permissions=[LETTER_TYPE]) - create_letter_contact(service=service, contact_block='ignored', is_default=True) - template = create_template(service=service, template_type='letter') - data = { - "template_id": template.id, - "personalisation": {'address_line_1': 'Foo', 'address_line_2': 'Bar', 'postcode': 'BA5 5AB'} - } - letter_request(client, data=data, service_id=service.id, key_type=KEY_TYPE_NORMAL) - - notifications = Notification.query.all() - assert len(notifications) == 1 - assert notifications[0].reply_to_text is None - - -def test_post_letter_notification_persists_notification_reply_to_text_for_template( - client, notify_db_session, mocker -): - mocker.patch('app.celery.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - - service = create_service(service_permissions=[LETTER_TYPE]) - create_letter_contact(service=service, contact_block='the default', is_default=True) - template_letter_contact = create_letter_contact(service=service, contact_block='not the default', is_default=False) - template = create_template(service=service, template_type='letter', reply_to=template_letter_contact.id) - data = { - "template_id": template.id, - "personalisation": {'address_line_1': 'Foo', 'address_line_2': 'Bar', 'postcode': 'BA5 5AB'} - } - letter_request(client, data=data, service_id=service.id, key_type=KEY_TYPE_NORMAL) - - notifications = Notification.query.all() - assert len(notifications) == 1 - assert notifications[0].reply_to_text == 'not the default' - - -def test_post_precompiled_letter_with_invalid_base64(client, notify_user, mocker): - sample_service = create_service(service_permissions=['letter']) - mocker.patch('app.v2.notifications.post_notifications.upload_letter_pdf') - - data = { - "reference": "letter-reference", - "content": "hi" - } - auth_header = create_service_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 == 400, response.get_data(as_text=True) - resp_json = json.loads(response.get_data(as_text=True)) - assert resp_json['errors'][0]['message'] == 'Cannot decode letter content (invalid base64 encoding)' - - assert not Notification.query.first() - - -@pytest.mark.parametrize('notification_postage, expected_postage', [ - ('second', 'second'), - ('first', 'first'), - (None, 'second') -]) -def test_post_precompiled_letter_notification_returns_201( - client, notify_user, mocker, notification_postage, expected_postage -): - sample_service = create_service(service_permissions=['letter']) - s3mock = mocker.patch('app.v2.notifications.post_notifications.upload_letter_pdf') - mocker.patch('app.celery.letters_pdf_tasks.notify_celery.send_task') - data = { - "reference": "letter-reference", - "content": "bGV0dGVyLWNvbnRlbnQ=" - } - if notification_postage: - data["postage"] = notification_postage - auth_header = create_service_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) - - s3mock.assert_called_once_with(ANY, b'letter-content', precompiled=True) - - notification = Notification.query.one() - - assert notification.billable_units == 0 - assert notification.status == NOTIFICATION_PENDING_VIRUS_CHECK - assert notification.postage == expected_postage - - resp_json = json.loads(response.get_data(as_text=True)) - assert resp_json == {'id': str(notification.id), 'reference': 'letter-reference', 'postage': expected_postage} - - -def test_post_precompiled_letter_notification_if_s3_upload_fails_notification_is_not_persisted( - client, notify_user, mocker -): - sample_service = create_service(service_permissions=['letter']) - persist_letter_mock = mocker.patch('app.v2.notifications.post_notifications.create_letter_notification', - side_effect=create_letter_notification) - s3mock = mocker.patch('app.v2.notifications.post_notifications.upload_letter_pdf', side_effect=Exception()) - mocker.patch('app.celery.letters_pdf_tasks.notify_celery.send_task') - data = { - "reference": "letter-reference", - "content": "bGV0dGVyLWNvbnRlbnQ=" - } - - auth_header = create_service_authorization_header(service_id=sample_service.id) - with pytest.raises(expected_exception=Exception): - client.post( - path="v2/notifications/letter", - data=json.dumps(data), - headers=[('Content-Type', 'application/json'), auth_header]) - - assert s3mock.called - assert persist_letter_mock.called - assert Notification.query.count() == 0 - - -def test_post_letter_notification_throws_error_for_invalid_postage(client, notify_user, mocker): - sample_service = create_service(service_permissions=['letter']) - data = { - "reference": "letter-reference", - "content": "bGV0dGVyLWNvbnRlbnQ=", - "postage": "space unicorn" - } - auth_header = create_service_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 == 400, response.get_data(as_text=True) - resp_json = json.loads(response.get_data(as_text=True)) - assert resp_json['errors'][0]['message'] == "postage invalid. It must be first, second, europe or rest-of-world." - - assert not Notification.query.first() - - -@pytest.mark.parametrize('content_type', - ['application/json', 'application/text']) -def test_post_letter_notification_when_payload_is_invalid_json_returns_400( - client, sample_service, content_type): - auth_header = create_service_authorization_header(service_id=sample_service.id) - payload_not_json = { - "template_id": "dont-convert-to-json", - } - response = client.post( - path='/v2/notifications/letter', - data=payload_not_json, - headers=[('Content-Type', content_type), auth_header], - ) - - assert response.status_code == 400 - error_msg = json.loads(response.get_data(as_text=True))["errors"][0]["message"] - - assert error_msg == 'Invalid JSON supplied in POST data' diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index e5b2ab073..3fd8a4c00 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -1156,32 +1156,3 @@ def test_post_notifications_doesnt_use_save_queue_for_test_notifications( assert mock_send_task.called assert not save_task.called assert len(Notification.query.all()) == 1 - - -def test_post_notification_does_not_use_save_queue_for_letters(client, sample_letter_template, mocker): - mock_save = mocker.patch("app.v2.notifications.post_notifications.save_email_or_sms_to_queue") - mock_create_pdf_task = mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async') - - with set_config_values(current_app, { - 'HIGH_VOLUME_SERVICE': [str(sample_letter_template.service_id)], - - }): - 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', - } - } - response = client.post( - path='/v2/notifications/letter', - data=json.dumps(data), - headers=[('Content-Type', 'application/json'), - create_service_authorization_header(service_id=sample_letter_template.service_id)] - ) - assert response.status_code == 201 - json_resp = response.get_json() - assert not mock_save.called - mock_create_pdf_task.assert_called_once_with([str(json_resp['id'])], queue='create-letters-pdf-tasks')