From 023e1b851188c85935d186362fa4e1056c245370 Mon Sep 17 00:00:00 2001 From: gphper <570165887@qq.com> Date: Tue, 5 Sep 2023 20:17:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../setting/adminGroupController_test.go | 8 +- .../api/user/userController_test.go | 155 ++++++++++++++++++ pkg/utils/httptestutil/httptest.go | 2 +- pkg/utils/httptestutil/router/default.go | 39 +++++ pkg/utils/httptestutil/router/icontroller.go | 7 + pkg/utils/httptestutil/router/router.go | 101 ++++++++++++ test/api_test.go | 92 ----------- 7 files changed, 309 insertions(+), 95 deletions(-) rename test/admin_test.go => internal/controllers/admin/setting/adminGroupController_test.go (91%) create mode 100644 internal/controllers/api/user/userController_test.go create mode 100644 pkg/utils/httptestutil/router/default.go create mode 100644 pkg/utils/httptestutil/router/icontroller.go create mode 100644 pkg/utils/httptestutil/router/router.go delete mode 100644 test/api_test.go diff --git a/test/admin_test.go b/internal/controllers/admin/setting/adminGroupController_test.go similarity index 91% rename from test/admin_test.go rename to internal/controllers/admin/setting/adminGroupController_test.go index e927a13..8bf4492 100644 --- a/test/admin_test.go +++ b/internal/controllers/admin/setting/adminGroupController_test.go @@ -3,7 +3,7 @@ * @Author: gphper * @Date: 2022-03-31 19:59:19 */ -package test +package setting import ( "log" @@ -12,10 +12,11 @@ import ( "testing" "github.com/gphper/ginadmin/configs" - "github.com/gphper/ginadmin/internal/router" + "github.com/gphper/ginadmin/internal/controllers/admin" "github.com/gphper/ginadmin/pkg/mysqlx" "github.com/gphper/ginadmin/pkg/redisx" "github.com/gphper/ginadmin/pkg/utils/httptestutil" + "github.com/gphper/ginadmin/pkg/utils/httptestutil/router" "github.com/gphper/ginadmin/web" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" @@ -34,6 +35,9 @@ func (suite *AdminTestSuite) SetupSuite() { log.Fatalf("router init fail: %s", err) } + router.SetAdminRoute("/admin", admin.NewLoginController()) + router.SetAdminRoute("/admin/setting/admingroup", NewAdminGroupController()) + suite.router = router } diff --git a/internal/controllers/api/user/userController_test.go b/internal/controllers/api/user/userController_test.go new file mode 100644 index 0000000..d663478 --- /dev/null +++ b/internal/controllers/api/user/userController_test.go @@ -0,0 +1,155 @@ +/* + * @Description: + * @Author: gphper + * @Date: 2022-04-03 10:16:52 + */ +package user + +import ( + "bytes" + "encoding/json" + "log" + "net/url" + "testing" + + "github.com/gphper/ginadmin/configs" + "github.com/gphper/ginadmin/internal/dao" + "github.com/gphper/ginadmin/internal/models" + "github.com/gphper/ginadmin/pkg/mysqlx" + "github.com/gphper/ginadmin/pkg/redisx" + "github.com/gphper/ginadmin/pkg/utils/httptestutil" + "github.com/gphper/ginadmin/pkg/utils/httptestutil/router" + "github.com/gphper/ginadmin/web" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" +) + +// api UserController 相关接口测试 +type ApiTestSuite struct { + suite.Suite + router *router.Router + jtoken string +} + +func (suite *ApiTestSuite) SetupSuite() { + + router, err := router.Init() + if err != nil { + log.Fatalf("router init fail: %s", err) + } + + router.SetApiRoute("/api/user", NewUserController()) + + suite.router = router +} + +// 注册接口测试 +func (suite *ApiTestSuite) TestARegister() { + + option := httptestutil.OptionValue{ + Param: url.Values{ + "nickname": {"gphper"}, + "password": {"111111"}, + "confirm_password": {"111111"}, + "email": {"570165887@qq.com"}, + }, + } + // 发起post请求,以表单形式传递参数 + body, _ := httptestutil.PostForm("/api/user/register", suite.router, option) + assert.JSONEq(suite.T(), `{"code":200,"msg":"success","data":{}}`, string(body)) +} + +func (suite *ApiTestSuite) TestARegisterErr() { + + option := httptestutil.OptionValue{ + Param: url.Values{ + "nickname": {"gphper"}, + "password": {"111111"}, + "confirm_password": {"111111"}, + "email": {"570165887@qq.com"}, + }, + } + // 发起post请求,以表单形式传递参数 + body, _ := httptestutil.PostForm("/api/user/register", suite.router, option) + assert.JSONEq(suite.T(), `{"code":2000, "msg":"该邮箱已存在"}`, string(body)) +} + +func (suite *ApiTestSuite) TestARegisterBindErr() { + + option := httptestutil.OptionValue{ + Param: url.Values{ + "nickname": {"gphper"}, + }, + } + // 发起post请求,以表单形式传递参数 + body, _ := httptestutil.PostForm("/api/user/register", suite.router, option) + assert.JSONEq(suite.T(), `{"code":2001, "msg":"绑定参数出错"}`, string(body)) +} + +// 登录接口测试 +func (suite *ApiTestSuite) TestBLogin() { + + type ( + Data struct { + Jtoken string + Retoken string + } + + response struct { + Code int + Msg string + Data Data + } + ) + + var resp response + + option := httptestutil.OptionValue{ + Param: url.Values{ + "password": {"111111"}, + "email": {"570165887@qq.com"}, + }, + } + + body, _ := httptestutil.PostForm("/api/user/login", suite.router, option) + assert.Contains(suite.T(), string(body), "jtoken") + + decode := json.NewDecoder(bytes.NewReader(body)) + if err := decode.Decode(&resp); err != nil { + assert.FailNow(suite.T(), err.Error()) + } + + suite.jtoken = resp.Data.Jtoken +} + +// 善后恢复数据处理 +func (s *ApiTestSuite) TearDownSuite() { + dao.NewUserDao().DB.Model(models.User{}).Where("email = ?", "570165887@qq.com").Delete(nil) +} + +func TestApiSuite(t *testing.T) { + + var err error + + err = configs.Init("") + if err != nil { + log.Fatalf("start fail:[Config Init] %s", err.Error()) + } + + err = web.Init() + if err != nil { + log.Fatalf("start fail:[Web Init] %s", err.Error()) + } + + err = redisx.Init() + if err != nil { + log.Fatalf("start fail:[Redis Init] %s", err.Error()) + } + + err = mysqlx.Init() + if err != nil { + log.Fatalf("start fail:[Mysql Init] %s", err.Error()) + } + + suite.Run(t, new(ApiTestSuite)) +} diff --git a/pkg/utils/httptestutil/httptest.go b/pkg/utils/httptestutil/httptest.go index f44ef1b..30af7bb 100644 --- a/pkg/utils/httptestutil/httptest.go +++ b/pkg/utils/httptestutil/httptest.go @@ -12,7 +12,7 @@ import ( "net/url" "strings" - "github.com/gphper/ginadmin/internal/router" + "github.com/gphper/ginadmin/pkg/utils/httptestutil/router" ) type OptionValue struct { diff --git a/pkg/utils/httptestutil/router/default.go b/pkg/utils/httptestutil/router/default.go new file mode 100644 index 0000000..d01ce07 --- /dev/null +++ b/pkg/utils/httptestutil/router/default.go @@ -0,0 +1,39 @@ +/* + * @Description: + * @Author: gphper + * @Date: 2021-06-01 20:15:04 + */ +package router + +import ( + "time" + + "github.com/gphper/ginadmin/internal/controllers" + "github.com/gphper/ginadmin/internal/middleware" + "github.com/gphper/ginadmin/pkg/loggers/facade" + "github.com/gphper/ginadmin/pkg/loggers/medium" + "github.com/gphper/ginadmin/web" + + "github.com/gin-gonic/gin" +) + +func Init() (*Router, error) { + + router := NewRouter(gin.Default()) + + //设置404错误处理 + router.SetRouteError(controllers.NewHandleController().Handle) + + //设置全局中间件 + router.SetGlobalMiddleware(middleware.Trace(), medium.GinLog(facade.NewLogger("admin"), time.RFC3339, false), medium.RecoveryWithLog(facade.NewLogger("admin"), true)) + + // 设置模板解析函数 + render, err := web.LoadTemplates() + if err != nil { + + return nil, err + } + router.SetHtmlRenderer(render) + + return &router, nil +} diff --git a/pkg/utils/httptestutil/router/icontroller.go b/pkg/utils/httptestutil/router/icontroller.go new file mode 100644 index 0000000..cb9fcd0 --- /dev/null +++ b/pkg/utils/httptestutil/router/icontroller.go @@ -0,0 +1,7 @@ +package router + +import "github.com/gin-gonic/gin" + +type IController interface { + Routes(*gin.RouterGroup) +} diff --git a/pkg/utils/httptestutil/router/router.go b/pkg/utils/httptestutil/router/router.go new file mode 100644 index 0000000..eba95fa --- /dev/null +++ b/pkg/utils/httptestutil/router/router.go @@ -0,0 +1,101 @@ +/** + * @Author: GPHPER + * @Date: 2022-12-12 15:06:04 + * @Github: https://github.com/gphper + * @LastEditTime: 2022-12-13 19:51:24 + * @FilePath: \ginadmin\pkg\router\router.go + * @Description: + */ +package router + +import ( + "net/http" + "os" + + "github.com/gin-contrib/gzip" + "github.com/gin-contrib/sessions" + "github.com/gin-contrib/sessions/cookie" + "github.com/gin-gonic/gin" + "github.com/gphper/ginadmin/internal" + "github.com/gphper/multitemplate" +) + +type Router struct { + r *gin.Engine +} + +func NewRouter(r *gin.Engine) Router { + return Router{ + r: r, + } +} + +func (route Router) SetGlobalMiddleware(middlewares ...gin.HandlerFunc) { + route.r.Use(middlewares...) +} + +// 设置自定义模板加载 +func (route Router) SetHtmlRenderer(render multitemplate.Renderer) { + route.r.HTMLRender = render +} + +// 设置swagger访问 +func (route Router) SetSwaagerHandle(path string, handle gin.HandlerFunc) { + route.r.GET(path, handle) +} + +// 设置静态路径 +func (route Router) SetStaticFile(path string, fs http.FileSystem) { + route.r.StaticFS(path, fs) +} + +// 设置附件保存地址 +func (route Router) SetUploadDir(path string) error { + _, err := os.Stat(path) + if err != nil { + if os.IsNotExist(err) { + err = os.Mkdir(path, os.ModeDir) + if err != nil { + + return err + } + } + } + + route.r.StaticFS("/uploadfile", http.Dir(path)) + + return nil +} + +func (route Router) SetEngine(app *internal.Application) { + app.Route = route.r +} + +func (route Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { + route.r.ServeHTTP(w, req) +} + +func (route Router) SetRouteError(handler gin.HandlerFunc) { + route.r.NoMethod(handler) + route.r.NoRoute(handler) +} + +func (route Router) SetApiRoute(path string, ic IController, middlewares ...gin.HandlerFunc) { + ar := route.r.Group(path) + if len(middlewares) > 0 { + ar.Use(middlewares...) + } + ic.Routes(ar) +} + +func (route Router) SetAdminRoute(path string, ic IController, middlewares ...gin.HandlerFunc) { + + store := cookie.NewStore([]byte("1GdFRMs4fcWBvLXT")) + middlewares = append(middlewares, gzip.Gzip(gzip.DefaultCompression), sessions.Sessions("mysession", store)) + + ar := route.r.Group(path) + if len(middlewares) > 0 { + ar.Use(middlewares...) + } + ic.Routes(ar) +} diff --git a/test/api_test.go b/test/api_test.go deleted file mode 100644 index f8d8f23..0000000 --- a/test/api_test.go +++ /dev/null @@ -1,92 +0,0 @@ -/* - * @Description: - * @Author: gphper - * @Date: 2022-04-03 10:16:52 - */ -package test - -import ( - "bytes" - "encoding/json" - "log" - "net/url" - "testing" - - "github.com/gphper/ginadmin/internal/router" - "github.com/gphper/ginadmin/pkg/utils/httptestutil" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/suite" -) - -type ApiTestSuite struct { - suite.Suite - router *router.Router - jtoken string -} - -func (suite *ApiTestSuite) SetupSuite() { - - router, err := router.Init() - if err != nil { - log.Fatalf("router init fail: %s", err) - } - - suite.router = router -} - -// 注册接口测试 -func (suite *ApiTestSuite) TestARegister() { - - option := httptestutil.OptionValue{ - Param: url.Values{ - "nickname": {"gphper"}, - "password": {"111111"}, - "confirm_password": {"111111"}, - "email": {"570165887@qq.com"}, - }, - } - // 发起post请求,以表单形式传递参数 - body, _ := httptestutil.PostForm("/api/user/register", suite.router, option) - assert.JSONEq(suite.T(), `{"code":200,"msg":"success","data":{}}`, string(body)) - -} - -// 登录接口测试 -func (suite *ApiTestSuite) TestBLogin() { - - type ( - Data struct { - Jtoken string - Retoken string - } - - response struct { - Code int - Msg string - Data Data - } - ) - - var resp response - - option := httptestutil.OptionValue{ - Param: url.Values{ - "password": {"111111"}, - "email": {"570165887@qq.com"}, - }, - } - - body, _ := httptestutil.PostForm("/api/user/login", suite.router, option) - assert.Contains(suite.T(), string(body), "jtoken") - - decode := json.NewDecoder(bytes.NewReader(body)) - if err := decode.Decode(&resp); err != nil { - assert.FailNow(suite.T(), err.Error()) - } - - suite.jtoken = resp.Data.Jtoken -} - -func TestApiSuite(t *testing.T) { - suite.Run(t, new(ApiTestSuite)) -}