-
Notifications
You must be signed in to change notification settings - Fork 2
/
validate_test.go
224 lines (201 loc) · 9.07 KB
/
validate_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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package raiden_test
import (
"encoding/json"
"testing"
"github.com/go-playground/validator/v10"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
"github.com/sev-2/raiden"
)
// Mock payload for testing
type TestPayload struct {
Name string `validate:"required"`
Email string `validate:"required,email"`
Age int `validate:"gte=0,lte=130"`
}
type TestAllPayload struct {
Name string `validate:"required"`
MinMax string `validate:"min=5,max=10"`
EqualText string `validate:"eq=custom"`
EqualIgnoreCase string `validate:"eq_ignore_case=custom"`
NotEqualText string `validate:"ne=custom"`
NotEqualIgnoreCase string `validate:"ne_ignore_case=custom"`
AlphaText string `validate:"alpha"`
AlphaNumericText string `validate:"alphanum"`
NumericOnly string `validate:"numeric"`
BooleanOnly string `validate:"boolean"`
AlphaUnicodeText string `validate:"alphaunicode"`
AlphaNumericUnicodeText string `validate:"alphanumunicode"`
AsciiText string `validate:"ascii"`
ContainsText string `validate:"contains=custom"`
ContainsAnyText string `validate:"containsany=custom"`
ContainsRuneText string `validate:"containsrune=custom"`
EndsNotWith string `validate:"endsnotwith=custom"`
EndsWith string `validate:"endswith=custom"`
ExcludeText string `validate:"excludes=custom"`
ExcludesAllText string `validate:"excludesall=custom"`
ExcludesRuneText string `validate:"excludesrune=custom"`
LowerCaseOnly string `validate:"lowercase"`
MultiByteText string `validate:"multibyte"`
FqdnOnly string `validate:"fqdn"`
HostnameOnly string `validate:"hostname"`
IPOnly string `validate:"ip"`
IPv4Only string `validate:"ipv4"`
IPv6Only string `validate:"ipv6"`
MACOnly string `validate:"mac"`
URIOnly string `validate:"uri"`
URLOnly string `validate:"url"`
SHA256Only string `validate:"sha256"`
DatetimeOnly string `validate:"datetime"`
}
// Custom validation function
func customValidation(fl validator.FieldLevel) bool {
return fl.Field().String() == "custom"
}
// Custom validation function name
const customValidationName = "custom_validation"
func TestValidate_Success(t *testing.T) {
payload := TestPayload{
Name: "John Doe",
Email: "[email protected]",
Age: 30,
}
err := raiden.Validate(payload)
assert.NoError(t, err)
}
func TestValidate_Failure(t *testing.T) {
payload := TestPayload{
Name: "",
Email: "invalid-email",
Age: 150,
}
err := raiden.Validate(payload)
assert.Error(t, err)
validationErr, ok := err.(*raiden.ErrorResponse)
assert.True(t, ok)
assert.Equal(t, fasthttp.StatusBadRequest, validationErr.StatusCode)
var details map[string][]string
err1 := json.Unmarshal([]byte(validationErr.Details.(string)), &details)
assert.NoError(t, err1)
assert.Contains(t, details["Name"], "name is required")
assert.Contains(t, details["Email"], "email should be a valid email address")
assert.Contains(t, details["Age"], "age should be less than or equal 130")
}
func TestValidate_SuccessAll(t *testing.T) {
payload := TestAllPayload{
Name: "John Doe",
MinMax: "123456",
EqualText: "custom",
EqualIgnoreCase: "Custom",
NotEqualText: "not-custom",
NotEqualIgnoreCase: "Not-Custom",
AlphaText: "abc",
AlphaNumericText: "abc123",
NumericOnly: "123",
BooleanOnly: "true",
AlphaUnicodeText: "abc",
AlphaNumericUnicodeText: "abc123",
AsciiText: "abc",
ContainsText: "custom",
ContainsAnyText: "custom",
ContainsRuneText: "custom",
EndsNotWith: "missing",
EndsWith: "custom",
ExcludeText: "missing",
ExcludesAllText: "new",
ExcludesRuneText: "missing",
LowerCaseOnly: "abc",
FqdnOnly: "example.com",
HostnameOnly: "example.com",
IPOnly: "192.0.0.1",
IPv4Only: "127.0.0.1",
IPv6Only: "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
MACOnly: "00:00:5e:00:53:01",
URIOnly: "http://example.com",
URLOnly: "http://example.com",
SHA256Only: "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b",
}
err := raiden.Validate(payload)
assert.NoError(t, err)
}
func TestValidate_FailureAll(t *testing.T) {
payload := TestAllPayload{
Name: "",
MinMax: "123",
EqualText: "not-custom",
EqualIgnoreCase: "Not-Custom",
NotEqualText: "custom",
NotEqualIgnoreCase: "Custom",
AlphaText: "abc123",
AlphaNumericText: "abc123!",
NumericOnly: "abc",
BooleanOnly: "yes",
AlphaUnicodeText: "abc123",
AlphaNumericUnicodeText: "abc123!",
AsciiText: "abc123",
ContainsText: "missing",
ContainsAnyText: "missing",
ContainsRuneText: "missing",
EndsNotWith: "custom",
EndsWith: "missing",
ExcludeText: "custom",
ExcludesAllText: "old",
ExcludesRuneText: "custom",
LowerCaseOnly: "ABC",
MultiByteText: "abc123",
FqdnOnly: "example",
HostnameOnly: "example",
IPOnly: "not-an-ip",
IPv4Only: "not-an-ipv4",
IPv6Only: "not-an-ipv6",
MACOnly: "not-a-mac",
URIOnly: "example.com",
URLOnly: "example.com",
SHA256Only: "not-a-sha256",
DatetimeOnly: "not-a-datetime",
}
err := raiden.Validate(payload)
assert.Error(t, err)
validationErr, ok := err.(*raiden.ErrorResponse)
assert.True(t, ok)
assert.Equal(t, fasthttp.StatusBadRequest, validationErr.StatusCode)
var details map[string][]string
err1 := json.Unmarshal([]byte(validationErr.Details.(string)), &details)
assert.NoError(t, err1)
assert.Contains(t, details["Name"], "name is required")
assert.Contains(t, details["MinMax"], "minmax should be at least 5")
assert.Contains(t, details["AlphaNumericText"], "alphanumerictext should contain only alphanumeric characters")
assert.Contains(t, details["NumericOnly"], "numericonly should be a numeric value")
assert.Contains(t, details["BooleanOnly"], "booleanonly should be a boolean value (true or false)")
assert.Contains(t, details["AlphaText"], "alphatext should contain only alphabetical characters")
assert.Contains(t, details["AlphaUnicodeText"], "alphaunicodetext should contain only alphabetical Unicode characters")
assert.Contains(t, details["ContainsRuneText"], "containsrunetext should contain the specified Unicode rune")
assert.Contains(t, details["ContainsText"], "containstext should contain the specified substring")
assert.Contains(t, details["EndsNotWith"], "endsnotwith should not end with the specified suffix")
assert.Contains(t, details["EndsWith"], "endswith should end with the specified suffix")
assert.Contains(t, details["EqualIgnoreCase"], "equalignorecase should be equal to the specified value (case-insensitive)")
assert.Contains(t, details["EqualText"], "equaltext should be equal to custom")
assert.Contains(t, details["ExcludeText"], "excludetext should not contain the specified substring")
assert.Contains(t, details["ExcludesAllText"], "excludesalltext should not contain any of the specified substrings")
assert.Contains(t, details["ExcludesRuneText"], "excludesrunetext should not contain the specified Unicode rune")
assert.Contains(t, details["LowerCaseOnly"], "lowercaseonly should be in lowercase")
assert.Contains(t, details["MultiByteText"], "multibytetext should contain only multi-byte characters")
assert.Contains(t, details["FqdnOnly"], "fqdnonly should be a Full Qualified Domain Name (FQDN)")
assert.Contains(t, details["IPOnly"], "iponly should be an Internet Protocol Address (IP)")
assert.Contains(t, details["IPv4Only"], "ipv4only should be an Internet Protocol Address (IPv4)")
assert.Contains(t, details["IPv6Only"], "ipv6only should be an Internet Protocol Address (IPv6)")
assert.Contains(t, details["MACOnly"], "maconly should be a Media Access Control Address (MAC)")
assert.Contains(t, details["URIOnly"], "urionly should be a URI String")
assert.Contains(t, details["URLOnly"], "urlonly should be a URL String")
assert.Contains(t, details["SHA256Only"], "sha256only should be a SHA256 hash")
assert.Contains(t, details["DatetimeOnly"], "datetimeonly should be a Datetime")
}
func TestValidate_CustomValidator(t *testing.T) {
payload := TestPayload{
Name: "custom",
Email: "[email protected]",
Age: 30,
}
err := raiden.Validate(payload, raiden.ValidatorFunc{Name: customValidationName, Validator: customValidation})
assert.NoError(t, err)
}