7
7
"testing"
8
8
"time"
9
9
10
- "github.com/golang-jwt/jwt/v4 "
10
+ "github.com/golang-jwt/jwt/v5 "
11
11
)
12
12
13
13
func TestValidClaims (t * testing.T ) {
@@ -21,69 +21,72 @@ func TestValidClaims(t *testing.T) {
21
21
IssuedAt : & iat ,
22
22
},
23
23
}
24
- if err := c .Valid (); err != nil {
24
+ v := jwt .NewValidator (
25
+ jwt .WithIssuedAt (),
26
+ )
27
+ if err := v .Validate (c ); err != nil {
25
28
t .Fatalf ("Failed to verify claims, wanted: %v got %v" , nil , err )
26
29
}
27
30
}
28
31
29
32
func TestInvalidClaims (t * testing.T ) {
30
- badClaims := []struct {
31
- c claims
32
- expectedError error
33
+ type fields struct {
34
+ leeway time.Duration
35
+ timeFunc func () time.Time
36
+ expectedAud string
37
+ expectAllAud []string
38
+ expectedIss string
39
+ expectedSub string
40
+ }
41
+ type args struct {
42
+ claims jwt.Claims
43
+ }
44
+ tests := []struct {
45
+ name string
46
+ fields fields
47
+ args args
48
+ wantErr error
33
49
}{
34
50
{
35
- claims {
36
- "" ,
37
- 1 ,
38
- "nonce" ,
39
- jwt.RegisteredClaims {
40
- IssuedAt : jwt .NewNumericDate (time .Now ().Add (time .Hour * - 1 )),
41
- },
42
- },
43
- errors .New ("token doesn't include the ProductCode" ),
51
+ name : "missing ProductCode" ,
52
+ fields : fields {},
53
+ args : args {jwt.RegisteredClaims {}},
54
+ wantErr : ErrMissingProductCode ,
44
55
},
45
56
{
46
- claims {
47
- "productCode" ,
48
- 1 ,
49
- "" ,
50
- jwt.RegisteredClaims {
51
- IssuedAt : jwt .NewNumericDate (time .Now ().Add (time .Hour * - 1 )),
52
- },
53
- },
54
- errors .New ("token doesn't include the Nonce" ),
57
+ name : "missing Nonce" ,
58
+ fields : fields {},
59
+ args : args {jwt.RegisteredClaims {}},
60
+ wantErr : ErrMissingNonce ,
55
61
},
56
62
{
57
- claims {
58
- "productCode" ,
59
- 0 ,
60
- "nonce" ,
61
- jwt.RegisteredClaims {
62
- IssuedAt : jwt .NewNumericDate (time .Now ().Add (time .Hour * - 1 )),
63
- },
64
- },
65
- errors .New ("token doesn't include the PublicKeyVersion" ),
63
+ name : "missing PublicKeyVersion" ,
64
+ fields : fields {},
65
+ args : args {jwt.RegisteredClaims {}},
66
+ wantErr : ErrMissingKeyVersion ,
66
67
},
67
68
{
68
- claims {
69
- "test" ,
70
- 1 ,
71
- "nonce" ,
72
- jwt.RegisteredClaims {
73
- IssuedAt : jwt .NewNumericDate (time .Now ().Add (time .Hour * + 2 )),
74
- },
75
- },
76
- errors .New ("token used before issued" ),
69
+ name : "iat is in the future" ,
70
+ fields : fields {},
71
+ args : args {jwt.RegisteredClaims {IssuedAt : jwt .NewNumericDate (time .Now ().Add (time .Hour * + 2 ))}},
72
+ wantErr : jwt .ErrTokenUsedBeforeIssued ,
77
73
},
78
74
}
79
75
80
- for _ , badC := range badClaims {
81
-
82
- err := badC .c .Valid ()
83
- if err == nil {
84
- t .Errorf ("Valid() returned no error when it should have returned error %q" , badC .expectedError )
85
- } else if err .Error () != badC .expectedError .Error () {
86
- t .Errorf ("Valid() returned error %q when it should have returned error %q" , err , badC .expectedError )
87
- }
76
+ for _ , tt := range tests {
77
+ t .Run (tt .name , func (t * testing.T ) {
78
+ v := jwt .NewValidator (
79
+ jwt .WithLeeway (tt .fields .leeway ),
80
+ jwt .WithTimeFunc (tt .fields .timeFunc ),
81
+ jwt .WithIssuedAt (),
82
+ jwt .WithAudience (tt .fields .expectedAud ),
83
+ jwt .WithAllAudiences (tt .fields .expectAllAud ... ),
84
+ jwt .WithIssuer (tt .fields .expectedIss ),
85
+ jwt .WithSubject (tt .fields .expectedSub ),
86
+ )
87
+ if err := v .Validate (tt .args .claims ); (err != nil ) && ! errors .Is (err , tt .wantErr ) {
88
+ t .Errorf ("validator.Validate() error = %v, wantErr = %v" , err , tt .wantErr )
89
+ }
90
+ })
88
91
}
89
92
}
0 commit comments