feat: lower Sense password minimum to six characters (#40)

This commit is contained in:
QiuSW
2026-08-13 08:51:35 +08:00
parent 8b3d814628
commit f841ef1913
3 changed files with 41 additions and 5 deletions
+2 -2
View File
@@ -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)
}
})
}
}