File: test_iot_things.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (315 lines) | stat: -rw-r--r-- 10,735 bytes parent folder | download
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import boto3

from moto import mock_aws
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID


@mock_aws
def test_create_thing():
    client = boto3.client("iot", region_name="ap-northeast-1")
    name = "my-thing"

    thing = client.create_thing(thingName=name)
    assert thing["thingName"] == name
    assert thing["thingArn"] is not None
    assert thing["thingId"] is not None

    res = client.list_things()
    assert len(res["things"]) == 1
    assert res["things"][0]["thingName"] == name
    assert res["things"][0]["thingArn"] == thing["thingArn"]


@mock_aws
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)
    assert thing["thingName"] == name
    assert "thingArn" in thing

    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

    thing = client.describe_thing(thingName=name)
    assert thing["thingTypeName"] == type_name


@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_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()
    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"}

    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"}

    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
    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
    assert res["things"][0]["attributes"] == {}


@mock_aws
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)
    assert thing["thingId"] is not None
    assert thing["thingName"] == name
    assert "defaultClientId" in thing
    assert thing["attributes"] == {"k1": "v1"}
    assert thing["version"] == 1


@mock_aws
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()
    assert len(res["things"]) == 0


@mock_aws
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()
    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"
    )
    assert things["things"][-1]["thingName"] == "50"
    assert (
        things["things"][-1]["thingArn"]
        == f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/50"
    )

    things = client.list_things(nextToken=things["nextToken"])
    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"
    )
    assert things["things"][-1]["thingName"] == "100"
    assert (
        things["things"][-1]["thingArn"]
        == f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/100"
    )

    things = client.list_things(nextToken=things["nextToken"])
    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"
    )
    assert things["things"][-1]["thingName"] == "150"
    assert (
        things["things"][-1]["thingArn"]
        == f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/150"
    )

    things = client.list_things(nextToken=things["nextToken"])
    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"
    )
    assert things["things"][-1]["thingName"] == "200"
    assert (
        things["things"][-1]["thingArn"]
        == f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/200"
    )


@mock_aws
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)
    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"
    )
    assert things["things"][-1]["thingName"] == "100"
    assert (
        things["things"][-1]["thingArn"]
        == f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/100"
    )
    assert all(item["thingTypeName"] == thing_type_name for item in things["things"])

    things = client.list_things(
        nextToken=things["nextToken"], thingTypeName=thing_type_name
    )
    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"
    )
    assert things["things"][-1]["thingName"] == "200"
    assert (
        things["things"][-1]["thingArn"]
        == f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/200"
    )
    assert all(item["thingTypeName"] == thing_type_name for item in things["things"])

    # Test filter for attributes
    things = client.list_things(attributeName="foo", attributeValue="bar")
    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"
    )
    assert things["things"][-1]["thingName"] == "150"
    assert (
        things["things"][-1]["thingArn"]
        == f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/150"
    )
    assert all(item["attributes"] == {"foo": "bar"} for item in things["things"])

    things = client.list_things(
        nextToken=things["nextToken"], attributeName="foo", attributeValue="bar"
    )
    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"
    )
    assert things["things"][-1]["thingName"] == "198"
    assert (
        things["things"][-1]["thingArn"]
        == f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/198"
    )
    assert all(item["attributes"] == {"foo": "bar"} for item in things["things"])

    # Test filter for attributes and thingTypeName
    things = client.list_things(
        thingTypeName=thing_type_name, attributeName="foo", attributeValue="bar"
    )
    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"
    )
    assert things["things"][-1]["thingName"] == "198"
    assert (
        things["things"][-1]["thingArn"]
        == f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/198"
    )
    assert all(
        item["attributes"] == {"foo": "bar"}
        and item["thingTypeName"] == thing_type_name
        for item in things["things"]
    )