-
Notifications
You must be signed in to change notification settings - Fork 60
/
webhook_test.go
118 lines (86 loc) · 3.28 KB
/
webhook_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package conversation
import (
messagebird "github.com/messagebird/go-rest-api/v9"
"net/http"
"testing"
"github.com/messagebird/go-rest-api/v9/internal/mbtest"
"github.com/stretchr/testify/assert"
)
func TestCreateWebhook(t *testing.T) {
mbtest.WillReturnTestdata(t, "webhookObject.json", http.StatusOK)
client := mbtest.Client(t)
webhook, err := CreateWebhook(client, &WebhookCreateRequest{
ChannelID: "chid",
Events: []WebhookEvent{
WebhookEventConversationCreated,
WebhookEventMessageUpdated,
},
URL: "https://example.com/webhooks",
})
assert.NoError(t, err)
assert.Equal(t, "whid", webhook.ID)
mbtest.AssertEndpointCalled(t, http.MethodPost, "/v1/webhooks")
mbtest.AssertTestdataJson(t, "webhookCreateRequest.json", mbtest.Request.Body)
}
func TestDeleteWebhook(t *testing.T) {
mbtest.WillReturn([]byte(""), http.StatusNoContent)
client := mbtest.Client(t)
err := DeleteWebhook(client, "whid")
assert.NoError(t, err)
mbtest.AssertEndpointCalled(t, http.MethodDelete, "/v1/webhooks/whid")
}
func TestListWebhooks(t *testing.T) {
t.Run("limit_offset", func(t *testing.T) {
mbtest.WillReturnTestdata(t, "webhookListObject.json", http.StatusOK)
client := mbtest.Client(t)
webhookList, err := ListWebhooks(client, &messagebird.PaginationRequest{Limit: 20, Offset: 2})
assert.NoError(t, err)
assert.Equal(t, 1, webhookList.TotalCount)
assert.Equal(t, WebhookEventMessageCreated, webhookList.Items[0].Events[0])
mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/webhooks")
query := mbtest.Request.URL.RawQuery
assert.Equal(t, "limit=20&offset=2", query)
})
t.Run("all", func(t *testing.T) {
mbtest.WillReturnTestdata(t, "allWebhookListObject.json", http.StatusOK)
client := mbtest.Client(t)
webhookList, err := ListWebhooks(client, nil)
assert.NoError(t, err)
assert.Equal(t, 10, webhookList.Limit)
assert.Equal(t, 0, webhookList.Offset)
assert.Equal(t, 2, webhookList.TotalCount)
assert.Equal(t, WebhookEventMessageCreated, webhookList.Items[0].Events[0])
assert.Len(t, webhookList.Items, 2)
mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/webhooks")
query := mbtest.Request.URL.RawQuery
assert.Equal(t, "", query)
})
}
func TestReadWebhook(t *testing.T) {
mbtest.WillReturnTestdata(t, "webhookObject.json", http.StatusOK)
client := mbtest.Client(t)
webhook, err := ReadWebhook(client, "whid")
assert.NoError(t, err)
assert.Equal(t, "chid", webhook.ChannelID)
count := len(webhook.Events)
assert.Equal(t, 2, count)
mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/webhooks/whid")
}
func TestUpdateWebhook(t *testing.T) {
mbtest.WillReturnTestdata(t, "webhookUpdatedObject.json", http.StatusOK)
client := mbtest.Client(t)
webhookUpdateRequest := &WebhookUpdateRequest{
Events: []WebhookEvent{
WebhookEventConversationUpdated,
},
URL: "https://example.com/mynewwebhookurl",
Status: WebhookStatusDisabled,
}
webhook, err := UpdateWebhook(client, "whid", webhookUpdateRequest)
assert.NoError(t, err)
assert.Equal(t, "https://example.com/mynewwebhookurl", webhook.URL)
assert.NotNil(t, webhook.UpdatedDatetime)
assert.Equal(t, WebhookStatusDisabled, webhook.Status)
mbtest.AssertEndpointCalled(t, http.MethodPatch, "/v1/webhooks/whid")
mbtest.AssertTestdataJson(t, "webhookUpdateRequest.json", mbtest.Request.Body)
}