forked from aarondl/authboss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules_test.go
More file actions
executable file
·161 lines (145 loc) · 3.12 KB
/
rules_test.go
File metadata and controls
executable file
·161 lines (145 loc) · 3.12 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package authboss
import (
"regexp"
"testing"
)
func TestRules_Errors(t *testing.T) {
t.Parallel()
tests := []struct {
Rules Rules
In string
Error string
}{
{
Rules{FieldName: "email", Required: true},
"",
"email: Cannot be blank",
},
{
Rules{FieldName: "email", Required: true},
" \t\t\n ",
"email: Cannot be blank",
},
{
Rules{FieldName: "email", MatchError: "Regexp must match!", MustMatch: regexp.MustCompile("abc")},
"hello",
"email: Regexp must match!",
},
{
Rules{FieldName: "email", MinLength: 5},
"hi",
"email: Must be at least 5 characters",
},
{
Rules{FieldName: "email", MaxLength: 3},
"hello",
"email: Must be at most 3 characters",
},
{
Rules{FieldName: "email", MinLength: 3, MaxLength: 5},
"hi",
"email: Must be between 3 and 5 characters",
},
{
Rules{FieldName: "email", MinLetters: 5},
"13345",
"email: Must contain at least 5 letters",
},
{
Rules{FieldName: "email", MinUpper: 5},
"hi",
"email: Must contain at least 5 uppercase letters",
},
{
Rules{FieldName: "email", MinLower: 5},
"hi",
"email: Must contain at least 5 lowercase letters",
},
{
Rules{FieldName: "email", MinSymbols: 5},
"hi",
"email: Must contain at least 5 symbols",
},
{
Rules{FieldName: "email", MinNumeric: 5},
"hi",
"email: Must contain at least 5 numbers",
},
{
Rules{FieldName: "email"},
"hi whitespace",
"email: No whitespace permitted",
},
}
for i, test := range tests {
i = i + 1
err := test.Rules.Errors(test.In)
if err == nil {
t.Errorf("(%d) Wanted: %q", i, test.Error)
continue
}
if e := err.Error(); e != test.Error {
t.Errorf("(%d) The error was wrong: %q", i, e)
}
}
}
func TestRules_Rules(t *testing.T) {
t.Parallel()
r := Rules{
FieldName: "email",
MatchError: "Must adhere to this regexp",
MustMatch: regexp.MustCompile(""),
MinLength: 1,
MaxLength: 2,
MinLetters: 3,
MinUpper: 4,
MinLower: 5,
MinNumeric: 6,
MinSymbols: 7,
AllowWhitespace: false,
}
rules := r.Rules()
mustFind := []string{
"Must adhere to this regexp",
"Must be between 1 and 2 characters",
"Must contain at least 3 letters",
"Must contain at least 4 uppercase letters",
"Must contain at least 5 lowercase letters",
"Must contain at least 6 numbers",
"Must contain at least 7 symbols",
}
for i, toFind := range mustFind {
if rules[i] != toFind {
t.Error("Expected:", toFind, "got:", rules[i])
}
}
}
func TestRules_IsValid(t *testing.T) {
t.Parallel()
r := Rules{FieldName: "email", Required: true}
if r.IsValid("") {
t.Error("It should not be valid.")
}
if !r.IsValid("joe@joe.com") {
t.Error("It should be valid.")
}
}
func TestTallyCharacters(t *testing.T) {
t.Parallel()
u, l, n, s, w := tallyCharacters("123abcDEF@#$%^* ")
if u != 3 {
t.Error("Number of upper:", u)
}
if l != 3 {
t.Error("Number of lower:", l)
}
if n != 3 {
t.Error("Number of numerics:", n)
}
if s != 6 {
t.Error("Number of symbols:", s)
}
if w != 3 {
t.Error("Number of whitespace:", w)
}
}