154 lines
3.9 KiB
Go
154 lines
3.9 KiB
Go
package test
|
|
|
|
import (
|
|
"RenewCMS/api/pkg/claims"
|
|
"math"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/go-chi/jwtauth/v5"
|
|
)
|
|
|
|
func requestWithJwtClaims(t *testing.T, token *jwtauth.JWTAuth, claimsToEncode map[string]any) *http.Request {
|
|
t.Helper()
|
|
_, tokenString, err := token.Encode(claimsToEncode)
|
|
if err != nil {
|
|
t.Fatalf("failed to encode jwt: %v", err)
|
|
}
|
|
decoded, err := token.Decode(tokenString)
|
|
if err != nil {
|
|
t.Fatalf("failed to decode jwt: %v", err)
|
|
}
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
return req.WithContext(jwtauth.NewContext(req.Context(), decoded, nil))
|
|
}
|
|
|
|
func TestClaimsTokenVersion(t *testing.T) {
|
|
token := jwtauth.New("HS256", []byte("test-secret"), nil)
|
|
|
|
t.Run("returns the numeric token_version claim", func(t *testing.T) {
|
|
req := requestWithJwtClaims(t, token, map[string]any{"user_id": uint32(7), "token_version": uint32(3)})
|
|
|
|
got, ok := claims.TokenVersion(req)
|
|
if !ok {
|
|
t.Fatalf("expected ok true")
|
|
}
|
|
if got != 3 {
|
|
t.Errorf("expected 3, got %d", got)
|
|
}
|
|
})
|
|
|
|
t.Run("returns ok false when the token_version key is missing", func(t *testing.T) {
|
|
req := requestWithJwtClaims(t, token, map[string]any{"user_id": uint32(7)})
|
|
|
|
_, ok := claims.TokenVersion(req)
|
|
if ok {
|
|
t.Errorf("expected ok false")
|
|
}
|
|
})
|
|
|
|
t.Run("returns ok false when token_version is not numeric", func(t *testing.T) {
|
|
req := requestWithJwtClaims(t, token, map[string]any{"user_id": uint32(7), "token_version": "0"})
|
|
|
|
_, ok := claims.TokenVersion(req)
|
|
if ok {
|
|
t.Errorf("expected ok false")
|
|
}
|
|
})
|
|
|
|
t.Run("returns ok false when there is no verified jwt in context", func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
_, ok := claims.TokenVersion(req)
|
|
if ok {
|
|
t.Errorf("expected ok false")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestClaimsUserId(t *testing.T) {
|
|
token := jwtauth.New("HS256", []byte("test-secret"), nil)
|
|
|
|
t.Run("returns the numeric user_id claim", func(t *testing.T) {
|
|
req := requestWithJwtClaims(t, token, map[string]any{"user_id": uint32(7)})
|
|
|
|
got, ok := claims.UserId(req)
|
|
if !ok {
|
|
t.Fatalf("expected ok true")
|
|
}
|
|
if got != 7 {
|
|
t.Errorf("expected 7, got %d", got)
|
|
}
|
|
})
|
|
|
|
t.Run("returns ok false when the user_id key is missing", func(t *testing.T) {
|
|
req := requestWithJwtClaims(t, token, map[string]any{"role": "user"})
|
|
|
|
_, ok := claims.UserId(req)
|
|
if ok {
|
|
t.Errorf("expected ok false")
|
|
}
|
|
})
|
|
|
|
t.Run("returns ok false when user_id is not numeric", func(t *testing.T) {
|
|
req := requestWithJwtClaims(t, token, map[string]any{"user_id": "one"})
|
|
|
|
_, ok := claims.UserId(req)
|
|
if ok {
|
|
t.Errorf("expected ok false")
|
|
}
|
|
})
|
|
|
|
t.Run("returns ok false when user_id is negative", func(t *testing.T) {
|
|
req := requestWithJwtClaims(t, token, map[string]any{"user_id": -1.0})
|
|
|
|
_, ok := claims.UserId(req)
|
|
if ok {
|
|
t.Errorf("expected ok false")
|
|
}
|
|
})
|
|
|
|
t.Run("returns ok false when user_id has a fractional part", func(t *testing.T) {
|
|
req := requestWithJwtClaims(t, token, map[string]any{"user_id": 7.5})
|
|
|
|
_, ok := claims.UserId(req)
|
|
if ok {
|
|
t.Errorf("expected ok false")
|
|
}
|
|
})
|
|
|
|
t.Run("returns ok true when user_id is exactly zero", func(t *testing.T) {
|
|
req := requestWithJwtClaims(t, token, map[string]any{"user_id": float64(0)})
|
|
|
|
got, ok := claims.UserId(req)
|
|
if !ok {
|
|
t.Fatalf("expected ok true")
|
|
}
|
|
if got != 0 {
|
|
t.Errorf("expected 0, got %d", got)
|
|
}
|
|
})
|
|
|
|
t.Run("returns ok true when user_id is exactly the uint32 max", func(t *testing.T) {
|
|
req := requestWithJwtClaims(t, token, map[string]any{"user_id": float64(math.MaxUint32)})
|
|
|
|
got, ok := claims.UserId(req)
|
|
if !ok {
|
|
t.Fatalf("expected ok true")
|
|
}
|
|
if got != math.MaxUint32 {
|
|
t.Errorf("expected %d, got %d", uint32(math.MaxUint32), got)
|
|
}
|
|
})
|
|
|
|
t.Run("returns ok false when user_id is one past the uint32 max", func(t *testing.T) {
|
|
req := requestWithJwtClaims(t, token, map[string]any{"user_id": float64(4294967296)})
|
|
|
|
_, ok := claims.UserId(req)
|
|
if ok {
|
|
t.Errorf("expected ok false")
|
|
}
|
|
})
|
|
}
|