Align GoChat with Chatwoot frontend contracts

This commit is contained in:
2026-06-13 22:13:32 +08:00
parent 71abf58636
commit d884fdda0a
162 changed files with 9825 additions and 528 deletions
@@ -3,8 +3,10 @@ package v1
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
@@ -19,6 +21,16 @@ import (
"github.com/gochat/gochat/internal/service"
)
type slackHandlerRoundTripFunc func(*http.Request) (*http.Response, error)
func (f slackHandlerRoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
func slackHandlerJSONResponse(body string) *http.Response {
return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body))}
}
func setupSlackIntegrationRouter() *gin.Engine {
gin.SetMode(gin.TestMode)
r := gin.New()
@@ -189,6 +201,35 @@ func TestSlackIntegration_Delete_NoTrailingSlash_ReturnsEmptyOK(t *testing.T) {
assert.Empty(t, w.Body.String())
}
func TestSlackIntegration_ListAllChannels_NoTrailingSlash_ReturnsChannelArray(t *testing.T) {
db, accountID := setupSlackIntegrationHandlerDB(t)
require.NoError(t, db.Create(&model.IntegrationHook{AccountID: accountID, AppID: "slack", HookType: model.HookTypeSlack, AccessToken: "xoxb-handler-token"}).Error)
svc := service.NewSlackIntegrationService(repository.NewIntegrationHookRepo(db), service.WithSlackHTTPClient("https://slack.test/api", &http.Client{Transport: slackHandlerRoundTripFunc(func(req *http.Request) (*http.Response, error) {
require.Equal(t, "/api/conversations.list", req.URL.Path)
require.Equal(t, "Bearer xoxb-handler-token", req.Header.Get("Authorization"))
if req.URL.Query().Get("types") == "private_channel" {
return slackHandlerJSONResponse(`{"ok":true,"channels":[{"id":"G1","name":"private-room","is_private":true}],"response_metadata":{"next_cursor":""}}`), nil
}
return slackHandlerJSONResponse(`{"ok":true,"channels":[{"id":"C1","name":"support","is_private":false}],"response_metadata":{"next_cursor":""}}`), nil
})}))
r := setupSlackIntegrationRouterWithService(svc)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/integrations/slack/list_all_channels", nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
var resp []map[string]interface{}
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &resp))
require.Len(t, resp, 2)
assert.Equal(t, "G1", resp[0]["id"])
assert.Equal(t, "private-room", resp[0]["name"])
assert.Equal(t, true, resp[0]["is_private"])
assert.Equal(t, "C1", resp[1]["id"])
assert.Equal(t, "support", resp[1]["name"])
assert.Equal(t, false, resp[1]["is_private"])
}
func TestSlackIntegration_Update_PutNoTrailingSlash_BadAccountID(t *testing.T) {
r := setupSlackIntegrationRouter()