[#7123] updated exp of test valid jwt tokens
This commit is contained in:
parent
41cc4fd36b
commit
172b1f96f7
|
|
@ -1,8 +1,6 @@
|
||||||
package security_test
|
package security_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -41,8 +39,8 @@ func TestParseUnverifiedJWT(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// properly formatted JWT with VALID claims (valid exp)
|
// properly formatted JWT with VALID claims (valid exp)
|
||||||
// {"name": "test", "exp": 2208985261}
|
// {"name": "test", "exp": 2524604461}
|
||||||
result4, err4 := security.ParseUnverifiedJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdCIsImV4cCI6MjIwODk4NTI2MX0._0KQu60hYNx5wkBIpEaoX35shXRicb0X_0VdWKWb-3k")
|
result4, err4 := security.ParseUnverifiedJWT("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoidGVzdCIsImV4cCI6MjUyNDYwNDQ2MX0.VIEO73GP5QRQOSfHgQhaqeuYqcx59vL3xlxmFP-fytQ")
|
||||||
if err4 != nil {
|
if err4 != nil {
|
||||||
t.Error("Expected nil, got", err4)
|
t.Error("Expected nil, got", err4)
|
||||||
}
|
}
|
||||||
|
|
@ -53,61 +51,54 @@ func TestParseUnverifiedJWT(t *testing.T) {
|
||||||
|
|
||||||
func TestParseJWT(t *testing.T) {
|
func TestParseJWT(t *testing.T) {
|
||||||
scenarios := []struct {
|
scenarios := []struct {
|
||||||
|
name string
|
||||||
token string
|
token string
|
||||||
secret string
|
secret string
|
||||||
expectError bool
|
expectError bool
|
||||||
expectClaims jwt.MapClaims
|
expectClaims jwt.MapClaims
|
||||||
}{
|
}{
|
||||||
// invalid formatted JWT
|
|
||||||
{
|
{
|
||||||
|
"invalid formatted JWT",
|
||||||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdCJ9",
|
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdCJ9",
|
||||||
"test",
|
"test",
|
||||||
true,
|
true,
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
// properly formatted JWT with INVALID claims and INVALID secret
|
|
||||||
// {"name": "test", "exp": 1516239022}
|
|
||||||
{
|
{
|
||||||
|
"properly formatted JWT with INVALID claims and INVALID secret",
|
||||||
|
// {"name": "test", "exp": 1516239022}
|
||||||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdCIsImV4cCI6MTUxNjIzOTAyMn0.xYHirwESfSEW3Cq2BL47CEASvD_p_ps3QCA54XtNktU",
|
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdCIsImV4cCI6MTUxNjIzOTAyMn0.xYHirwESfSEW3Cq2BL47CEASvD_p_ps3QCA54XtNktU",
|
||||||
"invalid",
|
"invalid",
|
||||||
true,
|
true,
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
// properly formatted JWT with INVALID claims and VALID secret
|
|
||||||
// {"name": "test", "exp": 1516239022}
|
|
||||||
{
|
{
|
||||||
|
"properly formatted JWT with INVALID claims and VALID secret",
|
||||||
|
// {"name": "test", "exp": 1516239022}
|
||||||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdCIsImV4cCI6MTUxNjIzOTAyMn0.xYHirwESfSEW3Cq2BL47CEASvD_p_ps3QCA54XtNktU",
|
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdCIsImV4cCI6MTUxNjIzOTAyMn0.xYHirwESfSEW3Cq2BL47CEASvD_p_ps3QCA54XtNktU",
|
||||||
"test",
|
"test",
|
||||||
true,
|
true,
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
// properly formatted JWT with VALID claims and INVALID secret
|
|
||||||
// {"name": "test", "exp": 1898636137}
|
|
||||||
{
|
{
|
||||||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdCIsImV4cCI6MTg5ODYzNjEzN30.gqRkHjpK5s1PxxBn9qPaWEWxTbpc1PPSD-an83TsXRY",
|
"properly formatted JWT with VALID claims and INVALID secret",
|
||||||
|
// {"name": "test", "exp": 2524604461}
|
||||||
|
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoidGVzdCIsImV4cCI6MjUyNDYwNDQ2MX0.VIEO73GP5QRQOSfHgQhaqeuYqcx59vL3xlxmFP-fytQ",
|
||||||
"invalid",
|
"invalid",
|
||||||
true,
|
true,
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
// properly formatted EXPIRED JWT with VALID secret
|
|
||||||
// {"name": "test", "exp": 1652097610}
|
|
||||||
{
|
{
|
||||||
"eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoidGVzdCIsImV4cCI6OTU3ODczMzc0fQ.0oUUKUnsQHs4nZO1pnxQHahKtcHspHu4_AplN2sGC4A",
|
"properly formatted JWT with VALID claims and VALID secret",
|
||||||
"test",
|
// {"name": "test", "exp": 2524604461}
|
||||||
true,
|
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoidGVzdCIsImV4cCI6MjUyNDYwNDQ2MX0.VIEO73GP5QRQOSfHgQhaqeuYqcx59vL3xlxmFP-fytQ",
|
||||||
nil,
|
|
||||||
},
|
|
||||||
// properly formatted JWT with VALID claims and VALID secret
|
|
||||||
// {"name": "test", "exp": 1898636137}
|
|
||||||
{
|
|
||||||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdCIsImV4cCI6MTg5ODYzNjEzN30.gqRkHjpK5s1PxxBn9qPaWEWxTbpc1PPSD-an83TsXRY",
|
|
||||||
"test",
|
"test",
|
||||||
false,
|
false,
|
||||||
jwt.MapClaims{"name": "test", "exp": 1898636137.0},
|
jwt.MapClaims{"name": "test", "exp": 2524604461.0},
|
||||||
},
|
},
|
||||||
// properly formatted JWT with VALID claims (without exp) and VALID secret
|
|
||||||
// {"name": "test"}
|
|
||||||
{
|
{
|
||||||
|
"properly formatted JWT with VALID claims (without exp) and VALID secret",
|
||||||
|
// {"name": "test"}
|
||||||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdCJ9.ml0QsTms3K9wMygTu41ZhKlTyjmW9zHQtoS8FUsCCjU",
|
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdCJ9.ml0QsTms3K9wMygTu41ZhKlTyjmW9zHQtoS8FUsCCjU",
|
||||||
"test",
|
"test",
|
||||||
false,
|
false,
|
||||||
|
|
@ -115,8 +106,8 @@ func TestParseJWT(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, s := range scenarios {
|
for _, s := range scenarios {
|
||||||
t.Run(fmt.Sprintf("%d_%s", i, s.token), func(t *testing.T) {
|
t.Run(s.name, func(t *testing.T) {
|
||||||
result, err := security.ParseJWT(s.token, s.secret)
|
result, err := security.ParseJWT(s.token, s.secret)
|
||||||
|
|
||||||
hasErr := err != nil
|
hasErr := err != nil
|
||||||
|
|
@ -144,34 +135,32 @@ func TestParseJWT(t *testing.T) {
|
||||||
|
|
||||||
func TestNewJWT(t *testing.T) {
|
func TestNewJWT(t *testing.T) {
|
||||||
scenarios := []struct {
|
scenarios := []struct {
|
||||||
|
name string
|
||||||
claims jwt.MapClaims
|
claims jwt.MapClaims
|
||||||
key string
|
key string
|
||||||
duration time.Duration
|
duration time.Duration
|
||||||
expectError bool
|
expectError bool
|
||||||
}{
|
}{
|
||||||
// empty, zero duration
|
{"empty, zero duration", jwt.MapClaims{}, "", 0, true},
|
||||||
{jwt.MapClaims{}, "", 0, true},
|
{"empty, 10 seconds duration", jwt.MapClaims{}, "", 10 * time.Second, false},
|
||||||
// empty, 10 seconds duration
|
{"non-empty, 10 seconds duration", jwt.MapClaims{"name": "test"}, "test", 10 * time.Second, false},
|
||||||
{jwt.MapClaims{}, "", 10 * time.Second, false},
|
|
||||||
// non-empty, 10 seconds duration
|
|
||||||
{jwt.MapClaims{"name": "test"}, "test", 10 * time.Second, false},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, scenario := range scenarios {
|
for _, s := range scenarios {
|
||||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
t.Run(s.name, func(t *testing.T) {
|
||||||
token, tokenErr := security.NewJWT(scenario.claims, scenario.key, scenario.duration)
|
token, tokenErr := security.NewJWT(s.claims, s.key, s.duration)
|
||||||
if tokenErr != nil {
|
if tokenErr != nil {
|
||||||
t.Fatalf("Expected NewJWT to succeed, got error %v", tokenErr)
|
t.Fatalf("Expected NewJWT to succeed, got error %v", tokenErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
claims, parseErr := security.ParseJWT(token, scenario.key)
|
claims, parseErr := security.ParseJWT(token, s.key)
|
||||||
|
|
||||||
hasParseErr := parseErr != nil
|
hasParseErr := parseErr != nil
|
||||||
if hasParseErr != scenario.expectError {
|
if hasParseErr != s.expectError {
|
||||||
t.Fatalf("Expected hasParseErr to be %v, got %v (%v)", scenario.expectError, hasParseErr, parseErr)
|
t.Fatalf("Expected hasParseErr to be %v, got %v (%v)", s.expectError, hasParseErr, parseErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if scenario.expectError {
|
if s.expectError {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -182,13 +171,13 @@ func TestNewJWT(t *testing.T) {
|
||||||
// clear exp claim to match with the scenario ones
|
// clear exp claim to match with the scenario ones
|
||||||
delete(claims, "exp")
|
delete(claims, "exp")
|
||||||
|
|
||||||
if len(claims) != len(scenario.claims) {
|
if len(claims) != len(s.claims) {
|
||||||
t.Fatalf("Expected %v claims, got %v", scenario.claims, claims)
|
t.Fatalf("Expected %v claims, got %v", s.claims, claims)
|
||||||
}
|
}
|
||||||
|
|
||||||
for j, k := range claims {
|
for k, v := range claims {
|
||||||
if claims[j] != scenario.claims[j] {
|
if v != s.claims[k] {
|
||||||
t.Fatalf("Expected %v for %q claim, got %v", claims[j], k, scenario.claims[j])
|
t.Fatalf("Expected %v for %q claim, got %v", s.claims[k], k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue