Files
gochat/backend/internal/service/webhook_subscription_service_test.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

75 lines
3.0 KiB
Go

package service
import (
"context"
"testing"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/repository"
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func TestWebhookSubscriptionServiceChatwootParity(t *testing.T) {
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.Inbox{}, &model.WebhookSubscription{}, &model.WebhookDelivery{}))
account := model.Account{Name: "webhook-service-account"}
require.NoError(t, db.Create(&account).Error)
inbox := model.Inbox{AccountID: account.ID, Name: "Support"}
require.NoError(t, db.Create(&inbox).Error)
svc := NewWebhookSubscriptionService(repository.NewWebhookSubscriptionRepo(db))
created, err := svc.CreateWebhook(context.Background(), account.ID, WebhookSubscriptionMutation{
InboxID: &inbox.ID,
Name: "Service hook",
URL: "https://example.com/service-hook",
Subscriptions: []string{"message_created", "conversation_updated"},
})
require.NoError(t, err)
require.NotEmpty(t, created.Secret)
require.Equal(t, "Service hook", created.Name)
require.True(t, created.IsEventSubscribed("message_created"))
updated, err := svc.UpdateWebhook(context.Background(), account.ID, created.ID, WebhookSubscriptionMutation{
Name: "Updated service hook",
URL: "https://example.com/service-hook-updated",
Subscriptions: []string{"contact_created"},
})
require.NoError(t, err)
require.Equal(t, "Updated service hook", updated.Name)
require.True(t, updated.IsEventSubscribed("contact_created"))
require.False(t, updated.IsEventSubscribed("message_created"))
matches, err := repository.NewWebhookSubscriptionRepo(db).ListByAccountAndEvent(context.Background(), account.ID, "contact_created")
require.NoError(t, err)
require.Len(t, matches, 1)
otherAccount := model.Account{Name: "other-webhook-service-account"}
require.NoError(t, db.Create(&otherAccount).Error)
require.Error(t, svc.DeleteWebhook(context.Background(), otherAccount.ID, created.ID))
require.NoError(t, svc.DeleteWebhook(context.Background(), account.ID, created.ID))
}
func TestWebhookSubscriptionServiceValidation(t *testing.T) {
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.WebhookSubscription{}))
svc := NewWebhookSubscriptionService(repository.NewWebhookSubscriptionRepo(db))
_, err = svc.CreateWebhook(context.Background(), 1, WebhookSubscriptionMutation{
URL: "ftp://example.com/hook",
Subscriptions: []string{"message_created"},
})
require.Error(t, err)
_, err = svc.CreateWebhook(context.Background(), 1, WebhookSubscriptionMutation{
URL: "https://example.com/hook",
Subscriptions: []string{"not_allowed"},
})
require.Error(t, err)
}