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

PDI-1904: Add Test Cases For Internal Profiles Logic #125

Merged
merged 2 commits into from
Jul 25, 2024
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
47 changes: 47 additions & 0 deletions cmd/common/cobra_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package common_test

import (
"testing"

"github.com/pingidentity/pingctl/cmd/common"
"github.com/pingidentity/pingctl/internal/testing/testutils"
"github.com/spf13/cobra"
)

// Test ExactArgs returns no error when the number of arguments matches the expected number
func TestExactArgs_Matches(t *testing.T) {
posArgsFunc := common.ExactArgs(2)
err := posArgsFunc(nil, []string{"arg1", "arg2"})
testutils.CheckExpectedError(t, err, nil)
}

// Test ExactArgs returns an error when the number of arguments does not match the expected number
func TestExactArgs_DoesNotMatch(t *testing.T) {
expectedErrorPattern := `^failed to execute 'test': command accepts 2 arg\(s\), received 3$`
posArgsFunc := common.ExactArgs(2)
err := posArgsFunc(&cobra.Command{Use: "test"}, []string{"arg1", "arg2", "arg3"})
testutils.CheckExpectedError(t, err, &expectedErrorPattern)
}

// Test RangeArgs returns no error when the number of arguments is within the expected range
func TestRangeArgs_Matches(t *testing.T) {
posArgsFunc := common.RangeArgs(2, 4)
err := posArgsFunc(nil, []string{"arg1", "arg2", "arg3"})
testutils.CheckExpectedError(t, err, nil)
}

// Test RangeArgs returns an error when the number of arguments is below the expected range
func TestRangeArgs_BelowRange(t *testing.T) {
expectedErrorPattern := `^failed to execute 'test': command accepts 2 to 4 arg\(s\), received 1$`
posArgsFunc := common.RangeArgs(2, 4)
err := posArgsFunc(&cobra.Command{Use: "test"}, []string{"arg1"})
testutils.CheckExpectedError(t, err, &expectedErrorPattern)
}

// Test RangeArgs returns an error when the number of arguments is above the expected range
func TestRangeArgs_AboveRange(t *testing.T) {
expectedErrorPattern := `^failed to execute 'test': command accepts 2 to 4 arg\(s\), received 5$`
posArgsFunc := common.RangeArgs(2, 4)
err := posArgsFunc(&cobra.Command{Use: "test"}, []string{"arg1", "arg2", "arg3", "arg4", "arg5"})
testutils.CheckExpectedError(t, err, &expectedErrorPattern)
}
13 changes: 13 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,16 @@ func TestRootCmd_NoValueConfigFlag(t *testing.T) {
err := testutils_cobra.ExecutePingctl(t, "--config")
testutils.CheckExpectedError(t, err, &expectedErrorPattern)
}

// Test Root Command Executes when provided the --active-profile flag
func TestRootCmd_ActiveProfileFlag(t *testing.T) {
err := testutils_cobra.ExecutePingctl(t, "--active-profile", "default")
testutils.CheckExpectedError(t, err, nil)
}

// Test Root Command fails when provided no value for the --active-profile flag
func TestRootCmd_NoValueActiveProfileFlag(t *testing.T) {
expectedErrorPattern := `^flag needs an argument: --active-profile$`
err := testutils_cobra.ExecutePingctl(t, "--active-profile")
testutils.CheckExpectedError(t, err, &expectedErrorPattern)
}
244 changes: 244 additions & 0 deletions internal/profiles/main_viper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
package profiles_test

import (
"testing"

"github.com/pingidentity/pingctl/internal/profiles"
"github.com/pingidentity/pingctl/internal/testing/testutils_viper"
)

// Test GetMainViper function
func TestGetMainViper(t *testing.T) {
testutils_viper.InitVipers(t)

v := profiles.GetMainViper()
if v == nil {
t.Errorf("GetMainViper returned nil")
}
}

// Test GetConfigActiveProfile function
func TestGetConfigActiveProfile(t *testing.T) {
testutils_viper.InitVipers(t)

profile := profiles.GetConfigActiveProfile()
if profile == "" {
t.Errorf("GetConfigActiveProfile returned empty string")
}

if profile != "default" {
t.Errorf("GetConfigActiveProfile returned %s, expected 'default'", profile)
}
}

// Test SetConfigActiveProfile function
func TestSetConfigActiveProfile(t *testing.T) {
testutils_viper.InitVipers(t)

pName := "test"

err := profiles.SetConfigActiveProfile(pName)
if err != nil {
t.Errorf("SetConfigActiveProfile returned error: %v", err)
}

profile := profiles.GetConfigActiveProfile()
if profile != pName {
t.Errorf("GetConfigActiveProfile returned '%s', expected '%s'", profile, pName)
}
}

// Test ConfigProfileNames function
func TestConfigProfileNames(t *testing.T) {
testutils_viper.InitVipers(t)

profileKeys := profiles.ConfigProfileNames()
if len(profileKeys) == 0 {
t.Errorf("ConfigProfileNames returned empty slice")
}

if len(profileKeys) != 2 {
t.Errorf("ConfigProfileNames returned %d profiles, expected 2", len(profileKeys))
}

if profileKeys[0] != "default" {
t.Errorf("ConfigProfileNames returned %s, expected 'default'", profileKeys[0])
}

if profileKeys[1] != "production" {
t.Errorf("ConfigProfileNames returned %s, expected 'production'", profileKeys[1])
}
}

// Test ValidateNewProfileName function
func TestValidateNewProfileName(t *testing.T) {
testutils_viper.InitVipers(t)

err := profiles.ValidateNewProfileName("")
if err == nil {
t.Errorf("ValidateNewProfileName returned nil, expected error")
}

err = profiles.ValidateNewProfileName("default")
if err == nil {
t.Errorf("ValidateNewProfileName returned nil, expected error")
}

err = profiles.ValidateNewProfileName("production")
if err == nil {
t.Errorf("ValidateNewProfileName returned nil, expected error")
}

err = profiles.ValidateNewProfileName("test")
if err != nil {
t.Errorf("ValidateNewProfileName returned error: %v", err)
}

err = profiles.ValidateNewProfileName("invalid(*^&^%&%&^$)")
if err == nil {
t.Errorf("ValidateNewProfileName returned nil, expected error")
}
}

// Test ValidateExistingProfileName function
func TestValidateExistingProfileName(t *testing.T) {
testutils_viper.InitVipers(t)

err := profiles.ValidateExistingProfileName("")
if err == nil {
t.Errorf("ValidateExistingProfileName returned nil, expected error")
}

err = profiles.ValidateExistingProfileName("default")
if err != nil {
t.Errorf("ValidateExistingProfileName returned error: %v", err)
}

err = profiles.ValidateExistingProfileName("production")
if err != nil {
t.Errorf("ValidateExistingProfileName returned error: %v", err)
}

err = profiles.ValidateExistingProfileName("test")
if err == nil {
t.Errorf("ValidateExistingProfileName returned nil, expected error")
}

err = profiles.ValidateExistingProfileName("invalid(*^&^%&%&^$)")
if err == nil {
t.Errorf("ValidateExistingProfileName returned nil, expected error")
}
}

// Test ValidateProfileNameFormat function
func TestValidateProfileNameFormat(t *testing.T) {
err := profiles.ValidateProfileNameFormat("")
if err == nil {
t.Errorf("ValidateProfileNameFormat returned nil, expected error")
}

err = profiles.ValidateProfileNameFormat("default")
if err != nil {
t.Errorf("ValidateProfileNameFormat returned error: %v", err)
}

err = profiles.ValidateProfileNameFormat("production")
if err != nil {
t.Errorf("ValidateProfileNameFormat returned error: %v", err)
}

err = profiles.ValidateProfileNameFormat("test")
if err != nil {
t.Errorf("ValidateProfileNameFormat returned error: %v", err)
}

err = profiles.ValidateProfileNameFormat("invalid(*^&^%&%&^$)")
if err == nil {
t.Errorf("ValidateProfileNameFormat returned nil, expected error")
}
}

// Test DeleteConfigProfile function
func TestDeleteConfigProfile(t *testing.T) {
testutils_viper.InitVipers(t)

err := profiles.DeleteConfigProfile("")
if err == nil {
t.Errorf("DeleteConfigProfile returned nil, expected error")
}

err = profiles.DeleteConfigProfile("default")
if err == nil {
t.Errorf("DeleteConfigProfile returned nil, expected error")
}

err = profiles.DeleteConfigProfile("production")
if err != nil {
t.Errorf("DeleteConfigProfile returned error: %v", err)
}

err = profiles.DeleteConfigProfile("test")
if err == nil {
t.Errorf("DeleteConfigProfile returned nil, expected error")
}

err = profiles.DeleteConfigProfile("invalid(*^&^%&%&^$)")
if err == nil {
t.Errorf("DeleteConfigProfile returned nil, expected error")
}

profileKeys := profiles.ConfigProfileNames()
if len(profileKeys) != 1 {
t.Errorf("ConfigProfileNames returned %d profiles, expected 1", len(profileKeys))
}

if profileKeys[0] != "default" {
t.Errorf("ConfigProfileNames returned %s, expected 'default'", profileKeys[0])
}
}

// Test SaveProfileViperToFile function
func TestSaveProfileViperToFile(t *testing.T) {
testutils_viper.InitVipers(t)

// Create a new profile
err := profiles.CreateNewProfile("test", "test", true)
if err != nil {
t.Errorf("CreateNewProfile returned error: %v", err)
}

// Use the new profile
err = profiles.SetConfigActiveProfile("test")
if err != nil {
t.Errorf("SetConfigActiveProfile returned error: %v", err)
}

err = profiles.SetProfileViperWithProfile("test")
if err != nil {
t.Errorf("SetProfileViperWithProfile returned error: %v", err)
}

// Save the new profile to file
err = profiles.SaveProfileViperToFile()
if err != nil {
t.Errorf("SaveProfileViperToFile returned error: %v", err)
}

// Check if the new profile was saved to file
profileKeys := profiles.ConfigProfileNames()
if len(profileKeys) != 3 {
t.Errorf("ConfigProfileNames returned %d profiles, expected 3", len(profileKeys))
}

if profileKeys[0] != "default" {
t.Errorf("ConfigProfileNames returned %s, expected 'default'", profileKeys[0])
}

if profileKeys[1] != "production" {
t.Errorf("ConfigProfileNames returned %s, expected 'production'", profileKeys[1])
}

if profileKeys[2] != "test" {
t.Errorf("ConfigProfileNames returned %s, expected 'test'", profileKeys[2])
}
}
Loading