137 lines
4.5 KiB
Go
137 lines
4.5 KiB
Go
package v1
|
|
|
|
import (
|
|
"bytes"
|
|
"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 InboxLimitHandlerTestSuite struct {
|
|
suite.Suite
|
|
db *gorm.DB
|
|
handler *InboxLimitHandler
|
|
account *model.Account
|
|
inbox *model.Inbox
|
|
}
|
|
|
|
func (s *InboxLimitHandlerTestSuite) 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.Inbox{}, &model.InboxLimit{}))
|
|
s.db = db
|
|
|
|
repo := repository.NewInboxLimitRepo(db)
|
|
svc := service.NewInboxLimitService(repo)
|
|
s.handler = NewInboxLimitHandler(svc)
|
|
|
|
s.account = &model.Account{Name: "test-inbox-limit-account"}
|
|
s.Require().NoError(db.Create(s.account).Error)
|
|
|
|
s.inbox = &model.Inbox{AccountID: s.account.ID, Name: "test-inbox", ChannelType: string(model.InboxChannelTypeWebWidget)}
|
|
s.Require().NoError(db.Create(s.inbox).Error)
|
|
}
|
|
|
|
func (s *InboxLimitHandlerTestSuite) TearDownSuite() {
|
|
if s.db != nil {
|
|
sqlDB, _ := s.db.DB()
|
|
sqlDB.Close()
|
|
}
|
|
}
|
|
|
|
func TestInboxLimitHandlerSuite(t *testing.T) {
|
|
suite.Run(t, new(InboxLimitHandlerTestSuite))
|
|
}
|
|
|
|
func (s *InboxLimitHandlerTestSuite) TestCreate_BadRequest_EmptyBody() {
|
|
r := gin.New()
|
|
r.POST("/api/v1/accounts/:account_id/inboxes/:inbox_id/inbox_limits", s.handler.Create)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/inboxes/%d/inbox_limits", s.account.ID, s.inbox.ID), nil)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *InboxLimitHandlerTestSuite) TestUpdate_BadRequest_InvalidID() {
|
|
r := gin.New()
|
|
r.PUT("/api/v1/accounts/:account_id/inboxes/:inbox_id/inbox_limits/:id", s.handler.Update)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/api/v1/accounts/%d/inboxes/%d/inbox_limits/abc", s.account.ID, s.inbox.ID), bytes.NewBufferString(`{"limit_count":10}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *InboxLimitHandlerTestSuite) TestDelete_BadRequest_InvalidID() {
|
|
r := gin.New()
|
|
r.DELETE("/api/v1/accounts/:account_id/inboxes/:inbox_id/inbox_limits/:id", s.handler.Delete)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/inboxes/%d/inbox_limits/abc", s.account.ID, s.inbox.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *InboxLimitHandlerTestSuite) TestCreate_Success() {
|
|
r := gin.New()
|
|
r.POST("/api/v1/accounts/:account_id/inboxes/:inbox_id/inbox_limits", s.handler.Create)
|
|
|
|
w := httptest.NewRecorder()
|
|
body := `{"type":"max_conversations","value":100}`
|
|
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/inboxes/%d/inbox_limits", s.account.ID, s.inbox.ID), bytes.NewBufferString(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
}
|
|
|
|
func (s *InboxLimitHandlerTestSuite) TestUpdate_Success() {
|
|
limit := &model.InboxLimit{InboxID: s.inbox.ID, Type: "max_conversations", Value: 50}
|
|
s.Require().NoError(s.db.Create(limit).Error)
|
|
|
|
r := gin.New()
|
|
r.PUT("/api/v1/accounts/:account_id/inboxes/:inbox_id/inbox_limits/:id", s.handler.Update)
|
|
|
|
w := httptest.NewRecorder()
|
|
body := `{"type":"max_conversations","value":200}`
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/api/v1/accounts/%d/inboxes/%d/inbox_limits/%d", s.account.ID, s.inbox.ID, limit.ID), bytes.NewBufferString(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
}
|
|
|
|
func (s *InboxLimitHandlerTestSuite) TestDelete_Success() {
|
|
limit := &model.InboxLimit{InboxID: s.inbox.ID, Type: "max_conversations", Value: 50}
|
|
s.Require().NoError(s.db.Create(limit).Error)
|
|
|
|
r := gin.New()
|
|
r.DELETE("/api/v1/accounts/:account_id/inboxes/:inbox_id/inbox_limits/:id", s.handler.Delete)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/inboxes/%d/inbox_limits/%d", s.account.ID, s.inbox.ID, limit.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
}
|