second commit
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
"github.com/gochat/gochat/internal/repository"
|
||||
"github.com/gochat/gochat/internal/service"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
type LabelHandlerTestSuite struct {
|
||||
suite.Suite
|
||||
db *gorm.DB
|
||||
handler *LabelHandler
|
||||
|
||||
account *model.Account
|
||||
}
|
||||
|
||||
func (s *LabelHandlerTestSuite) SetupSuite() {
|
||||
gin.SetMode(gin.TestMode)
|
||||
db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
s.Require().NoError(db.AutoMigrate(&model.Account{}, &model.User{}, &model.Tag{}, &model.ConversationLabel{}, &model.Conversation{}, &model.Inbox{}, &model.Contact{}))
|
||||
s.db = db
|
||||
|
||||
convLabelRepo := repository.NewConversationLabelRepo(db)
|
||||
tagRepo := repository.NewTagRepo(db)
|
||||
tagSvc := service.NewTagService(tagRepo)
|
||||
labelSvc := service.NewLabelService(convLabelRepo, tagRepo)
|
||||
s.handler = NewLabelHandler(tagSvc, labelSvc)
|
||||
|
||||
s.account = &model.Account{Name: "test-label-account"}
|
||||
s.Require().NoError(db.Create(s.account).Error)
|
||||
}
|
||||
|
||||
func (s *LabelHandlerTestSuite) TearDownSuite() {
|
||||
if s.db != nil {
|
||||
sqlDB, _ := s.db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func TestLabelHandlerSuite(t *testing.T) {
|
||||
suite.Run(t, new(LabelHandlerTestSuite))
|
||||
}
|
||||
|
||||
func (s *LabelHandlerTestSuite) TestListTags_Success() {
|
||||
r := gin.New()
|
||||
r.GET("/api/v1/accounts/:account_id/tags", s.handler.ListTags)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/tags", s.account.ID), nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
func (s *LabelHandlerTestSuite) TestCreateTag_BadRequest_EmptyBody() {
|
||||
r := gin.New()
|
||||
r.POST("/api/v1/accounts/:account_id/tags", s.handler.CreateTag)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/tags", s.account.ID), nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func (s *LabelHandlerTestSuite) TestCreateTag_Success() {
|
||||
r := gin.New()
|
||||
r.POST("/api/v1/accounts/:account_id/tags", s.handler.CreateTag)
|
||||
|
||||
body := map[string]interface{}{"name": "priority", "color": "#FF0000"}
|
||||
b, _ := json.Marshal(body)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/tags", s.account.ID), bytes.NewBuffer(b))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusCreated, w.Code)
|
||||
}
|
||||
|
||||
func (s *LabelHandlerTestSuite) TestGetTag_BadRequest_InvalidID() {
|
||||
r := gin.New()
|
||||
r.GET("/api/v1/accounts/:account_id/tags/:tag_id", s.handler.GetTag)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/tags/abc", s.account.ID), nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func (s *LabelHandlerTestSuite) TestGetTag_NotFound() {
|
||||
r := gin.New()
|
||||
r.GET("/api/v1/accounts/:account_id/tags/:tag_id", s.handler.GetTag)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/tags/99999", s.account.ID), nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusNotFound, w.Code)
|
||||
}
|
||||
|
||||
func (s *LabelHandlerTestSuite) TestUpdateTag_BadRequest_InvalidID() {
|
||||
r := gin.New()
|
||||
r.PUT("/api/v1/accounts/:account_id/tags/:tag_id", s.handler.UpdateTag)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("PUT", fmt.Sprintf("/api/v1/accounts/%d/tags/abc", s.account.ID), nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func (s *LabelHandlerTestSuite) TestDeleteTag_BadRequest_InvalidID() {
|
||||
r := gin.New()
|
||||
r.DELETE("/api/v1/accounts/:account_id/tags/:tag_id", s.handler.DeleteTag)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/tags/abc", s.account.ID), nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func (s *LabelHandlerTestSuite) TestAddLabelToConversation_BadRequest_InvalidConvID() {
|
||||
r := gin.New()
|
||||
r.POST("/api/v1/accounts/:account_id/conversations/:conversation_id/labels", s.handler.AddLabelToConversation)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/conversations/abc/labels", s.account.ID), nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func (s *LabelHandlerTestSuite) TestRemoveLabelFromConversation_BadRequest_InvalidIDs() {
|
||||
r := gin.New()
|
||||
r.DELETE("/api/v1/accounts/:account_id/conversations/:conversation_id/labels/:tag_id", s.handler.RemoveLabelFromConversation)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/conversations/abc/labels/1", s.account.ID), nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func (s *LabelHandlerTestSuite) TestGetConversationLabels_BadRequest_InvalidConvID() {
|
||||
r := gin.New()
|
||||
r.GET("/api/v1/accounts/:account_id/conversations/:conversation_id/labels", s.handler.GetConversationLabels)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/conversations/abc/labels", s.account.ID), nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func (s *LabelHandlerTestSuite) TestBatchAddLabel_BadRequest_InvalidAccountID() {
|
||||
r := gin.New()
|
||||
r.POST("/api/v1/accounts/:account_id/conversations/batch_labels/add", s.handler.BatchAddLabel)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/conversations/batch_labels/add", nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func (s *LabelHandlerTestSuite) TestBatchRemoveLabel_BadRequest_InvalidAccountID() {
|
||||
r := gin.New()
|
||||
r.POST("/api/v1/accounts/:account_id/conversations/batch_labels/remove", s.handler.BatchRemoveLabel)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/conversations/batch_labels/remove", nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
}
|
||||
Reference in New Issue
Block a user