Skip to content

Commit

Permalink
Stop task from running if user cannot be found for whatever reason (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesstottmoj authored Nov 28, 2024
1 parent 9ccef74 commit ff1b95d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
9 changes: 8 additions & 1 deletion controlpanel/api/tasks/handlers/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 24 additions & 0 deletions tests/api/tasks/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit ff1b95d

Please sign in to comment.