1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
import boto3
import pytest
from botocore.exceptions import ClientError
from moto import mock_aws
@mock_aws
def test_create_billing_group():
client = boto3.client("iot", region_name="us-east-1")
billing_group_name = "test-billing-group"
properties = {"billingGroupDescription": "Test billing group"}
response = client.create_billing_group(
billingGroupName=billing_group_name,
billingGroupProperties=properties,
)
assert response["billingGroupName"] == billing_group_name
assert "billingGroupArn" in response
assert "billingGroupId" in response
# Test creating a billing group that already exists
with pytest.raises(ClientError) as exc:
client.create_billing_group(
billingGroupName=billing_group_name,
billingGroupProperties=properties,
)
assert exc.value.response["Error"]["Code"] == "ResourceAlreadyExistsException"
@mock_aws
def test_describe_billing_group():
client = boto3.client("iot", region_name="us-east-1")
billing_group_name = "test-billing-group"
properties = {"billingGroupDescription": "Test billing group"}
client.create_billing_group(
billingGroupName=billing_group_name,
billingGroupProperties=properties,
)
response = client.describe_billing_group(billingGroupName=billing_group_name)
assert response["billingGroupName"] == billing_group_name
assert "billingGroupArn" in response
assert "billingGroupId" in response
assert response["billingGroupProperties"] == properties
assert "billingGroupMetadata" in response
# Test describing a non-existent billing group
with pytest.raises(ClientError) as exc:
client.describe_billing_group(billingGroupName="non-existent-group")
assert exc.value.response["Error"]["Code"] == "ResourceNotFoundException"
@mock_aws
def test_delete_billing_group():
client = boto3.client("iot", region_name="us-east-1")
billing_group_name = "test-billing-group"
properties = {"billingGroupDescription": "Test billing group"}
client.create_billing_group(
billingGroupName=billing_group_name,
billingGroupProperties=properties,
)
client.delete_billing_group(billingGroupName=billing_group_name)
# Verify the billing group is deleted
with pytest.raises(ClientError) as exc:
client.describe_billing_group(billingGroupName=billing_group_name)
assert exc.value.response["Error"]["Code"] == "ResourceNotFoundException"
# Test deleting a non-existent billing group
with pytest.raises(ClientError) as exc:
client.delete_billing_group(billingGroupName="non-existent-group")
assert exc.value.response["Error"]["Code"] == "ResourceNotFoundException"
@mock_aws
def test_list_billing_groups():
client = boto3.client("iot", region_name="us-east-1")
for i in range(5):
client.create_billing_group(
billingGroupName=f"billing-group-{i}",
billingGroupProperties={"billingGroupDescription": f"Group {i}"},
)
response = client.list_billing_groups()
assert len(response["billingGroups"]) == 5
response = client.list_billing_groups(maxResults=2)
assert len(response["billingGroups"]) == 2
assert "nextToken" in response
response = client.list_billing_groups(maxResults=3, nextToken=response["nextToken"])
assert len(response["billingGroups"]) == 3
@mock_aws
def test_update_billing_group():
client = boto3.client("iot", region_name="us-east-1")
billing_group_name = "test-billing-group"
properties = {"billingGroupDescription": "Test billing group"}
client.create_billing_group(
billingGroupName=billing_group_name,
billingGroupProperties=properties,
)
new_properties = {"billingGroupDescription": "Updated test billing group"}
response = client.update_billing_group(
billingGroupName=billing_group_name,
billingGroupProperties=new_properties,
)
assert "version" in response
described = client.describe_billing_group(billingGroupName=billing_group_name)
assert described["billingGroupProperties"] == new_properties
@mock_aws
def test_add_thing_to_billing_group():
client = boto3.client("iot", region_name="us-east-1")
billing_group_name = "test-billing-group"
thing_name = "test-thing"
client.create_billing_group(billingGroupName=billing_group_name)
client.create_thing(thingName=thing_name)
client.add_thing_to_billing_group(
billingGroupName=billing_group_name, thingName=thing_name
)
response = client.list_things_in_billing_group(billingGroupName=billing_group_name)
assert len(response["things"]) == 1
assert thing_name in response["things"]
@mock_aws
def test_remove_thing_from_billing_group():
client = boto3.client("iot", region_name="us-east-1")
billing_group_name = "test-billing-group"
thing_name = "test-thing"
client.create_billing_group(billingGroupName=billing_group_name)
client.create_thing(thingName=thing_name)
client.add_thing_to_billing_group(
billingGroupName=billing_group_name, thingName=thing_name
)
client.remove_thing_from_billing_group(
billingGroupName=billing_group_name, thingName=thing_name
)
response = client.list_things_in_billing_group(billingGroupName=billing_group_name)
assert len(response["things"]) == 0
@mock_aws
def test_remove_thing_from_billing_group_by_arn():
client = boto3.client("iot", region_name="us-east-1")
billing_group_name = "test-billing-group"
thing_name = "test-thing"
# Create billing group and thing
create_bg_resp = client.create_billing_group(billingGroupName=billing_group_name)
create_thing_resp = client.create_thing(thingName=thing_name)
# Add thing to billing group using names
client.add_thing_to_billing_group(
billingGroupName=billing_group_name, thingName=thing_name
)
# Verify the thing is in the billing group
response = client.list_things_in_billing_group(billingGroupName=billing_group_name)
assert len(response["things"]) == 1
assert thing_name in response["things"]
# Get ARNs
billing_group_arn = create_bg_resp["billingGroupArn"]
thing_arn = create_thing_resp["thingArn"]
# Remove thing from billing group using ARNs
client.remove_thing_from_billing_group(
billingGroupArn=billing_group_arn, thingArn=thing_arn
)
# Verify the thing is no longer in the billing group
response = client.list_things_in_billing_group(billingGroupName=billing_group_name)
assert len(response["things"]) == 0
@mock_aws
def test_list_things_in_billing_group():
client = boto3.client("iot", region_name="us-east-1")
billing_group_name = "test-billing-group"
client.create_billing_group(billingGroupName=billing_group_name)
for i in range(5):
thing_name = f"thing-{i}"
client.create_thing(thingName=thing_name)
client.add_thing_to_billing_group(
billingGroupName=billing_group_name, thingName=thing_name
)
response = client.list_things_in_billing_group(billingGroupName=billing_group_name)
assert len(response["things"]) == 5
response = client.list_things_in_billing_group(
billingGroupName=billing_group_name, maxResults=2
)
assert len(response["things"]) == 2
assert "nextToken" in response
response = client.list_things_in_billing_group(
billingGroupName=billing_group_name,
maxResults=3,
nextToken=response["nextToken"],
)
assert len(response["things"]) == 3
@mock_aws
def test_create_thing_with_billing_group():
client = boto3.client("iot", region_name="us-east-1")
billing_group_name = "test-billing-group"
thing_name = "test-thing"
client.create_billing_group(billingGroupName=billing_group_name)
client.create_thing(thingName=thing_name, billingGroupName=billing_group_name)
response = client.describe_thing(thingName=thing_name)
assert response["billingGroupName"] == billing_group_name
@mock_aws
def test_thing_removed_from_billing_group_on_thing_deletion():
client = boto3.client("iot", region_name="us-east-1")
billing_group_name = "test-billing-group"
thing_name = "test-thing"
client.create_billing_group(billingGroupName=billing_group_name)
client.create_thing(thingName=thing_name, billingGroupName=billing_group_name)
# Verify the thing is in the billing group
response = client.list_things_in_billing_group(billingGroupName=billing_group_name)
assert len(response["things"]) == 1
assert thing_name in response["things"]
# Delete the thing
client.delete_thing(thingName=thing_name)
# Verify the thing is no longer in the billing group
response = client.list_things_in_billing_group(billingGroupName=billing_group_name)
assert len(response["things"]) == 0
@mock_aws
def test_simple_list_billing_groups_with_prefix():
client = boto3.client("iot", region_name="us-east-1")
client.create_billing_group(billingGroupName="test-prefix-group")
client.create_billing_group(billingGroupName="other-prefix-group")
response = client.list_billing_groups(namePrefixFilter="test-prefix-")
assert len(response["billingGroups"]) == 1
assert response["billingGroups"][0]["groupName"] == "test-prefix-group"
|