From 090fbe243740b1e27255469f35488801fe9c054f Mon Sep 17 00:00:00 2001 From: Tomer Elmalem Date: Sat, 23 Jul 2022 16:27:31 -0700 Subject: [PATCH 1/5] Return a more detailed error for api keys if debug level is set --- pkg/config/profile.go | 10 +++++++++- pkg/config/profile_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/pkg/config/profile.go b/pkg/config/profile.go index 71b4786e..8b86375e 100644 --- a/pkg/config/profile.go +++ b/pkg/config/profile.go @@ -7,6 +7,7 @@ import ( "strings" "time" + log "github.com/sirupsen/logrus" "github.com/spf13/viper" "github.com/stripe/stripe-cli/pkg/validators" @@ -135,7 +136,7 @@ func (p *Profile) GetAPIKey(livemode bool) (string, error) { p.RegisterAlias(TestModeAPIKeyName, "api_key") } - if err := viper.ReadInConfig(); err == nil { + if err = viper.ReadInConfig(); err == nil { key = viper.GetString(p.GetConfigField(TestModeAPIKeyName)) } } else { @@ -153,6 +154,13 @@ func (p *Profile) GetAPIKey(livemode bool) (string, error) { return key, nil } + // If the log level is debug, return the raw error message. otherwise + // return a generic configuration message + level := log.GetLevel() + if level == log.DebugLevel { + return "", err + } + return "", validators.ErrAPIKeyNotConfigured } diff --git a/pkg/config/profile_test.go b/pkg/config/profile_test.go index be40bf9b..61505fa8 100644 --- a/pkg/config/profile_test.go +++ b/pkg/config/profile_test.go @@ -7,7 +7,9 @@ import ( "path/filepath" "testing" + "github.com/sirupsen/logrus" "github.com/spf13/viper" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -101,6 +103,34 @@ test_mode_key_expires_at = '` + expiresAt + `' cleanUp(c.ProfilesFile) } +func TestAPIKeyLogLevel(t *testing.T) { + // Set the level to debug + logrus.SetLevel(logrus.DebugLevel) + + c := &Config{ + Color: "auto", + LogLevel: "debug", + Profile: Profile{ + ProfileName: "tests", + TestModeAPIKey: "asdas", + }, + ProfilesFile: "", + } + + // For debug mode, the error should complain about a config file missing + // since we did not init the config + key, err := c.Profile.GetAPIKey(false) + assert.EqualError(t, err, `Config File "config" Not Found in "[]"`) + assert.Equal(t, "", key) + + // In info mode, it should give a cleaner error about the key not being + // configured + logrus.SetLevel(logrus.InfoLevel) + key, err = c.Profile.GetAPIKey(false) + assert.EqualError(t, err, "you have not configured API keys yet") + assert.Equal(t, "", key) +} + func helperLoadBytes(t *testing.T, name string) []byte { bytes, err := ioutil.ReadFile(name) if err != nil { From 883c2d11a62bf6e76fac5781f5607cec619ca99e Mon Sep 17 00:00:00 2001 From: Tomer Elmalem Date: Sun, 24 Jul 2022 15:43:59 -0700 Subject: [PATCH 2/5] Fix expected error assertion --- pkg/config/profile_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/profile_test.go b/pkg/config/profile_test.go index 61505fa8..afd18f5c 100644 --- a/pkg/config/profile_test.go +++ b/pkg/config/profile_test.go @@ -120,7 +120,7 @@ func TestAPIKeyLogLevel(t *testing.T) { // For debug mode, the error should complain about a config file missing // since we did not init the config key, err := c.Profile.GetAPIKey(false) - assert.EqualError(t, err, `Config File "config" Not Found in "[]"`) + assert.ErrorContains(t, err, `stripe/config.toml: no such file or directory`) assert.Equal(t, "", key) // In info mode, it should give a cleaner error about the key not being From dc3552f2e1875dc62895e1edca6251e7926d0a75 Mon Sep 17 00:00:00 2001 From: Tomer Elmalem Date: Sun, 24 Jul 2022 15:56:32 -0700 Subject: [PATCH 3/5] Remove stripe folder, path is different in windows --- pkg/config/profile_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/profile_test.go b/pkg/config/profile_test.go index afd18f5c..3ac19452 100644 --- a/pkg/config/profile_test.go +++ b/pkg/config/profile_test.go @@ -120,7 +120,7 @@ func TestAPIKeyLogLevel(t *testing.T) { // For debug mode, the error should complain about a config file missing // since we did not init the config key, err := c.Profile.GetAPIKey(false) - assert.ErrorContains(t, err, `stripe/config.toml: no such file or directory`) + assert.ErrorContains(t, err, `config.toml: no such file or directory`) assert.Equal(t, "", key) // In info mode, it should give a cleaner error about the key not being From 6a70d577b8b117f7bc4d6fbc18ed81cd32ae2494 Mon Sep 17 00:00:00 2001 From: Tomer Elmalem Date: Mon, 25 Jul 2022 08:40:16 -0700 Subject: [PATCH 4/5] Check for windows specifically --- pkg/config/profile_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/config/profile_test.go b/pkg/config/profile_test.go index 3ac19452..0af94fc2 100644 --- a/pkg/config/profile_test.go +++ b/pkg/config/profile_test.go @@ -120,7 +120,15 @@ func TestAPIKeyLogLevel(t *testing.T) { // For debug mode, the error should complain about a config file missing // since we did not init the config key, err := c.Profile.GetAPIKey(false) - assert.ErrorContains(t, err, `config.toml: no such file or directory`) + + // Little weird but windows returns a different error message than others + os := runtime.GOOS + switch os { + case "windows": + assert.ErrorContains(t, err, `config.toml: The system cannot find the file specified.`) + default: + assert.ErrorContains(t, err, `config.toml: no such file or directory`) + assert.Equal(t, "", key) // In info mode, it should give a cleaner error about the key not being From cf7bfe26d68df22374242417b62cc55b4a874d5c Mon Sep 17 00:00:00 2001 From: Tomer Elmalem Date: Mon, 25 Jul 2022 08:46:23 -0700 Subject: [PATCH 5/5] Fix the windows check (oops) --- pkg/config/profile_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/config/profile_test.go b/pkg/config/profile_test.go index 0af94fc2..e7eb5ad6 100644 --- a/pkg/config/profile_test.go +++ b/pkg/config/profile_test.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" "path/filepath" + "runtime" "testing" "github.com/sirupsen/logrus" @@ -122,12 +123,13 @@ func TestAPIKeyLogLevel(t *testing.T) { key, err := c.Profile.GetAPIKey(false) // Little weird but windows returns a different error message than others - os := runtime.GOOS - switch os { - case "windows": + os := runtime.GOOS + switch os { + case "windows": assert.ErrorContains(t, err, `config.toml: The system cannot find the file specified.`) default: assert.ErrorContains(t, err, `config.toml: no such file or directory`) + } assert.Equal(t, "", key)