forked from volatiletech/authboss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation_test.go
87 lines (73 loc) · 1.85 KB
/
validation_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package defaults
import (
"testing"
"github.com/volatiletech/authboss"
)
func TestValidate(t *testing.T) {
t.Parallel()
validator := HTTPFormValidator{
Values: map[string]string{
"username": "john",
"email": "[email protected]",
},
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")
}
}