77 "testing"
88 "time"
99
10- "github.com/golang-jwt/jwt/v4 "
10+ "github.com/golang-jwt/jwt/v5 "
1111)
1212
1313func TestValidClaims (t * testing.T ) {
@@ -21,69 +21,72 @@ func TestValidClaims(t *testing.T) {
2121 IssuedAt : & iat ,
2222 },
2323 }
24- if err := c .Valid (); err != nil {
24+ v := jwt .NewValidator (
25+ jwt .WithIssuedAt (),
26+ )
27+ if err := v .Validate (c ); err != nil {
2528 t .Fatalf ("Failed to verify claims, wanted: %v got %v" , nil , err )
2629 }
2730}
2831
2932func 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
3349 }{
3450 {
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 ,
4455 },
4556 {
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 ,
5561 },
5662 {
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 ,
6667 },
6768 {
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 ,
7773 },
7874 }
7975
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+ })
8891 }
8992}
0 commit comments