Skip to content

Commit

Permalink
fix: resolve a bug in NewCredentials
Browse files Browse the repository at this point in the history
  • Loading branch information
harper authored and rhamzeh committed Dec 6, 2023
1 parent 3c51f82 commit 3dc6f9f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
38 changes: 38 additions & 0 deletions api_open_fga_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions credentials/credentials.go
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 3dc6f9f

Please sign in to comment.