2022-05-12 13:58:17 +00:00
|
|
|
import boto3
|
2023-11-30 15:55:51 +00:00
|
|
|
import pytest
|
2022-05-12 13:58:17 +00:00
|
|
|
from botocore.exceptions import ClientError, ParamValidationError
|
2023-11-30 15:55:51 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
from moto import mock_aws
|
2022-08-13 09:49:43 +00:00
|
|
|
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
|
2023-11-30 15:55:51 +00:00
|
|
|
|
2022-05-12 13:58:17 +00:00
|
|
|
from . import cloudfront_test_scaffolding as scaffold
|
|
|
|
|
|
|
|
# See our Development Tips on writing tests for hints on how to write good tests:
|
|
|
|
# http://docs.getmoto.org/en/latest/docs/contributing/development_tips/tests.html
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-05-12 13:58:17 +00:00
|
|
|
def test_update_distribution():
|
|
|
|
client = boto3.client("cloudfront", region_name="us-east-1")
|
|
|
|
|
|
|
|
# Create standard distribution
|
|
|
|
config = scaffold.example_distribution_config(ref="ref")
|
|
|
|
dist = client.create_distribution(DistributionConfig=config)
|
|
|
|
dist_id = dist["Distribution"]["Id"]
|
|
|
|
dist_etag = dist["ETag"]
|
|
|
|
|
|
|
|
dist_config = dist["Distribution"]["DistributionConfig"]
|
|
|
|
aliases = ["alias1", "alias2"]
|
2022-12-08 00:02:01 +00:00
|
|
|
dist_config["Origins"]["Items"][0]["OriginPath"] = "/updated"
|
2022-05-12 13:58:17 +00:00
|
|
|
dist_config["Aliases"] = {"Quantity": len(aliases), "Items": aliases}
|
|
|
|
|
2023-07-05 22:49:24 +00:00
|
|
|
dist = client.update_distribution(
|
2022-05-12 13:58:17 +00:00
|
|
|
DistributionConfig=dist_config, Id=dist_id, IfMatch=dist_etag
|
2023-07-05 22:49:24 +00:00
|
|
|
)["Distribution"]
|
|
|
|
assert dist["ARN"] == f"arn:aws:cloudfront:{ACCOUNT_ID}:distribution/{dist['Id']}"
|
|
|
|
assert dist["Status"] == "Deployed"
|
|
|
|
assert "LastModifiedTime" in dist
|
|
|
|
assert dist["InProgressInvalidationBatches"] == 0
|
|
|
|
assert ".cloudfront.net" in dist["DomainName"]
|
|
|
|
|
|
|
|
assert dist["ActiveTrustedSigners"] == {
|
|
|
|
"Enabled": False,
|
|
|
|
"Items": [],
|
|
|
|
"Quantity": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert dist["ActiveTrustedKeyGroups"] == {
|
|
|
|
"Enabled": False,
|
|
|
|
"Items": [],
|
|
|
|
"Quantity": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
config = dist["DistributionConfig"]
|
|
|
|
assert config["CallerReference"] == "ref"
|
|
|
|
|
|
|
|
assert config["Aliases"] == dist_config["Aliases"]
|
|
|
|
|
2022-05-12 13:58:17 +00:00
|
|
|
origins = config["Origins"]
|
2023-07-05 22:49:24 +00:00
|
|
|
assert origins["Quantity"] == 1
|
|
|
|
assert len(origins["Items"]) == 1
|
2022-05-12 13:58:17 +00:00
|
|
|
origin = origins["Items"][0]
|
2023-07-05 22:49:24 +00:00
|
|
|
assert origin["Id"] == "origin1"
|
|
|
|
assert origin["DomainName"] == "asdf.s3.us-east-1.amazonaws.com"
|
|
|
|
assert origin["OriginPath"] == "/updated"
|
2022-05-12 13:58:17 +00:00
|
|
|
|
2023-07-05 22:49:24 +00:00
|
|
|
assert origin["CustomHeaders"]["Quantity"] == 0
|
2022-05-12 13:58:17 +00:00
|
|
|
|
2023-07-05 22:49:24 +00:00
|
|
|
assert origin["ConnectionAttempts"] == 3
|
|
|
|
assert origin["ConnectionTimeout"] == 10
|
|
|
|
assert origin["OriginShield"] == {"Enabled": False, "OriginShieldRegion": "None"}
|
2022-05-12 13:58:17 +00:00
|
|
|
|
2023-07-05 22:49:24 +00:00
|
|
|
assert config["OriginGroups"] == {"Quantity": 0}
|
2022-05-12 13:58:17 +00:00
|
|
|
|
|
|
|
default_cache = config["DefaultCacheBehavior"]
|
2023-07-05 22:49:24 +00:00
|
|
|
assert default_cache["TargetOriginId"] == "origin1"
|
2022-05-12 13:58:17 +00:00
|
|
|
|
|
|
|
signers = default_cache["TrustedSigners"]
|
2023-07-05 22:49:24 +00:00
|
|
|
assert signers["Enabled"] is False
|
|
|
|
assert signers["Quantity"] == 0
|
2022-05-12 13:58:17 +00:00
|
|
|
|
|
|
|
groups = default_cache["TrustedKeyGroups"]
|
2023-07-05 22:49:24 +00:00
|
|
|
assert groups["Enabled"] is False
|
|
|
|
assert groups["Quantity"] == 0
|
2022-05-12 13:58:17 +00:00
|
|
|
|
2023-07-05 22:49:24 +00:00
|
|
|
assert default_cache["ViewerProtocolPolicy"] == "allow-all"
|
2022-05-12 13:58:17 +00:00
|
|
|
|
|
|
|
methods = default_cache["AllowedMethods"]
|
2023-07-05 22:49:24 +00:00
|
|
|
assert methods["Quantity"] == 2
|
|
|
|
assert set(methods["Items"]) == {"HEAD", "GET"}
|
2022-05-12 13:58:17 +00:00
|
|
|
|
|
|
|
cached_methods = methods["CachedMethods"]
|
2023-07-05 22:49:24 +00:00
|
|
|
assert cached_methods["Quantity"] == 2
|
|
|
|
assert set(cached_methods["Items"]) == {"HEAD", "GET"}
|
|
|
|
|
|
|
|
assert default_cache["SmoothStreaming"] is False
|
|
|
|
assert default_cache["Compress"] is True
|
|
|
|
assert default_cache["LambdaFunctionAssociations"] == {"Quantity": 0}
|
|
|
|
assert default_cache["FunctionAssociations"] == {"Quantity": 0}
|
|
|
|
assert default_cache["FieldLevelEncryptionId"] == ""
|
|
|
|
assert "CachePolicyId" in default_cache
|
|
|
|
|
|
|
|
assert config["CacheBehaviors"] == {"Quantity": 0}
|
|
|
|
assert config["CustomErrorResponses"] == {"Quantity": 0}
|
|
|
|
assert config["Comment"] == "an optional comment that's not actually optional"
|
|
|
|
|
2022-05-12 13:58:17 +00:00
|
|
|
logging = config["Logging"]
|
2023-07-05 22:49:24 +00:00
|
|
|
assert logging["Enabled"] is False
|
|
|
|
assert logging["IncludeCookies"] is False
|
|
|
|
assert logging["Bucket"] == ""
|
|
|
|
assert logging["Prefix"] == ""
|
|
|
|
|
|
|
|
assert config["PriceClass"] == "PriceClass_All"
|
|
|
|
assert config["Enabled"] is False
|
|
|
|
assert "WebACLId" in config
|
|
|
|
assert config["HttpVersion"] == "http2"
|
|
|
|
assert config["IsIPV6Enabled"] is True
|
|
|
|
|
2022-05-12 13:58:17 +00:00
|
|
|
cert = config["ViewerCertificate"]
|
2023-07-05 22:49:24 +00:00
|
|
|
assert cert["CloudFrontDefaultCertificate"] is True
|
|
|
|
assert cert["MinimumProtocolVersion"] == "TLSv1"
|
|
|
|
assert cert["CertificateSource"] == "cloudfront"
|
2022-05-12 13:58:17 +00:00
|
|
|
|
|
|
|
restriction = config["Restrictions"]["GeoRestriction"]
|
2023-07-05 22:49:24 +00:00
|
|
|
assert restriction["RestrictionType"] == "none"
|
|
|
|
assert restriction["Quantity"] == 0
|
2022-05-12 13:58:17 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-05-12 13:58:17 +00:00
|
|
|
def test_update_distribution_no_such_distId():
|
|
|
|
client = boto3.client("cloudfront", region_name="us-east-1")
|
|
|
|
|
|
|
|
# Create standard distribution
|
|
|
|
config = scaffold.example_distribution_config(ref="ref")
|
|
|
|
dist = client.create_distribution(DistributionConfig=config)
|
|
|
|
|
|
|
|
# Make up a fake dist ID by reversing the actual ID
|
|
|
|
dist_id = dist["Distribution"]["Id"][::-1]
|
|
|
|
dist_etag = dist["ETag"]
|
|
|
|
|
|
|
|
dist_config = dist["Distribution"]["DistributionConfig"]
|
|
|
|
aliases = ["alias1", "alias2"]
|
|
|
|
dist_config["Aliases"] = {"Quantity": len(aliases), "Items": aliases}
|
|
|
|
|
|
|
|
with pytest.raises(ClientError) as error:
|
|
|
|
client.update_distribution(
|
|
|
|
DistributionConfig=dist_config, Id=dist_id, IfMatch=dist_etag
|
|
|
|
)
|
|
|
|
|
|
|
|
metadata = error.value.response["ResponseMetadata"]
|
2023-07-05 22:49:24 +00:00
|
|
|
assert metadata["HTTPStatusCode"] == 404
|
2022-05-12 13:58:17 +00:00
|
|
|
err = error.value.response["Error"]
|
2023-07-05 22:49:24 +00:00
|
|
|
assert err["Code"] == "NoSuchDistribution"
|
|
|
|
assert err["Message"] == "The specified distribution does not exist."
|
2022-05-12 13:58:17 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-05-12 13:58:17 +00:00
|
|
|
def test_update_distribution_distId_is_None():
|
|
|
|
client = boto3.client("cloudfront", region_name="us-east-1")
|
|
|
|
|
|
|
|
# Create standard distribution
|
|
|
|
config = scaffold.example_distribution_config(ref="ref")
|
|
|
|
dist = client.create_distribution(DistributionConfig=config)
|
|
|
|
|
|
|
|
# Make up a fake dist ID by reversing the actual ID
|
|
|
|
dist_id = None
|
|
|
|
dist_etag = dist["ETag"]
|
|
|
|
|
|
|
|
dist_config = dist["Distribution"]["DistributionConfig"]
|
|
|
|
aliases = ["alias1", "alias2"]
|
|
|
|
dist_config["Aliases"] = {"Quantity": len(aliases), "Items": aliases}
|
|
|
|
|
|
|
|
with pytest.raises(ParamValidationError) as error:
|
|
|
|
client.update_distribution(
|
|
|
|
DistributionConfig=dist_config, Id=dist_id, IfMatch=dist_etag
|
|
|
|
)
|
|
|
|
|
|
|
|
typename = error.typename
|
2023-07-05 22:49:24 +00:00
|
|
|
assert typename == "ParamValidationError"
|
2022-05-12 13:58:17 +00:00
|
|
|
error_str = "botocore.exceptions.ParamValidationError: Parameter validation failed:\nInvalid type for parameter Id, value: None, type: <class 'NoneType'>, valid types: <class 'str'>"
|
2023-07-05 22:49:24 +00:00
|
|
|
assert error.exconly() == error_str
|
2022-05-12 13:58:17 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-05-12 13:58:17 +00:00
|
|
|
def test_update_distribution_IfMatch_not_set():
|
|
|
|
client = boto3.client("cloudfront", region_name="us-east-1")
|
|
|
|
|
|
|
|
# Create standard distribution
|
|
|
|
config = scaffold.example_distribution_config(ref="ref")
|
|
|
|
dist = client.create_distribution(DistributionConfig=config)
|
|
|
|
|
|
|
|
# Make up a fake dist ID by reversing the actual ID
|
|
|
|
dist_id = dist["Distribution"]["Id"]
|
|
|
|
|
|
|
|
dist_config = dist["Distribution"]["DistributionConfig"]
|
|
|
|
aliases = ["alias1", "alias2"]
|
|
|
|
dist_config["Aliases"] = {"Quantity": len(aliases), "Items": aliases}
|
|
|
|
|
|
|
|
with pytest.raises(ClientError) as error:
|
|
|
|
client.update_distribution(
|
|
|
|
DistributionConfig=dist_config, Id=dist_id, IfMatch=""
|
|
|
|
)
|
|
|
|
|
|
|
|
metadata = error.value.response["ResponseMetadata"]
|
2023-07-05 22:49:24 +00:00
|
|
|
assert metadata["HTTPStatusCode"] == 400
|
2022-05-12 13:58:17 +00:00
|
|
|
err = error.value.response["Error"]
|
2023-07-05 22:49:24 +00:00
|
|
|
assert err["Code"] == "InvalidIfMatchVersion"
|
2022-05-12 13:58:17 +00:00
|
|
|
msg = "The If-Match version is missing or not valid for the resource."
|
2023-07-05 22:49:24 +00:00
|
|
|
assert err["Message"] == msg
|
2022-05-12 13:58:17 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-05-12 13:58:17 +00:00
|
|
|
def test_update_distribution_dist_config_not_set():
|
|
|
|
client = boto3.client("cloudfront", region_name="us-east-1")
|
|
|
|
|
|
|
|
# Create standard distribution
|
|
|
|
config = scaffold.example_distribution_config(ref="ref")
|
|
|
|
dist = client.create_distribution(DistributionConfig=config)
|
|
|
|
|
|
|
|
# Make up a fake dist ID by reversing the actual ID
|
|
|
|
dist_id = dist["Distribution"]["Id"]
|
|
|
|
dist_etag = dist["ETag"]
|
|
|
|
|
|
|
|
with pytest.raises(ParamValidationError) as error:
|
|
|
|
client.update_distribution(Id=dist_id, IfMatch=dist_etag)
|
|
|
|
|
|
|
|
typename = error.typename
|
2023-07-05 22:49:24 +00:00
|
|
|
assert typename == "ParamValidationError"
|
2022-05-12 13:58:17 +00:00
|
|
|
error_str = 'botocore.exceptions.ParamValidationError: Parameter validation failed:\nMissing required parameter in input: "DistributionConfig"'
|
2023-07-05 22:49:24 +00:00
|
|
|
assert error.exconly() == error_str
|
2023-07-24 08:20:27 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2023-07-24 08:20:27 +00:00
|
|
|
def test_update_default_root_object():
|
|
|
|
client = boto3.client("cloudfront", region_name="us-east-1")
|
|
|
|
|
|
|
|
config = scaffold.minimal_dist_custom_config("sth")
|
|
|
|
dist = client.create_distribution(DistributionConfig=config)
|
|
|
|
|
|
|
|
dist_id = dist["Distribution"]["Id"]
|
|
|
|
root_object = "index.html"
|
|
|
|
dist_config = client.get_distribution_config(Id=dist_id)
|
|
|
|
|
|
|
|
# Update the default root object
|
|
|
|
dist_config["DistributionConfig"]["DefaultRootObject"] = root_object
|
|
|
|
|
|
|
|
client.update_distribution(
|
|
|
|
DistributionConfig=dist_config["DistributionConfig"],
|
|
|
|
Id=dist_id,
|
|
|
|
IfMatch=dist_config["ETag"],
|
|
|
|
)
|
|
|
|
|
|
|
|
dist_config = client.get_distribution_config(Id=dist_id)
|
|
|
|
assert dist_config["DistributionConfig"]["DefaultRootObject"] == "index.html"
|
2024-03-04 23:16:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_aws
|
|
|
|
def test_update_distribution_applies_changes():
|
|
|
|
client = boto3.client("cloudfront", region_name="us-east-1")
|
|
|
|
|
|
|
|
# Create standard distribution
|
|
|
|
config = scaffold.example_distribution_config(ref="ref")
|
|
|
|
dist = client.create_distribution(DistributionConfig=config)
|
|
|
|
dist_id = dist["Distribution"]["Id"]
|
|
|
|
|
|
|
|
# Assert that default 'Enabled' value is false
|
|
|
|
actual_dist_config = client.get_distribution(Id=dist_id)["Distribution"][
|
|
|
|
"DistributionConfig"
|
|
|
|
]
|
|
|
|
assert not actual_dist_config["Enabled"]
|
|
|
|
|
|
|
|
# Update 'Enabled' value to true
|
|
|
|
get_config_response = client.get_distribution_config(Id=dist_id)
|
|
|
|
actual_dist_config = get_config_response["DistributionConfig"]
|
|
|
|
etag = get_config_response["ETag"]
|
|
|
|
actual_dist_config["Enabled"] = True
|
|
|
|
client.update_distribution(
|
|
|
|
DistributionConfig=actual_dist_config, Id=dist_id, IfMatch=etag
|
|
|
|
)
|
|
|
|
|
|
|
|
# Assert that 'Enabled' value is true
|
|
|
|
actual_dist_config = client.get_distribution(Id=dist_id)["Distribution"][
|
|
|
|
"DistributionConfig"
|
|
|
|
]
|
|
|
|
assert actual_dist_config["Enabled"]
|