feat(agents): align update validation errors

This commit is contained in:
2026-06-06 02:41:31 +08:00
parent ec1bb257dc
commit 0ebaa51cbd
4 changed files with 54 additions and 4 deletions
+7
View File
@@ -149,6 +149,13 @@ func (h *AgentHandler) Update(c *gin.Context) {
agent, svcErr := h.svc.Update(c.Request.Context(), uint(id), accountID, req)
if svcErr != nil {
if errors.Is(svcErr, service.ErrAgentNameBlank) {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"message": "Name can't be blank",
"attributes": []string{"name"},
})
return
}
applogger.L().Errorf("Update agent %d for account %d: %v", id, accountID, svcErr)
handleServiceError(c, svcErr)
return
@@ -293,6 +293,39 @@ func (s *AgentHandlerTestSuite) TestUpdateAgent() {
assert.Equal(s.T(), false, disableData["auto_offline"])
}
func (s *AgentHandlerTestSuite) TestUpdateAgentBlankNameReturnsRecordInvalidShape() {
req := service.CreateAgentRequest{
Email: "blank-update@test.com",
Name: "Blank Update",
Role: "agent",
}
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.StatusOK, w.Code)
var created map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &created)
agentID := uint(created["id"].(float64))
updateReq := map[string]any{
"agent": map[string]any{
"name": "",
},
}
w2, c2 := s.makeRequest("PATCH", fmt.Sprintf("/api/v1/accounts/1/agents/%d", agentID), updateReq, s.account.ID, s.user.ID)
s.handler.Update(c2)
assert.Equal(s.T(), http.StatusUnprocessableEntity, w2.Code, w2.Body.String())
var data map[string]interface{}
json.Unmarshal(w2.Body.Bytes(), &data)
assert.Equal(s.T(), "Name can't be blank", data["message"])
assert.Equal(s.T(), []interface{}{"name"}, data["attributes"])
var user model.User
s.Require().NoError(s.db.First(&user, agentID).Error)
assert.Equal(s.T(), "Blank Update", user.Name)
}
func (s *AgentHandlerTestSuite) TestDeleteAgent() {
// Create an agent
req := service.CreateAgentRequest{