316 lines
11 KiB
Go
316 lines
11 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/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 AgentBotHandlerTestSuite struct {
|
|
suite.Suite
|
|
db *gorm.DB
|
|
handler *AgentBotHandler
|
|
|
|
account *model.Account
|
|
user *model.User
|
|
}
|
|
|
|
func (s *AgentBotHandlerTestSuite) 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.AgentBot{}, &model.AgentBotInbox{}))
|
|
s.db = db
|
|
|
|
repo := repository.NewAgentBotRepo(db)
|
|
svc := service.NewAgentBotService(repo)
|
|
s.handler = NewAgentBotHandler(svc)
|
|
|
|
s.account = &model.Account{Name: "test-agent-bot-account"}
|
|
s.db.Create(s.account)
|
|
s.user = &model.User{Name: "test-agent-bot-user", Email: "bot@example.com"}
|
|
s.db.Create(s.user)
|
|
}
|
|
|
|
func (s *AgentBotHandlerTestSuite) TearDownSuite() {
|
|
if s.db != nil {
|
|
sqlDB, _ := s.db.DB()
|
|
sqlDB.Close()
|
|
}
|
|
}
|
|
|
|
func TestAgentBotHandlerSuite(t *testing.T) {
|
|
suite.Run(t, new(AgentBotHandlerTestSuite))
|
|
}
|
|
|
|
func (s *AgentBotHandlerTestSuite) TestPlatformList() {
|
|
r := gin.New()
|
|
r.GET("/platform/api/v1/agent_bots", s.handler.PlatformList)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/platform/api/v1/agent_bots", nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
}
|
|
|
|
func (s *AgentBotHandlerTestSuite) TestPlatformCreate_Success() {
|
|
r := gin.New()
|
|
r.POST("/platform/api/v1/agent_bots", s.handler.PlatformCreate)
|
|
|
|
body := map[string]interface{}{
|
|
"name": "test-global-bot",
|
|
"bot_type": "webhook",
|
|
"outgoing_url": "https://example.com/webhook",
|
|
}
|
|
b, _ := json.Marshal(body)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/platform/api/v1/agent_bots", bytes.NewBuffer(b))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusCreated, w.Code)
|
|
}
|
|
|
|
func (s *AgentBotHandlerTestSuite) TestPlatformCreate_BadRequest() {
|
|
r := gin.New()
|
|
r.POST("/platform/api/v1/agent_bots", s.handler.PlatformCreate)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/platform/api/v1/agent_bots", nil)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *AgentBotHandlerTestSuite) TestAccountList() {
|
|
r := gin.New()
|
|
r.GET("/api/v1/accounts/:account_id/agent_bots", s.handler.List)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/agent_bots", s.account.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
}
|
|
|
|
func (s *AgentBotHandlerTestSuite) TestAccountCreate_Success() {
|
|
r := gin.New()
|
|
r.POST("/api/v1/accounts/:account_id/agent_bots", s.handler.Create)
|
|
|
|
body := map[string]interface{}{
|
|
"name": "test-account-bot",
|
|
"bot_type": "webhook",
|
|
"outgoing_url": "https://example.com/webhook",
|
|
}
|
|
b, _ := json.Marshal(body)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/agent_bots", s.account.ID), bytes.NewBuffer(b))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
}
|
|
|
|
func (s *AgentBotHandlerTestSuite) TestAccountChatwootPayloadsAndRoutes() {
|
|
r := gin.New()
|
|
r.GET("/api/v1/accounts/:account_id/agent_bots", s.handler.List)
|
|
r.POST("/api/v1/accounts/:account_id/agent_bots", s.handler.Create)
|
|
r.GET("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id", s.handler.Get)
|
|
r.PATCH("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id", s.handler.Update)
|
|
r.DELETE("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id", s.handler.Delete)
|
|
r.DELETE("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id/avatar", s.handler.DeleteAvatar)
|
|
r.POST("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id/reset_access_token", s.handler.ResetToken)
|
|
r.POST("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id/reset_secret", s.handler.ResetSecret)
|
|
|
|
global := &model.AgentBot{Name: "system-bot", BotType: "webhook", OutgoingURL: "https://system.example/hook", AccessToken: "global-token", Secret: "global-secret", Config: json.RawMessage(`{"mode":"system"}`)}
|
|
s.Require().NoError(s.db.Create(global).Error)
|
|
|
|
createBody := []byte(`{"name":"chatwoot-bot","description":"before","bot_type":"webhook","outgoing_url":"https://example.com/webhook","avatar_url":"https://example.com/bot.png","bot_config":{"handoff":true}}`)
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/agent_bots", s.account.ID), bytes.NewReader(createBody))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
|
|
var created map[string]interface{}
|
|
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &created))
|
|
botID := uint(created["id"].(float64))
|
|
assert.NotContains(s.T(), created, "success")
|
|
assert.Equal(s.T(), "chatwoot-bot", created["name"])
|
|
assert.Equal(s.T(), "https://example.com/bot.png", created["thumbnail"])
|
|
assert.Equal(s.T(), "https://example.com/webhook", created["outgoing_url"])
|
|
assert.Equal(s.T(), false, created["system_bot"])
|
|
assert.NotEmpty(s.T(), created["access_token"])
|
|
assert.NotEmpty(s.T(), created["secret"])
|
|
assert.Contains(s.T(), created, "bot_config")
|
|
|
|
w = httptest.NewRecorder()
|
|
req, _ = http.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/agent_bots", s.account.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
var list []map[string]interface{}
|
|
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &list))
|
|
assert.GreaterOrEqual(s.T(), len(list), 2)
|
|
assert.NotContains(s.T(), list[0], "success")
|
|
|
|
w = httptest.NewRecorder()
|
|
updateBody := []byte(`{"description":"","outgoing_url":"","bot_config":{"handoff":false}}`)
|
|
req, _ = http.NewRequest(http.MethodPatch, fmt.Sprintf("/api/v1/accounts/%d/agent_bots/%d", s.account.ID, botID), bytes.NewReader(updateBody))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
var updated map[string]interface{}
|
|
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &updated))
|
|
assert.Equal(s.T(), "", updated["description"])
|
|
assert.Equal(s.T(), "", updated["outgoing_url"])
|
|
|
|
w = httptest.NewRecorder()
|
|
req, _ = http.NewRequest(http.MethodPatch, fmt.Sprintf("/api/v1/accounts/%d/agent_bots/%d", s.account.ID, global.ID), bytes.NewReader([]byte(`{"name":"blocked"}`)))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
assert.Equal(s.T(), http.StatusNotFound, w.Code)
|
|
|
|
oldToken := created["access_token"]
|
|
w = httptest.NewRecorder()
|
|
req, _ = http.NewRequest(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/agent_bots/%d/reset_access_token", s.account.ID, botID), nil)
|
|
r.ServeHTTP(w, req)
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
var resetToken map[string]interface{}
|
|
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &resetToken))
|
|
assert.Equal(s.T(), float64(botID), resetToken["id"])
|
|
assert.NotEqual(s.T(), oldToken, resetToken["access_token"])
|
|
assert.NotContains(s.T(), resetToken, "data")
|
|
|
|
oldSecret := resetToken["secret"]
|
|
w = httptest.NewRecorder()
|
|
req, _ = http.NewRequest(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/agent_bots/%d/reset_secret", s.account.ID, botID), nil)
|
|
r.ServeHTTP(w, req)
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
var resetSecret map[string]interface{}
|
|
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &resetSecret))
|
|
assert.NotEqual(s.T(), oldSecret, resetSecret["secret"])
|
|
|
|
w = httptest.NewRecorder()
|
|
req, _ = http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/v1/accounts/%d/agent_bots/%d/avatar", s.account.ID, botID), nil)
|
|
r.ServeHTTP(w, req)
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
var avatar map[string]interface{}
|
|
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &avatar))
|
|
assert.Equal(s.T(), "", avatar["thumbnail"])
|
|
|
|
w = httptest.NewRecorder()
|
|
req, _ = http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/v1/accounts/%d/agent_bots/%d", s.account.ID, botID), nil)
|
|
r.ServeHTTP(w, req)
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
assert.Empty(s.T(), w.Body.String())
|
|
}
|
|
|
|
func (s *AgentBotHandlerTestSuite) TestAccountCreate_BadRequest() {
|
|
r := gin.New()
|
|
r.POST("/api/v1/accounts/:account_id/agent_bots", s.handler.Create)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/agent_bots", 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 *AgentBotHandlerTestSuite) TestAccountGet_NotFound() {
|
|
r := gin.New()
|
|
r.GET("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id", s.handler.Get)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/agent_bots/99999", s.account.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusNotFound, w.Code)
|
|
}
|
|
|
|
func (s *AgentBotHandlerTestSuite) TestPlatformGet_NotFound() {
|
|
r := gin.New()
|
|
r.GET("/platform/api/v1/agent_bots/:agent_bot_id", s.handler.PlatformGet)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/platform/api/v1/agent_bots/99999", nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusNotFound, w.Code)
|
|
}
|
|
|
|
func (s *AgentBotHandlerTestSuite) TestUpdate_BadRequest() {
|
|
r := gin.New()
|
|
r.PUT("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id", s.handler.Update)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/api/v1/accounts/%d/agent_bots/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 *AgentBotHandlerTestSuite) TestDelete_BadRequest() {
|
|
r := gin.New()
|
|
r.DELETE("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id", s.handler.Delete)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/agent_bots/abc", s.account.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *AgentBotHandlerTestSuite) TestResetToken_BadRequest() {
|
|
r := gin.New()
|
|
r.POST("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id/reset_token", s.handler.ResetToken)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/agent_bots/abc/reset_token", s.account.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *AgentBotHandlerTestSuite) TestResetSecret_BadRequest() {
|
|
r := gin.New()
|
|
r.POST("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id/reset_secret", s.handler.ResetSecret)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/agent_bots/abc/reset_secret", s.account.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *AgentBotHandlerTestSuite) TestDeleteAvatar_BadRequest() {
|
|
r := gin.New()
|
|
r.DELETE("/api/v1/accounts/:account_id/agent_bots/:agent_bot_id/avatar", s.handler.DeleteAvatar)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/agent_bots/abc/avatar", s.account.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|