From 3adfcc890f6f408942eda022b25b5e6a9130ba35 Mon Sep 17 00:00:00 2001 From: qpismont Date: Wed, 26 Feb 2025 20:21:12 +0000 Subject: [PATCH] add jwt helpers test --- internal/core/jwt.go | 4 ++-- internal/core/jwt_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 internal/core/jwt_test.go diff --git a/internal/core/jwt.go b/internal/core/jwt.go index 0f5a19e..4515334 100644 --- a/internal/core/jwt.go +++ b/internal/core/jwt.go @@ -6,8 +6,8 @@ import ( type JWTClaims struct { jwt.RegisteredClaims - AccountId string `json:"account_id"` - RoleId string `json:"role_id"` + AccountId int `json:"account_id"` + RoleId int `json:"role_id"` } var jwtSecret string diff --git a/internal/core/jwt_test.go b/internal/core/jwt_test.go new file mode 100644 index 0000000..d9dce0f --- /dev/null +++ b/internal/core/jwt_test.go @@ -0,0 +1,29 @@ +package core + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestJWT_GenerateToken(t *testing.T) { + InitJWT("secret") + + token, err := SignJWT(JWTClaims{ + AccountId: 1, + RoleId: 1, + }) + if err != nil { + t.Fatalf("Failed to generate token: %v", err) + } + + assert.NotEmpty(t, token) + + claims, err := VerifyJWT(token) + if err != nil { + t.Fatalf("Failed to verify token: %v", err) + } + + assert.Equal(t, claims.AccountId, 1) + assert.Equal(t, claims.RoleId, 1) +}