feat 废弃jwt 、 学识分

This commit is contained in:
2026-02-06 22:31:29 +08:00
parent b1dfb88f71
commit 456f1970eb
17 changed files with 236 additions and 156 deletions

View File

@@ -2,23 +2,18 @@ package http_handler
import (
"bou.ke/monkey"
"context"
"fmt"
"git.hlsq.asia/mmorpg/service-common/db/redis"
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb/mocks"
"git.hlsq.asia/mmorpg/service-common/utils"
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
"git.hlsq.asia/mmorpg/service-gateway/internal/testutil"
"github.com/gin-gonic/gin"
"github.com/golang/mock/gomock"
redis2 "github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"net/http"
"strconv"
"testing"
)
@@ -26,17 +21,6 @@ type LoginTestSuite struct {
testutil.TestSuite
}
func (ts *LoginTestSuite) TestGenToken() {
usn := utils.RandInt(1, 100000000)
at, rt, err := genToken(context.Background(), strconv.Itoa(usn))
ts.Assert().NoError(err)
redisAt := redis.GetClient().Get(context.Background(), fmt.Sprintf(global.KeyGatewayAccessToken, usn)).Val()
ts.Assert().Equal(at, redisAt)
redisRt := redis.GetClient().Get(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, usn)).Val()
ts.Assert().Equal(rt, redisRt)
}
func (ts *LoginTestSuite) TestLogin() {
gin.SetMode(gin.TestMode)
@@ -90,7 +74,7 @@ func (ts *LoginTestSuite) TestLogin() {
client := mocks.NewMockUserClient(ctrl)
client.EXPECT().
PhoneLogin(gomock.Any(), gomock.Any()).
Return(&grpc_pb.PhoneLoginResp{USN: "1", Name: "hh"}, nil)
Return(&grpc_pb.PhoneLoginResp{USN: 1, Name: "hh"}, nil)
monkey.Patch(grpc_client.UserNewClientLB, func() (grpc_pb.UserClient, error) {
return client, nil
@@ -130,41 +114,41 @@ func (ts *LoginTestSuite) TestRefreshToken() {
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid)
})
ts.Run("Redis Get Failed", func() {
monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) {
if tokenString == "abc" {
return &utils.Claims{USN: "1"}, nil
}
return nil, assert.AnError
})
defer monkey.Unpatch(utils.ParseToken)
redis.GetClient().Set(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, 1), "ab", redis2.KeepTTL)
w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
RefreshToken: "abc",
})
RefreshToken(c)
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid)
})
ts.Run("OK", func() {
monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) {
if tokenString == "abc" {
return &utils.Claims{USN: "1"}, nil
}
return nil, assert.AnError
})
defer monkey.Unpatch(utils.ParseToken)
redis.GetClient().Set(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, 1), "abc", redis2.KeepTTL)
w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
RefreshToken: "abc",
})
RefreshToken(c)
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.OK)
})
//ts.Run("Redis Get Failed", func() {
// monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) {
// if tokenString == "abc" {
// return &utils.Claims{USN: 1}, nil
// }
// return nil, assert.AnError
// })
// defer monkey.Unpatch(utils.ParseToken)
//
// redis.GetClient().Set(context.Background(), global.KeyGatewayRefreshToken+1, "ab", redis2.KeepTTL)
//
// w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
// RefreshToken: "abc",
// })
// RefreshToken(c)
// utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid)
//})
//
//ts.Run("OK", func() {
// monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) {
// if tokenString == "abc" {
// return &utils.Claims{USN: 1}, nil
// }
// return nil, assert.AnError
// })
// defer monkey.Unpatch(utils.ParseToken)
//
// redis.GetClient().Set(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, 1), "abc", redis2.KeepTTL)
//
// w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
// RefreshToken: "abc",
// })
// RefreshToken(c)
// utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.OK)
//})
}
func TestLoginTestSuite(t *testing.T) {