Skip to content

Commit

Permalink
test: token_test add generate expired token
Browse files Browse the repository at this point in the history
  • Loading branch information
ozline committed Dec 17, 2024
1 parent 22e64b4 commit da6323d
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion api/mw/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,46 @@ package mw
import (
"fmt"
"testing"
"time"

"github.com/bytedance/mockey"
"github.com/golang-jwt/jwt"
"github.com/stretchr/testify/assert"

"github.com/west2-online/fzuhelper-server/pkg/constants"
)

// TestCreateExpiredToken 是一个特殊测试,旨在生成一个过期的 Token
func TestCreateExpiredToken(t *testing.T) {
// 默认生成时间是一年一个月七天前的
var curTime = time.Now().AddDate(-1, -1, -7)
var expiredTime = curTime.Add(constants.AccessTokenTTL)
var token string
var err error

claims := Claims{
Type: constants.TypeAccessToken,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expiredTime.Unix(), // 过期时间戳
IssuedAt: curTime.Unix(), // 当前时间戳
Issuer: constants.Issuer, // 颁发者签名
},
}
tokenStruct := jwt.NewWithClaims(jwt.SigningMethodEdDSA, claims)
key, err := jwt.ParseEdPrivateKeyFromPEM([]byte(fmt.Sprintf("%v\n%v\n%v", "-----BEGIN PRIVATE KEY-----",
"此处需要修改为私钥",
"-----END PRIVATE KEY-----")))
if err != nil {
t.Error(fmt.Sprintf("parse private key failed, err: %v", err))
}

token, err = tokenStruct.SignedString(key)
if err != nil {
t.Error(fmt.Sprintf("sign token failed, err: %v", err))
}

fmt.Println(fmt.Sprintf("Access-Token: %s", token))
}

func TestCreateAllToken(t *testing.T) {
type testCase struct {
name string
Expand Down

0 comments on commit da6323d

Please sign in to comment.