From 02c799401babb3922b27c626aace70e19e1b0acd Mon Sep 17 00:00:00 2001 From: Rick Date: Wed, 21 Aug 2019 18:57:32 +0800 Subject: [PATCH] Add test cases for password util --- util/password.go | 5 +++++ util/password_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++ util/setup_test.go | 13 ++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 util/password_test.go create mode 100644 util/setup_test.go diff --git a/util/password.go b/util/password.go index 6d77316..069f910 100644 --- a/util/password.go +++ b/util/password.go @@ -5,7 +5,12 @@ import ( "time" ) +// GeneratePassword generates a password with the specific length func GeneratePassword(length int) string { + if length <= 0 { + return "" + } + rand.Seed(time.Now().UnixNano()) digits := "0123456789" specials := "~=+%^*/()[]{}/!@#$?|" diff --git a/util/password_test.go b/util/password_test.go new file mode 100644 index 0000000..47584f8 --- /dev/null +++ b/util/password_test.go @@ -0,0 +1,48 @@ +package util + +import ( + "github.com/golang/mock/gomock" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Password util test", func() { + var ( + ctrl *gomock.Controller + length int + ) + + BeforeEach(func() { + ctrl = gomock.NewController(GinkgoT()) + length = 3 + }) + + AfterEach(func() { + ctrl.Finish() + }) + + Context("basic test", func() { + It("password length", func() { + pass := GeneratePassword(length) + Expect(len(pass)).To(Equal(length)) + }) + + It("Different length", func() { + length = 6 + pass := GeneratePassword(length) + Expect(len(pass)).To(Equal(length)) + }) + + It("Negative length", func() { + length = -1 + pass := GeneratePassword(length) + Expect(len(pass)).To(Equal(0)) + }) + + It("Zero length", func() { + length = 0 + pass := GeneratePassword(length) + Expect(len(pass)).To(Equal(length)) + }) + }) +}) diff --git a/util/setup_test.go b/util/setup_test.go new file mode 100644 index 0000000..713023c --- /dev/null +++ b/util/setup_test.go @@ -0,0 +1,13 @@ +package util + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestUtils(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "jcli utils test") +} -- GitLab