feat(integrations): align notion parity

This commit is contained in:
2026-06-06 17:38:23 +08:00
parent a9d69de440
commit 5ed9952b7e
8 changed files with 89 additions and 14 deletions
@@ -33,11 +33,12 @@ func (h *NotionIntegrationHandler) Delete(c *gin.Context) {
handleServiceError(c, svcErr)
return
}
response.OK(c, gin.H{"message": "Notion integration deleted"})
c.Status(http.StatusOK)
}
// RegisterNotionIntegrationRoutes registers Notion integration routes.
func RegisterNotionIntegrationRoutes(g *gin.RouterGroup, h *NotionIntegrationHandler) {
g.DELETE("/notion", h.Delete)
notion := g.Group("/notion")
{
notion.DELETE("/", h.Delete)
@@ -2,25 +2,43 @@ package v1
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"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 setupNotionIntegrationRouter() *gin.Engine {
func setupNotionIntegrationRouter(t *testing.T) (*gin.Engine, *gorm.DB) {
t.Helper()
gin.SetMode(gin.TestMode)
r := gin.New()
r.RedirectTrailingSlash = false
handler := NewNotionIntegrationHandler(nil)
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{}))
t.Cleanup(func() {
sqlDB, _ := db.DB()
_ = sqlDB.Close()
})
handler := NewNotionIntegrationHandler(service.NewNotionIntegrationService(repository.NewIntegrationHookRepo(db)))
integrations := r.Group("/api/v1/accounts/:account_id/integrations")
RegisterNotionIntegrationRoutes(integrations, handler)
return r
return r, db
}
// ========================================
@@ -28,7 +46,7 @@ func setupNotionIntegrationRouter() *gin.Engine {
// ========================================
func TestNotionIntegration_Delete_BadAccountID(t *testing.T) {
r := setupNotionIntegrationRouter()
r, _ := setupNotionIntegrationRouter(t)
w := httptest.NewRecorder()
req, _ := http.NewRequest("DELETE", "/api/v1/accounts/abc/integrations/notion/", nil)
@@ -41,3 +59,22 @@ func TestNotionIntegration_Delete_BadAccountID(t *testing.T) {
errBody := resp["error"].(map[string]interface{})
assert.Contains(t, errBody["message"], "invalid account_id")
}
func TestNotionIntegration_Delete_NoTrailingSlashReturnsEmptyOK(t *testing.T) {
r, db := setupNotionIntegrationRouter(t)
account := &model.Account{Name: "Test Account"}
require.NoError(t, db.Create(account).Error)
hook := &model.IntegrationHook{AccountID: account.ID, AppID: "notion", HookType: model.HookTypeNotion, Status: model.HookStatusActive, AccessToken: "notion-token"}
require.NoError(t, db.Create(hook).Error)
w := httptest.NewRecorder()
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/integrations/notion", account.ID), nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Empty(t, w.Body.String())
var count int64
db.Model(&model.IntegrationHook{}).Where("account_id = ? AND app_id = ?", account.ID, "notion").Count(&count)
assert.Equal(t, int64(0), count)
}