From 3dc6f9f1192e98cdcfefaf98ec14aa64e978aaa2 Mon Sep 17 00:00:00 2001 From: Harper Trow Date: Wed, 6 Dec 2023 11:39:03 -0500 Subject: [PATCH] fix: resolve a bug in `NewCredentials` Generator PR: https://github.com/openfga/sdk-generator/pull/237 --- api_open_fga_test.go | 38 ++++++++++++++++++++++++++++++++++++++ credentials/credentials.go | 6 +++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/api_open_fga_test.go b/api_open_fga_test.go index 37e8f9e..9ec946f 100644 --- a/api_open_fga_test.go +++ b/api_open_fga_test.go @@ -225,6 +225,44 @@ func TestOpenFgaApiConfiguration(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.fga.example", diff --git a/credentials/credentials.go b/credentials/credentials.go index 311ed33..8932329 100644 --- a/credentials/credentials.go +++ b/credentials/credentials.go @@ -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 {