diff --git a/pkg/config/profile.go b/pkg/config/profile.go index 923a5de0..62aa28c3 100644 --- a/pkg/config/profile.go +++ b/pkg/config/profile.go @@ -498,8 +498,9 @@ const ( ) // GetExperimentalFields returns a struct of the profile's experimental fields. These fields are only ever additive in functionality. +// If the API key is being overridden, via the --api-key flag or STRIPE_API_KEY env variable, this returns an empty struct. func (p *Profile) GetExperimentalFields() ExperimentalFields { - if err := viper.ReadInConfig(); err == nil { + if err := viper.ReadInConfig(); err == nil && os.Getenv("STRIPE_API_KEY") == "" && p.APIKey == "" { name := viper.GetString(p.GetConfigField(experimentalContextualName)) privKey := viper.GetString(p.GetConfigField(experimentalPrivateKey)) headers := viper.GetString(p.GetConfigField(experimentalStripeHeaders)) diff --git a/pkg/config/profile_test.go b/pkg/config/profile_test.go index da890f9b..46ecb5bf 100644 --- a/pkg/config/profile_test.go +++ b/pkg/config/profile_test.go @@ -202,6 +202,47 @@ func TestOldProfileDeleted(t *testing.T) { cleanUp(c.ProfilesFile) } +func TestExperimentalFieldsEmptyWhenAPIKeyIsOverridden(t *testing.T) { + profilesFile := filepath.Join(os.TempDir(), "stripe", "config.toml") + p := Profile{ + ProfileName: "tests", + DeviceName: "st-testing", + TestModeAPIKey: "sk_test_123", + DisplayName: "test-account-display-name", + } + c := &Config{ + Color: "auto", + LogLevel: "info", + Profile: p, + ProfilesFile: profilesFile, + } + c.InitConfig() + + v := viper.New() + + v.SetConfigFile(profilesFile) + err := p.writeProfile(v) + require.NoError(t, err) + + require.FileExists(t, c.ProfilesFile) + + require.NoError(t, err) + + p.WriteConfigField("experimental.stripe_headers", "test-headers") + p.WriteConfigField("experimental.contextual_name", "test-name") + p.WriteConfigField("experimental.private_key", "test-key") + + os.Setenv("STRIPE_API_KEY", "from-env") + defer os.Unsetenv("STRIPE_API_KEY") + + experimentalFields := p.GetExperimentalFields() + require.Equal(t, "", experimentalFields.ContextualName) + require.Equal(t, "", experimentalFields.StripeHeaders) + require.Equal(t, "", experimentalFields.PrivateKey) + + cleanUp(c.ProfilesFile) +} + func helperLoadBytes(t *testing.T, name string) []byte { bytes, err := os.ReadFile(name) if err != nil {