From d628def102b459e91d2c2ae6c625b62f0e6cf098 Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Wed, 24 Jan 2024 14:42:00 +0100 Subject: [PATCH] test: ensure random password generator adheres to default password policy --- go.mod | 1 + go.sum | 2 ++ internal/pkg/testingutils/rand.go | 13 +++++++++++++ test/c8y_test/user_test.go | 2 +- 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index a8352025..799b986d 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.18.0 + github.com/sethvargo/go-password v0.2.0 github.com/spf13/viper v1.18.2 github.com/tidwall/gjson v1.17.0 github.com/vbauerster/mpb/v6 v6.0.4 diff --git a/go.sum b/go.sum index 8d40c141..1fadcb29 100644 --- a/go.sum +++ b/go.sum @@ -77,6 +77,8 @@ github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6g github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4/go.mod h1:C1a7PQSMz9NShzorzCiG2fk9+xuCgLkPeCvMHYR2OWg= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= +github.com/sethvargo/go-password v0.2.0 h1:BTDl4CC/gjf/axHMaDQtw507ogrXLci6XRiLc7i/UHI= +github.com/sethvargo/go-password v0.2.0/go.mod h1:Ym4Mr9JXLBycr02MFuVQ/0JHidNetSgbzutTr3zsYXE= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= diff --git a/internal/pkg/testingutils/rand.go b/internal/pkg/testingutils/rand.go index 99def2b5..40ad28b7 100644 --- a/internal/pkg/testingutils/rand.go +++ b/internal/pkg/testingutils/rand.go @@ -3,6 +3,8 @@ package testingutils import ( "math/rand" "time" + + "github.com/sethvargo/go-password/password" ) const charset = "abcdefghijklmnopqrstuvwxyz" + @@ -22,3 +24,14 @@ func stringWithCharset(length int, charset string) string { func RandomString(length int) string { return stringWithCharset(length, charset) } + +// RandomPassword generate a random password that meets the default +// Cumulocity IoT password policy +func RandomPassword(length int) string { + value, err := password.Generate(length, 10, 10, false, false) + if err != nil { + // Panic as this should not happen + panic("could not generate password") + } + return value +} diff --git a/test/c8y_test/user_test.go b/test/c8y_test/user_test.go index d557e8bb..2c7701d3 100644 --- a/test/c8y_test/user_test.go +++ b/test/c8y_test/user_test.go @@ -53,7 +53,7 @@ func TestUserService_GetUserByUsername(t *testing.T) { func TestUserService_CRUD(t *testing.T) { client := createTestClient() name := "myciuser" + testingutils.RandomString(7) - password := testingutils.RandomString(32) + password := testingutils.RandomPassword(32) userInput := c8y.NewUser(name, name+"@no-reply.org", password) userInput. SetFirstName("User01").