feat(settings): align agents teams payloads

This commit is contained in:
2026-06-05 23:42:21 +08:00
parent f8263a3402
commit dc09036676
10 changed files with 340 additions and 162 deletions
+38 -71
View File
@@ -7,7 +7,6 @@ import (
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
"github.com/gin-gonic/gin"
@@ -78,15 +77,7 @@ func (s *AgentHandlerTestSuite) makeRequest(method, path string, body interface{
var bodyBytes []byte
if body != nil {
// Chatwoot params.require(:agent) → wrap body under "agent" key for POST/PUT/PATCH
// Exception: bulk_create does NOT use params.require
needsWrap := (method == "POST" || method == "PUT" || method == "PATCH") && !strings.Contains(path, "bulk_create")
if needsWrap {
wrapped := map[string]interface{}{"agent": body}
bodyBytes, _ = json.Marshal(wrapped)
} else {
bodyBytes, _ = json.Marshal(body)
}
bodyBytes, _ = json.Marshal(body)
}
c.Request = httptest.NewRequest(method, path, bytes.NewReader(bodyBytes))
if body != nil {
@@ -117,29 +108,30 @@ func (s *AgentHandlerTestSuite) TestListEmpty() {
s.handler.List(c)
assert.Equal(s.T(), http.StatusOK, w.Code)
var resp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &resp)
assert.True(s.T(), resp["success"].(bool))
data := resp["data"].([]interface{})
var data []interface{}
assert.NoError(s.T(), json.Unmarshal(w.Body.Bytes(), &data))
assert.Equal(s.T(), 0, len(data))
}
func (s *AgentHandlerTestSuite) TestCreateAgent() {
customRoleID := uint(7)
req := service.CreateAgentRequest{
Email: "agent1@test.com",
Name: "Agent One",
Role: "agent",
Email: "agent1@test.com",
Name: "Agent One",
Role: "agent",
CustomRoleID: &customRoleID,
}
w, c := s.makeRequest("POST", "/api/v1/accounts/1/agents", req, s.account.ID, s.user.ID)
s.handler.Create(c)
assert.Equal(s.T(), http.StatusCreated, w.Code)
var resp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &resp)
assert.True(s.T(), resp["success"].(bool))
data := resp["data"].(map[string]interface{})
assert.Equal(s.T(), http.StatusOK, w.Code)
var data map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &data)
assert.Equal(s.T(), "agent1@test.com", data["email"])
assert.Equal(s.T(), "Agent One", data["name"])
assert.Equal(s.T(), "offline", data["availability_status"])
assert.Equal(s.T(), float64(7), data["custom_role_id"])
assert.Contains(s.T(), data, "confirmed")
}
func (s *AgentHandlerTestSuite) TestCreateAgentDuplicate() {
@@ -151,7 +143,7 @@ func (s *AgentHandlerTestSuite) TestCreateAgentDuplicate() {
}
w, c := s.makeRequest("POST", "/api/v1/accounts/1/agents", req, s.account.ID, s.user.ID)
s.handler.Create(c)
assert.Equal(s.T(), http.StatusCreated, w.Code)
assert.Equal(s.T(), http.StatusOK, w.Code)
// Try creating again — should conflict
w2, c2 := s.makeRequest("POST", "/api/v1/accounts/1/agents", req, s.account.ID, s.user.ID)
@@ -178,11 +170,10 @@ func (s *AgentHandlerTestSuite) TestGetAgent() {
}
w, c := s.makeRequest("POST", "/api/v1/accounts/1/agents", req, s.account.ID, s.user.ID)
s.handler.Create(c)
assert.Equal(s.T(), http.StatusCreated, w.Code)
assert.Equal(s.T(), http.StatusOK, w.Code)
var createResp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &createResp)
data := createResp["data"].(map[string]interface{})
var data map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &data)
agentID := uint(data["id"].(float64))
// Get the agent
@@ -191,10 +182,8 @@ func (s *AgentHandlerTestSuite) TestGetAgent() {
s.handler.Get(c2)
assert.Equal(s.T(), http.StatusOK, w2.Code)
var getResp map[string]interface{}
json.Unmarshal(w2.Body.Bytes(), &getResp)
assert.True(s.T(), getResp["success"].(bool))
getData := getResp["data"].(map[string]interface{})
var getData map[string]interface{}
json.Unmarshal(w2.Body.Bytes(), &getData)
assert.Equal(s.T(), "agent3@test.com", getData["email"])
}
@@ -213,17 +202,16 @@ func (s *AgentHandlerTestSuite) TestUpdateAgent() {
}
w, c := s.makeRequest("POST", "/api/v1/accounts/1/agents", req, s.account.ID, s.user.ID)
s.handler.Create(c)
assert.Equal(s.T(), http.StatusCreated, w.Code)
assert.Equal(s.T(), http.StatusOK, w.Code)
var createResp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &createResp)
data := createResp["data"].(map[string]interface{})
var data map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &data)
agentID := uint(data["id"].(float64))
// Update the agent
updateReq := service.UpdateAgentRequest{
Name: "Updated Name",
Role: "administrator",
Name: "Updated Name",
Role: "administrator",
Availability: "online",
}
w2, c2 := s.makeRequest("PUT", fmt.Sprintf("/api/v1/accounts/1/agents/%d", agentID), updateReq, s.account.ID, s.user.ID)
@@ -231,13 +219,11 @@ func (s *AgentHandlerTestSuite) TestUpdateAgent() {
s.handler.Update(c2)
assert.Equal(s.T(), http.StatusOK, w2.Code)
var updateResp map[string]interface{}
json.Unmarshal(w2.Body.Bytes(), &updateResp)
assert.True(s.T(), updateResp["success"].(bool))
updateData := updateResp["data"].(map[string]interface{})
var updateData map[string]interface{}
json.Unmarshal(w2.Body.Bytes(), &updateData)
assert.Equal(s.T(), "Updated Name", updateData["name"])
assert.Equal(s.T(), "administrator", updateData["role"])
assert.Equal(s.T(), "online", updateData["availability"])
assert.Equal(s.T(), "online", updateData["availability_status"])
}
func (s *AgentHandlerTestSuite) TestDeleteAgent() {
@@ -249,11 +235,10 @@ func (s *AgentHandlerTestSuite) TestDeleteAgent() {
}
w, c := s.makeRequest("POST", "/api/v1/accounts/1/agents", req, s.account.ID, s.user.ID)
s.handler.Create(c)
assert.Equal(s.T(), http.StatusCreated, w.Code)
assert.Equal(s.T(), http.StatusOK, w.Code)
var createResp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &createResp)
data := createResp["data"].(map[string]interface{})
var data map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &data)
agentID := uint(data["id"].(float64))
// Delete the agent
@@ -278,18 +263,7 @@ func (s *AgentHandlerTestSuite) TestBulkCreate() {
s.handler.BulkCreate(c)
assert.Equal(s.T(), http.StatusOK, w.Code)
var resp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &resp)
assert.True(s.T(), resp["success"].(bool))
data := resp["data"].([]interface{})
assert.Equal(s.T(), 3, len(data))
// Verify emails
for i, agent := range data {
agentMap := agent.(map[string]interface{})
expectedEmail := fmt.Sprintf("bulk%d@test.com", i+1)
assert.Equal(s.T(), expectedEmail, agentMap["email"])
}
assert.Empty(s.T(), w.Body.String())
}
func (s *AgentHandlerTestSuite) TestBulkCreateValidation() {
@@ -310,7 +284,7 @@ func (s *AgentHandlerTestSuite) TestBulkCreateSkipsDuplicates() {
}
w, c := s.makeRequest("POST", "/api/v1/accounts/1/agents", preReq, s.account.ID, s.user.ID)
s.handler.Create(c)
assert.Equal(s.T(), http.StatusCreated, w.Code)
assert.Equal(s.T(), http.StatusOK, w.Code)
// Bulk create with a duplicate + a new one
req := service.BulkCreateAgentRequest{
@@ -320,13 +294,7 @@ func (s *AgentHandlerTestSuite) TestBulkCreateSkipsDuplicates() {
s.handler.BulkCreate(c2)
assert.Equal(s.T(), http.StatusOK, w2.Code)
var resp map[string]interface{}
json.Unmarshal(w2.Body.Bytes(), &resp)
data := resp["data"].([]interface{})
// Only the new one should be in results (existing skipped silently)
assert.Equal(s.T(), 1, len(data))
newAgent := data[0].(map[string]interface{})
assert.Equal(s.T(), "new@test.com", newAgent["email"])
assert.Empty(s.T(), w2.Body.String())
}
func (s *AgentHandlerTestSuite) TestListAfterCreate() {
@@ -339,7 +307,7 @@ func (s *AgentHandlerTestSuite) TestListAfterCreate() {
}
w, c := s.makeRequest("POST", "/api/v1/accounts/1/agents", req, s.account.ID, s.user.ID)
s.handler.Create(c)
assert.Equal(s.T(), http.StatusCreated, w.Code)
assert.Equal(s.T(), http.StatusOK, w.Code)
}
// List agents
@@ -347,9 +315,8 @@ func (s *AgentHandlerTestSuite) TestListAfterCreate() {
s.handler.List(c)
assert.Equal(s.T(), http.StatusOK, w.Code)
var resp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &resp)
data := resp["data"].([]interface{})
var data []interface{}
json.Unmarshal(w.Body.Bytes(), &data)
assert.Equal(s.T(), 2, len(data))
}
@@ -360,4 +327,4 @@ func (s *AgentHandlerTestSuite) TestListNoAccountID() {
// No account_id set
s.handler.List(c)
assert.Equal(s.T(), http.StatusUnauthorized, w.Code)
}
}