feat 包命名规范
This commit is contained in:
156
internal/handler/httphandler/login_test.go
Normal file
156
internal/handler/httphandler/login_test.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package httphandler
|
||||
|
||||
import (
|
||||
"bou.ke/monkey"
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpcclient"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/http/httpresp"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb/mocks"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-gateway/internal/testutil"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type LoginTestSuite struct {
|
||||
testutil.TestSuite
|
||||
}
|
||||
|
||||
func (ts *LoginTestSuite) TestLogin() {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
ts.Run("ShouldBindJSON Failed", func() {
|
||||
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
|
||||
Phone: "",
|
||||
Code: "",
|
||||
})
|
||||
Login(c)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusBadRequest, httpresp.ParamError)
|
||||
})
|
||||
|
||||
ts.Run("UserNewClientLB Failed", func() {
|
||||
monkey.Patch(grpcclient.UserNewClientLB, func() (rspb.UserClient, error) {
|
||||
return nil, assert.AnError
|
||||
})
|
||||
defer monkey.Unpatch(grpcclient.UserNewClientLB)
|
||||
|
||||
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
|
||||
Phone: "13800000000",
|
||||
Code: "1234",
|
||||
})
|
||||
Login(c)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, httpresp.Failed)
|
||||
})
|
||||
|
||||
ts.Run("Login Failed", func() {
|
||||
ctrl := gomock.NewController(ts.T())
|
||||
defer ctrl.Finish()
|
||||
client := mocks.NewMockUserClient(ctrl)
|
||||
client.EXPECT().
|
||||
PhoneLogin(gomock.Any(), gomock.Any()).
|
||||
Return(nil, fmt.Errorf("gRPC failed"))
|
||||
|
||||
monkey.Patch(grpcclient.UserNewClientLB, func() (rspb.UserClient, error) {
|
||||
return client, nil
|
||||
})
|
||||
defer monkey.Unpatch(grpcclient.UserNewClientLB)
|
||||
|
||||
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
|
||||
Phone: "13800000000",
|
||||
Code: "1234",
|
||||
})
|
||||
Login(c)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, httpresp.Failed)
|
||||
})
|
||||
|
||||
ts.Run("OK", func() {
|
||||
ctrl := gomock.NewController(ts.T())
|
||||
defer ctrl.Finish()
|
||||
client := mocks.NewMockUserClient(ctrl)
|
||||
client.EXPECT().
|
||||
PhoneLogin(gomock.Any(), gomock.Any()).
|
||||
Return(&rspb.PhoneLoginResp{USN: 1, Name: "hh"}, nil)
|
||||
|
||||
monkey.Patch(grpcclient.UserNewClientLB, func() (rspb.UserClient, error) {
|
||||
return client, nil
|
||||
})
|
||||
defer monkey.Unpatch(grpcclient.UserNewClientLB)
|
||||
|
||||
w, c := utils.CreateTestContext("POST", "/", &LoginReq{
|
||||
Phone: "13800000000",
|
||||
Code: "1234",
|
||||
})
|
||||
Login(c)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, httpresp.OK)
|
||||
})
|
||||
}
|
||||
|
||||
func (ts *LoginTestSuite) TestRefreshToken() {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
ts.Run("ShouldBindJSON Failed", func() {
|
||||
w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
|
||||
RefreshToken: "",
|
||||
})
|
||||
RefreshToken(c)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusBadRequest, httpresp.ParamError)
|
||||
})
|
||||
|
||||
ts.Run("ParseToken Failed", func() {
|
||||
monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) {
|
||||
return nil, assert.AnError
|
||||
})
|
||||
defer monkey.Unpatch(utils.ParseToken)
|
||||
|
||||
w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
|
||||
RefreshToken: "abc",
|
||||
})
|
||||
RefreshToken(c)
|
||||
utils.AssertResponse(&ts.Suite, w, http.StatusOK, httpresp.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(), 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) {
|
||||
suite.Run(t, &LoginTestSuite{})
|
||||
}
|
||||
Reference in New Issue
Block a user