From 4e9e014a0cab64bec1ddc53f7711b0ddd90f6de1 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 10 Sep 2024 11:12:43 -0700 Subject: [PATCH] add test --- .ds.baseline | 4 ++-- app/aws/s3.py | 8 +++++++- tests/app/aws/test_s3.py | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/.ds.baseline b/.ds.baseline index b0b843260..6ef3c9108 100644 --- a/.ds.baseline +++ b/.ds.baseline @@ -209,7 +209,7 @@ "filename": "tests/app/aws/test_s3.py", "hashed_secret": "67a74306b06d0c01624fe0d0249a570f4d093747", "is_verified": false, - "line_number": 25, + "line_number": 27, "is_secret": false } ], @@ -384,5 +384,5 @@ } ] }, - "generated_at": "2024-08-22T18:00:24Z" + "generated_at": "2024-09-10T18:12:39Z" } diff --git a/app/aws/s3.py b/app/aws/s3.py index dc4abeb19..96ce42907 100644 --- a/app/aws/s3.py +++ b/app/aws/s3.py @@ -82,15 +82,21 @@ def list_s3_objects(): ) +def get_bucket_name(): + return current_app.config["CSV_UPLOAD_BUCKET"]["bucket"] + + def cleanup_old_s3_objects(): - bucket_name = current_app.config["CSV_UPLOAD_BUCKET"]["bucket"] + bucket_name = get_bucket_name() + s3_client = get_s3_client() # Our reports only support 7 days, but can be scheduled 3 days in advance # Use 14 day for the v1.0 version of this behavior time_limit = aware_utcnow() - datetime.timedelta(days=14) try: response = s3_client.list_objects_v2(Bucket=bucket_name) + print(f"RESPONSE = {response}") while True: for obj in response.get("Contents", []): if obj["LastModified"] <= time_limit: diff --git a/tests/app/aws/test_s3.py b/tests/app/aws/test_s3.py index 4e844a1de..dcc1cbe44 100644 --- a/tests/app/aws/test_s3.py +++ b/tests/app/aws/test_s3.py @@ -5,6 +5,7 @@ import pytest from botocore.exceptions import ClientError from app.aws.s3 import ( + cleanup_old_s3_objects, file_exists, get_job_from_s3, get_personalisation_from_s3, @@ -14,6 +15,7 @@ from app.aws.s3 import ( remove_s3_object, ) from app.utils import utc_now +from notifications_utils import aware_utcnow default_access_key = getenv("CSV_AWS_ACCESS_KEY_ID") default_secret_key = getenv("CSV_AWS_SECRET_ACCESS_KEY") @@ -28,6 +30,18 @@ def single_s3_object_stub(key="foo", last_modified=None): } +def test_cleanup_old_s3_objects(mocker): + mocker.patch("app.aws.s3.get_bucket_name", return_value="Bucket") + mock_s3_client = mocker.Mock() + mocker.patch("app.aws.s3.get_s3_client", return_value=mock_s3_client) + + mock_s3_client.list_objects_v2.return_value = { + "Contents": [{"Key": "A", "LastModified": aware_utcnow()}] + } + cleanup_old_s3_objects() + mock_s3_client.list_objects_v2.assert_called_with(Bucket="Bucket") + + def test_get_s3_file_makes_correct_call(notify_api, mocker): get_s3_mock = mocker.patch("app.aws.s3.get_s3_object") get_s3_file(