Skip to content
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
10 changes: 10 additions & 0 deletions cmd/config/add_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,13 @@ func TestConfigAddProfileCmd_InvalidSetActiveValue(t *testing.T) {
"--set-active=invalid-value")
testutils.CheckExpectedError(t, err, &expectedErrorPattern)
}

// Test config add profile command fails when using activeprofile as the profile name
func TestConfigSetCmd_InvalidAddActiveProfile(t *testing.T) {
expectedErrorPattern := `^failed to add profile: invalid profile name: '.*'. name cannot be the same as the active profile key$`
err := testutils_cobra.ExecutePingcli(t, "config", "add-profile",
"--name", "activeprofile",
"--description", "test description",
"--set-active=true")
testutils.CheckExpectedError(t, err, &expectedErrorPattern)
}
7 changes: 7 additions & 0 deletions cmd/feedback/feedback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package feedback_test
import (
"testing"

"github.com/pingidentity/pingcli/internal/configuration/options"
"github.com/pingidentity/pingcli/internal/testing/testutils"
"github.com/pingidentity/pingcli/internal/testing/testutils_cobra"
)
Expand Down Expand Up @@ -37,3 +38,9 @@ func TestFeedbackCmd_InvalidFlag(t *testing.T) {
err := testutils_cobra.ExecutePingcli(t, "feedback", "--invalid")
testutils.CheckExpectedError(t, err, &expectedErrorPattern)
}

// Test Feedback Command with valid profile
func TestFeedbackCmd_Profile(t *testing.T) {
err := testutils_cobra.ExecutePingcli(t, "feedback", "--"+options.RootProfileOption.CobraParamName, "default")
testutils.CheckExpectedError(t, err, nil)
}
4 changes: 4 additions & 0 deletions internal/commands/config/set_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func parseKeyValuePair(kvPair string) (string, string, error) {
return "", "", fmt.Errorf("invalid assignment format '%s'. Expect 'key=value' format", kvPair)
}

if strings.EqualFold(parsedInput[0], options.RootActiveProfileOption.KoanfKey) {
return "", "", fmt.Errorf("invalid assignment. Please use the 'pingcli config set active-profile <profile-name>' command to set the active profile")
}

return parsedInput[0], parsedInput[1], nil
}

Expand Down
15 changes: 15 additions & 0 deletions internal/commands/config/set_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ func Test_RunInternalConfigSet(t *testing.T) {
}
}

// Test RunInternalConfigSet function fails when active profile is set
func Test_RunInternalConfigSet_InvalidActiveProfileUse(t *testing.T) {
testutils_koanf.InitKoanfs(t)

var (
profileName = customtypes.String("default")
)

options.RootProfileOption.Flag.Changed = true
options.RootProfileOption.CobraParamValue = &profileName
expectedErrorPattern := `^failed to set configuration: invalid assignment. Please use the 'pingcli config set active-profile <profile-name>' command to set the active profile`
err := RunInternalConfigSet("activeProfile=myNewProfile")
testutils.CheckExpectedError(t, err, &expectedErrorPattern)
}

// Test RunInternalConfigSet function fails with invalid key
func Test_RunInternalConfigSet_InvalidKey(t *testing.T) {
testutils_koanf.InitKoanfs(t)
Expand Down
4 changes: 4 additions & 0 deletions internal/profiles/koanf.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ func (k KoanfConfig) ValidateProfileNameFormat(pName string) (err error) {
return fmt.Errorf("invalid profile name: '%s'. name must contain only alphanumeric characters, underscores, and dashes", pName)
}

if strings.EqualFold(pName, options.RootActiveProfileOption.KoanfKey) {
return fmt.Errorf("invalid profile name: '%s'. name cannot be the same as the active profile key", pName)
}

return nil
}

Expand Down
15 changes: 14 additions & 1 deletion internal/profiles/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,25 @@ func Validate() (err error) {
return err
}

// Make sure selected active profile is in the configuration file
profileName, err := GetOptionValue(options.RootProfileOption)
if err != nil {
return fmt.Errorf("failed to validate Ping CLI configuration: %w", err)
}

if profileName != "" {
// Make sure selected profile is in the configuration file
if !slices.Contains(profileNames, profileName) {
return fmt.Errorf("failed to validate Ping CLI configuration: '%s' profile not found in configuration "+
"file %s", profileName, GetKoanfConfig().GetKoanfConfigFile())
}
}

activeProfileName, err := GetOptionValue(options.RootActiveProfileOption)
if err != nil {
return fmt.Errorf("failed to validate Ping CLI configuration: %w", err)
}

// Make sure selected active profile is in the configuration file
if !slices.Contains(profileNames, activeProfileName) {
return fmt.Errorf("failed to validate Ping CLI configuration: active profile '%s' not found in configuration "+
"file %s", activeProfileName, GetKoanfConfig().GetKoanfConfigFile())
Expand Down
2 changes: 1 addition & 1 deletion internal/testing/testutils_koanf/koanf_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func configureMainKoanf(t *testing.T) {
mainKoanf.SetKoanfConfigFile(configFilePath)

if err := mainKoanf.KoanfInstance().Load(file.Provider(configFilePath), yaml.Parser()); err != nil {
t.Fatalf("Failed to load configurationhere from file '%s': %v", configFilePath, err)
t.Fatalf("Failed to load configuration from file '%s': %v", configFilePath, err)
}
}

Expand Down