2018-01-30 02:24:42 +02:00
|
|
|
package defaults
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/volatiletech/authboss"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidate(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
validator := HTTPFormValidator{
|
2018-02-05 04:30:39 +02:00
|
|
|
Values: map[string]string{
|
|
|
|
"username": "john",
|
|
|
|
"email": "john@john.com",
|
|
|
|
},
|
|
|
|
Ruleset: []Rules{
|
|
|
|
Rules{
|
2018-01-30 02:24:42 +02:00
|
|
|
FieldName: "username",
|
2018-02-05 04:30:39 +02:00
|
|
|
MinLength: 5,
|
2018-01-30 02:24:42 +02:00
|
|
|
},
|
2018-02-05 04:30:39 +02:00
|
|
|
Rules{
|
2018-01-30 02:24:42 +02:00
|
|
|
FieldName: "missing_field",
|
2018-02-05 04:30:39 +02:00
|
|
|
Required: true,
|
2018-01-30 02:24:42 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-02-05 04:30:39 +02:00
|
|
|
errList := authboss.ErrorList(validator.Validate())
|
2018-01-30 02:24:42 +02:00
|
|
|
|
|
|
|
errs := errList.Map()
|
2018-02-05 04:30:39 +02:00
|
|
|
if errs["username"][0] != "Must be at least 5 characters" {
|
2018-01-30 02:24:42 +02:00
|
|
|
t.Error("Expected a different error for username:", errs["username"][0])
|
|
|
|
}
|
2018-02-05 04:30:39 +02:00
|
|
|
if errs["missing_field"][0] != "Cannot be blank" {
|
2018-01-30 02:24:42 +02:00
|
|
|
t.Error("Expected a different error for missing_field:", errs["missing_field"][0])
|
|
|
|
}
|
|
|
|
if _, ok := errs["email"]; ok {
|
|
|
|
t.Error("Expected no errors for email.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidate_Confirm(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
validator := HTTPFormValidator{
|
2018-02-05 04:30:39 +02:00
|
|
|
Values: map[string]string{
|
|
|
|
"username": "john",
|
|
|
|
"confirm_username": "johnny",
|
|
|
|
},
|
|
|
|
ConfirmFields: []string{"username", "confirm_username"},
|
2018-01-30 02:24:42 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 04:30:39 +02:00
|
|
|
mapped := authboss.ErrorList(validator.Validate()).Map()
|
|
|
|
if mapped["confirm_username"][0] != "Does not match username" {
|
|
|
|
t.Error("Expected a different error for confirmUsername:", mapped["confirmUsername"][0])
|
2018-01-30 02:24:42 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 04:30:39 +02:00
|
|
|
validator.Values = map[string]string{
|
|
|
|
"username": "john",
|
|
|
|
"confirm_username": "john",
|
|
|
|
}
|
|
|
|
errs := authboss.ErrorList(validator.Validate())
|
2018-01-30 02:24:42 +02:00
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Error("Expected no errors:", errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
validator = HTTPFormValidator{
|
|
|
|
ConfirmFields: []string{"username"},
|
|
|
|
}
|
|
|
|
|
2018-02-05 04:30:39 +02:00
|
|
|
paniced := false
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
paniced = true
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
errs = authboss.ErrorList(validator.Validate())
|
2018-01-30 02:24:42 +02:00
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Error("Expected no errors:", errs)
|
|
|
|
}
|
2018-02-05 04:30:39 +02:00
|
|
|
|
|
|
|
if !paniced {
|
|
|
|
t.Error("Want a panic due to bad confirm fields slice")
|
|
|
|
}
|
2018-01-30 02:24:42 +02:00
|
|
|
}
|