From 356a8c451ffade03844b502b6a6cadd7bad79b3e Mon Sep 17 00:00:00 2001 From: stvnrlly Date: Wed, 7 Dec 2022 12:39:37 -0500 Subject: [PATCH] remove rest of update-letter-* tasks --- app/celery/tasks.py | 101 ---------------- tests/app/celery/test_ftp_update_tasks.py | 135 ---------------------- 2 files changed, 236 deletions(-) delete mode 100644 tests/app/celery/test_ftp_update_tasks.py diff --git a/app/celery/tasks.py b/app/celery/tasks.py index 27b528e67..c4cfcfef6 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -392,41 +392,6 @@ def save_letter( handle_exception(self, notification, notification_id, e) -@notify_celery.task(bind=True, name='update-letter-notifications-to-sent') -def update_letter_notifications_to_sent_to_dvla(self, notification_references): - # This task will be called by the FTP app to update notifications as sent to DVLA - provider = get_provider_details_by_notification_type(LETTER_TYPE)[0] - - updated_count, _ = dao_update_notifications_by_reference( - notification_references, - { - 'status': NOTIFICATION_SENDING, - 'sent_by': provider.identifier, - 'sent_at': datetime.utcnow(), - 'updated_at': datetime.utcnow() - } - ) - - current_app.logger.info("Updated {} letter notifications to sending".format(updated_count)) - - -@notify_celery.task(bind=True, name='update-letter-notifications-to-error') -def update_letter_notifications_to_error(self, notification_references): - # This task will be called by the FTP app to update notifications as sent to DVLA - - updated_count, _ = dao_update_notifications_by_reference( - notification_references, - { - 'status': NOTIFICATION_TECHNICAL_FAILURE, - 'updated_at': datetime.utcnow() - } - ) - message = "Updated {} letter notifications to technical-failure with references {}".format( - updated_count, notification_references - ) - raise NotificationTechnicalFailureException(message) - - def handle_exception(task, notification, notification_id, exc): if not get_notification_by_id(notification_id): retry_msg = '{task} notification for job {job} row number {row} and notification id {noti}'.format( @@ -446,72 +411,6 @@ def handle_exception(task, notification, notification_id, exc): current_app.logger.error('Max retry failed' + retry_msg) -def parse_dvla_file(filename): - bucket_location = '{}-ftp'.format(current_app.config['NOTIFY_EMAIL_DOMAIN']) - response_file_content = s3.get_s3_file(bucket_location, filename) - - try: - return process_updates_from_file(response_file_content) - except TypeError: - raise DVLAException('DVLA response file: {} has an invalid format'.format(filename)) - - -def get_local_billing_date_from_filename(filename): - # exclude seconds from the date since we don't need it. We got a date ending in 60 second - which is not valid. - datetime_string = filename.split('-')[1][:-2] - datetime_obj = datetime.strptime(datetime_string, '%Y%m%d%H%M') - return convert_utc_to_local_timezone(datetime_obj).date() - - -def persist_daily_sorted_letter_counts(day, file_name, sorted_letter_counts): - daily_letter_count = DailySortedLetter( - billing_day=day, - file_name=file_name, - unsorted_count=sorted_letter_counts['unsorted'], - sorted_count=sorted_letter_counts['sorted'] - ) - dao_create_or_update_daily_sorted_letter(daily_letter_count) - - -def process_updates_from_file(response_file): - NotificationUpdate = namedtuple('NotificationUpdate', ['reference', 'status', 'page_count', 'cost_threshold']) - notification_updates = [NotificationUpdate(*line.split('|')) for line in response_file.splitlines()] - return notification_updates - - -def update_letter_notification(filename, temporary_failures, update): - if update.status == DVLA_RESPONSE_STATUS_SENT: - status = NOTIFICATION_DELIVERED - else: - status = NOTIFICATION_TEMPORARY_FAILURE - temporary_failures.append(update.reference) - - updated_count, _ = dao_update_notifications_by_reference( - references=[update.reference], - update_dict={"status": status, - "updated_at": datetime.utcnow() - } - ) - - if not updated_count: - msg = "Update letter notification file {filename} failed: notification either not found " \ - "or already updated from delivered. Status {status} for notification reference {reference}".format( - filename=filename, status=status, reference=update.reference) - current_app.logger.info(msg) - - -def check_billable_units(notification_update): - notification = dao_get_notification_history_by_reference(notification_update.reference) - - if int(notification_update.page_count) != notification.billable_units: - msg = 'Notification with id {} has {} billable_units but DVLA says page count is {}'.format( - notification.id, notification.billable_units, notification_update.page_count) - try: - raise DVLAException(msg) - except DVLAException: - current_app.logger.exception(msg) - - @notify_celery.task(bind=True, name="send-inbound-sms", max_retries=5, default_retry_delay=300) def send_inbound_sms_to_service(self, inbound_sms_id, service_id): inbound_api = get_service_inbound_api_for_service(service_id=service_id) diff --git a/tests/app/celery/test_ftp_update_tasks.py b/tests/app/celery/test_ftp_update_tasks.py deleted file mode 100644 index c4bad2e12..000000000 --- a/tests/app/celery/test_ftp_update_tasks.py +++ /dev/null @@ -1,135 +0,0 @@ -import os -from collections import defaultdict, namedtuple -from datetime import date, datetime - -import pytest -from flask import current_app -from freezegun import freeze_time - -from app.celery.tasks import ( - check_billable_units, - get_local_billing_date_from_filename, - persist_daily_sorted_letter_counts, - process_updates_from_file, - update_letter_notifications_to_error, - update_letter_notifications_to_sent_to_dvla, -) -from app.dao.daily_sorted_letter_dao import ( - dao_get_daily_sorted_letter_by_billing_day, -) -from app.exceptions import DVLAException, NotificationTechnicalFailureException -from app.models import ( - NOTIFICATION_CREATED, - NOTIFICATION_DELIVERED, - NOTIFICATION_SENDING, - NOTIFICATION_TECHNICAL_FAILURE, - NOTIFICATION_TEMPORARY_FAILURE, - DailySortedLetter, - NotificationHistory, -) -from tests.app.db import ( - create_notification, - create_notification_history, - create_service_callback_api, -) -from tests.conftest import set_config - - -@pytest.fixture -def notification_update(): - """ - Returns a namedtuple to use as the argument for the check_billable_units function - """ - NotificationUpdate = namedtuple('NotificationUpdate', ['reference', 'status', 'page_count', 'cost_threshold']) - return NotificationUpdate('REFERENCE_ABC', 'sent', '1', 'cost') - - -def test_update_letter_notifications_to_sent_to_dvla_updates_based_on_notification_references( - client, - sample_letter_template -): - first = create_notification(sample_letter_template, reference='first ref') - second = create_notification(sample_letter_template, reference='second ref') - - dt = datetime.utcnow() - with freeze_time(dt): - update_letter_notifications_to_sent_to_dvla([first.reference]) - - assert first.status == NOTIFICATION_SENDING - assert first.sent_by == 'dvla' - assert first.sent_at == dt - assert first.updated_at == dt - assert second.status == NOTIFICATION_CREATED - - -def test_update_letter_notifications_to_error_updates_based_on_notification_references( - sample_letter_template -): - first = create_notification(sample_letter_template, reference='first ref') - second = create_notification(sample_letter_template, reference='second ref') - create_service_callback_api(service=sample_letter_template.service, url="https://original_url.com") - dt = datetime.utcnow() - with freeze_time(dt): - with pytest.raises(NotificationTechnicalFailureException) as e: - update_letter_notifications_to_error([first.reference]) - assert first.reference in str(e.value) - - assert first.status == NOTIFICATION_TECHNICAL_FAILURE - assert first.sent_by is None - assert first.sent_at is None - assert first.updated_at == dt - assert second.status == NOTIFICATION_CREATED - - -def test_check_billable_units_when_billable_units_matches_page_count( - client, - sample_letter_template, - mocker, - notification_update -): - mock_logger = mocker.patch('app.celery.tasks.current_app.logger.error') - - create_notification(sample_letter_template, reference='REFERENCE_ABC', billable_units=1) - - check_billable_units(notification_update) - - mock_logger.assert_not_called() - - -def test_check_billable_units_when_billable_units_does_not_match_page_count( - client, - sample_letter_template, - mocker, - notification_update -): - mock_logger = mocker.patch('app.celery.tasks.current_app.logger.exception') - - notification = create_notification(sample_letter_template, reference='REFERENCE_ABC', billable_units=3) - - check_billable_units(notification_update) - - mock_logger.assert_called_once_with( - 'Notification with id {} has 3 billable_units but DVLA says page count is 1'.format(notification.id) - ) - - -@pytest.mark.parametrize('filename_date, billing_date', [ - ('20170820000000', date(2017, 8, 19)), - ('20170120230000', date(2017, 1, 20)) -]) -def test_get_local_billing_date_from_filename(filename_date, billing_date): - filename = 'NOTIFY-{}-RSP.TXT'.format(filename_date) - result = get_local_billing_date_from_filename(filename) - - assert result == billing_date - - -@freeze_time("2018-01-11 09:00:00") -def test_persist_daily_sorted_letter_counts_saves_sorted_and_unsorted_values(client, notify_db_session): - letter_counts = defaultdict(int, **{'unsorted': 5, 'sorted': 1}) - persist_daily_sorted_letter_counts(date.today(), "test.txt", letter_counts) - day = dao_get_daily_sorted_letter_by_billing_day(date.today()) - - assert day.unsorted_count == 5 - assert day.sorted_count == 1 -