forked from golang-jwt/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
118 lines (116 loc) · 2.79 KB
/
errors_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
package jwt
import (
"fmt"
"testing"
)
func TestValidationErrorIncludes(t *testing.T) {
type checks struct {
params []uint32 // the params to pass to .IncludesAll and .IncludesAny
wantAll bool // the desired result of .IncludesAll
wantAny bool // the desired result of .IncludesAny
}
cases := []struct {
name string // the name of the test case
errors uint32 // the errors to put into the ValidationError
wantValid bool // true if the error should be .valid()
checks []checks // the checks to perform against the ValidationError
}{
{
name: "valid",
errors: 0,
wantValid: true,
checks: []checks{
{
params: []uint32{},
wantAll: true,
wantAny: false,
},
{
params: []uint32{ValidationErrorMalformed},
wantAll: false,
wantAny: false,
},
},
},
{
name: "one error",
errors: ValidationErrorExpired,
checks: []checks{
{
params: []uint32{},
wantAll: true,
wantAny: false,
},
{
params: []uint32{ValidationErrorExpired},
wantAll: true,
wantAny: true,
},
{
params: []uint32{ValidationErrorExpired, ValidationErrorAudience},
wantAll: false,
wantAny: true,
},
{
params: []uint32{ValidationErrorAudience},
wantAll: false,
wantAny: false,
},
},
},
{
name: "many errors",
errors: ValidationErrorAudience | ValidationErrorIssuer,
checks: []checks{
{
params: []uint32{},
wantAll: true,
wantAny: false,
},
{
params: []uint32{ValidationErrorAudience},
wantAll: true,
wantAny: true,
},
{
params: []uint32{ValidationErrorAudience, ValidationErrorId},
wantAll: false,
wantAny: true,
},
{
params: []uint32{ValidationErrorAudience, ValidationErrorIssuer},
wantAll: true,
wantAny: true,
},
{
params: []uint32{ValidationErrorAudience, ValidationErrorIssuer, ValidationErrorNotValidYet},
wantAll: false,
wantAny: true,
},
{
params: []uint32{ValidationErrorExpired, ValidationErrorSignatureInvalid},
wantAll: false,
wantAny: false,
},
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
ve := NewValidationError(tc.name, tc.errors)
if got := ve.valid(); got != tc.wantValid {
t.Errorf("ve.valid() = %v, want %v", got, tc.wantValid)
}
for _, ch := range tc.checks {
t.Run(fmt.Sprint(ch.params), func(t *testing.T) {
if got := ve.IncludesAll(ch.params...); got != ch.wantAll {
t.Errorf("ve.IncludesAll(%v) = %v; want %v", ch.params, got, ch.wantAll)
}
if got := ve.IncludesAny(ch.params...); got != ch.wantAny {
t.Errorf("ve.IncludesAny(%v) = %v; want %v", ch.params, got, ch.wantAny)
}
})
}
})
}
}