Skip to content

Commit

Permalink
split meta tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rasoro committed Jan 14, 2025
1 parent 3aed4d4 commit dcc48d9
Show file tree
Hide file tree
Showing 190 changed files with 7,014 additions and 271 deletions.
60 changes: 30 additions & 30 deletions handlers/facebookapp/facebookapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ import (

// Endpoints we hit
var (
sendURL = "https://graph.facebook.com/v12.0/me/messages"
graphURL = "https://graph.facebook.com/v12.0/"
SendURL = "https://graph.facebook.com/v12.0/me/messages"
GraphURL = "https://graph.facebook.com/v12.0/"

signatureHeader = "X-Hub-Signature"

// max for the body
maxMsgLengthIG = 1000
maxMsgLengthFBA = 2000
maxMsgLengthWAC = 4096
maxMsgLengthInteractiveWAC = 1024
MaxMsgLengthIG = 1000
MaxMsgLengthFBA = 2000
MaxMsgLengthWAC = 4096
MaxMsgLengthInteractiveWAC = 1024

// Sticker ID substitutions
stickerIDToEmoji = map[int64]string{
StickerIDToEmoji = map[int64]string{
369239263222822: "👍", // small
369239343222814: "👍", // medium
369239383222810: "👍", // big
}

tagByTopic = map[string]string{
TagByTopic = map[string]string{
"event": "CONFIRMED_EVENT_UPDATE",
"purchase": "POST_PURCHASE_UPDATE",
"account": "ACCOUNT_UPDATE",
Expand Down Expand Up @@ -93,14 +93,14 @@ const (
InteractiveProductCatalogMessageType = "catalog_message"
)

func newHandler(channelType courier.ChannelType, name string, useUUIDRoutes bool) courier.ChannelHandler {
func NewHandler(channelType courier.ChannelType, name string, useUUIDRoutes bool) courier.ChannelHandler {
return &handler{handlers.NewBaseHandlerWithParams(channelType, name, useUUIDRoutes)}
}

func init() {
courier.RegisterHandler(newHandler("IG", "Instagram", false))
courier.RegisterHandler(newHandler("FBA", "Facebook", false))
courier.RegisterHandler(newHandler("WAC", "WhatsApp Cloud", false))
courier.RegisterHandler(NewHandler("IG", "Instagram", false))
courier.RegisterHandler(NewHandler("FBA", "Facebook", false))
courier.RegisterHandler(NewHandler("WAC", "WhatsApp Cloud", false))

failedMediaCache = cache.New(15*time.Minute, 15*time.Minute)
}
Expand Down Expand Up @@ -462,13 +462,13 @@ func (h *handler) receiveVerify(ctx context.Context, channel courier.Channel, w
return nil, err
}

func resolveMediaURL(channel courier.Channel, mediaID string, token string) (string, error) {
func ResolveMediaURL(channel courier.Channel, mediaID string, token string) (string, error) {

if token == "" {
return "", fmt.Errorf("missing token for WAC channel")
}

base, _ := url.Parse(graphURL)
base, _ := url.Parse(GraphURL)
path, _ := url.Parse(fmt.Sprintf("/%s", mediaID))
retreiveURL := base.ResolveReference(path)

Expand Down Expand Up @@ -565,23 +565,23 @@ func (h *handler) processCloudWhatsAppPayload(ctx context.Context, channel couri
text = msg.Text.Body
} else if msg.Type == "audio" && msg.Audio != nil {
text = msg.Audio.Caption
mediaURL, err = resolveMediaURL(channel, msg.Audio.ID, token)
mediaURL, err = ResolveMediaURL(channel, msg.Audio.ID, token)
} else if msg.Type == "voice" && msg.Voice != nil {
text = msg.Voice.Caption
mediaURL, err = resolveMediaURL(channel, msg.Voice.ID, token)
mediaURL, err = ResolveMediaURL(channel, msg.Voice.ID, token)
} else if msg.Type == "button" && msg.Button != nil {
text = msg.Button.Text
} else if msg.Type == "document" && msg.Document != nil {
text = msg.Document.Caption
mediaURL, err = resolveMediaURL(channel, msg.Document.ID, token)
mediaURL, err = ResolveMediaURL(channel, msg.Document.ID, token)
} else if msg.Type == "image" && msg.Image != nil {
text = msg.Image.Caption
mediaURL, err = resolveMediaURL(channel, msg.Image.ID, token)
mediaURL, err = ResolveMediaURL(channel, msg.Image.ID, token)
} else if msg.Type == "sticker" && msg.Sticker != nil {
mediaURL, err = resolveMediaURL(channel, msg.Sticker.ID, token)
mediaURL, err = ResolveMediaURL(channel, msg.Sticker.ID, token)
} else if msg.Type == "video" && msg.Video != nil {
text = msg.Video.Caption
mediaURL, err = resolveMediaURL(channel, msg.Video.ID, token)
mediaURL, err = ResolveMediaURL(channel, msg.Video.ID, token)
} else if msg.Type == "location" && msg.Location != nil {
mediaURL = fmt.Sprintf("geo:%f,%f;name:%s;address:%s", msg.Location.Latitude, msg.Location.Longitude, msg.Location.Name, msg.Location.Address)
} else if msg.Type == "interactive" && msg.Interactive.Type == "button_reply" {
Expand Down Expand Up @@ -900,7 +900,7 @@ func (h *handler) processFacebookInstagramPayload(ctx context.Context, channel c
// if we have a sticker ID, use that as our text
for _, att := range msg.Message.Attachments {
if att.Type == "image" && att.Payload != nil && att.Payload.StickerID != 0 {
text = stickerIDToEmoji[att.Payload.StickerID]
text = StickerIDToEmoji[att.Payload.StickerID]
}

if att.Type == "location" {
Expand Down Expand Up @@ -1064,7 +1064,7 @@ func (h *handler) sendFacebookInstagramMsg(ctx context.Context, msg courier.Msg)
payload.MessagingType = "RESPONSE"
} else if topic != "" {
payload.MessagingType = "MESSAGE_TAG"
payload.Tag = tagByTopic[topic]
payload.Tag = TagByTopic[topic]
} else {
payload.MessagingType = "UPDATE"
}
Expand All @@ -1076,7 +1076,7 @@ func (h *handler) sendFacebookInstagramMsg(ctx context.Context, msg courier.Msg)
payload.Recipient.ID = msg.URN().Path()
}

msgURL, _ := url.Parse(sendURL)
msgURL, _ := url.Parse(SendURL)
query := url.Values{}
query.Set("access_token", accessToken)
msgURL.RawQuery = query.Encode()
Expand Down Expand Up @@ -1201,9 +1201,9 @@ func (h *handler) sendFacebookInstagramMsg(ctx context.Context, msg courier.Msg)
msgParts := make([]string, 0)
if msg.Text() != "" {
if msg.Channel().ChannelType() == "IG" {
msgParts = handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), maxMsgLengthIG)
msgParts = handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), MaxMsgLengthIG)
} else {
msgParts = handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), maxMsgLengthFBA)
msgParts = handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), MaxMsgLengthFBA)
}

}
Expand Down Expand Up @@ -1509,7 +1509,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
hasNewURN := false
hasCaption := false

base, _ := url.Parse(graphURL)
base, _ := url.Parse(GraphURL)
path, _ := url.Parse(fmt.Sprintf("/%s/messages", msg.Channel().Address()))
wacPhoneURL := base.ResolveReference(path)

Expand All @@ -1518,9 +1518,9 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
msgParts := make([]string, 0)
if msg.Text() != "" {
if len(msg.ListMessage().ListItems) > 0 || len(msg.QuickReplies()) > 0 || msg.InteractionType() == "location" {
msgParts = handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), maxMsgLengthInteractiveWAC)
msgParts = handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), MaxMsgLengthInteractiveWAC)
} else {
msgParts = handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), maxMsgLengthWAC)
msgParts = handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), MaxMsgLengthWAC)
}
}
qrs := msg.QuickReplies()
Expand Down Expand Up @@ -2676,7 +2676,7 @@ func (h *handler) DescribeURN(ctx context.Context, channel courier.Channel, urn
}

// build a request to lookup the stats for this contact
base, _ := url.Parse(graphURL)
base, _ := url.Parse(GraphURL)
path, _ := url.Parse(fmt.Sprintf("/%s", urn.Path()))
u := base.ResolveReference(path)
query := url.Values{}
Expand Down Expand Up @@ -2950,7 +2950,7 @@ func (h *handler) fetchWACMediaID(msg courier.Msg, mimeType, mediaURL string, ac
}

// upload media to WhatsAppCloud
base, _ := url.Parse(graphURL)
base, _ := url.Parse(GraphURL)
path, _ := url.Parse(fmt.Sprintf("/%s/media", msg.Channel().Address()))
wacPhoneURLMedia := base.ResolveReference(path)
mediaID, logs, err = requestWACMediaUpload(rr.Body, mediaURL, wacPhoneURLMedia.String(), mimeType, msg, accessToken)
Expand Down
48 changes: 24 additions & 24 deletions handlers/facebookapp/facebookapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func buildMockFBGraphFBA(testCases []ChannelHandleTestCase) *httptest.Server {
// no name
w.Write([]byte(`{ "first_name": "", "last_name": ""}`))
}))
graphURL = server.URL
GraphURL = server.URL

return server
}
Expand All @@ -185,7 +185,7 @@ func buildMockFBGraphIG(testCases []ChannelHandleTestCase) *httptest.Server {
// no name
w.Write([]byte(`{ "name": ""}`))
}))
graphURL = server.URL
GraphURL = server.URL

return server
}
Expand All @@ -194,7 +194,7 @@ func TestDescribeFBA(t *testing.T) {
fbGraph := buildMockFBGraphFBA(testCasesFBA)
defer fbGraph.Close()

handler := newHandler("FBA", "Facebook", false).(courier.URNDescriber)
handler := NewHandler("FBA", "Facebook", false).(courier.URNDescriber)
tcs := []struct {
urn urns.URN
metadata map[string]string
Expand All @@ -212,7 +212,7 @@ func TestDescribeIG(t *testing.T) {
fbGraph := buildMockFBGraphIG(testCasesIG)
defer fbGraph.Close()

handler := newHandler("IG", "Instagram", false).(courier.URNDescriber)
handler := NewHandler("IG", "Instagram", false).(courier.URNDescriber)
tcs := []struct {
urn urns.URN
metadata map[string]string
Expand All @@ -226,7 +226,7 @@ func TestDescribeIG(t *testing.T) {
}

func TestDescribeWAC(t *testing.T) {
handler := newHandler("WAC", "Cloud API WhatsApp", false).(courier.URNDescriber)
handler := NewHandler("WAC", "Cloud API WhatsApp", false).(courier.URNDescriber)

tcs := []struct {
urn urns.URN
Expand All @@ -250,10 +250,10 @@ func TestResolveMediaURL(t *testing.T) {
}{{"id_media", "", "", "missing token for WAC channel"},
{"id_media", "token", "", `unsupported protocol scheme ""`}}

graphURL = "url"
GraphURL = "url"

for _, tc := range tcs {
_, err := resolveMediaURL(testChannelsWAC[0], tc.id, tc.token)
_, err := ResolveMediaURL(testChannelsWAC[0], tc.id, tc.token)
assert.Equal(t, err.Error(), tc.err)
}
}
Expand Down Expand Up @@ -417,28 +417,28 @@ func TestHandler(t *testing.T) {
w.Write([]byte(`{"url": "https://foo.bar/attachmentURL"}`))

}))
graphURL = server.URL
GraphURL = server.URL

RunChannelTestCases(t, testChannelsWAC, newHandler("WAC", "Cloud API WhatsApp", false), testCasesWAC)
RunChannelTestCases(t, testChannelsFBA, newHandler("FBA", "Facebook", false), testCasesFBA)
RunChannelTestCases(t, testChannelsIG, newHandler("IG", "Instagram", false), testCasesIG)
RunChannelTestCases(t, testChannelsWAC, NewHandler("WAC", "Cloud API WhatsApp", false), testCasesWAC)
RunChannelTestCases(t, testChannelsFBA, NewHandler("FBA", "Facebook", false), testCasesFBA)
RunChannelTestCases(t, testChannelsIG, NewHandler("IG", "Instagram", false), testCasesIG)
}

func BenchmarkHandler(b *testing.B) {
fbService := buildMockFBGraphFBA(testCasesFBA)

RunChannelBenchmarks(b, testChannelsFBA, newHandler("FBA", "Facebook", false), testCasesFBA)
RunChannelBenchmarks(b, testChannelsFBA, NewHandler("FBA", "Facebook", false), testCasesFBA)
fbService.Close()

fbServiceIG := buildMockFBGraphIG(testCasesIG)

RunChannelBenchmarks(b, testChannelsIG, newHandler("IG", "Instagram", false), testCasesIG)
RunChannelBenchmarks(b, testChannelsIG, NewHandler("IG", "Instagram", false), testCasesIG)
fbServiceIG.Close()
}

func TestVerify(t *testing.T) {

RunChannelTestCases(t, testChannelsFBA, newHandler("FBA", "Facebook", false), []ChannelHandleTestCase{
RunChannelTestCases(t, testChannelsFBA, NewHandler("FBA", "Facebook", false), []ChannelHandleTestCase{
{Label: "Valid Secret", URL: "/c/fba/receive?hub.mode=subscribe&hub.verify_token=fb_webhook_secret&hub.challenge=yarchallenge", Status: 200,
Response: "yarchallenge", NoQueueErrorCheck: true, NoInvalidChannelCheck: true},
{Label: "Verify No Mode", URL: "/c/fba/receive", Status: 400, Response: "unknown request"},
Expand All @@ -447,7 +447,7 @@ func TestVerify(t *testing.T) {
{Label: "Valid Secret", URL: "/c/fba/receive?hub.mode=subscribe&hub.verify_token=fb_webhook_secret&hub.challenge=yarchallenge", Status: 200, Response: "yarchallenge"},
})

RunChannelTestCases(t, testChannelsIG, newHandler("IG", "Instagram", false), []ChannelHandleTestCase{
RunChannelTestCases(t, testChannelsIG, NewHandler("IG", "Instagram", false), []ChannelHandleTestCase{
{Label: "Valid Secret", URL: "/c/ig/receive?hub.mode=subscribe&hub.verify_token=fb_webhook_secret&hub.challenge=yarchallenge", Status: 200,
Response: "yarchallenge", NoQueueErrorCheck: true, NoInvalidChannelCheck: true},
{Label: "Verify No Mode", URL: "/c/ig/receive", Status: 400, Response: "unknown request"},
Expand All @@ -456,7 +456,7 @@ func TestVerify(t *testing.T) {
{Label: "Valid Secret", URL: "/c/ig/receive?hub.mode=subscribe&hub.verify_token=fb_webhook_secret&hub.challenge=yarchallenge", Status: 200, Response: "yarchallenge"},
})

RunChannelTestCases(t, testChannelsWAC, newHandler("WAC", "WhatsApp Cloud", false), []ChannelHandleTestCase{
RunChannelTestCases(t, testChannelsWAC, NewHandler("WAC", "WhatsApp Cloud", false), []ChannelHandleTestCase{
{Label: "Valid Secret", URL: "/c/wac/receive?hub.mode=subscribe&hub.verify_token=wac_webhook_secret&hub.challenge=yarchallenge", Status: 200,
Response: "yarchallenge", NoQueueErrorCheck: true, NoInvalidChannelCheck: true},
{Label: "Verify No Mode", URL: "/c/wac/receive", Status: 400, Response: "unknown request"},
Expand All @@ -468,8 +468,8 @@ func TestVerify(t *testing.T) {

// setSendURL takes care of setting the send_url to our test server host
func setSendURL(s *httptest.Server, h courier.ChannelHandler, c courier.Channel, m courier.Msg) {
sendURL = s.URL
graphURL = s.URL
SendURL = s.URL
GraphURL = s.URL
}

var SendTestCasesFBA = []ChannelSendTestCase{
Expand Down Expand Up @@ -1246,15 +1246,15 @@ func TestSending(t *testing.T) {
SendTestCasesWAC = append(SendTestCasesWAC, FailingCachedSendTestCasesWAC...)

// shorter max msg length for testing
maxMsgLengthFBA = 100
maxMsgLengthIG = 100
maxMsgLengthWAC = 100
MaxMsgLengthFBA = 100
MaxMsgLengthIG = 100
MaxMsgLengthWAC = 100
var ChannelFBA = courier.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "FBA", "12345", "", map[string]interface{}{courier.ConfigAuthToken: "a123"})
var ChannelIG = courier.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "IG", "12345", "", map[string]interface{}{courier.ConfigAuthToken: "a123"})
var ChannelWAC = courier.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "WAC", "12345_ID", "", map[string]interface{}{courier.ConfigAuthToken: "a123", "catalog_id": "c4t4l0g-1D"})
RunChannelSendTestCases(t, ChannelFBA, newHandler("FBA", "Facebook", false), SendTestCasesFBA, nil)
RunChannelSendTestCases(t, ChannelIG, newHandler("IG", "Instagram", false), SendTestCasesIG, nil)
RunChannelSendTestCases(t, ChannelWAC, newHandler("WAC", "Cloud API WhatsApp", false), SendTestCasesWAC, nil)
RunChannelSendTestCases(t, ChannelFBA, NewHandler("FBA", "Facebook", false), SendTestCasesFBA, nil)
RunChannelSendTestCases(t, ChannelIG, NewHandler("IG", "Instagram", false), SendTestCasesIG, nil)
RunChannelSendTestCases(t, ChannelWAC, NewHandler("WAC", "Cloud API WhatsApp", false), SendTestCasesWAC, nil)

}

Expand Down
10 changes: 0 additions & 10 deletions handlers/meta/fba/facebook.go

This file was deleted.

Loading

0 comments on commit dcc48d9

Please sign in to comment.