diff --git a/app/aws/s3.py b/app/aws/s3.py index 8a8cc79ff..e8ef69edc 100644 --- a/app/aws/s3.py +++ b/app/aws/s3.py @@ -119,12 +119,8 @@ def get_list_of_files_by_suffix(bucket_name, subfolder='', suffix='', last_modif ) for page in page_iterator: - try: - for obj in page['Contents']: - key = obj['Key'].lower() - if key.endswith(suffix.lower()): - if not last_modified or obj['LastModified'] >= last_modified: - yield key - except KeyError: - # Not content for today - pass + for obj in page.get('Contents', []): + key = obj['Key'].lower() + if key.endswith(suffix.lower()): + if not last_modified or obj['LastModified'] >= last_modified: + yield key diff --git a/tests/app/aws/test_s3.py b/tests/app/aws/test_s3.py index d4d3f196c..81ee10bfd 100644 --- a/tests/app/aws/test_s3.py +++ b/tests/app/aws/test_s3.py @@ -208,3 +208,18 @@ def test_get_list_of_files_by_suffix(notify_api, mocker, suffix_str, days_before assert sum(1 for x in key) == returned_no for k in key: assert k == 'bar/foo.ACK.txt' + + +def test_get_list_of_files_by_suffix_empty_contents_return_with_no_error(notify_api, mocker): + paginator_mock = mocker.patch('app.aws.s3.client') + multiple_pages_s3_object = [ + { + "other_content": [ + 'some_values', + ] + } + ] + paginator_mock.return_value.get_paginator.return_value.paginate.return_value = multiple_pages_s3_object + key = get_list_of_files_by_suffix('foo-bucket', subfolder='bar', suffix='.pdf') + + assert sum(1 for x in key) == 0 diff --git a/tests/app/celery/test_scheduled_tasks.py b/tests/app/celery/test_scheduled_tasks.py index 6b97ee9b3..7178d8681 100644 --- a/tests/app/celery/test_scheduled_tasks.py +++ b/tests/app/celery/test_scheduled_tasks.py @@ -1058,8 +1058,9 @@ def test_letter_not_raise_alert_if_ack_files_match_zip_list(mocker, notify_db): subfoldername = datetime.utcnow().strftime('%Y-%m-%d') assert mock_file_list.call_count == 2 assert mock_file_list.call_args_list == [ - call(bucket_name='test-letters-pdf', subfolder=subfoldername, suffix='.zip'), - call(bucket_name='test.notify.com-ftp', subfolder='root/dispatch', suffix='.ACK.txt', last_modified=yesterday), + call(bucket_name=current_app.config['LETTERS_PDF_BUCKET_NAME'], subfolder=subfoldername, suffix='.zip'), + call(bucket_name=current_app.config['DVLA_RESPONSE_BUCKET_NAME'], subfolder='root/dispatch', + suffix='.ACK.txt', last_modified=yesterday), ] assert mock_get_file.call_count == 1