108 lines
2.8 KiB
Go
108 lines
2.8 KiB
Go
package v1
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gochat/gochat/internal/automation"
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/suite"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
type testMacroDBProvider struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func (p *testMacroDBProvider) DB() *gorm.DB { return p.db }
|
|
|
|
type MacroHandlerTestSuite struct {
|
|
suite.Suite
|
|
db *gorm.DB
|
|
handler *MacroHandler
|
|
|
|
account *model.Account
|
|
user *model.User
|
|
}
|
|
|
|
func (s *MacroHandlerTestSuite) 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{}, &automation.Macro{}))
|
|
s.db = db
|
|
|
|
macroSvc := automation.NewMacroService(&testMacroDBProvider{db: db})
|
|
s.handler = NewMacroHandler(macroSvc)
|
|
|
|
s.account = &model.Account{Name: "test-macro-account"}
|
|
s.db.Create(s.account)
|
|
s.user = &model.User{Name: "test-macro-user", Email: "macro@example.com"}
|
|
s.db.Create(s.user)
|
|
}
|
|
|
|
func (s *MacroHandlerTestSuite) TearDownSuite() {
|
|
if s.db != nil {
|
|
sqlDB, _ := s.db.DB()
|
|
sqlDB.Close()
|
|
}
|
|
}
|
|
|
|
func TestMacroHandlerSuite(t *testing.T) {
|
|
suite.Run(t, new(MacroHandlerTestSuite))
|
|
}
|
|
|
|
func (s *MacroHandlerTestSuite) TestList_Empty() {
|
|
r := gin.New()
|
|
r.GET("/api/v1/accounts/:account_id/macros", s.handler.List)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/macros", s.account.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
}
|
|
|
|
func (s *MacroHandlerTestSuite) TestCreate_Success() {
|
|
r := gin.New()
|
|
r.POST("/api/v1/accounts/:account_id/macros", func(c *gin.Context) {
|
|
c.Set("user_id", float64(s.user.ID))
|
|
c.Next()
|
|
}, s.handler.Create)
|
|
|
|
body := map[string]interface{}{
|
|
"name": "test-macro",
|
|
"visibility": 0,
|
|
"actions": []map[string]interface{}{{"action_name": "assign_agent", "action_params": map[string]interface{}{"agent_id": "1"}}},
|
|
}
|
|
b, _ := json.Marshal(body)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/macros", 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 *MacroHandlerTestSuite) TestCreate_BadRequest() {
|
|
r := gin.New()
|
|
r.POST("/api/v1/accounts/:account_id/macros", s.handler.Create)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/macros", s.account.ID), nil)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
} |