Skip to content

Commit

Permalink
removed repetitve code and added test matrix
Browse files Browse the repository at this point in the history
fixed typo

Update manualtoken_test.go

updated tests

Create manualtoken_test.go
  • Loading branch information
khareyash05 committed May 5, 2023
1 parent 401550a commit ff6fe8e
Showing 1 changed file with 95 additions and 0 deletions.
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() {
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)
}
}
}

0 comments on commit ff6fe8e

Please sign in to comment.