merch-api/internal/merch/handler_test.go
2026-03-13 21:18:16 +03:00

68 lines
1.2 KiB
Go

package merch
import (
"bytes"
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"merch-api/pkg/utils"
"net/http"
"net/http/httptest"
"testing"
)
func setupTestRouter(mockRepo Repository) *gin.Engine {
ut := utils.New()
tService := newService(mockRepo, ut)
tController := newController(tService, ut)
handler := &Handler{
controller: tController,
}
gin.SetMode(gin.TestMode)
router := gin.New()
var userId int64 = 789
router.Use(
func(c *gin.Context) {
c.Set("userId", userId)
c.Next()
})
handler.RegisterRoutes(router.Group(""))
return router
}
func TestCreateLabelSuccess(t *testing.T) {
mockRepo := new(MockRepository)
mockRepo.
On("createLabel", mock.Anything, mock.Anything, mock.Anything).
Return(nil)
router := setupTestRouter(mockRepo)
label := LabelDTO{
Name: "Success",
Color: "1",
BgColor: "2",
}
payload, err := json.Marshal(label)
if err != nil {
t.Fatal(err)
}
body := bytes.NewBuffer(payload)
req, _ := http.NewRequest("POST", "/merch/labels", body)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
mockRepo.AssertExpectations(t)
}