Skip to content

Commit

Permalink
Merge pull request #1040 from buhongw7583c/issue1037UnitTestPassword
Browse files Browse the repository at this point in the history
issue1037 add unit test for newPassword functions: check the password…
  • Loading branch information
buhongw7583c authored May 14, 2020
2 parents e8f97bc + 2c5ac1f commit b4f1f9a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions pkg/helpers/helpers_strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,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 unicode.IsLower(p):
testOutput1++
case unicode.IsUpper(p):
testOutput2++
case unicode.IsNumber(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)
}
}

0 comments on commit b4f1f9a

Please # to comment.