|
| 1 | +""" |
| 2 | +Route53 has some idiosyncrasies related to HostedZone ids |
| 3 | +Some operations return `/hosted_zone/{id}` |
| 4 | +Other operations return `{id}` |
| 5 | +Some operations expect `/hosted_zone/{id}` |
| 6 | +Other operations expect `{id}` |
| 7 | +Some operations allow both (probably..) |
| 8 | +
|
| 9 | +The tests in this file are purely to test the different scenarios. |
| 10 | +
|
| 11 | +Note that all tests are verified against AWS. |
| 12 | +If you want to run the tests against AWS yourself, make sure `TEST_DOMAIN_NAME` is changed to a domain name that's known to AWS. |
| 13 | +""" |
| 14 | + |
| 15 | +from functools import wraps |
| 16 | +from uuid import uuid4 |
| 17 | + |
| 18 | +import boto3 |
| 19 | +import pytest |
| 20 | +from botocore.exceptions import ClientError |
| 21 | + |
| 22 | +from moto import mock_aws |
| 23 | +from tests import allow_aws_request |
| 24 | + |
| 25 | +TEST_DOMAIN_NAME = "mototests.click" |
| 26 | +TEST_HOSTED_ZONE_NAME = f"test.{TEST_DOMAIN_NAME}" |
| 27 | + |
| 28 | + |
| 29 | +def route53_aws_verified(func): |
| 30 | + @wraps(func) |
| 31 | + def pagination_wrapper(**kwargs): |
| 32 | + def create_hosted_zone(): |
| 33 | + client = boto3.client("route53", "us-east-1") |
| 34 | + |
| 35 | + hosted_zone = client.create_hosted_zone( |
| 36 | + Name=TEST_HOSTED_ZONE_NAME, CallerReference=str(uuid4()) |
| 37 | + )["HostedZone"] |
| 38 | + |
| 39 | + kwargs["hosted_zone"] = hosted_zone |
| 40 | + |
| 41 | + try: |
| 42 | + return func(**kwargs) |
| 43 | + finally: |
| 44 | + client.delete_hosted_zone(Id=hosted_zone["Id"]) |
| 45 | + |
| 46 | + if allow_aws_request(): |
| 47 | + return create_hosted_zone() |
| 48 | + else: |
| 49 | + with mock_aws(): |
| 50 | + return create_hosted_zone() |
| 51 | + |
| 52 | + return pagination_wrapper |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.aws_verified |
| 56 | +@route53_aws_verified |
| 57 | +def test_hosted_zone_id_in_change_tags(hosted_zone=None): |
| 58 | + client = boto3.client("route53", "us-east-1") |
| 59 | + |
| 60 | + full_zone_id = hosted_zone["Id"] |
| 61 | + assert full_zone_id.startswith("/hostedzone/") |
| 62 | + |
| 63 | + # Can't use full ID here |
| 64 | + with pytest.raises(ClientError) as exc: |
| 65 | + client.change_tags_for_resource( |
| 66 | + ResourceType="hostedzone", |
| 67 | + ResourceId=full_zone_id, |
| 68 | + AddTags=[{"Key": "foo", "Value": "bar"}], |
| 69 | + ) |
| 70 | + err = exc.value.response["Error"] |
| 71 | + assert err["Code"] == "InvalidInput" |
| 72 | + assert ( |
| 73 | + err["Message"] |
| 74 | + == f"1 validation error detected: Value '{full_zone_id}' at 'resourceId' failed to satisfy constraint: Member must have length less than or equal to 32" |
| 75 | + ) |
| 76 | + |
| 77 | + # if we naively limit the id-length to 32, we get a NoSuchHostedZone-exception (as expected) |
| 78 | + with pytest.raises(ClientError) as exc: |
| 79 | + client.change_tags_for_resource( |
| 80 | + ResourceType="hostedzone", |
| 81 | + ResourceId=full_zone_id[0:32], |
| 82 | + AddTags=[{"Key": "foo", "Value": "bar"}], |
| 83 | + ) |
| 84 | + err = exc.value.response["Error"] |
| 85 | + assert err["Code"] == "NoSuchHostedZone" |
| 86 | + |
| 87 | + # Need to strip the '/hosted_zone/'-prefix |
| 88 | + id_without_prefix = full_zone_id.replace("/hostedzone/", "") |
| 89 | + client.change_tags_for_resource( |
| 90 | + ResourceType="hostedzone", |
| 91 | + ResourceId=id_without_prefix, |
| 92 | + AddTags=[{"Key": "foo", "Value": "bar"}], |
| 93 | + ) |
| 94 | + |
| 95 | + # Test retrieval of tags with full ID |
| 96 | + with pytest.raises(ClientError) as exc: |
| 97 | + client.list_tags_for_resource( |
| 98 | + ResourceType="hostedzone", ResourceId=full_zone_id |
| 99 | + ) |
| 100 | + err = exc.value.response["Error"] |
| 101 | + assert err["Code"] == "InvalidInput" |
| 102 | + assert ( |
| 103 | + err["Message"] |
| 104 | + == f"1 validation error detected: Value '{full_zone_id}' at 'resourceId' failed to satisfy constraint: Member must have length less than or equal to 32" |
| 105 | + ) |
| 106 | + |
| 107 | + # Test retrieval of tags with stripped ID |
| 108 | + tags = client.list_tags_for_resource( |
| 109 | + ResourceType="hostedzone", ResourceId=id_without_prefix |
| 110 | + )["ResourceTagSet"] |
| 111 | + assert tags == { |
| 112 | + "ResourceId": id_without_prefix, |
| 113 | + "ResourceType": "hostedzone", |
| 114 | + "Tags": [{"Key": "foo", "Value": "bar"}], |
| 115 | + } |
0 commit comments