From 7ad08623bd8f0829bd6ec0b8741e031a51696679 Mon Sep 17 00:00:00 2001 From: hobu <37413937+buhongw7583c@users.noreply.github.com> Date: Mon, 11 May 2020 10:58:06 +0800 Subject: [PATCH 1/3] issue1037 add unit test for newPassword functions: check the password length, validity and random. --- pkg/helpers/helpers_strings_test.go | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/pkg/helpers/helpers_strings_test.go b/pkg/helpers/helpers_strings_test.go index 6ac1dee4744..c7150c91c5c 100644 --- a/pkg/helpers/helpers_strings_test.go +++ b/pkg/helpers/helpers_strings_test.go @@ -4,6 +4,7 @@ package helpers import ( + "strings" "testing" "unicode" ) @@ -67,3 +68,60 @@ func TestDecodingFromBase64EncodedString(t *testing.T) { t.Errorf("Test output string '%s' is not as expected: '%s'.", testOutput2, expectedOutput2) } } + +func TestNewPasswordLengthCheck(t *testing.T) { + expectedOutput := passwordLength + testOutput := len(NewPassword()) + + if testOutput != expectedOutput { + t.Errorf("Test output password length %d is not as expected: %d. ", testOutput, expectedOutput) + } +} + +func TestNewPasswordGenerateRule(t *testing.T) { + // To verify the generated password must contain at least one caracter from + // upperAlpha, lowerAlpha and number + expectedOutput1 := 1 + expectedOutput2 := 1 + expectedOutput3 := 1 + + testOutput1 := 0 + testOutput2 := 0 + testOutput3 := 0 + testPassword := NewPassword() + + for _, p := range testPassword { + + switch { + case strings.Contains(lowerAlphaChars, string(p)): + testOutput1++ + case strings.Contains(upperAlphaChars, string(p)): + testOutput2++ + case strings.Contains(numberChars, string(p)): + testOutput3++ + + } + } + + if testOutput1 < expectedOutput1 { + t.Errorf("Test password '%s' doesnot contain any lower alphabet! ", testPassword) + } + + if testOutput2 < expectedOutput2 { + t.Errorf("Test password '%s' doesnot contain any upper alphabet! ", testPassword) + } + + if testOutput3 < expectedOutput3 { + t.Errorf("Test password '%s' doesnot contain any number! ", testPassword) + } + +} + +func TestNewPasswordCheckRandom(t *testing.T) { + testPassword1 := NewPassword() + testPassword2 := NewPassword() + + if testPassword1 == testPassword2 { + t.Errorf("The two random passwords '%s' and '%s' are the same!", testPassword1, testPassword2) + } +} From da4a022efce19334f1db86e2346bf6477335f60b Mon Sep 17 00:00:00 2001 From: hobu <37413937+buhongw7583c@users.noreply.github.com> Date: Thu, 14 May 2020 09:41:35 +0800 Subject: [PATCH 2/3] use unicode.Is** () function instead of strings.Contains(**, ) --- pkg/helpers/helpers_strings_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/helpers/helpers_strings_test.go b/pkg/helpers/helpers_strings_test.go index c7150c91c5c..0dbb7a179c1 100644 --- a/pkg/helpers/helpers_strings_test.go +++ b/pkg/helpers/helpers_strings_test.go @@ -4,7 +4,6 @@ package helpers import ( - "strings" "testing" "unicode" ) @@ -93,11 +92,11 @@ func TestNewPasswordGenerateRule(t *testing.T) { for _, p := range testPassword { switch { - case strings.Contains(lowerAlphaChars, string(p)): + case unicode.IsLower(p): testOutput1++ - case strings.Contains(upperAlphaChars, string(p)): + case unicode.IsUpper(p): testOutput2++ - case strings.Contains(numberChars, string(p)): + case unicode.IsNumber(p): testOutput3++ } From 2c5ac1ff2857bfffdba62c658d9ee4199f7140dc Mon Sep 17 00:00:00 2001 From: hobu <37413937+buhongw7583c@users.noreply.github.com> Date: Thu, 14 May 2020 09:53:14 +0800 Subject: [PATCH 3/3] add /pkg/helpers unittest to MakeFile --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b6de73fe74e..d2ce9f01509 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,9 @@ test-existing-controllers: generate fmt vet manifests TEST_RESOURCE_PREFIX=$(TEST_RESOURCE_PREFIX) TEST_USE_EXISTING_CLUSTER=true REQUEUE_AFTER=20 go test -tags "$(BUILD_TAGS)" -parallel 4 -v ./controllers/... -timeout 45m unit-tests: - go test ./pkg/resourcemanager/keyvaults/unittest/ + go test -v \ + ./pkg/resourcemanager/keyvaults/unittest/ \ + ./pkg/helpers/ # Run tests with existing cluster