From 005cca5d2a6581369b8edcafeb796b22250fa8f5 Mon Sep 17 00:00:00 2001 From: jamesstottmoj Date: Thu, 28 Nov 2024 12:30:45 +0000 Subject: [PATCH] Stop task from running if user cannot be found for whatever reason --- controlpanel/api/tasks/handlers/s3.py | 9 ++++++++- tests/api/tasks/test_s3.py | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/controlpanel/api/tasks/handlers/s3.py b/controlpanel/api/tasks/handlers/s3.py index f75b48b15..303a6f548 100644 --- a/controlpanel/api/tasks/handlers/s3.py +++ b/controlpanel/api/tasks/handlers/s3.py @@ -58,7 +58,14 @@ class S3BucketRevokeUserAccess(BaseTaskHandler): name = "revoke_user_s3bucket_access" def handle(self, bucket_identifier, bucket_user_pk, is_folder): - bucket_user = User.objects.get(pk=bucket_user_pk) + try: + bucket_user = User.objects.get(pk=bucket_user_pk) + except User.DoesNotExist: + # if the user doesnt exist, nothing to revoke, so mark completed + log.warn("User does not exist. Skipping...") + self.complete() + return + if is_folder: cluster.User(bucket_user).revoke_folder_access(bucket_identifier) else: diff --git a/tests/api/tasks/test_s3.py b/tests/api/tasks/test_s3.py index 36f41da84..9174de265 100644 --- a/tests/api/tasks/test_s3.py +++ b/tests/api/tasks/test_s3.py @@ -97,6 +97,30 @@ def test_revoke_user_access(cluster, complete, bucket_name, is_folder): complete.assert_called_once() +@pytest.mark.django_db +@pytest.mark.parametrize( + "bucket_name, is_folder", + [ + ("example-bucket", False), + ("example-bucket/folder", True), + ], +) +@patch("controlpanel.api.tasks.handlers.base.BaseTaskHandler.complete") +@patch("controlpanel.api.tasks.handlers.s3.cluster") +def test_revoke_user_access_user_does_not_exist(cluster, complete, bucket_name, is_folder): + user_bucket_access = baker.make("api.UserS3Bucket", s3bucket__name=bucket_name) + s3bucket = user_bucket_access.s3bucket + bucket_identifier = s3bucket.name if is_folder else s3bucket.arn + revoke_user_s3bucket_access( + bucket_identifier=bucket_identifier, + bucket_user_pk="ShouldFail", + is_folder=is_folder, + ) + + cluster.User.assert_not_called() + complete.assert_called_once() + + @pytest.mark.django_db @patch("controlpanel.api.tasks.handlers.base.BaseTaskHandler.complete") @patch("controlpanel.api.tasks.handlers.s3.cluster")