feat: lower Sense password minimum to six characters (#40)
This commit is contained in:
@@ -9,8 +9,8 @@ import (
|
||||
)
|
||||
|
||||
func ValidatePassword(username, password string) error {
|
||||
if len([]rune(password)) < 12 {
|
||||
return fmt.Errorf("密码至少需要 12 个字符")
|
||||
if len([]rune(password)) < 6 {
|
||||
return fmt.Errorf("密码至少需要 6 个字符")
|
||||
}
|
||||
var upper, lower, digit bool
|
||||
for _, value := range password {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package identity
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidatePasswordLengthAndComplexity(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
username string
|
||||
password string
|
||||
want string
|
||||
}{
|
||||
{name: "six characters", username: "operator", password: "Abc123"},
|
||||
{name: "five characters", username: "operator", password: "Ab123", want: "至少需要 6 个字符"},
|
||||
{name: "missing uppercase", username: "operator", password: "abc123", want: "大写字母、小写字母和数字"},
|
||||
{name: "missing lowercase", username: "operator", password: "ABC123", want: "大写字母、小写字母和数字"},
|
||||
{name: "missing digit", username: "operator", password: "Abcdef", want: "大写字母、小写字母和数字"},
|
||||
{name: "contains username", username: "admin", password: "MyAdmin1", want: "不能包含用户名"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
err := ValidatePassword(test.username, test.password)
|
||||
if test.want == "" {
|
||||
if err != nil {
|
||||
t.Fatalf("ValidatePassword() error = %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err == nil || !strings.Contains(err.Error(), test.want) {
|
||||
t.Fatalf("ValidatePassword() error = %v, want substring %q", err, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user