// Copyright 2022 Huawei Technologies Co.,Ltd.
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
//go:build !linux
// +build !linux

package provider

import (
	"github.com/stretchr/testify/assert"
	"os"
	"testing"
)

func TestNewCredentialProviderChain(t *testing.T) {
	chain := NewCredentialProviderChain([]ICredentialProvider{})
	assert.Equal(t, &CredentialProviderChain{providers: []ICredentialProvider{}}, chain)
}

func TestDefaultCredentialProviderChain(t *testing.T) {
	credentialType := "basic"
	chain := DefaultCredentialProviderChain(credentialType)
	expected := NewCredentialProviderChain([]ICredentialProvider{
		NewEnvCredentialProvider(credentialType),
		NewProfileCredentialProvider(credentialType),
		NewMetadataCredentialProvider(credentialType),
	})
	assert.Equal(t, expected, chain)
}

func TestCredentialProviderChain_GetCredentials(t *testing.T) {
	providers := []ICredentialProvider{
		NewEnvCredentialProvider(basicCredentialType),
		NewProfileCredentialProvider(basicCredentialType),
	}
	chain := NewCredentialProviderChain(providers)
	err := resetEnv()
	assert.NoError(t, err)
	err = os.Setenv("HUAWEICLOUD_SDK_AK", MockAk)
	assert.NoError(t, err)
	err = os.Setenv("HUAWEICLOUD_SDK_SK", MockSk)
	assert.NoError(t, err)
	credentials, err := chain.GetCredentials()
	assert.NoError(t, err)
	assert.NotNil(t, credentials)
	err = resetEnv()
	assert.NoError(t, err)
}
