package main import ( "strings" "testing" ) func TestValidatePasswordOk(t *testing.T) { err := validatePassword("Testp4ssw0r_d") if err != nil { t.Error("Test password should pass validation") } } func TestValidatePasswordLength(t *testing.T) { err := validatePassword("test") if err == nil { t.Error("Password should fail validation") } if !strings.Contains(err.Error(), "least 10 characters") { t.Error("Error should tell that the password is too short") } if !strings.Contains(err.Error(), "was 4 characters") { t.Error("Error should contain the current password length") } } func TestValidatePasswordUpperAndLower(t *testing.T) { err := validatePassword("testtestfest") if err == nil { t.Error("All lowercase should fail uppercase validation") } if !strings.Contains(err.Error(), "uppercase") { t.Error("Error message should contain uppercase") } err = validatePassword("TESTTESTFEST") if err == nil { t.Error("All uppercase should fail lowercase validation") } if !strings.Contains(err.Error(), "lowercase") { t.Error("Error message should contain lowercase") } } func TestValidatePasswordSpecialAndNumbers(t *testing.T) { base_pass := "testTestFest" bad_passwords := []string{"", "2", "#3"} var err error for _, chr := range bad_passwords { err = validatePassword(base_pass + chr) if err == nil { t.Errorf("Password %s should fail 3 numbers and/or special", base_pass+chr) } if !strings.Contains(err.Error(), "special characters") { t.Errorf("Error message should contain special characters: %s", base_pass+chr) } if !strings.Contains(err.Error(), "numbers") { t.Errorf("Error message should contain 'numbers': %s", base_pass+chr) } } }