260 lines
5 KiB
Go
260 lines
5 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"
|
|
)
|
|
|
|
const userId int64 = 789
|
|
|
|
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()
|
|
|
|
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, userId, 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)
|
|
}
|
|
|
|
func TestGetLabelsSuccess(t *testing.T) {
|
|
expectedList := []LabelsList{
|
|
{LabelUuid: "123", Name: "First", Color: "1", BgColor: "2"},
|
|
{LabelUuid: "456", Name: "Second", Color: "3", BgColor: "4"},
|
|
}
|
|
|
|
mockRepo := new(MockRepository)
|
|
mockRepo.
|
|
On("getLabels", mock.Anything, userId).
|
|
Return(expectedList, nil)
|
|
|
|
router := setupTestRouter(mockRepo)
|
|
|
|
req, _ := http.NewRequest("GET", "/merch/labels", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, 200, w.Code)
|
|
|
|
var response []LabelsList
|
|
err := json.Unmarshal(w.Body.Bytes(), &response)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assert.Equal(t, expectedList, response)
|
|
|
|
mockRepo.AssertExpectations(t)
|
|
}
|
|
|
|
func TestUpdateLabelSuccess(t *testing.T) {
|
|
mockRepo := new(MockRepository)
|
|
mockRepo.
|
|
On("updateLabel",
|
|
mock.Anything,
|
|
userId,
|
|
mock.MatchedBy(func(arg any) bool {
|
|
label, ok := arg.(*Label)
|
|
if !ok {
|
|
return false
|
|
}
|
|
return label.Uuid == "u123" && label.Name == "Test Label1"
|
|
}),
|
|
).
|
|
Return(nil)
|
|
|
|
router := setupTestRouter(mockRepo)
|
|
|
|
l := &LabelDTO{
|
|
Name: "Test Label1",
|
|
Color: "1",
|
|
BgColor: "2",
|
|
}
|
|
|
|
payload, err := json.Marshal(l)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := bytes.NewBuffer(payload)
|
|
|
|
req, _ := http.NewRequest("PUT", "/merch/labels/u123", body)
|
|
w := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, 200, w.Code)
|
|
}
|
|
|
|
func TestDeleteLabelSuccess(t *testing.T) {
|
|
mockRepo := new(MockRepository)
|
|
|
|
expectedUuid := mock.MatchedBy(func(arg any) bool {
|
|
u, ok := arg.(string)
|
|
if !ok {
|
|
return false
|
|
}
|
|
return u == "u123"
|
|
})
|
|
|
|
mockRepo.
|
|
On("deleteLabel", mock.Anything, userId, expectedUuid, mock.Anything).
|
|
Return(nil)
|
|
|
|
router := setupTestRouter(mockRepo)
|
|
|
|
req, _ := http.NewRequest("DELETE", "/merch/labels/u123", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, 200, w.Code)
|
|
}
|
|
|
|
func TestAttachLabelSuccess(t *testing.T) {
|
|
mockRepo := new(MockRepository)
|
|
|
|
expected := mock.MatchedBy(func(arg any) bool {
|
|
val, ok := arg.(*LabelLink)
|
|
if !ok {
|
|
return false
|
|
}
|
|
return val.LabelUuid == "l123"
|
|
})
|
|
|
|
mockRepo.
|
|
On("attachLabel", mock.Anything, userId, expected, mock.Anything).
|
|
Return(nil)
|
|
|
|
router := setupTestRouter(mockRepo)
|
|
|
|
l := LabelLink{
|
|
MerchUuid: "m123",
|
|
LabelUuid: "l123",
|
|
}
|
|
payload, err := json.Marshal(l)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := bytes.NewBuffer(payload)
|
|
|
|
req, _ := http.NewRequest("POST", "/merch/labels/attach", body)
|
|
w := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, 200, w.Code)
|
|
mockRepo.AssertExpectations(t)
|
|
}
|
|
|
|
func TestDetachLabelSuccess(t *testing.T) {
|
|
mockRepo := new(MockRepository)
|
|
|
|
expected := mock.MatchedBy(func(arg any) bool {
|
|
val, ok := arg.(*LabelLink)
|
|
if !ok {
|
|
return false
|
|
}
|
|
return val.LabelUuid == "l123"
|
|
})
|
|
|
|
mockRepo.
|
|
On("detachLabel", mock.Anything, userId, expected, mock.Anything).
|
|
Return(nil)
|
|
|
|
router := setupTestRouter(mockRepo)
|
|
|
|
l := LabelLink{
|
|
MerchUuid: "m123",
|
|
LabelUuid: "l123",
|
|
}
|
|
payload, err := json.Marshal(l)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := bytes.NewBuffer(payload)
|
|
|
|
req, _ := http.NewRequest("POST", "/merch/labels/detach", body)
|
|
w := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, 200, w.Code)
|
|
mockRepo.AssertExpectations(t)
|
|
}
|
|
|
|
func TestGetMerchLabelsSuccess(t *testing.T) {
|
|
mockRepo := new(MockRepository)
|
|
|
|
expected := []string{"l123", "l456", "l789"}
|
|
mockRepo.
|
|
On("getAttachedLabelsByUuid", mock.Anything, userId, "m123").
|
|
Return(expected, nil)
|
|
|
|
router := setupTestRouter(mockRepo)
|
|
|
|
req, _ := http.NewRequest("GET", "/merch/labels/m123", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, 200, w.Code)
|
|
|
|
var response []string
|
|
err := json.Unmarshal(w.Body.Bytes(), &response)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assert.Equal(t, expected, response)
|
|
|
|
mockRepo.AssertExpectations(t)
|
|
}
|