Files
gochat/backend/internal/handler/webhook/coverage4_test.go
T

265 lines
7.1 KiB
Go

package webhook
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func init() {
gin.SetMode(gin.TestMode)
}
// safeCall_Cov4 wraps a function call in recover to handle panics from nil-service handlers.
func safeCall_Cov4(t *testing.T, f func()) {
t.Helper()
defer func() { _ = recover() }()
f()
}
func makeGinContext_Cov4(method, path string, body []byte, headers map[string]string) (*gin.Context, *httptest.ResponseRecorder) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
var req *http.Request
if body != nil {
req = httptest.NewRequest(method, path, bytes.NewReader(body))
} else {
req = httptest.NewRequest(method, path, nil)
}
for k, v := range headers {
req.Header.Set(k, v)
}
c.Request = req
c.Params = gin.Params{}
return c, w
}
// === WebhookHandler.Handle ===
func TestWebhookHandler_Handle_NilHandler_Cov4(t *testing.T) {
var h *WebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("POST", "/webhooks/web_widget/1", nil, nil)
h.Handle(c)
})
}
func TestWebhookHandler_Handle_UnknownChannel_Cov4(t *testing.T) {
// Test with nil registry — will panic
safeCall_Cov4(t, func() {
h := &WebhookHandler{registry: nil}
c, _ := makeGinContext_Cov4("POST", "/webhooks/unknown/1", nil, nil)
h.Handle(c)
})
}
// === EmailWebhookHandler ===
func TestEmailWebhookHandler_NilHandler_Cov4(t *testing.T) {
var h *EmailWebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("POST", "/webhooks/email/1", []byte(`{}`), nil)
h.HandleEmailWebhook(c)
})
}
func TestEmailWebhookHandler_NilHandlerVerification_Cov4(t *testing.T) {
var h *EmailWebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("GET", "/webhooks/email/1", nil, nil)
h.HandleEmailVerification(c)
})
}
// === FacebookWebhookHandler ===
func TestFacebookWebhookHandler_NilHandler_Cov4(t *testing.T) {
var h *FacebookWebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("GET", "/webhooks/facebook", nil, map[string]string{
"hub.mode": "subscribe",
"hub.verify_token": "test",
"hub.challenge": "challenge123",
})
h.HandleFacebookVerification(c)
})
}
func TestFacebookWebhookHandler_NilHandleWebhook_Cov4(t *testing.T) {
var h *FacebookWebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("POST", "/webhooks/facebook", []byte(`{"object":"page"}`), nil)
h.HandleFacebookWebhook(c)
})
}
func TestFacebookWebhookHandler_NilHandleInstagramVerification_Cov4(t *testing.T) {
var h *FacebookWebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("GET", "/webhooks/instagram", nil, map[string]string{
"hub.mode": "subscribe",
"hub.verify_token": "test",
"hub.challenge": "challenge123",
})
h.HandleInstagramVerification(c)
})
}
func TestFacebookWebhookHandler_NilHandleInstagramWebhook_Cov4(t *testing.T) {
var h *FacebookWebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("POST", "/webhooks/instagram", []byte(`{"object":"instagram"}`), nil)
h.HandleInstagramWebhook(c)
})
}
// === TelegramWebhookHandler ===
func TestTelegramWebhookHandler_NilHandler_Cov4(t *testing.T) {
var h *TelegramWebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("POST", "/webhooks/telegram/bot123", []byte(`{}`), nil)
h.HandleTelegramWebhook(c)
})
}
// === TikTokWebhookHandler ===
func TestTikTokWebhookHandler_NilHandler_Cov4(t *testing.T) {
var h *TikTokWebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("POST", "/webhooks/tiktok", []byte(`{}`), nil)
h.HandleTikTokWebhook(c)
})
}
// === TwilioWebhookHandler ===
func TestTwilioWebhookHandler_NilHandler_InboundSMS_Cov4(t *testing.T) {
var h *TwilioWebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("POST", "/webhooks/sms/+1234567890", []byte(`{}`), nil)
h.HandleTwilioInboundSMS(c)
})
}
func TestTwilioWebhookHandler_NilHandler_Callback_Cov4(t *testing.T) {
var h *TwilioWebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("POST", "/webhooks/twilio/sms/+1234567890", []byte(`{}`), nil)
h.HandleTwilioCallback(c)
})
}
func TestTwilioWebhookHandler_NilHandler_DeliveryStatus_Cov4(t *testing.T) {
var h *TwilioWebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("POST", "/webhooks/twilio/status/+1234567890", []byte(`{}`), nil)
h.HandleTwilioDeliveryStatus(c)
})
}
// === LineWebhookHandler ===
func TestLineWebhookHandler_NilHandler_Cov4(t *testing.T) {
var h *LineWebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("POST", "/webhooks/line", []byte(`{}`), nil)
h.HandleLineWebhook(c)
})
}
// === ShopifyWebhookHandler ===
func TestShopifyWebhookHandler_NilHandler_Cov4(t *testing.T) {
var h *ShopifyWebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("POST", "/webhooks/shopify", []byte(`{}`), nil)
h.HandleShopifyWebhook(c)
})
}
// === WhatsAppWebhookHandler ===
func TestWhatsAppWebhookHandler_NilHandler_Cov4(t *testing.T) {
var h *WhatsAppWebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("POST", "/webhooks/whatsapp", []byte(`{}`), nil)
h.HandleWhatsAppWebhook(c)
})
}
func TestWhatsAppWebhookHandler_NilHandlerVerification_Cov4(t *testing.T) {
var h *WhatsAppWebhookHandler
safeCall_Cov4(t, func() {
c, _ := makeGinContext_Cov4("GET", "/webhooks/whatsapp", nil, map[string]string{
"hub.mode": "subscribe",
"hub.verify_token": "test",
"hub.challenge": "challenge123",
})
h.HandleWhatsAppVerification(c)
})
}
// === IncomingPersister additional tests ===
func TestIncomingPersister_NilDB_Cov4(t *testing.T) {
p := NewIncomingPersister(nil)
assert.Nil(t, p)
}
func TestIncomingPersister_SetWorkerPool_Nil_Cov4(t *testing.T) {
var p *IncomingPersister
result := p.SetWorkerPool(nil)
assert.Nil(t, result)
}
func TestIncomingPersister_SetSearchIndexer_Nil_Cov4(t *testing.T) {
var p *IncomingPersister
result := p.SetSearchIndexer(nil)
assert.Nil(t, result)
}
func TestIncomingPersister_PersistIncoming_NilPersister_Cov4(t *testing.T) {
var p *IncomingPersister
result, err := p.PersistIncoming(nil, nil, nil)
assert.Nil(t, result)
assert.Nil(t, err)
}
func TestIncomingPersister_UpdateMessageStatusWithError_Nil_Cov4(t *testing.T) {
var p *IncomingPersister
err := p.UpdateMessageStatusWithError(nil, nil, "", "", nil, "")
assert.Nil(t, err)
}
func TestIncomingPersister_UpdateContactConversationMessagesStatus_Nil_Cov4(t *testing.T) {
var p *IncomingPersister
err := p.UpdateContactConversationMessagesStatus(nil, nil, "", "", nil)
assert.Nil(t, err)
}
// === providerMessageStatusRank ===
func TestProviderMessageStatusRank_Cov4(t *testing.T) {
// Test various status values
rank := providerMessageStatusRank("sent")
assert.GreaterOrEqual(t, rank, 0)
rank2 := providerMessageStatusRank("delivered")
assert.GreaterOrEqual(t, rank2, 0)
rank3 := providerMessageStatusRank("read")
assert.GreaterOrEqual(t, rank3, 0)
rank4 := providerMessageStatusRank("failed")
assert.GreaterOrEqual(t, rank4, 0)
rank5 := providerMessageStatusRank("unknown")
assert.Equal(t, 0, rank5)
}