-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
82 lines (73 loc) · 2.59 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"bytes"
fastshot "github.com/opus-domini/fast-shot"
goredislib "github.com/redis/go-redis/v9"
"github.com/slack-go/slack"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"net/http"
"net/http/httptest"
"testing"
)
func TestHandleWhatsappInitiatedMessage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockFbClient := NewMockFbClient(ctrl)
mockSlackClient := NewMockSlackClient(ctrl)
mockLinkedAccount := NewMockLinkedAccountStore(ctrl)
mockRedisClient := NewMockRedisClient(ctrl)
mockFbClient.EXPECT().
markWhatsappMessageAsRead("36825", "message_id_123").
Return(fastshot.Response{}, nil)
mockSlackClient.EXPECT().getSlackChannel(gomock.Any(), gomock.Any()).Return(slack.Channel{}, nil)
mockLinkedAccount.EXPECT().lookupLinkedAccount(gomock.Any()).Return(&LinkedAccount{WhatsappBusinessPhoneNumberId: "36825", SlackUserId: "slack_123"}, nil)
mockRedisClient.EXPECT().LPush(gomock.Any(), gomock.Any(), gomock.Any()).Return(goredislib.NewIntCmd(nil))
handler := handleWhatsappInitiatedMessage(mockFbClient, mockSlackClient, mockLinkedAccount, mockRedisClient)
body := `{
"object": "whatsapp_business_account",
"entry": [
{
"id": "36825",
"changes": [
{
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "40742321321",
"phone_number_id": "36825"
},
"contacts": [
{
"profile": {
"name": "Bob"
},
"wa_id": "40742123123"
}
],
"messages": [
{
"id": "message_id_123",
"from": "40742123123",
"text": {
"body": "message from whatsapp"
},
"type": "text"
}
]
},
"field": "messages"
}
]
}
]
}
`
req, err := http.NewRequest("POST", "/whatsapp/webhook", bytes.NewBufferString(body))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code, "Expected status code 200")
}