Repository: getmoto/moto

Issue:
IoT models hard-codes account ID as 1
the cert that is created when doing something such as client('iot').create_keys_and_certificate(setAsActive=True) has an account id hard-coded to 1 instead of retrieving the accountid that other things do.


https://github.com/spulec/moto/blob/master/moto/iot/models.py#L139

I changed it to

`self.arn = "arn:aws:iot:%s:%s:cert/%s" % (region_name, ACCOUNT_ID, self.certificate_id)`

and added ACCOUNT_ID import from moto.core and it worked.


Discussion:
Good spot @dhmistry3 - thanks for raising this!

Resolving patch:
diff --git a/moto/iot/models.py b/moto/iot/models.py
--- a/moto/iot/models.py
+++ b/moto/iot/models.py
@@ -10,7 +10,7 @@
 from .utils import PAGINATION_MODEL
 
 from boto3 import Session
-from moto.core import BaseBackend, BaseModel
+from moto.core import ACCOUNT_ID, BaseBackend, BaseModel
 from moto.utilities.utils import random_string
 from moto.utilities.paginator import paginate
 from .exceptions import (
@@ -31,7 +31,7 @@ def __init__(self, thing_name, thing_type, attributes, region_name):
         self.thing_name = thing_name
         self.thing_type = thing_type
         self.attributes = attributes
-        self.arn = "arn:aws:iot:%s:1:thing/%s" % (self.region_name, thing_name)
+        self.arn = f"arn:aws:iot:{region_name}:{ACCOUNT_ID}:thing/{thing_name}"
         self.version = 1
         # TODO: we need to handle "version"?
 


Test patch:
diff --git a/tests/test_iot/test_iot.py b/tests/test_iot/test_iot.py
--- a/tests/test_iot/test_iot.py
+++ b/tests/test_iot/test_iot.py
@@ -3,6 +3,7 @@
 import boto3
 
 from moto import mock_iot, mock_cognitoidentity
+from moto.core import ACCOUNT_ID
 from botocore.exceptions import ClientError
 import pytest
 
@@ -395,10 +396,12 @@ def test_list_things_with_next_token():
     things.should.have.key("nextToken")
     things.should.have.key("things").which.should.have.length_of(50)
     things["things"][0]["thingName"].should.equal("1")
-    things["things"][0]["thingArn"].should.equal("arn:aws:iot:ap-northeast-1:1:thing/1")
+    things["things"][0]["thingArn"].should.equal(
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/1"
+    )
     things["things"][-1]["thingName"].should.equal("50")
     things["things"][-1]["thingArn"].should.equal(
-        "arn:aws:iot:ap-northeast-1:1:thing/50"
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/50"
     )
 
     things = client.list_things(nextToken=things["nextToken"])
@@ -406,11 +409,11 @@ def test_list_things_with_next_token():
     things.should.have.key("things").which.should.have.length_of(50)
     things["things"][0]["thingName"].should.equal("51")
     things["things"][0]["thingArn"].should.equal(
-        "arn:aws:iot:ap-northeast-1:1:thing/51"
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/51"
     )
     things["things"][-1]["thingName"].should.equal("100")
     things["things"][-1]["thingArn"].should.equal(
-        "arn:aws:iot:ap-northeast-1:1:thing/100"
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/100"
     )
 
     things = client.list_things(nextToken=things["nextToken"])
@@ -418,11 +421,11 @@ def test_list_things_with_next_token():
     things.should.have.key("things").which.should.have.length_of(50)
     things["things"][0]["thingName"].should.equal("101")
     things["things"][0]["thingArn"].should.equal(
-        "arn:aws:iot:ap-northeast-1:1:thing/101"
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/101"
     )
     things["things"][-1]["thingName"].should.equal("150")
     things["things"][-1]["thingArn"].should.equal(
-        "arn:aws:iot:ap-northeast-1:1:thing/150"
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/150"
     )
 
     things = client.list_things(nextToken=things["nextToken"])
@@ -430,11 +433,11 @@ def test_list_things_with_next_token():
     things.should.have.key("things").which.should.have.length_of(50)
     things["things"][0]["thingName"].should.equal("151")
     things["things"][0]["thingArn"].should.equal(
-        "arn:aws:iot:ap-northeast-1:1:thing/151"
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/151"
     )
     things["things"][-1]["thingName"].should.equal("200")
     things["things"][-1]["thingArn"].should.equal(
-        "arn:aws:iot:ap-northeast-1:1:thing/200"
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/200"
     )
 
 
@@ -468,10 +471,12 @@ def test_list_things_with_attribute_and_thing_type_filter_and_next_token():
     things.should.have.key("nextToken")
     things.should.have.key("things").which.should.have.length_of(50)
     things["things"][0]["thingName"].should.equal("2")
-    things["things"][0]["thingArn"].should.equal("arn:aws:iot:ap-northeast-1:1:thing/2")
+    things["things"][0]["thingArn"].should.equal(
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/2"
+    )
     things["things"][-1]["thingName"].should.equal("100")
     things["things"][-1]["thingArn"].should.equal(
-        "arn:aws:iot:ap-northeast-1:1:thing/100"
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/100"
     )
     all(item["thingTypeName"] == thing_type_name for item in things["things"])
 
@@ -482,11 +487,11 @@ def test_list_things_with_attribute_and_thing_type_filter_and_next_token():
     things.should.have.key("things").which.should.have.length_of(50)
     things["things"][0]["thingName"].should.equal("102")
     things["things"][0]["thingArn"].should.equal(
-        "arn:aws:iot:ap-northeast-1:1:thing/102"
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/102"
     )
     things["things"][-1]["thingName"].should.equal("200")
     things["things"][-1]["thingArn"].should.equal(
-        "arn:aws:iot:ap-northeast-1:1:thing/200"
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/200"
     )
     all(item["thingTypeName"] == thing_type_name for item in things["things"])
 
@@ -495,10 +500,12 @@ def test_list_things_with_attribute_and_thing_type_filter_and_next_token():
     things.should.have.key("nextToken")
     things.should.have.key("things").which.should.have.length_of(50)
     things["things"][0]["thingName"].should.equal("3")
-    things["things"][0]["thingArn"].should.equal("arn:aws:iot:ap-northeast-1:1:thing/3")
+    things["things"][0]["thingArn"].should.equal(
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/3"
+    )
     things["things"][-1]["thingName"].should.equal("150")
     things["things"][-1]["thingArn"].should.equal(
-        "arn:aws:iot:ap-northeast-1:1:thing/150"
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/150"
     )
     all(item["attributes"] == {"foo": "bar"} for item in things["things"])
 
@@ -509,11 +516,11 @@ def test_list_things_with_attribute_and_thing_type_filter_and_next_token():
     things.should.have.key("things").which.should.have.length_of(16)
     things["things"][0]["thingName"].should.equal("153")
     things["things"][0]["thingArn"].should.equal(
-        "arn:aws:iot:ap-northeast-1:1:thing/153"
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/153"
     )
     things["things"][-1]["thingName"].should.equal("198")
     things["things"][-1]["thingArn"].should.equal(
-        "arn:aws:iot:ap-northeast-1:1:thing/198"
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/198"
     )
     all(item["attributes"] == {"foo": "bar"} for item in things["things"])
 
@@ -524,10 +531,12 @@ def test_list_things_with_attribute_and_thing_type_filter_and_next_token():
     things.should_not.have.key("nextToken")
     things.should.have.key("things").which.should.have.length_of(33)
     things["things"][0]["thingName"].should.equal("6")
-    things["things"][0]["thingArn"].should.equal("arn:aws:iot:ap-northeast-1:1:thing/6")
+    things["things"][0]["thingArn"].should.equal(
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/6"
+    )
     things["things"][-1]["thingName"].should.equal("198")
     things["things"][-1]["thingArn"].should.equal(
-        "arn:aws:iot:ap-northeast-1:1:thing/198"
+        f"arn:aws:iot:ap-northeast-1:{ACCOUNT_ID}:thing/198"
     )
     all(
         item["attributes"] == {"foo": "bar"}