143 lines
3.9 KiB
Go
143 lines
3.9 KiB
Go
package webhook
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
channelmodel "github.com/gochat/gochat/internal/model/channel"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func newWebhookLookupTestDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
|
|
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(
|
|
&model.Inbox{},
|
|
&channelmodel.ChannelTelegram{},
|
|
&channelmodel.ChannelLINE{},
|
|
&channelmodel.ChannelTwilioSMS{},
|
|
&channelmodel.ChannelTikTok{},
|
|
); err != nil {
|
|
t.Fatalf("migrate webhook lookup models: %v", err)
|
|
}
|
|
return db
|
|
}
|
|
|
|
func seedWebhookInbox(t *testing.T, db *gorm.DB, channelType string) model.Inbox {
|
|
t.Helper()
|
|
|
|
inbox := model.Inbox{
|
|
AccountID: 1,
|
|
Name: channelType + " inbox",
|
|
ChannelType: channelType,
|
|
ChannelID: 1,
|
|
Enabled: true,
|
|
}
|
|
if err := db.Create(&inbox).Error; err != nil {
|
|
t.Fatalf("create inbox: %v", err)
|
|
}
|
|
return inbox
|
|
}
|
|
|
|
func TestTelegramWebhookLookupInboxByBotToken(t *testing.T) {
|
|
db := newWebhookLookupTestDB(t)
|
|
inbox := seedWebhookInbox(t, db, "telegram")
|
|
channel := channelmodel.ChannelTelegram{
|
|
AccountID: 1,
|
|
InboxID: inbox.ID,
|
|
BotToken: "123:secret-token",
|
|
BotName: "support_bot",
|
|
}
|
|
if err := db.Create(&channel).Error; err != nil {
|
|
t.Fatalf("create telegram channel: %v", err)
|
|
}
|
|
|
|
h := NewTelegramWebhookHandler(nil, nil, db)
|
|
found, err := h.lookupInbox("123:secret-token")
|
|
if err != nil {
|
|
t.Fatalf("lookup inbox: %v", err)
|
|
}
|
|
if found.ID != inbox.ID || found.ChannelType != "telegram" {
|
|
t.Fatalf("unexpected inbox: id=%d type=%s", found.ID, found.ChannelType)
|
|
}
|
|
}
|
|
|
|
func TestLineWebhookLookupInboxByLineChannelID(t *testing.T) {
|
|
db := newWebhookLookupTestDB(t)
|
|
inbox := seedWebhookInbox(t, db, "line")
|
|
channel := channelmodel.ChannelLINE{
|
|
AccountID: 1,
|
|
InboxID: inbox.ID,
|
|
ChannelID: "line-channel-1",
|
|
Name: "LINE OA",
|
|
}
|
|
if err := db.Create(&channel).Error; err != nil {
|
|
t.Fatalf("create line channel: %v", err)
|
|
}
|
|
|
|
h := NewLineWebhookHandler(nil, nil, nil, db)
|
|
found, err := h.lookupInboxByLineChannelID("line-channel-1")
|
|
if err != nil {
|
|
t.Fatalf("lookup inbox: %v", err)
|
|
}
|
|
if found.ID != inbox.ID || found.ChannelType != "line" {
|
|
t.Fatalf("unexpected inbox: id=%d type=%s", found.ID, found.ChannelType)
|
|
}
|
|
}
|
|
|
|
func TestTwilioWebhookLookupInboxByPhoneNumber(t *testing.T) {
|
|
db := newWebhookLookupTestDB(t)
|
|
inbox := seedWebhookInbox(t, db, "twilio_sms")
|
|
channel := channelmodel.ChannelTwilioSMS{
|
|
AccountID: 1,
|
|
InboxID: inbox.ID,
|
|
AccountSID: "AC123",
|
|
PhoneNumber: "+15551234567",
|
|
MessagingServiceSID: "MG123",
|
|
}
|
|
if err := db.Create(&channel).Error; err != nil {
|
|
t.Fatalf("create twilio channel: %v", err)
|
|
}
|
|
|
|
h := NewTwilioWebhookHandler(nil, db)
|
|
found, err := h.lookupInboxByPhoneNumber("+15551234567")
|
|
if err != nil {
|
|
t.Fatalf("lookup inbox: %v", err)
|
|
}
|
|
if found.ID != inbox.ID || found.ChannelType != "twilio_sms" {
|
|
t.Fatalf("unexpected inbox: id=%d type=%s", found.ID, found.ChannelType)
|
|
}
|
|
}
|
|
|
|
func TestTikTokWebhookLookupInboxByBusinessIDAndPayloadExtractor(t *testing.T) {
|
|
db := newWebhookLookupTestDB(t)
|
|
inbox := seedWebhookInbox(t, db, "tiktok")
|
|
channel := channelmodel.ChannelTikTok{
|
|
AccountID: 1,
|
|
InboxID: inbox.ID,
|
|
TikTokBusinessID: "biz-123",
|
|
WebhookVerifyToken: "verify-token",
|
|
}
|
|
if err := db.Create(&channel).Error; err != nil {
|
|
t.Fatalf("create tiktok channel: %v", err)
|
|
}
|
|
|
|
h := NewTikTokWebhookHandler(nil, nil, db)
|
|
found, err := h.lookupInboxByBusinessID("biz-123")
|
|
if err != nil {
|
|
t.Fatalf("lookup inbox: %v", err)
|
|
}
|
|
if found.ID != inbox.ID || found.ChannelType != "tiktok" {
|
|
t.Fatalf("unexpected inbox: id=%d type=%s", found.ID, found.ChannelType)
|
|
}
|
|
|
|
if got := extractTikTokBusinessID([]byte(`{"data":{"business_id":"biz-123"}}`)); got != "biz-123" {
|
|
t.Fatalf("unexpected extracted business id: %s", got)
|
|
}
|
|
}
|