mirror of
https://github.com/volatiletech/authboss.git
synced 2024-11-30 09:06:45 +02:00
386133a84b
In order to support multiple different types of requests, there needed to be an interface to be able to read values from a request, and subsequently validate them to return any errors. So we've adjusted the Validator interface to no longer validate a request but instead validate the object it lives on. And we've created a new BodyReader interface.
88 lines
1.8 KiB
Go
88 lines
1.8 KiB
Go
package defaults
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/volatiletech/authboss"
|
|
)
|
|
|
|
func TestValidate(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
validator := HTTPFormValidator{
|
|
Values: map[string]string{
|
|
"username": "john",
|
|
"email": "john@john.com",
|
|
},
|
|
Ruleset: []Rules{
|
|
Rules{
|
|
FieldName: "username",
|
|
MinLength: 5,
|
|
},
|
|
Rules{
|
|
FieldName: "missing_field",
|
|
Required: true,
|
|
},
|
|
},
|
|
}
|
|
|
|
errList := authboss.ErrorList(validator.Validate())
|
|
|
|
errs := errList.Map()
|
|
if errs["username"][0] != "Must be at least 5 characters" {
|
|
t.Error("Expected a different error for username:", errs["username"][0])
|
|
}
|
|
if errs["missing_field"][0] != "Cannot be blank" {
|
|
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{
|
|
Values: map[string]string{
|
|
"username": "john",
|
|
"confirm_username": "johnny",
|
|
},
|
|
ConfirmFields: []string{"username", "confirm_username"},
|
|
}
|
|
|
|
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])
|
|
}
|
|
|
|
validator.Values = map[string]string{
|
|
"username": "john",
|
|
"confirm_username": "john",
|
|
}
|
|
errs := authboss.ErrorList(validator.Validate())
|
|
if len(errs) != 0 {
|
|
t.Error("Expected no errors:", errs)
|
|
}
|
|
|
|
validator = HTTPFormValidator{
|
|
ConfirmFields: []string{"username"},
|
|
}
|
|
|
|
paniced := false
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
paniced = true
|
|
}
|
|
}()
|
|
|
|
errs = authboss.ErrorList(validator.Validate())
|
|
if len(errs) != 0 {
|
|
t.Error("Expected no errors:", errs)
|
|
}
|
|
|
|
if !paniced {
|
|
t.Error("Want a panic due to bad confirm fields slice")
|
|
}
|
|
}
|