From aba1fea1805a26424e6baba1ed7d206d156343cd Mon Sep 17 00:00:00 2001 From: Harper Trow Date: Wed, 22 Nov 2023 20:43:50 -0500 Subject: [PATCH] Fixed a error return bug in credentials.NewCredentials in the Go SDK. --- config/clients/go/template/api_test.mustache | 40 ++++++++++++++++++- .../clients/go/template/credentials.mustache | 6 +-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/config/clients/go/template/api_test.mustache b/config/clients/go/template/api_test.mustache index 17aca0c4..7be57069 100644 --- a/config/clients/go/template/api_test.mustache +++ b/config/clients/go/template/api_test.mustache @@ -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}}", @@ -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()) } }) -} +} \ No newline at end of file diff --git a/config/clients/go/template/credentials.mustache b/config/clients/go/template/credentials.mustache index 9e1a18a4..03bfc01f 100644 --- a/config/clients/go/template/credentials.mustache +++ b/config/clients/go/template/credentials.mustache @@ -12,7 +12,7 @@ import ( const ApiTokenHeaderKey = "Authorization" const ApiTokenHeaderValuePrefix = "Bearer" -// Avaialable credential methods +// Available credential methods type CredentialsMethod string const ( @@ -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 {