Align GoChat with Chatwoot frontend contracts
This commit is contained in:
@@ -62,6 +62,7 @@ type MessageHandlerTestSuite struct {
|
||||
testContact *model.Contact
|
||||
testConv *model.Conversation
|
||||
testMessage *model.Message
|
||||
testUser *model.User
|
||||
mockLLM *mockMsgHandlerLLMProvider
|
||||
dispatcher *channel.Dispatcher
|
||||
}
|
||||
@@ -78,6 +79,7 @@ func (s *MessageHandlerTestSuite) SetupSuite() {
|
||||
// AutoMigrate all required models
|
||||
err = db.AutoMigrate(
|
||||
&model.Account{},
|
||||
&model.User{},
|
||||
&model.Inbox{},
|
||||
&model.Contact{},
|
||||
&model.ContactInbox{},
|
||||
@@ -111,7 +113,11 @@ func (s *MessageHandlerTestSuite) SetupSuite() {
|
||||
|
||||
// Auth middleware: inject user_id into context for all message routes
|
||||
r.Use(func(c *gin.Context) {
|
||||
c.Set("user_id", uint(1))
|
||||
userID := uint(1)
|
||||
if s.testUser != nil {
|
||||
userID = s.testUser.ID
|
||||
}
|
||||
c.Set("user_id", userID)
|
||||
c.Next()
|
||||
})
|
||||
|
||||
@@ -146,6 +152,10 @@ func (s *MessageHandlerTestSuite) SetupTest() {
|
||||
s.Require().NoError(s.db.Create(account).Error)
|
||||
s.testAccount = account
|
||||
|
||||
user := &model.User{AccountID: account.ID, Name: "Msg Handler Agent", DisplayName: "Message Agent", Email: "message-agent@example.com", Provider: "email", PubsubToken: "pubsub-message-agent"}
|
||||
s.Require().NoError(s.db.Create(user).Error)
|
||||
s.testUser = user
|
||||
|
||||
inbox := &model.Inbox{AccountID: account.ID, Name: "MsgHandlerTestInbox", ChannelType: "web_widget", ChannelID: 1}
|
||||
s.Require().NoError(s.db.Create(inbox).Error)
|
||||
s.testInbox = inbox
|
||||
@@ -200,6 +210,7 @@ func (s *MessageHandlerTestSuite) TearDownTest() {
|
||||
s.db.Exec("DELETE FROM contact_inboxes")
|
||||
s.db.Exec("DELETE FROM contacts")
|
||||
s.db.Exec("DELETE FROM inbox_members")
|
||||
s.db.Exec("DELETE FROM users")
|
||||
s.db.Exec("DELETE FROM inboxes")
|
||||
s.db.Exec("DELETE FROM accounts")
|
||||
}
|
||||
@@ -387,11 +398,25 @@ func (s *MessageHandlerTestSuite) TestCreate_ChatwootFrontendPayloadDefaultsOutg
|
||||
var resp map[string]interface{}
|
||||
json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
assert.Equal(s.T(), "Frontend payload", resp["content"])
|
||||
assert.Equal(s.T(), float64(s.testAccount.ID), resp["account_id"])
|
||||
assert.Equal(s.T(), float64(s.testInbox.ID), resp["inbox_id"])
|
||||
assert.Equal(s.T(), float64(*s.testConv.DisplayID), resp["conversation_id"])
|
||||
assert.Equal(s.T(), true, resp["private"])
|
||||
assert.Equal(s.T(), "tmp-123", resp["echo_id"])
|
||||
assert.Equal(s.T(), float64(1), resp["message_type"])
|
||||
assert.Equal(s.T(), "text", resp["content_type"])
|
||||
assert.NotNil(s.T(), resp["content_attributes"])
|
||||
assert.Equal(s.T(), "sent", resp["status"])
|
||||
assert.Equal(s.T(), "", resp["source_id"])
|
||||
assert.NotContains(s.T(), resp, "success")
|
||||
contentAttrs, ok := resp["content_attributes"].(map[string]interface{})
|
||||
s.Require().True(ok)
|
||||
assert.Equal(s.T(), []interface{}{}, contentAttrs["submitted_values"])
|
||||
sender, ok := resp["sender"].(map[string]interface{})
|
||||
s.Require().True(ok)
|
||||
assert.Equal(s.T(), float64(s.testUser.ID), sender["id"])
|
||||
assert.Equal(s.T(), "Msg Handler Agent", sender["name"])
|
||||
assert.Equal(s.T(), "Message Agent", sender["available_name"])
|
||||
assert.Equal(s.T(), "message-agent@example.com", sender["email"])
|
||||
}
|
||||
|
||||
func (s *MessageHandlerTestSuite) TestCreate_MultipartAttachmentPersistsAndSerializes() {
|
||||
@@ -524,7 +549,7 @@ func (s *MessageHandlerTestSuite) TestUpdate_StatusExternalError() {
|
||||
body, _ := json.Marshal(payload)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
url := msgDetailURL(s.testAccount.ID, s.testConv.ID, s.testMessage.ID)
|
||||
url := msgDetailURL(s.testAccount.ID, *s.testConv.DisplayID, s.testMessage.ID)
|
||||
req, _ := http.NewRequest("PATCH", url, bytes.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
s.router.ServeHTTP(w, req)
|
||||
@@ -542,7 +567,7 @@ func (s *MessageHandlerTestSuite) TestUpdate_StatusForbiddenForNonAPIInbox() {
|
||||
body, _ := json.Marshal(payload)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
url := msgDetailURL(s.testAccount.ID, s.testConv.ID, s.testMessage.ID)
|
||||
url := msgDetailURL(s.testAccount.ID, *s.testConv.DisplayID, s.testMessage.ID)
|
||||
req, _ := http.NewRequest("PATCH", url, bytes.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
s.router.ServeHTTP(w, req)
|
||||
@@ -572,7 +597,7 @@ func (s *MessageHandlerTestSuite) TestDelete_Success() {
|
||||
s.Require().NoError(s.db.Create(attachment).Error)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
url := msgDetailURL(s.testAccount.ID, s.testConv.ID, s.testMessage.ID)
|
||||
url := msgDetailURL(s.testAccount.ID, *s.testConv.DisplayID, s.testMessage.ID)
|
||||
req, _ := http.NewRequest("DELETE", url, nil)
|
||||
s.router.ServeHTTP(w, req)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user