From 3c437e723801d4a7010391d5d4dfd9967fa800d8 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Mon, 25 Mar 2024 11:31:41 +0100 Subject: [PATCH] Add mobile-friendly password grouping feature This commit introduces a new mobile grouping feature in our password generator package. Furthermore, a new unit test was added to the 'random_test.go' file to validate this functionality. This change allows users to better handle passwords on mobile devices by altering their formation. --- random.go | 3 +++ random_test.go | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/random.go b/random.go index 1195b2e..312dfa5 100644 --- a/random.go +++ b/random.go @@ -354,6 +354,9 @@ func (g *Generator) generateRandom() (string, error) { ok = g.checkMinimumRequirements(password) } + if g.config.MobileGrouping { + return GroupCharsForMobile(password), nil + } return password, nil } diff --git a/random_test.go b/random_test.go index 2b088bf..2d4c8b0 100644 --- a/random_test.go +++ b/random_test.go @@ -453,6 +453,27 @@ func TestGenerateRandom(t *testing.T) { } } +func TestGenerateRandom_withGrouping(t *testing.T) { + config := NewConfig(WithAlgorithm(AlgoRandom), WithMinLength(1), + WithMaxLength(1), WithMobileGrouping()) + config.MinNumeric = 1 + generator := New(config) + pw, err := generator.generateRandom() + if err != nil { + t.Errorf("generateRandom() failed: %s", err) + } + if len(pw) > 1 { + t.Errorf("expected password with length 1 but got: %d", len(pw)) + } + n, err := strconv.Atoi(pw) + if err != nil { + t.Errorf("expected password to be a number but got an error: %s", err) + } + if n < 0 || n > 9 { + t.Errorf("expected password to be a number between 0 and 9, got: %d", n) + } +} + func TestGenerate(t *testing.T) { tests := []struct { name string