Path: blob/develop/tests/unit/customizations/test_assumerole.py
1567 views
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12from botocore.hooks import HierarchicalEmitter13from botocore.exceptions import ProfileNotFound14from botocore.session import Session15from botocore.credentials import (16AssumeRoleProvider,17CredentialResolver,18AssumeRoleWithWebIdentityProvider19)2021from awscli.testutils import mock, unittest22from awscli.customizations import assumerole232425class TestAssumeRolePlugin(unittest.TestCase):26def test_assume_role_provider_injected(self):27mock_assume_role = mock.Mock(spec=AssumeRoleProvider)28mock_web_identity = mock.Mock(spec=AssumeRoleWithWebIdentityProvider)29providers = {30'assume-role': mock_assume_role,31'assume-role-with-web-identity': mock_web_identity,32}33mock_resolver = mock.Mock(spec=CredentialResolver)34mock_resolver.get_provider = providers.get35session = mock.Mock(spec=Session)36session.get_component.return_value = mock_resolver3738assumerole.inject_assume_role_provider_cache(39session, event_name='building-command-table.foo')40session.get_component.assert_called_with('credential_provider')41self.assertIsInstance(mock_assume_role.cache, assumerole.JSONFileCache)42self.assertIsInstance(43mock_web_identity.cache,44assumerole.JSONFileCache,45)4647def test_assume_role_provider_registration(self):48event_handlers = HierarchicalEmitter()49assumerole.register_assume_role_provider(event_handlers)50session = mock.Mock(spec=Session)51event_handlers.emit('session-initialized', session=session)52# Just verifying that anything on the session was called ensures53# that our handler was called, as it's the only thing that should54# be registered.55session.get_component.assert_called_with('credential_provider')5657def test_no_registration_if_profile_does_not_exist(self):58session = mock.Mock(spec=Session)59session.get_component.side_effect = ProfileNotFound(60profile='unknown')6162assumerole.inject_assume_role_provider_cache(63session, event_name='building-command-table.foo')6465credential_provider = session.get_component.return_value66self.assertFalse(credential_provider.get_provider.called)676869