Skip to content

Commit

Permalink
fix(tests): patch head_bucket function correctly (#5246)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergargar authored Sep 30, 2024
1 parent 7732ec7 commit da87c0d
Showing 1 changed file with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_no_distributions(self):
# Distributions
cloudfront_client = mock.MagicMock
cloudfront_client.distributions = {}
s3_client = mock.MagicMock
s3_client = mock.MagicMock()
# Buckets
s3_client.buckets = {}

Expand All @@ -38,10 +38,6 @@ def test_no_distributions(self):

assert len(result) == 0

@mock.patch(
"prowler.providers.aws.services.s3.s3_service.S3._head_bucket",
new=mock.MagicMock(return_value=False),
)
def test_distribution_nonexistent_origins(self):
# Distributions
domain = "nonexistent-bucket.s3.eu-west-1.amazonaws.com"
Expand All @@ -64,14 +60,17 @@ def test_distribution_nonexistent_origins(self):
}
# Buckets
nonexistent_bucket = "nonexistent-bucket"
s3_client = mock.MagicMock
s3_client = mock.MagicMock()
s3_client.buckets = {}

with mock.patch(
"prowler.providers.aws.services.s3.s3_service.S3", new=s3_client
), mock.patch(
"prowler.providers.aws.services.cloudfront.cloudfront_service.CloudFront",
new=cloudfront_client,
), mock.patch(
"prowler.providers.aws.services.cloudfront.cloudfront_distributions_s3_origin_non_existent_bucket.cloudfront_distributions_s3_origin_non_existent_bucket.s3_client._head_bucket",
new=mock.MagicMock(return_value=False),
):
# Test Check
from prowler.providers.aws.services.cloudfront.cloudfront_distributions_s3_origin_non_existent_bucket.cloudfront_distributions_s3_origin_non_existent_bucket import (
Expand All @@ -91,10 +90,6 @@ def test_distribution_nonexistent_origins(self):
== f"CloudFront Distribution {DISTRIBUTION_ID} has non-existent S3 buckets as origins: {nonexistent_bucket}."
)

@mock.patch(
"prowler.providers.aws.services.s3.s3_service.S3._head_bucket",
new=mock.MagicMock(return_value=True),
)
def test_distribution_no_nonexistent_origins(self):
# Distributions
domain = "existent-bucket.s3.eu-west-1.amazonaws.com"
Expand All @@ -117,7 +112,7 @@ def test_distribution_no_nonexistent_origins(self):
}
# Buckets
bucket_name = "existent-bucket"
s3_client = mock.MagicMock
s3_client = mock.MagicMock()
s3_client.audited_account = AWS_ACCOUNT_NUMBER
s3_client.buckets = {
f"arn:aws:s3:::{bucket_name}": Bucket(
Expand All @@ -131,6 +126,9 @@ def test_distribution_no_nonexistent_origins(self):
), mock.patch(
"prowler.providers.aws.services.cloudfront.cloudfront_service.CloudFront",
new=cloudfront_client,
), mock.patch(
"prowler.providers.aws.services.cloudfront.cloudfront_distributions_s3_origin_non_existent_bucket.cloudfront_distributions_s3_origin_non_existent_bucket.s3_client._head_bucket",
new=mock.MagicMock(return_value=True),
):
# Test Check
from prowler.providers.aws.services.cloudfront.cloudfront_distributions_s3_origin_non_existent_bucket.cloudfront_distributions_s3_origin_non_existent_bucket import (
Expand Down

0 comments on commit da87c0d

Please sign in to comment.