2021-12-22 15:41:34 +00:00
|
|
|
import boto3
|
|
|
|
|
|
|
|
from moto import mock_iot
|
2022-08-13 09:49:43 +00:00
|
|
|
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
|
2021-12-22 15:41:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_iot
|
|
|
|
def test_create_thing():
|
|
|
|
client = boto3.client("iot", region_name="ap-northeast-1")
|
|
|
|
name = "my-thing"
|
|
|
|
|
|
|
|
thing = client.create_thing(thingName=name)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert thing["thingName"] == name
|
|
|
|
assert "thingArn" in thing
|
2021-12-22 15:41:34 +00:00
|
|
|
|
|
|
|
res = client.list_things()
|
2023-07-31 21:50:24 +00:00
|
|
|
assert len(res["things"]) == 1
|
|
|
|
assert res["things"][0]["thingName"] is not None
|
|
|
|
assert res["things"][0]["thingArn"] is not None
|
2021-12-22 15:41:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_iot
|
|
|
|
def test_create_thing_with_type():
|
|
|
|
client = boto3.client("iot", region_name="ap-northeast-1")
|
|
|
|
name = "my-thing"
|
|
|
|
type_name = "my-type-name"
|
|
|
|
|
|
|
|
client.create_thing_type(thingTypeName=type_name)
|
|
|
|
|
|
|
|
thing = client.create_thing(thingName=name, thingTypeName=type_name)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert thing["thingName"] == name
|
|
|
|
assert "thingArn" in thing
|
2021-12-22 15:41:34 +00:00
|
|
|
|
|
|
|
res = client.list_things()
|
2023-07-31 21:50:24 +00:00
|
|
|
assert len(res["things"]) == 1
|
|
|
|
assert res["things"][0]["thingName"] is not None
|
|
|
|
assert res["things"][0]["thingArn"] is not None
|
2021-12-22 15:41:34 +00:00
|
|
|
|
|
|
|
thing = client.describe_thing(thingName=name)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert thing["thingTypeName"] == type_name
|
2021-12-22 15:41:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_iot
|
|
|
|
def test_update_thing():
|
|
|
|
client = boto3.client("iot", region_name="ap-northeast-1")
|
|
|
|
name = "my-thing"
|
|
|
|
|
|
|
|
client.create_thing(thingName=name)
|
|
|
|
|
|
|
|
client.update_thing(thingName=name, attributePayload={"attributes": {"k1": "v1"}})
|
|
|
|
res = client.list_things()
|
2023-07-31 21:50:24 +00:00
|
|
|
assert len(res["things"]) == 1
|
|
|
|
assert res["things"][0]["thingName"] is not None
|
|
|
|
assert res["things"][0]["thingArn"] is not None
|
2023-10-15 21:40:15 +00:00
|
|
|
assert res["things"][0]["attributes"] == {"k1": "v1"}
|
|
|
|
|
|
|
|
client.update_thing(thingName=name, attributePayload={"attributes": {"k2": "v2"}})
|
|
|
|
res = client.list_things()
|
|
|
|
assert len(res["things"]) == 1
|
|
|
|
assert res["things"][0]["thingName"] is not None
|
|
|
|
assert res["things"][0]["thingArn"] is not None
|
|
|
|
assert res["things"][0]["attributes"] == {"k2": "v2"}
|
|
|
|
|
|
|
|
client.update_thing(
|
|
|
|
thingName=name, attributePayload={"attributes": {"k1": "v1"}, "merge": True}
|
|
|
|
)
|
|
|
|
res = client.list_things()
|
|
|
|
assert len(res["things"]) == 1
|
|
|
|
assert res["things"][0]["thingName"] is not None
|
|
|
|
assert res["things"][0]["thingArn"] is not None
|
|
|
|
assert res["things"][0]["attributes"] == {"k1": "v1", "k2": "v2"}
|
|
|
|
|
|
|
|
client.update_thing(
|
|
|
|
thingName=name, attributePayload={"attributes": {"k1": "v1.1"}, "merge": True}
|
|
|
|
)
|
|
|
|
res = client.list_things()
|
|
|
|
assert len(res["things"]) == 1
|
|
|
|
assert res["things"][0]["thingName"] is not None
|
|
|
|
assert res["things"][0]["thingArn"] is not None
|
|
|
|
assert res["things"][0]["attributes"] == {"k1": "v1.1", "k2": "v2"}
|
2021-12-22 15:41:34 +00:00
|
|
|
|
2023-10-13 19:09:30 +00:00
|
|
|
client.update_thing(
|
|
|
|
thingName=name, attributePayload={"attributes": {"k1": ""}, "merge": True}
|
|
|
|
)
|
|
|
|
res = client.list_things()
|
|
|
|
assert len(res["things"]) == 1
|
|
|
|
assert res["things"][0]["thingName"] is not None
|
|
|
|
assert res["things"][0]["thingArn"] is not None
|
2023-10-15 21:40:15 +00:00
|
|
|
assert res["things"][0]["attributes"] == {"k2": "v2"}
|
|
|
|
|
|
|
|
client.update_thing(thingName=name, attributePayload={"attributes": {"k2": ""}})
|
|
|
|
res = client.list_things()
|
|
|
|
assert len(res["things"]) == 1
|
|
|
|
assert res["things"][0]["thingName"] is not None
|
|
|
|
assert res["things"][0]["thingArn"] is not None
|
2023-10-13 19:09:30 +00:00
|
|
|
assert res["things"][0]["attributes"] == {}
|
|
|
|
|
2021-12-22 15:41:34 +00:00
|
|
|
|
|
|
|
@mock_iot
|
|
|
|
def test_describe_thing():
|
|
|
|
client = boto3.client("iot", region_name="ap-northeast-1")
|
|
|
|
name = "my-thing"
|
|
|
|
|
|
|
|
client.create_thing(thingName=name)
|
|
|
|
client.update_thing(thingName=name, attributePayload={"attributes": {"k1": "v1"}})
|
|
|
|
|
|
|
|
thing = client.describe_thing(thingName=name)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert thing["thingName"] == name
|
|
|
|
assert "defaultClientId" in thing
|
|
|
|
assert thing["attributes"] == {"k1": "v1"}
|
|
|
|
assert thing["version"] == 1
|
2021-12-22 15:41:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_iot
|
|
|
|
def test_delete_thing():
|
|
|
|
client = boto3.client("iot", region_name="ap-northeast-1")
|
|
|
|
name = "my-thing"
|
|
|
|
|
|
|
|
client.create_thing(thingName=name)
|
|
|
|
|
|
|
|
# delete thing
|
|
|
|
client.delete_thing(thingName=name)
|
|
|
|
|
|
|
|
res = client.list_things()
|
2023-07-31 21:50:24 +00:00
|
|
|
assert len(res["things"]) == 0
|
2021-12-22 15:41:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_iot
|
|
|
|
def test_list_things_with_next_token():
|
|
|
|
client = boto3.client("iot", region_name="ap-northeast-1")
|
|
|
|
|
|
|
|
for i in range(0, 200):
|
|
|
|
client.create_thing(thingName=str(i + 1))
|
|
|
|
|
|
|
|
things = client.list_things()
|
2023-07-31 21:50:24 +00:00
|
|
|
assert len(things["things"]) == 50
|
|
|
|
assert things["things"][0]["thingName"] == "1"
|
|
|
|
assert (
|
|
|
|
things["things"][0]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/1"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert things["things"][-1]["thingName"] == "50"
|
|
|
|
assert (
|
|
|
|
things["things"][-1]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/50"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
things = client.list_things(nextToken=things["nextToken"])
|
2023-07-31 21:50:24 +00:00
|
|
|
assert len(things["things"]) == 50
|
|
|
|
assert things["things"][0]["thingName"] == "51"
|
|
|
|
assert (
|
|
|
|
things["things"][0]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/51"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert things["things"][-1]["thingName"] == "100"
|
|
|
|
assert (
|
|
|
|
things["things"][-1]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/100"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
things = client.list_things(nextToken=things["nextToken"])
|
2023-07-31 21:50:24 +00:00
|
|
|
assert len(things["things"]) == 50
|
|
|
|
assert things["things"][0]["thingName"] == "101"
|
|
|
|
assert (
|
|
|
|
things["things"][0]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/101"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert things["things"][-1]["thingName"] == "150"
|
|
|
|
assert (
|
|
|
|
things["things"][-1]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/150"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
things = client.list_things(nextToken=things["nextToken"])
|
2023-07-31 21:50:24 +00:00
|
|
|
assert "nextToken" not in things
|
|
|
|
assert len(things["things"]) == 50
|
|
|
|
assert things["things"][0]["thingName"] == "151"
|
|
|
|
assert (
|
|
|
|
things["things"][0]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/151"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert things["things"][-1]["thingName"] == "200"
|
|
|
|
assert (
|
|
|
|
things["things"][-1]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/200"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@mock_iot
|
|
|
|
def test_list_things_with_attribute_and_thing_type_filter_and_next_token():
|
|
|
|
client = boto3.client("iot", region_name="ap-northeast-1")
|
|
|
|
client.create_thing_type(thingTypeName="my-thing-type")
|
|
|
|
|
|
|
|
for i in range(0, 200):
|
|
|
|
if not (i + 1) % 3:
|
|
|
|
attribute_payload = {"attributes": {"foo": "bar"}}
|
|
|
|
elif not (i + 1) % 5:
|
|
|
|
attribute_payload = {"attributes": {"bar": "foo"}}
|
|
|
|
else:
|
|
|
|
attribute_payload = {}
|
|
|
|
|
|
|
|
if not (i + 1) % 2:
|
|
|
|
thing_type_name = "my-thing-type"
|
|
|
|
client.create_thing(
|
|
|
|
thingName=str(i + 1),
|
|
|
|
thingTypeName=thing_type_name,
|
|
|
|
attributePayload=attribute_payload,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
client.create_thing(
|
|
|
|
thingName=str(i + 1), attributePayload=attribute_payload
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test filter for thingTypeName
|
|
|
|
things = client.list_things(thingTypeName=thing_type_name)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert "nextToken" in things
|
|
|
|
assert len(things["things"]) == 50
|
|
|
|
assert things["things"][0]["thingName"] == "2"
|
|
|
|
assert (
|
|
|
|
things["things"][0]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/2"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert things["things"][-1]["thingName"] == "100"
|
|
|
|
assert (
|
|
|
|
things["things"][-1]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/100"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert all(item["thingTypeName"] == thing_type_name for item in things["things"])
|
2021-12-22 15:41:34 +00:00
|
|
|
|
|
|
|
things = client.list_things(
|
|
|
|
nextToken=things["nextToken"], thingTypeName=thing_type_name
|
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert "nextToken" not in things
|
|
|
|
assert len(things["things"]) == 50
|
|
|
|
assert things["things"][0]["thingName"] == "102"
|
|
|
|
assert (
|
|
|
|
things["things"][0]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/102"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert things["things"][-1]["thingName"] == "200"
|
|
|
|
assert (
|
|
|
|
things["things"][-1]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/200"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert all(item["thingTypeName"] == thing_type_name for item in things["things"])
|
2021-12-22 15:41:34 +00:00
|
|
|
|
|
|
|
# Test filter for attributes
|
|
|
|
things = client.list_things(attributeName="foo", attributeValue="bar")
|
2023-07-31 21:50:24 +00:00
|
|
|
assert "nextToken" in things
|
|
|
|
assert len(things["things"]) == 50
|
|
|
|
assert things["things"][0]["thingName"] == "3"
|
|
|
|
assert (
|
|
|
|
things["things"][0]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/3"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert things["things"][-1]["thingName"] == "150"
|
|
|
|
assert (
|
|
|
|
things["things"][-1]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/150"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert all(item["attributes"] == {"foo": "bar"} for item in things["things"])
|
2021-12-22 15:41:34 +00:00
|
|
|
|
|
|
|
things = client.list_things(
|
|
|
|
nextToken=things["nextToken"], attributeName="foo", attributeValue="bar"
|
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert "nextToken" not in things
|
|
|
|
assert len(things["things"]) == 16
|
|
|
|
assert things["things"][0]["thingName"] == "153"
|
|
|
|
assert (
|
|
|
|
things["things"][0]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/153"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert things["things"][-1]["thingName"] == "198"
|
|
|
|
assert (
|
|
|
|
things["things"][-1]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/198"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert all(item["attributes"] == {"foo": "bar"} for item in things["things"])
|
2021-12-22 15:41:34 +00:00
|
|
|
|
|
|
|
# Test filter for attributes and thingTypeName
|
|
|
|
things = client.list_things(
|
|
|
|
thingTypeName=thing_type_name, attributeName="foo", attributeValue="bar"
|
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert "nextToken" not in things
|
|
|
|
assert len(things["things"]) == 33
|
|
|
|
assert things["things"][0]["thingName"] == "6"
|
|
|
|
assert (
|
|
|
|
things["things"][0]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/6"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert things["things"][-1]["thingName"] == "198"
|
|
|
|
assert (
|
|
|
|
things["things"][-1]["thingArn"]
|
|
|
|
== f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/198"
|
2021-12-22 15:41:34 +00:00
|
|
|
)
|
2023-07-31 21:50:24 +00:00
|
|
|
assert all(
|
2021-12-22 15:41:34 +00:00
|
|
|
item["attributes"] == {"foo": "bar"}
|
|
|
|
and item["thingTypeName"] == thing_type_name
|
|
|
|
for item in things["things"]
|
|
|
|
)
|