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.
This commit is contained in:
Winni Neessen 2024-03-25 11:31:41 +01:00
parent 0c942a47c1
commit 3c437e7238
Signed by: wneessen
GPG key ID: 385AC9889632126E
2 changed files with 24 additions and 0 deletions

View file

@ -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
}

View file

@ -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