Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit tests for manualtoken_test.go #268

Merged
merged 1 commit into from
May 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions pkg/token/manualtoken_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package token

import (
"errors"
"testing"

"github.com/Azure/go-autorest/autorest/adal"
)

func TestNewManualToken(t *testing.T) {
testCases := []struct {
name string
oAuthCfg adal.OAuthConfig
clientID string
resourceID string
tenantID string
token *adal.Token
wantErr error
}{
{
name: "Valid input",
oAuthCfg: adal.OAuthConfig{},
clientID: "clientID",
resourceID: "resourceID",
tenantID: "tenantID",
token: &adal.Token{},
wantErr: nil,
},
{
name: "Nil token",
oAuthCfg: adal.OAuthConfig{},
clientID: "clientID",
resourceID: "resourceID",
tenantID: "tenantID",
token: nil,
wantErr: errors.New("token cannot be nil"),
},
{
name: "Empty clientID",
oAuthCfg: adal.OAuthConfig{},
clientID: "",
resourceID: "resourceID",
tenantID: "tenantID",
token: &adal.Token{},
wantErr: errors.New("clientID cannot be empty"),
},
{
name: "Empty resourceID",
oAuthCfg: adal.OAuthConfig{},
clientID: "clientID",
resourceID: "",
tenantID: "tenantID",
token: &adal.Token{},
wantErr: errors.New("resourceID cannot be empty"),
},
{
name: "Empty tenantID",
oAuthCfg: adal.OAuthConfig{},
clientID: "clientID",
resourceID: "resourceID",
tenantID: "",
token: &adal.Token{},
wantErr: errors.New("tenantID cannot be empty"),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := newManualToken(tc.oAuthCfg, tc.clientID, tc.resourceID, tc.tenantID, tc.token)
if err != nil && err.Error() != tc.wantErr.Error() {
weinong marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("expected error %v, but got %v", tc.wantErr, err)
}
if err == nil && tc.wantErr != nil {
t.Errorf("expected err: %v, got nil", tc.wantErr)
}
})
}
}

func TestManualTokenToken(t *testing.T) {
oAuthConfig := adal.OAuthConfig{}
clientID := "test-client-id"
resourceID := "test-resource-id"
tenantID := "test-tenant-id"
token := &adal.Token{AccessToken: "test-access-token"}

provider, _ := newManualToken(oAuthConfig, clientID, resourceID, tenantID, token)

// Test successful token refresh
if _, err := provider.Token(); err == nil {
if err == nil {
t.Errorf("Expected no error, but got %v", err)
}
}
}