Giter Site home page Giter Site logo

Comments (3)

bblommers avatar bblommers commented on June 14, 2024 1

Yes @0ri0nRo, that looks like it should work.

from moto.

bblommers avatar bblommers commented on June 14, 2024

Hi @0ri0nRo, Moto v5 (the latest release) usesmock_aws instead of mock_s3.

Which test example are you using? If it comes from the official Moto documentation, it should always say mock_aws.

from moto.

0ri0nRo avatar 0ri0nRo commented on June 14, 2024

If my code is:

import unittest
import lambda_function
import boto3
import botocore
import json

from moto import mock_iam
from lambda_function import AccessKey

class AccessKeyTest(unittest.TestCase):
    def test_init(self):
        username = 'abc'
        a = AccessKey(username, 'eu-north-1')
        self.assertEqual(a.user, username)
        self.assertEqual(a.param_region, 'eu-north-1')
        self.assertEqual(a.param_access, f'/{username}/ACCESS_KEY_ID')
        self.assertEqual(a.param_secret, f'/{username}/ACCESS_SECRET_KEY')
    
    def test_custom_params(self):
        a = AccessKey('abc', 'eu-north-1', '/custom/access', '/custom/secret')
        self.assertEqual(a.param_access, '/custom/access')
        self.assertEqual(a.param_secret, '/custom/secret')
    
    @mock_iam
    def create_user_with_access_key_and_multiple_policies(
        user_name,
        inline_policy_document,
        attached_policy_document,
        inline_policy_name="policy1",
        attached_policy_name="policy1",
    ):
        client = boto3.client("iam", region_name="us-east-1")
        client.create_user(UserName=user_name)
        policy_arn = client.create_policy(
            PolicyName=attached_policy_name,
            PolicyDocument=json.dumps(attached_policy_document),
        )["Policy"]["Arn"]
        client.attach_user_policy(UserName=user_name, PolicyArn=policy_arn)
        client.put_user_policy(
            UserName=user_name,
            PolicyName=inline_policy_name,
            PolicyDocument=json.dumps(inline_policy_document),
        )
        return client.create_access_key(UserName=user_name)["AccessKey"]



if __name__ == '__main__':
    unittest.main() 

I need to change as follow (?):

import unittest
import lambda_function
import boto3
import botocore
import json


from moto import mock_aws
from lambda_function import AccessKey

class AccessKeyTest(unittest.TestCase):
    def test_init(self):
        username = 'abc'
        a = AccessKey(username, 'eu-north-1')
        self.assertEqual(a.user, username)
        self.assertEqual(a.param_region, 'eu-north-1')
        self.assertEqual(a.param_access, f'/{username}/ACCESS_KEY_ID')
        self.assertEqual(a.param_secret, f'/{username}/ACCESS_SECRET_KEY')
    
    def test_custom_params(self):
        a = AccessKey('abc', 'eu-north-1', '/custom/access', '/custom/secret')
        self.assertEqual(a.param_access, '/custom/access')
        self.assertEqual(a.param_secret, '/custom/secret')
    
    @mock_aws 
    def create_user_with_access_key_and_multiple_policies(
        user_name,
        inline_policy_document,
        attached_policy_document,
        inline_policy_name="policy1",
        attached_policy_name="policy1",
    ):
        client = boto3.client("iam", region_name="us-east-1")
        client.create_user(UserName=user_name)
        policy_arn = client.create_policy(
            PolicyName=attached_policy_name,
            PolicyDocument=json.dumps(attached_policy_document),
        )["Policy"]["Arn"]
        client.attach_user_policy(UserName=user_name, PolicyArn=policy_arn)
        client.put_user_policy(
            UserName=user_name,
            PolicyName=inline_policy_name,
            PolicyDocument=json.dumps(inline_policy_document),
        )
        return client.create_access_key(UserName=user_name)["AccessKey"]



if __name__ == '__main__':
    unittest.main()

from moto.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.