PandasM
PandasM

Reputation: 31

Mock DynamoDB with assumed role using Moto

I am trying to mock a DynamoDB class that uses a session client after assuming a role, and using Moto inside my unit test. The error I keep getting is NoCredentialsError. The DynamoDB class's constructor contains the session client. I am uncertain on how to mock sts when its inside the DynamoDB class. Any help would be appreciated. Thanks.

ddb.py

@mock_sts
@mock_dynamodb
class TestDatabaseConnector(unittest.TestCase):
    def test_dynamodb(self):
        os.environ["AWS_ACCESS_KEY_ID"] = "testing"
        os.environ["AWS_SECRET_ACCESS_KEY"] = "testing"
        os.environ["AWS_SECURITY_TOKEN"] = "testing"
        os.environ["AWS_SESSION_TOKEN"] = "testing"
        os.environ["AWS_DEFAULT_REGION"] = "us-east-1"

        ddb = DynamoDB(table_name="metrics")
        ddb.create_table(
            TableName="metrics",
            KeySchema=[
                {"AttributeName": "partition", "KeyType": "HASH"},
            ],
            AttributeDefinitions=[
                {"AttributeName": "partition", "AttributeType": "S"},
            ],
            ProvisionedThroughput={"ReadCapacityUnits": 1, "WriteCapacityUnits": 1},
        )

        response = ddb.put_item(item={"partition": {"S": "test"}})
        response = ddb.get_item(
            item={
                "partition": {"S": "test"},
            }
        )
        value = response["partition"]["S"]
        assert value == "test"

Upvotes: 0

Views: 323

Answers (0)

Related Questions