-
Notifications
You must be signed in to change notification settings - Fork 89
/
eventsub_test.go
381 lines (336 loc) · 11.5 KB
/
eventsub_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package helix
import (
"context"
"net/http"
"testing"
)
func TestGetEventSubSubscriptions(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
params *EventSubSubscriptionsParams
count int
respBody string
}{
{
http.StatusUnauthorized,
&Options{ClientID: "my-client-id"},
&EventSubSubscriptionsParams{},
0,
`{"error":"Unauthorized","status":401,"message":"OAuth token is missing"}`,
},
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
&EventSubSubscriptionsParams{},
2,
`{"total":2,"data":[{"id":"832389eb-0d0b-41f8-b564-da039f6c4c75","status":"enabled","type":"channel.follow","version":"1","condition":{"broadcaster_user_id":"12345678"},"created_at":"2021-03-09T10:37:32.308415339Z","transport":{"method":"webhook","callback":"https://example.com/eventsub/follow"},"cost":1},{"id":"832389eb-0d0b-41f8-b564-da039f6c4c73","status":"enabled","type":"channel.follow","version":"1","condition":{"broadcaster_user_id":"12345679"},"created_at":"2021-03-09T10:37:32.308415339Z","transport":{"method":"webhook","callback":"https://example.com/eventsub/follow"},"cost":1}],"limit":100000000,"max_total_cost":10000,"total_cost":2,"pagination":{}}`,
},
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
&EventSubSubscriptionsParams{Status: "webhook_callback_verification_failed"},
0,
`{"total":1,"data":[],"limit":100000000,"max_total_cost":10000,"total_cost":1,"pagination":{}}`,
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
resp, err := c.GetEventSubSubscriptions(testCase.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != testCase.statusCode {
t.Errorf("expected status code to be \"%d\", got \"%d\"", testCase.statusCode, resp.StatusCode)
}
if resp.StatusCode == http.StatusUnauthorized {
if resp.Error != "Unauthorized" {
t.Errorf("expected error to be \"%s\", got \"%s\"", "Unauthorized", resp.Error)
}
if resp.ErrorStatus != http.StatusUnauthorized {
t.Errorf("expected error status to be \"%d\", got \"%d\"", http.StatusUnauthorized, resp.ErrorStatus)
}
expectedErrMsg := "OAuth token is missing"
if resp.ErrorMessage != expectedErrMsg {
t.Errorf("expected error message to be \"%s\", got \"%s\"", expectedErrMsg, resp.ErrorMessage)
}
continue
}
if len(resp.Data.EventSubSubscriptions) != testCase.count {
t.Errorf("expected result length to be \"%d\", got \"%d\"", testCase.count, len(resp.Data.EventSubSubscriptions))
}
}
// Test with HTTP Failure
options := &Options{
ClientID: "my-client-id",
HTTPClient: &badMockHTTPClient{
newMockHandler(0, "", nil),
},
}
c := &Client{
opts: options,
ctx: context.Background(),
}
_, err := c.GetEventSubSubscriptions(&EventSubSubscriptionsParams{})
if err == nil {
t.Error("expected error but got nil")
}
if err.Error() != "Failed to execute API request: Oops, that's bad :(" {
t.Error("expected error does match return error")
}
}
func TestRemoveEventSubSubscriptions(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
params string
respBody string
}{
{
http.StatusUnauthorized,
&Options{ClientID: "my-client-id"},
"",
`{"error":"Unauthorized","status":401,"message":"OAuth token is missing"}`,
},
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
"",
`{"error":"Bad Request","status":400,"message":"Missing required parameter \"id\""}`,
},
{
http.StatusNoContent,
&Options{ClientID: "my-client-id"},
"832389eb-0d0b-41f8-b564-da039f6c4c75",
``,
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
resp, err := c.RemoveEventSubSubscription(testCase.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != testCase.statusCode {
t.Errorf("expected status code to be \"%d\", got \"%d\"", testCase.statusCode, resp.StatusCode)
}
if resp.StatusCode == http.StatusUnauthorized {
if resp.Error != "Unauthorized" {
t.Errorf("expected error to be \"%s\", got \"%s\"", "Unauthorized", resp.Error)
}
if resp.ErrorStatus != http.StatusUnauthorized {
t.Errorf("expected error status to be \"%d\", got \"%d\"", http.StatusUnauthorized, resp.ErrorStatus)
}
expectedErrMsg := "OAuth token is missing"
if resp.ErrorMessage != expectedErrMsg {
t.Errorf("expected error message to be \"%s\", got \"%s\"", expectedErrMsg, resp.ErrorMessage)
}
continue
}
if resp.StatusCode == http.StatusBadRequest {
if resp.Error != "Bad Request" {
t.Errorf("expected error to be \"%s\", got \"%s\"", "Bad Request", resp.Error)
}
expectedErrMsg := `Missing required parameter "id"`
if resp.ErrorMessage != expectedErrMsg {
t.Errorf("expected error message to be \"%s\", got \"%s\"", expectedErrMsg, resp.ErrorMessage)
}
continue
}
}
// Test with HTTP Failure
options := &Options{
ClientID: "my-client-id",
HTTPClient: &badMockHTTPClient{
newMockHandler(0, "", nil),
},
}
c := &Client{
opts: options,
ctx: context.Background(),
}
_, err := c.RemoveEventSubSubscription("832389eb-0d0b-41f8-b564-da039f6c4c75")
if err == nil {
t.Error("expected error but got nil")
}
if err.Error() != "Failed to execute API request: Oops, that's bad :(" {
t.Error("expected error does match return error")
}
}
func TestCreateEventSubSubscriptions(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
params *EventSubSubscription
respBody string
validationErr string
}{
{
http.StatusUnauthorized,
&Options{ClientID: "my-client-id"},
&EventSubSubscription{},
`{"error":"Unauthorized","status":401,"message":"OAuth token is missing"}`,
"error: unsupported transport method: ",
},
{
http.StatusUnauthorized,
&Options{ClientID: "my-client-id"},
&EventSubSubscription{
Transport: EventSubTransport{
Method: "custom",
},
},
`{"error":"Unauthorized","status":401,"message":"OAuth token is missing"}`,
"error: unsupported transport method: custom",
},
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
&EventSubSubscription{
Type: "channel.follow",
Version: "1",
Condition: EventSubCondition{
BroadcasterUserID: "12345678",
},
Transport: EventSubTransport{
Method: "webhook",
Callback: "https://example.com/eventsub/follow",
Secret: "111",
},
},
`{"error":"Bad Request","status":400,"message":"secret must be between 10 and 100 characters"}`,
"error: secret must be between 10 and 100 characters",
},
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
&EventSubSubscription{
Type: "channel.follow",
Version: "1",
Condition: EventSubCondition{
BroadcasterUserID: "12345678",
},
Transport: EventSubTransport{
Method: "webhook",
Callback: "http://example.com/eventsub/follow",
Secret: "s3cr37w0rd",
},
},
`{"error":"Bad Request","status":400,"message":"call back must be https protocol"}`,
"error: callback must use https",
},
{
http.StatusConflict,
&Options{ClientID: "my-client-id"},
&EventSubSubscription{
Type: "channel.follow",
Version: "1",
Condition: EventSubCondition{
BroadcasterUserID: "12345678",
},
Transport: EventSubTransport{
Method: "webhook",
Callback: "https://example.com/eventsub/follow",
Secret: "s3cr37w0rd",
},
},
`{"error":"Conflict","status":409,"message":"subscription already exists"}`,
"",
},
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
&EventSubSubscription{
Type: "channel.follow",
Version: "1",
Condition: EventSubCondition{
BroadcasterUserID: "12345678",
},
Transport: EventSubTransport{
Method: "webhook",
Callback: "https://example.com/eventsub/follow",
Secret: "s3cr37w0rd",
},
},
`{"data":[{"id":"4d06fabc-4cf4-4e99-a60f-b457d5c69305","status":"webhook_callback_verification_pending","type":"channel.follow","version":"1","condition":{"broadcaster_user_id":"12345678"},"created_at":"2021-03-10T23:38:50.311154721Z","transport":{"method":"webhook","callback":"https://example.com/eventsub/follow"},"cost":1}],"limit":10000,"total":1,"max_total_cost":10000,"total_cost":1}`,
"",
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
resp, err := c.CreateEventSubSubscription(testCase.params)
if err != nil {
if err.Error() == testCase.validationErr {
continue
}
t.Error(err)
}
if resp.StatusCode != testCase.statusCode {
t.Errorf("expected status code to be \"%d\", got \"%d\"", testCase.statusCode, resp.StatusCode)
}
if resp.StatusCode == http.StatusUnauthorized {
if resp.Error != "Unauthorized" {
t.Errorf("expected error to be \"%s\", got \"%s\"", "Unauthorized", resp.Error)
}
if resp.ErrorStatus != http.StatusUnauthorized {
t.Errorf("expected error status to be \"%d\", got \"%d\"", http.StatusUnauthorized, resp.ErrorStatus)
}
expectedErrMsg := "OAuth token is missing"
if resp.ErrorMessage != expectedErrMsg {
t.Errorf("expected error message to be \"%s\", got \"%s\"", expectedErrMsg, resp.ErrorMessage)
}
continue
}
if resp.StatusCode == http.StatusConflict {
if resp.Error != "Conflict" {
t.Errorf("expected error to be \"%s\", got \"%s\"", "Conflict", resp.Error)
}
if resp.ErrorStatus != http.StatusConflict {
t.Errorf("expected error status to be \"%d\", got \"%d\"", http.StatusUnauthorized, resp.ErrorStatus)
}
expectedErrMsg := "subscription already exists"
if resp.ErrorMessage != expectedErrMsg {
t.Errorf("expected error message to be \"%s\", got \"%s\"", expectedErrMsg, resp.ErrorMessage)
}
continue
}
if len(resp.Data.EventSubSubscriptions) != 1 {
t.Errorf("expected result length to be \"%d\", got \"%d\"", 1, len(resp.Data.EventSubSubscriptions))
continue
}
if resp.Data.EventSubSubscriptions[0].Transport.Method != "webhook" {
t.Errorf("expected result transport method to be \"%s\", got \"%s\"", "webhook", resp.Data.EventSubSubscriptions[0].Transport.Method)
}
}
}
func TestVerifyEventSubNotification(t *testing.T) {
t.Parallel()
testCases := []struct {
messageID string
messageSignature string
messageTimestamp string
respBody string
secret string
}{
{
"e76c6bd4-55c9-4987-8304-da1588d8988b",
"sha256=7e5a96480c29cdf834b371e7a5b049638cba6e425ea51b9b2a9fabf69bc5d227",
"2019-11-16T10:11:12.123Z",
`{"challenge":"pogchamp-kappa-360noscope-vohiyo","subscription":{"id":"f1c2a387-161a-49f9-a165-0f21d7a4e1c4","status":"webhook_callback_verification_pending","type":"channel.follow","version":"1","condition":{"broadcaster_user_id":"12826"},"transport":{"method":"webhook","callback":"https://example.com/webhooks/callback"},"created_at":"2019-11-16T10:11:12.123Z"}}`,
"s3cRe7",
},
}
for _, testCase := range testCases {
header := http.Header{}
header.Add("Twitch-Eventsub-Message-Id", testCase.messageID)
header.Add("Twitch-Eventsub-Message-Signature", testCase.messageSignature)
header.Add("Twitch-Eventsub-Message-Timestamp", testCase.messageTimestamp)
signatureOk := VerifyEventSubNotification(testCase.secret, header, testCase.respBody)
if !signatureOk {
t.Errorf("expected signature to match \"%s\", but it didn't", testCase.messageSignature)
}
}
}