diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index eb1561e5b..6b4c3baac 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -428,9 +428,12 @@ def _delete_letters_from_s3( try: letter_pdf = find_letter_pdf_in_s3(letter) letter_pdf.delete() - except (ClientError, LetterPDFNotFound): + except ClientError: current_app.logger.exception( - "Could not delete S3 object for letter: {}".format(letter.id)) + "Error deleting S3 object for letter: {}".format(letter.id)) + except LetterPDFNotFound: + current_app.logger.warning( + "No S3 object to delete for letter: {}".format(letter.id)) @autocommit diff --git a/tests/app/dao/notification_dao/test_notification_dao_delete_notifications.py b/tests/app/dao/notification_dao/test_notification_dao_delete_notifications.py index b1f5f09bc..a616dee31 100644 --- a/tests/app/dao/notification_dao/test_notification_dao_delete_notifications.py +++ b/tests/app/dao/notification_dao/test_notification_dao_delete_notifications.py @@ -51,6 +51,23 @@ def test_move_notifications_deletes_letters_from_s3(sample_letter_template, mock s3.get_object(Bucket=bucket_name, Key=filename) +@mock_s3 +@freeze_time('2019-09-01 04:30') +def test_move_notifications_copes_if_letter_not_in_s3(sample_letter_template, mocker): + s3 = boto3.client('s3', region_name='eu-west-1') + s3.create_bucket( + Bucket=current_app.config['LETTERS_PDF_BUCKET_NAME'], + CreateBucketConfiguration={'LocationConstraint': 'eu-west-1'} + ) + + eight_days_ago = datetime.utcnow() - timedelta(days=8) + create_notification(template=sample_letter_template, status='delivered', sent_at=eight_days_ago) + + move_notifications_to_notification_history('letter', sample_letter_template.service_id, datetime(2020, 1, 2)) + assert Notification.query.count() == 0 + assert NotificationHistory.query.count() == 1 + + def test_move_notifications_does_nothing_if_notification_history_row_already_exists( sample_email_template, mocker ):