Skip to content

Commit

Permalink
Fixed a error return bug in credentials.NewCredentials in the Go SDK. (
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh authored Dec 6, 2023
2 parents e548df8 + 8f800b1 commit fa7d3b9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
40 changes: 39 additions & 1 deletion config/clients/go/template/api_test.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,44 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {
}
})

t.Run("NewCredentials should validate properly", func(t *testing.T){
// Passing valid credentials to NewCredentials should not error
creds, err := credentials.NewCredentials(credentials.Credentials{
Method: credentials.CredentialsMethodApiToken,
Config: &credentials.Config{
ApiToken: "some-token",
},
})

if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if creds == nil {
t.Fatalf("Expected creds to be non-nil")
}

if creds.Method != credentials.CredentialsMethodApiToken {
t.Fatalf("Expected method to be %v, got %v", credentials.CredentialsMethodApiToken, creds.Method)
}

if creds.Config.ApiToken != "some-token" {
t.Fatalf("Expected ApiToken to be %v, got %v", "some-token", creds.Config.ApiToken)
}

// Passing invalid credentials to NewCredentials should error
_, err = credentials.NewCredentials(credentials.Credentials{
Method: credentials.CredentialsMethodApiToken,
Config: &credentials.Config{
ClientCredentialsClientSecret: "some-secret",
},
})

if err == nil {
t.Fatalf("Expected validation error")
}
})

t.Run("should issue a network call to get the token at the first request if client id is provided", func(t *testing.T) {
configuration, err := NewConfiguration(Configuration{
ApiHost: "api.{{sampleApiDomain}}",
Expand Down Expand Up @@ -1289,4 +1327,4 @@ func Test{{appShortName}}Api(t *testing.T) {
t.Fatalf("Expected response code to be INTERNAL_ERROR but actual %s", internalError.ResponseCode())
}
})
}
}
6 changes: 3 additions & 3 deletions config/clients/go/template/credentials.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
const ApiTokenHeaderKey = "Authorization"
const ApiTokenHeaderValuePrefix = "Bearer"

// Avaialable credential methods
// Available credential methods
type CredentialsMethod string

const (
Expand Down Expand Up @@ -60,10 +60,10 @@ func NewCredentials(config Credentials) (*Credentials, error) {
err := creds.ValidateCredentialsConfig()

if err != nil {
return creds, nil
return nil, err
}

return nil, err
return creds, nil
}

func (c *Credentials) ValidateCredentialsConfig() error {
Expand Down

0 comments on commit fa7d3b9

Please sign in to comment.