Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve a bug in NewCredentials #60

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading