feat(integrations): align slack parity
This commit is contained in:
@@ -9,6 +9,14 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
"github.com/gochat/gochat/internal/repository"
|
||||
"github.com/gochat/gochat/internal/service"
|
||||
)
|
||||
|
||||
func setupSlackIntegrationRouter() *gin.Engine {
|
||||
@@ -24,6 +32,31 @@ func setupSlackIntegrationRouter() *gin.Engine {
|
||||
return r
|
||||
}
|
||||
|
||||
func setupSlackIntegrationRouterWithService(svc *service.SlackIntegrationService) *gin.Engine {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.RedirectTrailingSlash = false
|
||||
|
||||
handler := NewSlackIntegrationHandler(svc)
|
||||
integrations := r.Group("/api/v1/accounts/:account_id/integrations")
|
||||
RegisterSlackIntegrationRoutes(integrations, handler)
|
||||
return r
|
||||
}
|
||||
|
||||
func setupSlackIntegrationHandlerDB(t *testing.T) (*gorm.DB, uint) {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.AutoMigrate(&model.Account{}, &model.IntegrationHook{}))
|
||||
account := &model.Account{Name: "Slack Handler Account"}
|
||||
require.NoError(t, db.Create(account).Error)
|
||||
t.Cleanup(func() {
|
||||
sqlDB, _ := db.DB()
|
||||
_ = sqlDB.Close()
|
||||
})
|
||||
return db, account.ID
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// SlackIntegration — param validation tests
|
||||
// ========================================
|
||||
@@ -117,3 +150,51 @@ func TestSlackIntegration_ListAllChannels_BadAccountID(t *testing.T) {
|
||||
errBody := resp["error"].(map[string]interface{})
|
||||
assert.Contains(t, errBody["message"], "invalid account_id")
|
||||
}
|
||||
|
||||
func TestSlackIntegration_Create_NoTrailingSlash_ReturnsRawAppPayload(t *testing.T) {
|
||||
db, accountID := setupSlackIntegrationHandlerDB(t)
|
||||
svc := service.NewSlackIntegrationService(repository.NewIntegrationHookRepo(db))
|
||||
r := setupSlackIntegrationRouterWithService(svc)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
body := bytes.NewReader([]byte(`{"slack_token":"xoxb-handler-token"}`))
|
||||
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/integrations/slack", body)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
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))
|
||||
assert.Equal(t, "slack", resp["id"])
|
||||
assert.NotContains(t, resp, "success")
|
||||
hooks := resp["hooks"].([]interface{})
|
||||
require.Len(t, hooks, 1)
|
||||
hook := hooks[0].(map[string]interface{})
|
||||
assert.Equal(t, "slack", hook["app_id"])
|
||||
assert.Equal(t, accountID, uint(hook["account_id"].(float64)))
|
||||
assert.Equal(t, false, hook["status"])
|
||||
}
|
||||
|
||||
func TestSlackIntegration_Delete_NoTrailingSlash_ReturnsEmptyOK(t *testing.T) {
|
||||
db, accountID := setupSlackIntegrationHandlerDB(t)
|
||||
require.NoError(t, db.Create(&model.IntegrationHook{AccountID: accountID, AppID: "slack", HookType: model.HookTypeSlack}).Error)
|
||||
svc := service.NewSlackIntegrationService(repository.NewIntegrationHookRepo(db))
|
||||
r := setupSlackIntegrationRouterWithService(svc)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("DELETE", "/api/v1/accounts/1/integrations/slack", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Empty(t, w.Body.String())
|
||||
}
|
||||
|
||||
func TestSlackIntegration_Update_PutNoTrailingSlash_BadAccountID(t *testing.T) {
|
||||
r := setupSlackIntegrationRouter()
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("PUT", "/api/v1/accounts/abc/integrations/slack", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user