Files
gochat/internal/handler/api/v1/slack_integration_handler_test.go
T
2026-06-04 15:44:48 +08:00

120 lines
3.4 KiB
Go

package v1
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func setupSlackIntegrationRouter() *gin.Engine {
gin.SetMode(gin.TestMode)
r := gin.New()
r.RedirectTrailingSlash = false
handler := NewSlackIntegrationHandler(nil)
integrations := r.Group("/api/v1/accounts/:account_id/integrations")
RegisterSlackIntegrationRoutes(integrations, handler)
return r
}
// ========================================
// SlackIntegration — param validation tests
// ========================================
func TestSlackIntegration_Create_BadAccountID(t *testing.T) {
r := setupSlackIntegrationRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/integrations/slack/", nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
var resp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &resp)
errBody := resp["error"].(map[string]interface{})
assert.Contains(t, errBody["message"], "invalid account_id")
}
func TestSlackIntegration_Create_InvalidJSON(t *testing.T) {
r := setupSlackIntegrationRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/integrations/slack/", bytes.NewReader([]byte("invalid json")))
req.Header.Set("Content-Type", "application/json")
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
var resp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &resp)
assert.False(t, resp["success"].(bool))
}
func TestSlackIntegration_Update_BadAccountID(t *testing.T) {
r := setupSlackIntegrationRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("PATCH", "/api/v1/accounts/abc/integrations/slack/", nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
var resp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &resp)
errBody := resp["error"].(map[string]interface{})
assert.Contains(t, errBody["message"], "invalid account_id")
}
func TestSlackIntegration_Update_InvalidJSON(t *testing.T) {
r := setupSlackIntegrationRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("PATCH", "/api/v1/accounts/1/integrations/slack/", bytes.NewReader([]byte("invalid json")))
req.Header.Set("Content-Type", "application/json")
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
var resp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &resp)
assert.False(t, resp["success"].(bool))
}
func TestSlackIntegration_Delete_BadAccountID(t *testing.T) {
r := setupSlackIntegrationRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("DELETE", "/api/v1/accounts/abc/integrations/slack/", nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
var resp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &resp)
errBody := resp["error"].(map[string]interface{})
assert.Contains(t, errBody["message"], "invalid account_id")
}
func TestSlackIntegration_ListAllChannels_BadAccountID(t *testing.T) {
r := setupSlackIntegrationRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/integrations/slack/list_all_channels", nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
var resp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &resp)
errBody := resp["error"].(map[string]interface{})
assert.Contains(t, errBody["message"], "invalid account_id")
}