feat(agent-bots): align account payloads

This commit is contained in:
2026-06-06 06:03:42 +08:00
parent 689c79bb9c
commit 1adf1c9c31
9 changed files with 394 additions and 75 deletions
@@ -74,8 +74,8 @@ func (s *AgentBotHandlerTestSuite) TestPlatformCreate_Success() {
r.POST("/platform/api/v1/agent_bots", s.handler.PlatformCreate)
body := map[string]interface{}{
"name": "test-global-bot",
"bot_type": "webhook",
"name": "test-global-bot",
"bot_type": "webhook",
"outgoing_url": "https://example.com/webhook",
}
b, _ := json.Marshal(body)
@@ -116,8 +116,8 @@ func (s *AgentBotHandlerTestSuite) TestAccountCreate_Success() {
r.POST("/api/v1/accounts/:account_id/agent_bots", s.handler.Create)
body := map[string]interface{}{
"name": "test-account-bot",
"bot_type": "webhook",
"name": "test-account-bot",
"bot_type": "webhook",
"outgoing_url": "https://example.com/webhook",
}
b, _ := json.Marshal(body)
@@ -127,7 +127,101 @@ func (s *AgentBotHandlerTestSuite) TestAccountCreate_Success() {
req.Header.Set("Content-Type", "application/json")
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusCreated, w.Code)
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() {
@@ -218,4 +312,4 @@ func (s *AgentBotHandlerTestSuite) TestDeleteAvatar_BadRequest() {
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
}
}