second commit
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
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 InboxCsatTemplateHandlerTestSuite struct {
|
||||
suite.Suite
|
||||
db *gorm.DB
|
||||
handler *InboxCsatTemplateHandler
|
||||
account *model.Account
|
||||
inbox *model.Inbox
|
||||
}
|
||||
|
||||
func (s *InboxCsatTemplateHandlerTestSuite) 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.CsatTemplate{}))
|
||||
s.db = db
|
||||
|
||||
repo := repository.NewCsatTemplateRepo(db)
|
||||
svc := service.NewCsatTemplateService(repo)
|
||||
s.handler = NewInboxCsatTemplateHandler(svc)
|
||||
|
||||
s.account = &model.Account{Name: "test-csat-template-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 *InboxCsatTemplateHandlerTestSuite) TearDownSuite() {
|
||||
if s.db != nil {
|
||||
sqlDB, _ := s.db.DB()
|
||||
sqlDB.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboxCsatTemplateHandlerSuite(t *testing.T) {
|
||||
suite.Run(t, new(InboxCsatTemplateHandlerTestSuite))
|
||||
}
|
||||
|
||||
func (s *InboxCsatTemplateHandlerTestSuite) TestShow_BadRequest_InvalidInboxID() {
|
||||
r := gin.New()
|
||||
r.GET("/api/v1/accounts/:account_id/inboxes/:inbox_id/csat_template", s.handler.Show)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/inboxes/abc/csat_template", s.account.ID), nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func (s *InboxCsatTemplateHandlerTestSuite) TestShow_Success() {
|
||||
r := gin.New()
|
||||
r.GET("/api/v1/accounts/:account_id/inboxes/:inbox_id/csat_template", s.handler.Show)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/inboxes/%d/csat_template", s.account.ID, s.inbox.ID), nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
func (s *InboxCsatTemplateHandlerTestSuite) TestCreate_BadRequest_InvalidInboxID() {
|
||||
r := gin.New()
|
||||
r.POST("/api/v1/accounts/:account_id/inboxes/:inbox_id/csat_template", s.handler.Create)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/inboxes/abc/csat_template", s.account.ID), bytes.NewBufferString(`{"questions":[{"question":"How was your experience?","type":"text"}]}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func (s *InboxCsatTemplateHandlerTestSuite) TestAnalyze_BadRequest_InvalidInboxID() {
|
||||
r := gin.New()
|
||||
r.POST("/api/v1/accounts/:account_id/inboxes/:inbox_id/csat_template/analyze", s.handler.Analyze)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/inboxes/abc/csat_template/analyze", s.account.ID), bytes.NewBufferString(`{"message":"Great service!"}`))
|
||||
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