diff --git a/moto/iot/models.py b/moto/iot/models.py index 96078710125..a828a05f89e 100644 --- a/moto/iot/models.py +++ b/moto/iot/models.py @@ -331,6 +331,12 @@ def delete_thing(self, thing_name, expected_version): # can raise ResourceNotFoundError thing = self.describe_thing(thing_name) + + # detach all principals + for k in list(self.principal_things.keys()): + if k[1] == thing_name: + del self.principal_things[k] + del self.things[thing.arn] def delete_thing_type(self, thing_type_name): diff --git a/tests/test_iot/test_iot.py b/tests/test_iot/test_iot.py index a580f56d17a..23d4e78764f 100644 --- a/tests/test_iot/test_iot.py +++ b/tests/test_iot/test_iot.py @@ -519,6 +519,25 @@ def test_principal_thing(): res.should.have.key('principals').which.should.have.length_of(0) +@mock_iot +def test_delete_principal_thing(): + client = boto3.client('iot', region_name='ap-northeast-1') + thing_name = 'my-thing' + thing = client.create_thing(thingName=thing_name) + cert = client.create_keys_and_certificate(setAsActive=True) + cert_arn = cert['certificateArn'] + cert_id = cert['certificateId'] + + client.attach_thing_principal(thingName=thing_name, principal=cert_arn) + + client.delete_thing(thingName=thing_name) + res = client.list_principal_things(principal=cert_arn) + res.should.have.key('things').which.should.have.length_of(0) + + client.update_certificate(certificateId=cert_id, newStatus="INACTIVE") + client.delete_certificate(certificateId=cert_id) + + @mock_iot def test_thing_groups(): client = boto3.client('iot', region_name='ap-northeast-1')