75 lines
3.0 KiB
Go
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)
|
|
}
|