-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added test to token validation * corrected logic for token validation times
- Loading branch information
Showing
2 changed files
with
70 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package auth | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
) | ||
|
||
func Test_IsTokenValid(t *testing.T) { | ||
mySigningKey := []byte("test_key") | ||
ts := time.Now() | ||
tests := []struct { | ||
expiresAt, issuedAt time.Time | ||
name string | ||
expireDurationDiff time.Duration | ||
expected bool | ||
}{ | ||
{ | ||
name: "valid claims", | ||
expiresAt: ts.Add(time.Hour), | ||
expireDurationDiff: time.Minute * 5, | ||
issuedAt: ts.Add(-(time.Minute * 10)), | ||
expected: true, | ||
}, | ||
{ | ||
name: "issuedAt in future", | ||
expiresAt: ts.Add(time.Hour), | ||
expireDurationDiff: time.Minute * 5, | ||
issuedAt: ts.Add(time.Hour), | ||
expected: false, | ||
}, | ||
{ | ||
name: "tokenexpiration inside diff window", | ||
expiresAt: ts.Add(time.Minute * 4), | ||
expireDurationDiff: time.Minute * 5, | ||
issuedAt: ts, | ||
expected: false, | ||
}, | ||
{ | ||
name: "token expired", | ||
expiresAt: ts.Add(-time.Minute), | ||
expireDurationDiff: 0, | ||
issuedAt: ts.Add(-(time.Minute * 10)), | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
claims := &jwt.RegisteredClaims{ | ||
ExpiresAt: &jwt.NumericDate{Time: test.expiresAt}, | ||
IssuedAt: &jwt.NumericDate{Time: test.issuedAt}, | ||
Issuer: "test", | ||
} | ||
|
||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
ss, err := token.SignedString(mySigningKey) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, test.expected, IsTokenValid(ss, test.expireDurationDiff)) | ||
}) | ||
} | ||
} |