How to use the moto.mock_iam function in moto

To help you get started, we’ve selected a few moto examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github spulec / moto / tests / test_core / test_auth.py View on Github external
@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,
github spulec / moto / tests / test_iam / test_iam.py View on Github external
@mock_iam
def test_list_policy_versions():
    conn = boto3.client("iam", region_name="us-east-1")
    with assert_raises(ClientError):
        versions = conn.list_policy_versions(
            PolicyArn="arn:aws:iam::123456789012:policy/TestListPolicyVersions"
        )
    conn.create_policy(PolicyName="TestListPolicyVersions", PolicyDocument=MOCK_POLICY)
    versions = conn.list_policy_versions(
        PolicyArn="arn:aws:iam::123456789012:policy/TestListPolicyVersions"
    )
    versions.get("Versions")[0].get("VersionId").should.equal("v1")
    versions.get("Versions")[0].get("IsDefaultVersion").should.be.ok

    conn.create_policy_version(
        PolicyArn="arn:aws:iam::123456789012:policy/TestListPolicyVersions",
        PolicyDocument=MOCK_POLICY_2,
github rackerlabs / ebs_snapper / tests / test_utils.py View on Github external
@mock_iam
@mock_sts
def test_region_contains_snapshots():
    """Test for method of the same name."""
    client = boto3.client('ec2', region_name='us-west-2')

    # toss a volume in us-west-2 and snapshot it
    volume_results = client.create_volume(Size=100, AvailabilityZone='us-west-1a')
    client.create_snapshot(VolumeId=volume_results['VolumeId'])

    # be sure we get us-west-2
    assert utils.region_contains_snapshots('us-west-2')

    # be sure we don't get us-east-1
    assert not utils.region_contains_snapshots('us-east-1')
github spulec / moto / tests / test_iam / test_iam.py View on Github external
@mock_iam
def test_boto3_create_login_profile():
    conn = boto3.client("iam", region_name="us-east-1")

    with assert_raises(ClientError):
        conn.create_login_profile(UserName="my-user", Password="Password")

    conn.create_user(UserName="my-user")
    conn.create_login_profile(UserName="my-user", Password="Password")

    with assert_raises(ClientError):
        conn.create_login_profile(UserName="my-user", Password="Password")
github spulec / moto / tests / test_iam / test_iam_policies.py View on Github external
@mock_iam
def check_create_policy_with_invalid_policy_document(test_case):
    conn = boto3.client("iam", region_name="us-east-1")
    with assert_raises(ClientError) as ex:
        conn.create_policy(
            PolicyName="TestCreatePolicy",
            PolicyDocument=json.dumps(test_case["document"]),
        )
    ex.exception.response["Error"]["Code"].should.equal("MalformedPolicyDocument")
    ex.exception.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
    ex.exception.response["Error"]["Message"].should.equal(test_case["error_message"])
github spulec / moto / tests / test_iam / test_iam.py View on Github external
@mock_iam
def test_create_policy_versions():
    conn = boto3.client("iam", region_name="us-east-1")
    with assert_raises(ClientError):
        conn.create_policy_version(
            PolicyArn="arn:aws:iam::123456789012:policy/TestCreatePolicyVersion",
            PolicyDocument='{"some":"policy"}',
        )
    conn.create_policy(PolicyName="TestCreatePolicyVersion", PolicyDocument=MOCK_POLICY)
    version = conn.create_policy_version(
        PolicyArn="arn:aws:iam::123456789012:policy/TestCreatePolicyVersion",
        PolicyDocument=MOCK_POLICY,
        SetAsDefault=True,
    )
    version.get("PolicyVersion").get("Document").should.equal(json.loads(MOCK_POLICY))
    version.get("PolicyVersion").get("VersionId").should.equal("v2")
    version.get("PolicyVersion").get("IsDefaultVersion").should.be.ok
github rackerlabs / ebs_snapper / tests / test_snapshot.py View on Github external
@mock_ec2
@mock_dynamodb2
@mock_sns
@mock_iam
@mock_sts
def test_perform_snapshot_ignore_instance(mocker):
    """Test for method of the same name."""
    # some default settings for this test
    region = 'us-west-2'

    snapshot_settings = {
        'snapshot': {'minimum': 5, 'frequency': '2 hours', 'retention': '5 days'},
        'match': {'tag:backup': 'yes'},
        'ignore': []
    }

    # create an instance and record the id
    instance_id = mocks.create_instances(region, count=1)[0]
    snapshot_settings['ignore'].append(instance_id)
github spulec / moto / tests / test_awslambda / test_lambda_cloudformation.py View on Github external
def get_role_arn():
    with mock_iam():
        iam = boto3.client("iam", region_name="us-west-2")
        try:
            return iam.get_role(RoleName="my-role")["Role"]["Arn"]
        except ClientError:
            return iam.create_role(
                RoleName="my-role",
                AssumeRolePolicyDocument="some policy",
                Path="/my-path/",
            )["Role"]["Arn"]
github spulec / moto / tests / test_iam / test_iam_account_aliases.py View on Github external
@mock_iam()
def test_account_aliases():
    client = boto3.client("iam", region_name="us-east-1")

    alias = "my-account-name"
    aliases = client.list_account_aliases()
    aliases.should.have.key("AccountAliases").which.should.equal([])

    client.create_account_alias(AccountAlias=alias)
    aliases = client.list_account_aliases()
    aliases.should.have.key("AccountAliases").which.should.equal([alias])

    client.delete_account_alias(AccountAlias=alias)
    aliases = client.list_account_aliases()
    aliases.should.have.key("AccountAliases").which.should.equal([])
github spulec / moto / tests / test_iam / test_iam.py View on Github external
@mock_iam
def test_signing_certs():
    client = boto3.client("iam", region_name="us-east-1")

    # Create the IAM user first:
    client.create_user(UserName="testing")

    # Upload the cert:
    resp = client.upload_signing_certificate(
        UserName="testing", CertificateBody=MOCK_CERT
    )["Certificate"]
    cert_id = resp["CertificateId"]

    assert resp["UserName"] == "testing"
    assert resp["Status"] == "Active"
    assert resp["CertificateBody"] == MOCK_CERT
    assert resp["CertificateId"]