Пример #1
0
def test_get_assumed_profile_credentials_via_botocore_botocore_present(mocker):
    expected_credentials = {
        "AccessKeyId": ACCESS_KEY_ID_VAL,
        "SecretAccessKey": SECRET_ACCESS_KEY_VAL,
        "Token": SESSION_TOKEN_VAL,
    }

    boto_session_mock = MagicMock()
    boto_session_mock.set_config_variable.return_value = None

    ReadOnlyCredentials = namedtuple("ReadOnlyCredentials",
                                     ["access_key", "secret_key", "token"])
    frozen_credentials = ReadOnlyCredentials(ACCESS_KEY_ID_VAL,
                                             SECRET_ACCESS_KEY_VAL,
                                             SESSION_TOKEN_VAL)

    get_credential_session_mock = MagicMock()
    boto_session_mock.get_credentials.return_value = get_credential_session_mock
    get_credential_session_mock.get_frozen_credentials.return_value = frozen_credentials

    mocker.patch("botocore.session.get_session",
                 return_value=boto_session_mock)

    credentials = watchdog.botocore_credentials_helper("test_profile")
    assert credentials == expected_credentials

    boto_session_mock.set_config_variable.assert_called_once()
    boto_session_mock.get_credentials.assert_called_once()
    get_credential_session_mock.get_frozen_credentials.assert_called_once()
Пример #2
0
def test_get_assumed_profile_credentials_via_botocore_botocore_not_present(
        mocker):
    expected_credentials = {
        "AccessKeyId": None,
        "SecretAccessKey": None,
        "Token": None
    }
    mocker.patch.dict("sys.modules", {"botocore": None})

    credentials = watchdog.botocore_credentials_helper("test_profile")
    assert credentials == expected_credentials
Пример #3
0
def test_get_assumed_profile_credentials_via_botocore_botocore_present_profile_not_found(mocker):
    expected_credentials = {'AccessKeyId': None, 'SecretAccessKey': None, 'Token': None}

    boto_session_mock = MagicMock()
    boto_session_mock.set_config_variable.return_value = None

    boto_session_mock.get_credentials.side_effect = [ProfileNotFound(profile='test_profile')]

    mocker.patch('botocore.session.get_session', return_value=boto_session_mock)

    credentials = watchdog.botocore_credentials_helper('test_profile')

    assert credentials == expected_credentials

    boto_session_mock.set_config_variable.assert_called_once()
    boto_session_mock.get_credentials.assert_called_once()
Пример #4
0
def test_get_assumed_profile_credentials_via_botocore_botocore_present(mocker):
    expected_credentials = {'AccessKeyId': ACCESS_KEY_ID_VAL,
                            'SecretAccessKey': SECRET_ACCESS_KEY_VAL,
                            'Token': SESSION_TOKEN_VAL}

    boto_session_mock = MagicMock()
    boto_session_mock.set_config_variable.return_value = None

    ReadOnlyCredentials = namedtuple('ReadOnlyCredentials', ['access_key', 'secret_key', 'token'])
    frozen_credentials = ReadOnlyCredentials(ACCESS_KEY_ID_VAL, SECRET_ACCESS_KEY_VAL, SESSION_TOKEN_VAL)

    get_credential_session_mock = MagicMock()
    boto_session_mock.get_credentials.return_value = get_credential_session_mock
    get_credential_session_mock.get_frozen_credentials.return_value = frozen_credentials

    mocker.patch('botocore.session.get_session', return_value=boto_session_mock)

    credentials = watchdog.botocore_credentials_helper('test_profile')
    assert credentials == expected_credentials

    boto_session_mock.set_config_variable.assert_called_once()
    boto_session_mock.get_credentials.assert_called_once()
    get_credential_session_mock.get_frozen_credentials.assert_called_once()
Пример #5
0
def test_get_assumed_profile_credentials_via_botocore_botocore_present_profile_not_found(
    mocker, ):
    expected_credentials = {
        "AccessKeyId": None,
        "SecretAccessKey": None,
        "Token": None
    }

    boto_session_mock = MagicMock()
    boto_session_mock.set_config_variable.return_value = None

    boto_session_mock.get_credentials.side_effect = [
        ProfileNotFound(profile="test_profile")
    ]

    mocker.patch("botocore.session.get_session",
                 return_value=boto_session_mock)

    credentials = watchdog.botocore_credentials_helper("test_profile")

    assert credentials == expected_credentials

    boto_session_mock.set_config_variable.assert_called_once()
    boto_session_mock.get_credentials.assert_called_once()
Пример #6
0
def test_get_assumed_profile_credentials_via_botocore_botocore_not_present(mocker):
    expected_credentials = {'AccessKeyId': None, 'SecretAccessKey': None, 'Token': None}
    mocker.patch.dict('sys.modules', {'botocore': None})

    credentials = watchdog.botocore_credentials_helper('test_profile')
    assert credentials == expected_credentials