From b08dc4375df009139ecb1d67736d90e9b9358e9b Mon Sep 17 00:00:00 2001 From: stvnrlly Date: Wed, 7 Dec 2022 12:21:32 -0500 Subject: [PATCH] remove update-letter-notifications-statuses task --- app/celery/tasks.py | 16 --- .../notifications_letter_callback.py | 2 - tests/app/celery/test_ftp_update_tasks.py | 118 ------------------ .../test_notifications_letter_callbacks.py | 53 -------- 4 files changed, 189 deletions(-) diff --git a/app/celery/tasks.py b/app/celery/tasks.py index 937596d03..cafce5b2e 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -446,22 +446,6 @@ def handle_exception(task, notification, notification_id, exc): current_app.logger.error('Max retry failed' + retry_msg) -@notify_celery.task(bind=True, name='update-letter-notifications-statuses') -def update_letter_notifications_statuses(self, filename): - notification_updates = parse_dvla_file(filename) - - temporary_failures = [] - - for update in notification_updates: - check_billable_units(update) - update_letter_notification(filename, temporary_failures, update) - if temporary_failures: - # This will alert Notify that DVLA was unable to deliver the letters, we need to investigate - message = "DVLA response file: {filename} has failed letters with notification.reference {failures}" \ - .format(filename=filename, failures=temporary_failures) - raise DVLAException(message) - - @notify_celery.task(bind=True, name="record-daily-sorted-counts") def record_daily_sorted_counts(self, filename): sorted_letter_counts = defaultdict(int) diff --git a/app/notifications/notifications_letter_callback.py b/app/notifications/notifications_letter_callback.py index 9108d5c45..0e343cb0e 100644 --- a/app/notifications/notifications_letter_callback.py +++ b/app/notifications/notifications_letter_callback.py @@ -5,7 +5,6 @@ from flask import Blueprint, current_app, jsonify, request from app.celery.tasks import ( record_daily_sorted_counts, - update_letter_notifications_statuses, ) from app.config import QueueNames from app.notifications.utils import autoconfirm_subscription @@ -53,7 +52,6 @@ def process_letter_response(): if filename.lower().endswith('rs.txt') or filename.lower().endswith('rsp.txt'): current_app.logger.info('DVLA callback: Calling task to update letter notifications') - update_letter_notifications_statuses.apply_async([filename], queue=QueueNames.NOTIFY) record_daily_sorted_counts.apply_async([filename], queue=QueueNames.NOTIFY) return jsonify( diff --git a/tests/app/celery/test_ftp_update_tasks.py b/tests/app/celery/test_ftp_update_tasks.py index d973f7a93..862c17aae 100644 --- a/tests/app/celery/test_ftp_update_tasks.py +++ b/tests/app/celery/test_ftp_update_tasks.py @@ -12,7 +12,6 @@ from app.celery.tasks import ( persist_daily_sorted_letter_counts, process_updates_from_file, record_daily_sorted_counts, - update_letter_notifications_statuses, update_letter_notifications_to_error, update_letter_notifications_to_sent_to_dvla, ) @@ -46,123 +45,6 @@ def notification_update(): return NotificationUpdate('REFERENCE_ABC', 'sent', '1', 'cost') -def test_update_letter_notifications_statuses_raises_for_invalid_format(notify_api, mocker): - invalid_file = 'ref-foo|Sent|1|Unsorted\nref-bar|Sent|2' - mocker.patch('app.celery.tasks.s3.get_s3_file', return_value=invalid_file) - - with pytest.raises(DVLAException) as e: - update_letter_notifications_statuses(filename='NOTIFY-20170823160812-RSP.TXT') - assert 'DVLA response file: {} has an invalid format'.format('NOTIFY-20170823160812-RSP.TXT') in str(e.value) - - -def test_update_letter_notification_statuses_when_notification_does_not_exist_updates_notification_history( - sample_letter_template, - mocker -): - valid_file = 'ref-foo|Sent|1|Unsorted' - mocker.patch('app.celery.tasks.s3.get_s3_file', return_value=valid_file) - notification = create_notification_history(sample_letter_template, reference='ref-foo', status=NOTIFICATION_SENDING, - billable_units=1) - - update_letter_notifications_statuses(filename="NOTIFY-20170823160812-RSP.TXT") - - updated_history = NotificationHistory.query.filter_by(id=notification.id).one() - assert updated_history.status == NOTIFICATION_DELIVERED - - -def test_update_letter_notifications_statuses_raises_dvla_exception(notify_api, mocker, sample_letter_template): - valid_file = 'ref-foo|Failed|1|Unsorted' - mocker.patch('app.celery.tasks.s3.get_s3_file', return_value=valid_file) - create_notification(sample_letter_template, reference='ref-foo', status=NOTIFICATION_SENDING, - billable_units=0) - - with pytest.raises(DVLAException) as e: - update_letter_notifications_statuses(filename="failed.txt") - failed = ["ref-foo"] - assert "DVLA response file: {filename} has failed letters with notification.reference {failures}".format( - filename="failed.txt", failures=failed - ) in str(e.value) - - -def test_update_letter_notifications_statuses_calls_with_correct_bucket_location(notify_api, mocker): - s3_mock = mocker.patch('app.celery.tasks.s3.get_s3_object') - - with set_config(notify_api, 'NOTIFY_EMAIL_DOMAIN', 'foo.bar'): - update_letter_notifications_statuses(filename='NOTIFY-20170823160812-RSP.TXT') - s3_mock.assert_called_with('{}-ftp'.format( - current_app.config['NOTIFY_EMAIL_DOMAIN']), - 'NOTIFY-20170823160812-RSP.TXT', - os.environ.get('AWS_ACCESS_KEY_ID'), - os.environ.get('AWS_SECRET_ACCESS_KEY'), - os.environ.get('AWS_REGION'), - ) - - -def test_update_letter_notifications_statuses_builds_updates_from_content(notify_api, mocker): - valid_file = 'ref-foo|Sent|1|Unsorted\nref-bar|Sent|2|Sorted' - mocker.patch('app.celery.tasks.s3.get_s3_file', return_value=valid_file) - update_mock = mocker.patch('app.celery.tasks.process_updates_from_file') - - update_letter_notifications_statuses(filename='NOTIFY-20170823160812-RSP.TXT') - - update_mock.assert_called_with('ref-foo|Sent|1|Unsorted\nref-bar|Sent|2|Sorted') - - -def test_update_letter_notifications_statuses_builds_updates_list(notify_api, mocker): - valid_file = 'ref-foo|Sent|1|Unsorted\nref-bar|Sent|2|Sorted' - updates = process_updates_from_file(valid_file) - - assert len(updates) == 2 - - assert updates[0].reference == 'ref-foo' - assert updates[0].status == 'Sent' - assert updates[0].page_count == '1' - assert updates[0].cost_threshold == 'Unsorted' - - assert updates[1].reference == 'ref-bar' - assert updates[1].status == 'Sent' - assert updates[1].page_count == '2' - assert updates[1].cost_threshold == 'Sorted' - - -def test_update_letter_notifications_statuses_persisted(notify_api, mocker, sample_letter_template): - sent_letter = create_notification(sample_letter_template, reference='ref-foo', status=NOTIFICATION_SENDING, - billable_units=1) - failed_letter = create_notification(sample_letter_template, reference='ref-bar', status=NOTIFICATION_SENDING, - billable_units=2) - create_service_callback_api(service=sample_letter_template.service, url="https://original_url.com") - valid_file = '{}|Sent|1|Unsorted\n{}|Failed|2|Sorted'.format( - sent_letter.reference, failed_letter.reference) - mocker.patch('app.celery.tasks.s3.get_s3_file', return_value=valid_file) - - with pytest.raises(expected_exception=DVLAException) as e: - update_letter_notifications_statuses(filename='NOTIFY-20170823160812-RSP.TXT') - - assert sent_letter.status == NOTIFICATION_DELIVERED - assert sent_letter.billable_units == 1 - assert sent_letter.updated_at - assert failed_letter.status == NOTIFICATION_TEMPORARY_FAILURE - assert failed_letter.billable_units == 2 - assert failed_letter.updated_at - assert "DVLA response file: {filename} has failed letters with notification.reference {failures}".format( - filename="NOTIFY-20170823160812-RSP.TXT", failures=[format(failed_letter.reference)]) in str(e.value) - - -def test_update_letter_notifications_does_not_call_send_callback_if_no_db_entry(notify_api, mocker, - sample_letter_template): - sent_letter = create_notification(sample_letter_template, reference='ref-foo', status=NOTIFICATION_SENDING, - billable_units=0) - valid_file = '{}|Sent|1|Unsorted\n'.format(sent_letter.reference) - mocker.patch('app.celery.tasks.s3.get_s3_file', return_value=valid_file) - - send_mock = mocker.patch( - 'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async' - ) - - update_letter_notifications_statuses(filename='NOTIFY-20170823160812-RSP.TXT') - send_mock.assert_not_called() - - def test_update_letter_notifications_to_sent_to_dvla_updates_based_on_notification_references( client, sample_letter_template diff --git a/tests/app/notifications/test_notifications_letter_callbacks.py b/tests/app/notifications/test_notifications_letter_callbacks.py index c0d1a1321..9fd0fb01d 100644 --- a/tests/app/notifications/test_notifications_letter_callbacks.py +++ b/tests/app/notifications/test_notifications_letter_callbacks.py @@ -25,59 +25,6 @@ def test_dvla_callback_autoconfirms_subscription(client, mocker): assert autoconfirm_mock.called -def test_dvla_callback_autoconfirm_does_not_call_update_letter_notifications_task(client, mocker): - autoconfirm_mock = mocker.patch('app.notifications.notifications_letter_callback.autoconfirm_subscription') - update_task = \ - mocker.patch('app.notifications.notifications_letter_callback.update_letter_notifications_statuses.apply_async') - - data = _sns_confirmation_callback() - response = dvla_post(client, data) - - assert response.status_code == 200 - assert autoconfirm_mock.called - assert not update_task.called - - -def test_dvla_callback_calls_does_not_update_letter_notifications_task_with_invalid_file_type(client, mocker): - update_task = \ - mocker.patch('app.notifications.notifications_letter_callback.update_letter_notifications_statuses.apply_async') - - data = _sample_sns_s3_callback("bar.txt") - response = dvla_post(client, data) - - assert response.status_code == 200 - assert not update_task.called - - -@pytest.mark.parametrize("filename", - ['Notify-20170411153023-rs.txt', 'Notify-20170411153023-rsp.txt']) -def test_dvla_rs_and_rsp_txt_file_callback_calls_update_letter_notifications_task(client, mocker, filename): - update_task = mocker.patch( - 'app.notifications.notifications_letter_callback.update_letter_notifications_statuses.apply_async') - daily_sorted_counts_task = mocker.patch( - 'app.notifications.notifications_letter_callback.record_daily_sorted_counts.apply_async') - data = _sample_sns_s3_callback(filename) - response = dvla_post(client, data) - - assert response.status_code == 200 - assert update_task.called - update_task.assert_called_with([filename], queue='notify-internal-tasks') - daily_sorted_counts_task.assert_called_with([filename], queue='notify-internal-tasks') - - -def test_dvla_ack_calls_does_not_call_letter_notifications_task(client, mocker): - update_task = mocker.patch( - 'app.notifications.notifications_letter_callback.update_letter_notifications_statuses.apply_async') - daily_sorted_counts_task = mocker.patch( - 'app.notifications.notifications_letter_callback.record_daily_sorted_counts.apply_async') - data = _sample_sns_s3_callback('bar.ack.txt') - response = dvla_post(client, data) - - assert response.status_code == 200 - update_task.assert_not_called() - daily_sorted_counts_task.assert_not_called() - - def _sample_sns_s3_callback(filename): message_contents = '''{"Records":[{"eventVersion":"2.0","eventSource":"aws:s3","awsRegion":"eu-west-1","eventTime":"2017-05-16T11:38:41.073Z","eventName":"ObjectCreated:Put","userIdentity":{"principalId":"some-p-id"},"requestParameters":{"sourceIPAddress":"8.8.8.8"},"responseElements":{"x-amz-request-id":"some-r-id","x-amz-id-2":"some-x-am-id"},"s3":{"s3SchemaVersion":"1.0","configurationId":"some-c-id","bucket":{"name":"some-bucket","ownerIdentity":{"principalId":"some-p-id"},"arn":"some-bucket-arn"}, "object":{"key":"%s"}}}]}''' % (filename) # noqa