Debian Codegraph @

validate_test.go

raw symbols SCIP
1 // Copyright 2014 Martini Authors
2 // Copyright 2014 The Macaron Authors
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License"): you may
5 // not use this file except in compliance with the License. You may obtain
6 // a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 // License for the specific language governing permissions and limitations
14 // under the License.
15
16 package binding
17
18 import (
19 "fmt"
20 "net/http"
21 "net/http/httptest"
22 "testing"
23
24 chi "github.com/go-chi/chi/v5"
25 "github.com/stretchr/testify/assert"
26 )
27
28 var (
29 emptyStr = ""
30 urlStr = "http://example.com/"
31 alphaDashStr = "aB-12"
32 idInt = 1
33 )
34
35 var validationTestCases = []validationTestCase{
36 {
37 description: "No errors",
38 data: BlogPost{
39 Id: 1,
40 Post: Post{
41 Title: "Behold The Title!",
42 Content: "And some content",
43 },
44 Author: Person{
45 Name: "Matt Holt",
46 },
47 },
48 expectedErrors: Errors{},
49 },
50 {
51 description: "ID required",
52 data: BlogPost{
53 Post: Post{
54 Title: "Behold The Title!",
55 Content: "And some content",
56 },
57 Author: Person{
58 Name: "Matt Holt",
59 },
60 },
61 expectedErrors: Errors{
62 Error{
63 FieldNames: []string{"id"},
64 Classification: ERR_REQUIRED,
65 Message: "Required",
66 },
67 },
68 },
69 {
70 description: "Embedded struct field required",
71 data: BlogPost{
72 Id: 1,
73 Post: Post{
74 Content: "Content given, but title is required",
75 },
76 Author: Person{
77 Name: "Matt Holt",
78 },
79 },
80 expectedErrors: Errors{
81 Error{
82 FieldNames: []string{"title"},
83 Classification: ERR_REQUIRED,
84 Message: "Required",
85 },
86 Error{
87 FieldNames: []string{"title"},
88 Classification: "LengthError",
89 Message: "Life is too short",
90 },
91 },
92 },
93 {
94 description: "Nested struct field required",
95 data: BlogPost{
96 Id: 1,
97 Post: Post{
98 Title: "Behold The Title!",
99 Content: "And some content",
100 },
101 },
102 expectedErrors: Errors{
103 Error{
104 FieldNames: []string{"name"},
105 Classification: ERR_REQUIRED,
106 Message: "Required",
107 },
108 },
109 },
110 {
111 description: "Required field missing in nested struct pointer",
112 data: BlogPost{
113 Id: 1,
114 Post: Post{
115 Title: "Behold The Title!",
116 Content: "And some content",
117 },
118 Author: Person{
119 Name: "Matt Holt",
120 },
121 Coauthor: &Person{},
122 },
123 expectedErrors: Errors{
124 Error{
125 FieldNames: []string{"name"},
126 Classification: ERR_REQUIRED,
127 Message: "Required",
128 },
129 },
130 },
131 {
132 description: "All required fields specified in nested struct pointer",
133 data: BlogPost{
134 Id: 1,
135 Post: Post{
136 Title: "Behold The Title!",
137 Content: "And some content",
138 },
139 Author: Person{
140 Name: "Matt Holt",
141 },
142 Coauthor: &Person{
143 Name: "Jeremy Saenz",
144 },
145 },
146 expectedErrors: Errors{},
147 },
148 {
149 description: "Custom validation should put an error",
150 data: BlogPost{
151 Id: 1,
152 Post: Post{
153 Title: "Too short",
154 Content: "And some content",
155 },
156 Author: Person{
157 Name: "Matt Holt",
158 },
159 },
160 expectedErrors: Errors{
161 Error{
162 FieldNames: []string{"title"},
163 Classification: "LengthError",
164 Message: "Life is too short",
165 },
166 },
167 },
168 {
169 description: "List Validation",
170 data: []BlogPost{
171 {
172 Id: 1,
173 Post: Post{
174 Title: "First Post",
175 Content: "And some content",
176 },
177 Author: Person{
178 Name: "Leeor Aharon",
179 },
180 },
181 {
182 Id: 2,
183 Post: Post{
184 Title: "Second Post",
185 Content: "And some content",
186 },
187 Author: Person{
188 Name: "Leeor Aharon",
189 },
190 },
191 },
192 expectedErrors: Errors{},
193 },
194 {
195 description: "List Validation w/ Errors",
196 data: []BlogPost{
197 {
198 Id: 1,
199 Post: Post{
200 Title: "First Post",
201 Content: "And some content",
202 },
203 Author: Person{
204 Name: "Leeor Aharon",
205 },
206 },
207 {
208 Id: 2,
209 Post: Post{
210 Title: "Too Short",
211 Content: "And some content",
212 },
213 Author: Person{
214 Name: "Leeor Aharon",
215 },
216 },
217 },
218 expectedErrors: Errors{
219 Error{
220 FieldNames: []string{"title"},
221 Classification: "LengthError",
222 Message: "Life is too short",
223 },
224 },
225 },
226 {
227 description: "List of invalid custom validations",
228 data: []SadForm{
229 {
230 AlphaDash: ",",
231 AlphaDashDot: ",",
232 Size: "123",
233 SizeSlice: []string{"1", "2", "3"},
234 MinSize: ",",
235 MinSizeSlice: []string{",", ","},
236 MaxSize: ",,",
237 MaxSizeSlice: []string{",", ","},
238 Range: 3,
239 Email: ",",
240 URL: ",",
241 URLEmpty: "",
242 InInvalid: "4",
243 NotIn: "1",
244 Include: "def",
245 Exclude: "abc",
246 },
247 },
248 expectedErrors: Errors{
249 Error{
250 FieldNames: []string{"AlphaDash"},
251 Classification: "AlphaDashError",
252 Message: "AlphaDash",
253 },
254 Error{
255 FieldNames: []string{"AlphaDashDot"},
256 Classification: "AlphaDashDot",
257 Message: "AlphaDashDot",
258 },
259 Error{
260 FieldNames: []string{"Size"},
261 Classification: "Size",
262 Message: "Size",
263 },
264 Error{
265 FieldNames: []string{"Size"},
266 Classification: "Size",
267 Message: "Size",
268 },
269 Error{
270 FieldNames: []string{"MinSize"},
271 Classification: "MinSize",
272 Message: "MinSize",
273 },
274 Error{
275 FieldNames: []string{"MinSize"},
276 Classification: "MinSize",
277 Message: "MinSize",
278 },
279 Error{
280 FieldNames: []string{"MaxSize"},
281 Classification: "MaxSize",
282 Message: "MaxSize",
283 },
284 Error{
285 FieldNames: []string{"MaxSize"},
286 Classification: "MaxSize",
287 Message: "MaxSize",
288 },
289 Error{
290 FieldNames: []string{"Range"},
291 Classification: "Range",
292 Message: "Range",
293 },
294 Error{
295 FieldNames: []string{"Email"},
296 Classification: "Email",
297 Message: "Email",
298 },
299 Error{
300 FieldNames: []string{"Url"},
301 Classification: "Url",
302 Message: "Url",
303 },
304 Error{
305 FieldNames: []string{"Default"},
306 Classification: "Default",
307 Message: "Default",
308 },
309 Error{
310 FieldNames: []string{"InInvalid"},
311 Classification: "In",
312 Message: "In",
313 },
314 Error{
315 FieldNames: []string{"NotIn"},
316 Classification: "NotIn",
317 Message: "NotIn",
318 },
319 Error{
320 FieldNames: []string{"Include"},
321 Classification: "Include",
322 Message: "Include",
323 },
324 Error{
325 FieldNames: []string{"Exclude"},
326 Classification: "Exclude",
327 Message: "Exclude",
328 },
329 },
330 },
331 {
332 description: "List of valid custom validations",
333 data: []SadForm{
334 {
335 AlphaDash: "123-456",
336 AlphaDashDot: "123.456",
337 Size: "1",
338 SizeSlice: []string{"1"},
339 MinSize: "12345",
340 MinSizeSlice: []string{"1", "2", "3", "4", "5"},
341 MaxSize: "1",
342 MaxSizeSlice: []string{"1"},
343 Range: 2,
344 In: "1",
345 InInvalid: "1",
346 Email: "[email protected]",
347 URL: "http://123.456",
348 Include: "abc",
349 },
350 },
351 },
352 {
353 description: "slice of structs Validation",
354 data: Group{
355 Name: "group1",
356 People: []Person{
357 {Name: "anthony"},
358 {Name: "awoods"},
359 },
360 },
361 expectedErrors: Errors{},
362 },
363 {
364 description: "slice of structs Validation failer",
365 data: Group{
366 Name: "group1",
367 People: []Person{
368 {Name: "anthony"},
369 {Name: ""},
370 },
371 },
372 expectedErrors: Errors{
373 Error{
374 FieldNames: []string{"name"},
375 Classification: ERR_REQUIRED,
376 Message: "Required",
377 },
378 },
379 },
380 {
381 description: "email fail",
382 data: struct {
383 EmailValid string `binding:"Email"`
384 EmailFail string `binding:"Email"`
385 EmailFail2 string `binding:"Email"`
386 EmailFail3 string `binding:"Email"`
387 }{
388 EmailValid: "[email protected]",
389 EmailFail: "test [email protected]",
390 EmailFail2: "[email protected] test",
391 EmailFail3: "test [email protected] test",
392 },
393 expectedErrors: Errors{
394 Error{
395 FieldNames: []string{"EmailFail"},
396 Classification: ERR_EMAIL,
397 Message: "Email",
398 },
399 Error{
400 FieldNames: []string{"EmailFail2"},
401 Classification: ERR_EMAIL,
402 Message: "Email",
403 },
404 Error{
405 FieldNames: []string{"EmailFail3"},
406 Classification: ERR_EMAIL,
407 Message: "Email",
408 },
409 },
410 },
411 {
412 description: "pointer form empty and nil",
413 data: PointerForm{},
414 expectedErrors: Errors{},
415 },
416 {
417 description: "pointer form empty",
418 data: PointerForm{
419 URL: "",
420 URLPointer: &emptyStr,
421 AlphaDash: "",
422 AlphaDashPointer: &emptyStr,
423 },
424 expectedErrors: Errors{},
425 },
426 {
427 description: "no errors with not required fields",
428 data: []*struct {
429 AlphaDash string `binding:"AlphaDash"`
430 AlphaDashDot string `binding:"AlphaDashDot"`
431 Size string `binding:"Size(1)"`
432 SizeSlice []string `binding:"Size(1)"`
433 MinSize string `binding:"MinSize(5)"`
434 MinSizeSlice []string `binding:"MinSize(5)"`
435 MaxSize string `binding:"MaxSize(1)"`
436 MaxSizeSlice []string `binding:"MaxSize(1)"`
437 Range int `binding:"Range(1,2)"`
438 Email string `binding:"Email"`
439 URL string `binding:"Url"`
440 In string `binding:"Default(0);In(1,2,3)"`
441 NotIn string `binding:"NotIn(1,2,3)"`
442 }{
443 {},
444 },
445 expectedErrors: Errors{},
446 },
447 {
448 description: "pointer form with valid data",
449 data: PointerForm{
450 URL: urlStr,
451 URLPointer: &urlStr,
452 AlphaDash: alphaDashStr,
453 AlphaDashPointer: &alphaDashStr,
454 },
455 expectedErrors: Errors{},
456 },
457 {
458 description: "pointer form with invalid data",
459 data: PointerForm{
460 URL: alphaDashStr,
461 URLPointer: &alphaDashStr,
462 AlphaDash: urlStr,
463 AlphaDashPointer: &urlStr,
464 },
465 expectedErrors: Errors{
466 Error{
467 FieldNames: []string{"Url", "UrlPointer"},
468 Classification: "Url",
469 Message: "Url",
470 },
471 Error{
472 FieldNames: []string{"UrlPointer"},
473 Classification: "Url",
474 Message: "Url",
475 },
476 Error{
477 FieldNames: []string{"AlphaDash"},
478 Classification: "AlphaDash",
479 Message: "AlphaDash",
480 },
481 Error{
482 FieldNames: []string{"AlphaDashPointer"},
483 Classification: "AlphaDash",
484 Message: "AlphaDash",
485 },
486 },
487 },
488 {
489 description: "errors with required fields",
490 data: []*struct {
491 AlphaDash string `binding:"Required;AlphaDash"`
492 AlphaDashDot string `binding:"Required;AlphaDashDot"`
493 Size string `binding:"Required;Size(1)"`
494 SizeSlice []string `binding:"Required;Size(1)"`
495 MinSize string `binding:"Required;MinSize(5)"`
496 MinSizeSlice []string `binding:"Required;MinSize(5)"`
497 MaxSize string `binding:"Required;MaxSize(1)"`
498 MaxSizeSlice []string `binding:"Required;MaxSize(1)"`
499 Range int `binding:"Required;Range(1,2)"`
500 Email string `binding:"Required;Email"`
501 URL string `binding:"Required;Url"`
502 In string `binding:"Required;Default(0);In(1,2,3)"`
503 NotIn string `binding:"Required;NotIn(1,2,3)"`
504 }{
505 {},
506 },
507 expectedErrors: Errors{
508 Error{
509 FieldNames: []string{"AlphaDash"},
510 Classification: "Required",
511 Message: "Required",
512 },
513 Error{
514 FieldNames: []string{"AlphaDashDot"},
515 Classification: "Required",
516 Message: "Required",
517 },
518 Error{
519 FieldNames: []string{"Size"},
520 Classification: "Required",
521 Message: "Required",
522 },
523 Error{
524 FieldNames: []string{"SizeSlice"},
525 Classification: "Required",
526 Message: "Required",
527 },
528 Error{
529 FieldNames: []string{"MinSize"},
530 Classification: "Required",
531 Message: "Required",
532 },
533 Error{
534 FieldNames: []string{"MinSizeSlice"},
535 Classification: "Required",
536 Message: "Required",
537 },
538 Error{
539 FieldNames: []string{"MaxSize"},
540 Classification: "Required",
541 Message: "Required",
542 },
543 Error{
544 FieldNames: []string{"MaxSizeSlice"},
545 Classification: "Required",
546 Message: "Required",
547 },
548 Error{
549 FieldNames: []string{"Range"},
550 Classification: "Required",
551 Message: "Required",
552 },
553 Error{
554 FieldNames: []string{"Email"},
555 Classification: "Required",
556 Message: "Required",
557 },
558 Error{
559 FieldNames: []string{"Url"},
560 Classification: "Required",
561 Message: "Required",
562 },
563 Error{
564 FieldNames: []string{"In"},
565 Classification: "Required",
566 Message: "Required",
567 },
568 Error{
569 FieldNames: []string{"NotIn"},
570 Classification: "Required",
571 Message: "Required",
572 },
573 },
574 },
575 }
576
577 func Test_Validation(t *testing.T) {
578 for _, testCase := range validationTestCases {
579 performValidationTest(t, testCase)
580 }
581 }
582
583 func performValidationTest(t *testing.T, testCase validationTestCase) {
584 httpRecorder := httptest.NewRecorder()
585 m := chi.NewRouter()
586
587 m.Post(testRoute, func(_ http.ResponseWriter, req *http.Request) {
588 actual := Validate(req, testCase.data)
589 assert.Equal(t, fmt.Sprintf("%+v", testCase.expectedErrors), fmt.Sprintf("%+v", actual), testCase.description)
590 })
591
592 req, err := http.NewRequest("POST", testRoute, nil)
593 if err != nil {
594 panic(err)
595 }
596
597 m.ServeHTTP(httpRecorder, req)
598
599 switch httpRecorder.Code {
600 case http.StatusNotFound:
601 panic("Routing is messed up in test fixture (got 404): check methods and paths")
602 case http.StatusInternalServerError:
603 panic("Something bad happened on '" + testCase.description + "'")
604 }
605 }
606
607 type (
608 validationTestCase struct {
609 description string
610 data any
611 expectedErrors Errors
612 }
613 )