Add new configuration options and tests

This commit introduces new methods to enable HIBP checks, exclude specific characters, and set a fixed password length in the password generator configuration. It also includes tests to verify that these new options work correctly in the password configuration.
This commit is contained in:
Winni Neessen 2024-03-13 20:48:03 +01:00
parent d4d2067cbf
commit d6cfc79cf2
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
2 changed files with 60 additions and 0 deletions

View file

@ -88,6 +88,28 @@ func WithAlgorithm(algo Algorithm) Option {
} }
} }
// WithCheckHIBP enables the HIBP check for newly generated passwords
func WithCheckHIBP() Option {
return func(config *Config) {
config.CheckHIBP = true
}
}
// WithExcludeChars sets a list of characters to be excluded in the generated
// passwords
func WithExcludeChars(chars string) Option {
return func(config *Config) {
config.ExcludeChars = chars
}
}
// WithFixedLength sets a fixed password length
func WithFixedLength(length int64) Option {
return func(config *Config) {
config.FixedLength = length
}
}
// WithMinLength overrides the minimum password length // WithMinLength overrides the minimum password length
func WithMinLength(length int64) Option { func WithMinLength(length int64) Option {
return func(config *Config) { return func(config *Config) {

View file

@ -63,6 +63,44 @@ func TestWithAlgorithm(t *testing.T) {
} }
} }
func TestWithCheckHIBP(t *testing.T) {
c := NewConfig(WithCheckHIBP())
if c == nil {
t.Errorf("NewConfig(WithCheckHIBP()) failed, expected config pointer but got nil")
return
}
if c.CheckHIBP != true {
t.Errorf("NewConfig(WithCheckHIBP()) failed, expected min length: %t, got: %t",
true, c.CheckHIBP)
}
}
func TestWithExcludeChars(t *testing.T) {
e := "abcdefg"
c := NewConfig(WithExcludeChars(e))
if c == nil {
t.Errorf("NewConfig(WithExcludeChars()) failed, expected config pointer but got nil")
return
}
if c.ExcludeChars != e {
t.Errorf("NewConfig(WithExcludeChars()) failed, expected min length: %s, got: %s",
e, c.ExcludeChars)
}
}
func TestWithFixedLength(t *testing.T) {
var e int64 = 10
c := NewConfig(WithFixedLength(e))
if c == nil {
t.Errorf("NewConfig(WithFixedLength()) failed, expected config pointer but got nil")
return
}
if c.FixedLength != e {
t.Errorf("NewConfig(WithFixedLength()) failed, expected min length: %d, got: %d",
e, c.FixedLength)
}
}
func TestWithMaxLength(t *testing.T) { func TestWithMaxLength(t *testing.T) {
var e int64 = 123 var e int64 = 123
c := NewConfig(WithMaxLength(e)) c := NewConfig(WithMaxLength(e))