-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PDI-1904: Add Test Cases For Internal Profiles Logic
- Loading branch information
1 parent
1a30bd3
commit c596507
Showing
7 changed files
with
705 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
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) | ||
|
||
err := profiles.SetConfigActiveProfile("production") | ||
if err != nil { | ||
t.Errorf("SetConfigActiveProfile returned error: %v", err) | ||
} | ||
|
||
profile := profiles.GetConfigActiveProfile() | ||
if profile != "production" { | ||
t.Errorf("GetConfigActiveProfile returned %s, expected 'test'", profile) | ||
} | ||
} | ||
|
||
// 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]) | ||
} | ||
} |
Oops, something went wrong.