-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Anuj Chaudhari <[email protected]>
- Loading branch information
Showing
2 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package command | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/otiai10/copy" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
|
||
"github.com/vmware-tanzu/tanzu-plugin-runtime/config" | ||
configtypes "github.com/vmware-tanzu/tanzu-plugin-runtime/config/types" | ||
|
||
"github.com/vmware-tanzu/tanzu-cli/pkg/auth/common" | ||
"github.com/vmware-tanzu/tanzu-cli/pkg/auth/uaa" | ||
) | ||
|
||
// MockTanzuLogin is a mock implementation of the TanzuLogin function | ||
type MockTanzuLogin struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockTanzuLogin) TanzuLogin(issuerURL string, opts ...common.LoginOption) (*common.Token, error) { | ||
args := m.Called(issuerURL, opts) | ||
return args.Get(0).(*common.Token), args.Error(1) | ||
} | ||
|
||
func TestCreateAPIToken(t *testing.T) { | ||
var configFile, configFileNG *os.File | ||
var err error | ||
|
||
setupEnv := func() { | ||
configFile, err = os.CreateTemp("", "config") | ||
assert.NoError(t, err) | ||
|
||
err = copy.Copy(filepath.Join("..", "fakes", "config", "tanzu_config.yaml"), configFile.Name()) | ||
assert.NoError(t, err) | ||
|
||
os.Setenv("TANZU_CONFIG", configFile.Name()) | ||
|
||
configFileNG, err = os.CreateTemp("", "config_ng") | ||
assert.NoError(t, err) | ||
|
||
os.Setenv("TANZU_CONFIG_NEXT_GEN", configFileNG.Name()) | ||
err = copy.Copy(filepath.Join("..", "fakes", "config", "tanzu_config_ng.yaml"), configFileNG.Name()) | ||
assert.NoError(t, err) | ||
} | ||
|
||
teardownEnv := func() { | ||
os.Unsetenv("TANZU_CONFIG") | ||
os.Unsetenv("TANZU_CONFIG_NEXT_GEN") | ||
os.RemoveAll(configFile.Name()) | ||
os.RemoveAll(configFileNG.Name()) | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
context *configtypes.Context | ||
tanzuLoginErr error | ||
wantErr bool | ||
errMsg string | ||
}{ | ||
{ | ||
name: "success", | ||
context: &configtypes.Context{ | ||
Name: "fakecontext", | ||
ContextType: configtypes.ContextTypeTanzu, | ||
GlobalOpts: &configtypes.GlobalServer{ | ||
Auth: configtypes.GlobalServerAuth{ | ||
Issuer: "https://example.com", | ||
}, | ||
}, | ||
AdditionalMetadata: map[string]interface{}{ | ||
config.TanzuIdpTypeKey: "uaa", | ||
}, | ||
}, | ||
tanzuLoginErr: nil, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "no active context", | ||
context: nil, | ||
tanzuLoginErr: nil, | ||
wantErr: true, | ||
errMsg: "No active context of type `tanzu`. Please login to Tanzu Platform first to generate the API token", | ||
}, | ||
{ | ||
name: "invalid active context", | ||
context: &configtypes.Context{ | ||
Name: "fakecontext", | ||
ContextType: configtypes.ContextTypeTanzu, | ||
AdditionalMetadata: map[string]interface{}{ | ||
config.TanzuIdpTypeKey: "uaa", | ||
}, | ||
}, | ||
tanzuLoginErr: nil, | ||
wantErr: true, | ||
errMsg: "Invalid active context of type `tanzu`. Please login to Tanzu Platform first to generate the API token", | ||
}, | ||
{ | ||
name: "invalid IDP type", | ||
context: &configtypes.Context{ | ||
Name: "fakecontext", | ||
ContextType: configtypes.ContextTypeTanzu, | ||
GlobalOpts: &configtypes.GlobalServer{ | ||
Auth: configtypes.GlobalServerAuth{ | ||
Issuer: "https://example.com", | ||
}, | ||
}, | ||
AdditionalMetadata: map[string]interface{}{ | ||
config.TanzuIdpTypeKey: "other", | ||
}, | ||
}, | ||
tanzuLoginErr: nil, | ||
wantErr: true, | ||
errMsg: "invalid IDP type for the active context. Only UAA IDP type is supported for generating API token", | ||
}, | ||
{ | ||
name: "TanzuLogin error", | ||
context: &configtypes.Context{ | ||
Name: "fakecontext", | ||
ContextType: configtypes.ContextTypeTanzu, | ||
GlobalOpts: &configtypes.GlobalServer{ | ||
Auth: configtypes.GlobalServerAuth{ | ||
Issuer: "https://example.com", | ||
}, | ||
}, | ||
AdditionalMetadata: map[string]interface{}{ | ||
config.TanzuIdpTypeKey: "uaa", | ||
}, | ||
}, | ||
tanzuLoginErr: errors.New("TanzuLogin error"), | ||
wantErr: true, | ||
errMsg: "unable to login, TanzuLogin error", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
setupEnv() | ||
defer teardownEnv() | ||
|
||
cmd := &cobra.Command{} | ||
buf := &bytes.Buffer{} | ||
cmd.SetOutput(buf) | ||
|
||
mockTanzuLogin := &MockTanzuLogin{} | ||
uaa.TanzuLogin = mockTanzuLogin.TanzuLogin | ||
if tt.context != nil && tt.context.GlobalOpts != nil && tt.context.AdditionalMetadata[config.TanzuIdpTypeKey] == "uaa" { | ||
mockTanzuLogin.On("TanzuLogin", tt.context.GlobalOpts.Auth.Issuer, mock.Anything). | ||
Return(&common.Token{RefreshToken: "refresh-token", ExpiresIn: 3600}, tt.tanzuLoginErr) | ||
} | ||
|
||
if tt.context != nil { | ||
err = config.SetContext(tt.context, true) | ||
assert.NoError(t, err) | ||
} | ||
|
||
err := createAPIToken(cmd, nil) | ||
|
||
// TODO: Compare the output string as well for this unit tests | ||
|
||
if tt.wantErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
|
||
mockTanzuLogin.AssertExpectations(t) | ||
}) | ||
} | ||
} |