add jwt helpers test
This commit is contained in:
parent
997a79fcbe
commit
3adfcc890f
2 changed files with 31 additions and 2 deletions
|
@ -6,8 +6,8 @@ import (
|
||||||
|
|
||||||
type JWTClaims struct {
|
type JWTClaims struct {
|
||||||
jwt.RegisteredClaims
|
jwt.RegisteredClaims
|
||||||
AccountId string `json:"account_id"`
|
AccountId int `json:"account_id"`
|
||||||
RoleId string `json:"role_id"`
|
RoleId int `json:"role_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var jwtSecret string
|
var jwtSecret string
|
||||||
|
|
29
internal/core/jwt_test.go
Normal file
29
internal/core/jwt_test.go
Normal file
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in a new issue