Ejemplo n.º 1
0
def test_get_secret_and_memoization(
        get_client,
        secret_key_env,
        secret_str,
        ):
    with mock.patch.dict(os.environ, {}):
        secret_key_memo = f'{secret_key_env}_MEMOIZED'
        secret_str_memo = f'{secret_str}_MEMOIZED'

        assert secret_key_memo not in os.environ.keys()

        client = mock.Mock()
        client.get_secret_value = mock.Mock(
            return_value={'SecretString': secret_str_memo}
        )
        get_client.return_value = client

        # Confirm that it's not memoizing when not requested
        secret = get_secret(secret_key_memo, client=client)
        assert secret == secret_str_memo
        assert secret_key_memo not in os.environ.keys()

        # Verify whether it's memoizing when requested to do so
        secret = get_secret(secret_key_memo, client=client, memoize=True)
        assert secret == secret_str_memo
        assert secret_key_memo in os.environ.keys()
        assert os.environ[secret_key_memo] == secret_str_memo

        new_client = mock.Mock()
        new_client.get_string_value = mock.Mock(
            return_value={'SecretString': secret_str_memo},
        )
        get_client.return_value = new_client

        # Confirm that it retrieves from env vars after memoization
        secret = get_secret(secret_key_memo, client=new_client, memoize=True)
        new_client.assert_not_called()
        assert secret == secret_str_memo
Ejemplo n.º 2
0
def test_get_secret_client_mock(
        get_client,
        mock_secret_str_response,
        secret_str,
        secret_key,
        ):
    client = mock.Mock()
    client.get_secret_value = mock.Mock(return_value=mock_secret_str_response)
    get_client.return_value = client

    secret = get_secret(secret_key, client=client)

    get_client.assert_called_with(client)

    client.get_secret_value.assert_called_with(SecretId=secret_key)
    assert secret == secret_str
Ejemplo n.º 3
0
def test_get_secret_load_json_client_mock(
        get_client,
        mock_secret_str_json_response,
        secret_str_json,
        secret_key_json,
        ):
    client = mock.Mock()
    client.get_secret_value = mock.Mock(
        return_value=mock_secret_str_json_response,
    )
    get_client.return_value = client

    secret = get_secret(secret_key_json, client=client)

    get_client.assert_called_with(client)

    client.get_secret_value.assert_called_with(SecretId=secret_key_json)
    assert secret == json.loads(secret_str_json)
Ejemplo n.º 4
0
def test_get_secret_from_env_var_load_json(
        mock_secret_str_json_response,
        secret_env_var_dict,
        secret_key_env_json,
        secret_str_json,
):
    with mock.patch.dict(os.environ, secret_env_var_dict):
        assert secret_key_env_json in os.environ.keys()
        assert os.environ[secret_key_env_json] == \
            secret_env_var_dict[secret_key_env_json]

        client = mock.Mock()
        client.get_secret_value = mock.Mock(
            return_value=mock_secret_str_json_response,
        )

        secret = get_secret(secret_key_env_json, client=client, memoize=True)

        client.get_secret_value.assert_not_called()

        assert secret == json.loads(secret_str_json)