2022-09-21 15:02:43 -04:00
|
|
|
import os
|
2017-06-12 15:55:05 +01:00
|
|
|
from datetime import datetime, timedelta
|
2021-03-10 13:55:06 +00:00
|
|
|
|
2017-12-04 16:39:32 +00:00
|
|
|
import pytest
|
2018-01-16 09:29:31 +00:00
|
|
|
import pytz
|
2017-06-12 15:55:05 +01:00
|
|
|
from freezegun import freeze_time
|
|
|
|
|
|
2021-03-09 16:44:31 +00:00
|
|
|
from app.aws.s3 import get_list_of_files_by_suffix, get_s3_file
|
2017-06-13 15:22:31 +01:00
|
|
|
from tests.app.conftest import datetime_in_past
|
2017-06-12 15:55:05 +01:00
|
|
|
|
|
|
|
|
|
2020-12-22 15:46:31 +00:00
|
|
|
def single_s3_object_stub(key='foo', last_modified=None):
|
2017-06-12 15:55:05 +01:00
|
|
|
return {
|
|
|
|
|
'ETag': '"d41d8cd98f00b204e9800998ecf8427e"',
|
|
|
|
|
'Key': key,
|
2020-12-22 15:46:31 +00:00
|
|
|
'LastModified': last_modified or datetime.utcnow(),
|
2017-06-12 15:55:05 +01:00
|
|
|
}
|
2017-06-07 16:31:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_s3_file_makes_correct_call(notify_api, mocker):
|
2017-05-12 17:39:15 +01:00
|
|
|
get_s3_mock = mocker.patch('app.aws.s3.get_s3_object')
|
|
|
|
|
get_s3_file('foo-bucket', 'bar-file.txt')
|
|
|
|
|
|
|
|
|
|
get_s3_mock.assert_called_with(
|
|
|
|
|
'foo-bucket',
|
2022-09-21 15:02:43 -04:00
|
|
|
'bar-file.txt',
|
|
|
|
|
os.environ['AWS_ACCESS_KEY_ID'],
|
|
|
|
|
os.environ['AWS_SECRET_ACCESS_KEY']
|
2017-05-12 17:39:15 +01:00
|
|
|
)
|
2017-06-07 16:31:14 +01:00
|
|
|
|
|
|
|
|
|
2018-01-16 09:29:31 +00:00
|
|
|
@freeze_time("2018-01-11 00:00:00")
|
|
|
|
|
@pytest.mark.parametrize('suffix_str, days_before, returned_no', [
|
|
|
|
|
('.ACK.txt', None, 1),
|
|
|
|
|
('.ack.txt', None, 1),
|
|
|
|
|
('.ACK.TXT', None, 1),
|
|
|
|
|
('', None, 2),
|
|
|
|
|
('', 1, 1),
|
|
|
|
|
])
|
|
|
|
|
def test_get_list_of_files_by_suffix(notify_api, mocker, suffix_str, days_before, returned_no):
|
|
|
|
|
paginator_mock = mocker.patch('app.aws.s3.client')
|
|
|
|
|
multiple_pages_s3_object = [
|
|
|
|
|
{
|
|
|
|
|
"Contents": [
|
|
|
|
|
single_s3_object_stub('bar/foo.ACK.txt', datetime_in_past(1, 0)),
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"Contents": [
|
|
|
|
|
single_s3_object_stub('bar/foo1.rs.txt', datetime_in_past(2, 0)),
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
paginator_mock.return_value.get_paginator.return_value.paginate.return_value = multiple_pages_s3_object
|
|
|
|
|
if (days_before):
|
|
|
|
|
key = get_list_of_files_by_suffix('foo-bucket', subfolder='bar', suffix=suffix_str,
|
|
|
|
|
last_modified=datetime.now(tz=pytz.utc) - timedelta(days=days_before))
|
|
|
|
|
else:
|
|
|
|
|
key = get_list_of_files_by_suffix('foo-bucket', subfolder='bar', suffix=suffix_str)
|
|
|
|
|
|
|
|
|
|
assert sum(1 for x in key) == returned_no
|
|
|
|
|
for k in key:
|
|
|
|
|
assert k == 'bar/foo.ACK.txt'
|
2018-01-17 13:51:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|