user_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package handler
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/handler"
  7. "github.com/go-nunu/nunu-layout-advanced/mocks/service"
  8. "github.com/go-nunu/nunu-layout-advanced/internal/server"
  9. "net/http"
  10. "net/http/httptest"
  11. "os"
  12. "testing"
  13. "github.com/gin-gonic/gin"
  14. "github.com/go-nunu/nunu-layout-advanced/internal/middleware"
  15. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  16. "github.com/go-nunu/nunu-layout-advanced/internal/service"
  17. "github.com/go-nunu/nunu-layout-advanced/pkg/config"
  18. "github.com/go-nunu/nunu-layout-advanced/pkg/log"
  19. "github.com/golang/mock/gomock"
  20. "github.com/stretchr/testify/assert"
  21. )
  22. var (
  23. userId = "yhs6HesfgF"
  24. token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOiJ5aHM2SGVzZmdGIiwiZXhwIjoxNjkzOTE0ODgwLCJuYmYiOjE2ODYxMzg4ODAsImlhdCI6MTY4NjEzODg4MH0.NnFrZFgc_333a9PXqaoongmIDksNvQoHzgM_IhJM4MQ"
  25. )
  26. var hdl *handler.Handler
  27. func TestMain(m *testing.M) {
  28. fmt.Println("begin")
  29. os.Setenv("APP_CONF", "../../../config/local.yml")
  30. conf := config.NewConfig()
  31. logger := log.NewLog(conf)
  32. hdl = handler.NewHandler(logger)
  33. code := m.Run()
  34. fmt.Println("test end")
  35. os.Exit(code)
  36. }
  37. func TestUserHandler_Register(t *testing.T) {
  38. ctrl := gomock.NewController(t)
  39. defer ctrl.Finish()
  40. params := service.RegisterRequest{
  41. Username: "xxx",
  42. Password: "123456",
  43. Email: "xxx@gmail.com",
  44. }
  45. mockUserService := mock_service.NewMockUserService(ctrl)
  46. mockUserService.EXPECT().Register(gomock.Any(), &params).Return(nil)
  47. router := setupRouter(mockUserService)
  48. paramsJson, _ := json.Marshal(params)
  49. resp := performRequest(router, "POST", "/register", bytes.NewBuffer(paramsJson))
  50. assert.Equal(t, resp.Code, http.StatusOK)
  51. // Add assertions for the response body if needed
  52. }
  53. func TestUserHandler_Login(t *testing.T) {
  54. ctrl := gomock.NewController(t)
  55. defer ctrl.Finish()
  56. params := service.LoginRequest{
  57. Username: "xxx",
  58. Password: "123456",
  59. }
  60. mockUserService := mock_service.NewMockUserService(ctrl)
  61. mockUserService.EXPECT().Login(gomock.Any(), &params).Return(token, nil)
  62. router := setupRouter(mockUserService)
  63. paramsJson, _ := json.Marshal(params)
  64. resp := performRequest(router, "POST", "/login", bytes.NewBuffer(paramsJson))
  65. assert.Equal(t, resp.Code, http.StatusOK)
  66. // Add assertions for the response body if needed
  67. }
  68. func TestUserHandler_GetProfile(t *testing.T) {
  69. ctrl := gomock.NewController(t)
  70. defer ctrl.Finish()
  71. mockUserService := mock_service.NewMockUserService(ctrl)
  72. mockUserService.EXPECT().GetProfile(gomock.Any(), userId).Return(&model.User{
  73. Id: 1,
  74. UserId: userId,
  75. Username: "xxxxx",
  76. Nickname: "xxxxx",
  77. Password: "xxxxx",
  78. Email: "xxxxx@gmail.com",
  79. }, nil)
  80. router := setupRouter(mockUserService)
  81. req, _ := http.NewRequest("GET", "/user", nil)
  82. req.Header.Set("Authorization", "Bearer "+token)
  83. resp := httptest.NewRecorder()
  84. router.ServeHTTP(resp, req)
  85. assert.Equal(t, resp.Code, http.StatusOK)
  86. // Add assertions for the response body if needed
  87. }
  88. func TestUserHandler_UpdateProfile(t *testing.T) {
  89. ctrl := gomock.NewController(t)
  90. defer ctrl.Finish()
  91. params := service.UpdateProfileRequest{
  92. Nickname: "alan",
  93. Email: "alan@gmail.com",
  94. Avatar: "xxx",
  95. }
  96. mockUserService := mock_service.NewMockUserService(ctrl)
  97. mockUserService.EXPECT().UpdateProfile(gomock.Any(), userId, &params).Return(nil)
  98. router := setupRouter(mockUserService)
  99. paramsJson, _ := json.Marshal(params)
  100. req, _ := http.NewRequest("PUT", "/user", bytes.NewBuffer(paramsJson))
  101. req.Header.Set("Authorization", "Bearer "+token)
  102. req.Header.Set("Content-Type", "application/json")
  103. resp := httptest.NewRecorder()
  104. router.ServeHTTP(resp, req)
  105. assert.Equal(t, resp.Code, http.StatusOK)
  106. // Add assertions for the response body if needed
  107. }
  108. func setupRouter(mockUserService *mock_service.MockUserService) *gin.Engine {
  109. conf := config.NewConfig()
  110. logger := log.NewLog(conf)
  111. jwt := middleware.NewJwt(conf)
  112. userHandler := handler.NewUserHandler(hdl, mockUserService)
  113. gin.SetMode(gin.TestMode)
  114. router := server.NewServerHTTP(logger, jwt, userHandler)
  115. return router
  116. }
  117. func performRequest(r http.Handler, method, path string, body *bytes.Buffer) *httptest.ResponseRecorder {
  118. req, _ := http.NewRequest(method, path, body)
  119. resp := httptest.NewRecorder()
  120. r.ServeHTTP(resp, req)
  121. return resp
  122. }