-
Notifications
You must be signed in to change notification settings - Fork 88
/
users_test.go
538 lines (455 loc) · 15.5 KB
/
users_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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package helix
import (
"context"
"net/http"
"testing"
)
func TestGetUsers(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
IDs []string
Logins []string
respBody string
expectUsers []string
}{
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
[]string{},
[]string{},
`{"error":"Bad Request","status":400,"message":"Must provide an ID, Login or OAuth Token"}`,
[]string{},
},
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
[]string{"26301881"},
[]string{"summit1g"},
`{"data":[{"id":"26301881","login":"sodapoppin","display_name":"sodapoppin","type":"","broadcaster_type":"partner","description":"Wtf do i write here? Click my stream, or i scream.","profile_image_url":"https://static-cdn.jtvnw.net/jtv_user_pictures/sodapoppin-profile_image-10049b6200f90c14-300x300.png","offline_image_url":"https://static-cdn.jtvnw.net/jtv_user_pictures/sodapoppin-channel_offline_image-2040c6fcacec48db-1920x1080.jpeg","view_count":190154823,"created_at":"2011-11-22T04:40:56.75883Z"},{"id":"26490481","login":"summit1g","display_name":"summit1g","type":"","broadcaster_type":"partner","description":"I'm a competitive CounterStrike player who likes to play casually now and many other games. You will mostly see me play CS, H1Z1,and single player games at night. There will be many othergames played on this stream in the future as they come out:D","profile_image_url":"https://static-cdn.jtvnw.net/jtv_user_pictures/200cea12142f2384-profile_image-300x300.png","offline_image_url":"https://static-cdn.jtvnw.net/jtv_user_pictures/summit1g-channel_offline_image-e2f9a1df9e695ec1-1920x1080.png","view_count":202707885,"created_at":"2011-12-01T06:33:31.487567Z"}]}`,
[]string{"sodapoppin", "summit1g"},
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
resp, err := c.GetUsers(&UsersParams{
IDs: testCase.IDs,
Logins: testCase.Logins,
})
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.StatusBadRequest {
if resp.Error != "Bad Request" {
t.Errorf("expected error to be \"%s\", got \"%s\"", "Bad Request", resp.Error)
}
if resp.ErrorStatus != http.StatusBadRequest {
t.Errorf("expected error status to be \"%d\", got \"%d\"", http.StatusBadRequest, resp.ErrorStatus)
}
expectedErrMsg := "Must provide an ID, Login or OAuth Token"
if resp.ErrorMessage != expectedErrMsg {
t.Errorf("expected error message to be \"%s\", got \"%s\"", expectedErrMsg, resp.ErrorMessage)
}
continue
}
if resp.Data.Users[0].Login != testCase.expectUsers[0] { // sodapoppin
t.Errorf("expected username 1 to be \"%s\", got \"%s\"", testCase.expectUsers[0], resp.Data.Users[0].Login)
}
if resp.Data.Users[1].Login != testCase.expectUsers[1] { // summit1g
t.Errorf("expected username 2 to be \"%s\", got \"%s\"", testCase.expectUsers[0], resp.Data.Users[0].Login)
}
}
// Test with HTTP Failure
options := &Options{
ClientID: "my-client-id",
HTTPClient: &badMockHTTPClient{
newMockHandler(0, "", nil),
},
}
c := &Client{
opts: options,
ctx: context.Background(),
}
_, err := c.GetUsers(&UsersParams{})
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 TestUpdateUser(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
respBody string
description string
}{
{
http.StatusForbidden,
&Options{ClientID: "my-client-id"},
`{"error":"Forbidden","status":403,"message":"Missing user:edit scope"}`, // missing required scope
"new description",
},
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
`{"data":[{"id":"26301881","login":"sodapoppin","display_name":"sodapoppin","type":"","broadcaster_type":"partner","description":"new description","profile_image_url":"https://static-cdn.jtvnw.net/jtv_user_pictures/sodapoppin-profile_image-10049b6200f90c14-300x300.png","offline_image_url":"https://static-cdn.jtvnw.net/jtv_user_pictures/sodapoppin-channel_offline_image-2040c6fcacec48db-1920x1080.jpeg","view_count":190154823,"created_at":"2011-11-22T04:40:56.75883Z"}]}`,
"new description",
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
resp, err := c.UpdateUser(&UpdateUserParams{
Description: testCase.description,
})
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.StatusForbidden {
if resp.Error != "Forbidden" {
t.Errorf("expected error to be \"%s\", got \"%s\"", "Bad Request", resp.Error)
}
if resp.ErrorStatus != http.StatusForbidden {
t.Errorf("expected error status to be \"%d\", got \"%d\"", http.StatusForbidden, resp.ErrorStatus)
}
expectedErrMsg := "Missing user:edit scope"
if resp.ErrorMessage != expectedErrMsg {
t.Errorf("expected error message to be \"%s\", got \"%s\"", expectedErrMsg, resp.ErrorMessage)
}
continue
}
if resp.Data.Users[0].Description != testCase.description {
t.Errorf("expected description to be \"%s\", got \"%s\"", testCase.description, resp.Data.Users[0].Description)
}
}
// Test with HTTP Failure
options := &Options{
ClientID: "my-client-id",
HTTPClient: &badMockHTTPClient{
newMockHandler(0, "", nil),
},
}
c := &Client{
opts: options,
ctx: context.Background(),
}
_, err := c.UpdateUser(&UpdateUserParams{})
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 TestGetUsersFollows(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
FromID string
First int
respBody string
}{
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
"", // missing from_id
2,
`{"error":"Bad Request","status":400,"message":"Must provide either from_id or to_id"}`,
},
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
"23161357",
2,
`{"total":89,"data":[{"from_id":"23161357","from_name":"lirik","to_id":"23528098","to_name":"avoidingthepuddle","followed_at":"2017-10-01T03:57:21Z"},{"from_id":"23161357","from_name":"lirik","to_id":"127506955","to_name":"playbattlegrounds","followed_at":"2017-08-23T15:04:20Z"}],"pagination":{"cursor":"eyJiIjpudWxsLCJhIjoiMTUwMzUwMDY2MDYwNzAyNTAwMCJ9"}}`,
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
resp, err := c.GetUsersFollows(&UsersFollowsParams{
First: testCase.First,
FromID: testCase.FromID,
})
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.StatusBadRequest {
if resp.Error != "Bad Request" {
t.Errorf("expected error to be \"%s\", got \"%s\"", "Bad Request", resp.Error)
}
if resp.ErrorStatus != http.StatusBadRequest {
t.Errorf("expected error status to be \"%d\", got \"%d\"", http.StatusBadRequest, resp.ErrorStatus)
}
expectedErrMsg := "Must provide either from_id or to_id"
if resp.ErrorMessage != expectedErrMsg {
t.Errorf("expected error message to be \"%s\", got \"%s\"", expectedErrMsg, resp.ErrorMessage)
}
continue
}
if len(resp.Data.Follows) != testCase.First {
t.Errorf("expected result length to be \"%d\", got \"%d\"", testCase.First, len(resp.Data.Follows))
}
for _, follow := range resp.Data.Follows {
if follow.FromID != testCase.FromID {
t.Errorf("expected from_id to be \"%s\", got \"%s\"", testCase.FromID, follow.FromID)
}
}
}
// Test with HTTP Failure
options := &Options{
ClientID: "my-client-id",
HTTPClient: &badMockHTTPClient{
newMockHandler(0, "", nil),
},
}
c := &Client{
opts: options,
ctx: context.Background(),
}
_, err := c.GetUsersFollows(&UsersFollowsParams{})
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 TestGetUsersBlocked(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
BroadcasterID string
First int
respBody string
}{
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
"",
1,
`{"error":"Bad Request","status":400,"message":"Missing required parameter \"broadcaster_id\""}`,
},
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
"23161357",
1,
`{"data":[{"user_id":"199340135","user_login":"jlarkyzus","display_name":"JLArkyzus"}],"pagination":{"cursor":"xxx"}}`,
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
resp, err := c.GetUsersBlocked(&UsersBlockedParams{
First: testCase.First,
BroadcasterID: testCase.BroadcasterID,
})
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.StatusBadRequest {
if resp.Error != "Bad Request" {
t.Errorf("expected error to be \"%s\", got \"%s\"", "Bad Request", resp.Error)
}
if resp.ErrorStatus != http.StatusBadRequest {
t.Errorf("expected error status to be \"%d\", got \"%d\"", http.StatusBadRequest, resp.ErrorStatus)
}
expectedErrMsg := "Missing required parameter \"broadcaster_id\""
if resp.ErrorMessage != expectedErrMsg {
t.Errorf("expected error message to be \"%s\", got \"%s\"", expectedErrMsg, resp.ErrorMessage)
}
continue
}
if len(resp.Data.Users) != testCase.First {
t.Errorf("expected result length to be \"%d\", got \"%d\"", testCase.First, len(resp.Data.Users))
}
userID := "199340135"
if resp.Data.Users[0].UserID != userID {
t.Errorf("expected user id to be \"%s\", got \"%s\"", userID, resp.Data.Users[0].UserID)
}
userLogin := "jlarkyzus"
if resp.Data.Users[0].UserLogin != userLogin {
t.Errorf("expected user id to be \"%s\", got \"%s\"", userLogin, resp.Data.Users[0].UserLogin)
}
displayName := "JLArkyzus"
if resp.Data.Users[0].DisplayName != displayName {
t.Errorf("expected user id to be \"%s\", got \"%s\"", displayName, resp.Data.Users[0].DisplayName)
}
cursor := "xxx"
if resp.Data.Pagination.Cursor != cursor {
t.Errorf("expected cursor to be \"%s\", got \"%s\"", cursor, resp.Data.Pagination.Cursor)
}
}
// Test with HTTP Failure
options := &Options{
ClientID: "my-client-id",
HTTPClient: &badMockHTTPClient{
newMockHandler(0, "", nil),
},
}
c := &Client{
opts: options,
ctx: context.Background(),
}
_, err := c.GetUsersBlocked(&UsersBlockedParams{})
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 TestBlockUser(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
Params *BlockUserParams
respBody string
}{
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
&BlockUserParams{
TargetUserID: "199340135",
SourceContext: "twitter",
Reason: "spam",
},
`{"error":"Bad Request","status":400,"message":"The parameter \"source_context\" was malformed: value must be one of \"chat\", \"whisper\", \"\""}`,
},
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
&BlockUserParams{
TargetUserID: "199340135",
SourceContext: "chat",
Reason: "spam",
},
``,
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
resp, err := c.BlockUser(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.StatusBadRequest {
if resp.Error != "Bad Request" {
t.Errorf("expected error to be \"%s\", got \"%s\"", "Bad Request", resp.Error)
}
if resp.ErrorStatus != http.StatusBadRequest {
t.Errorf("expected error status to be \"%d\", got \"%d\"", http.StatusBadRequest, resp.ErrorStatus)
}
expectedErrMsg := "The parameter \"source_context\" was malformed: value must be one of \"chat\", \"whisper\", \"\""
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.BlockUser(&BlockUserParams{})
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 TestUnblockUser(t *testing.T) {
t.Parallel()
testCases := []struct {
statusCode int
options *Options
Params *UnblockUserParams
respBody string
}{
{
http.StatusBadRequest,
&Options{ClientID: "my-client-id"},
&UnblockUserParams{
TargetUserID: "",
},
`{"error":"Bad Request","status":400,"message":"Missing required parameter \"target_user_id\""}`,
},
{
http.StatusOK,
&Options{ClientID: "my-client-id"},
&UnblockUserParams{
TargetUserID: "199340135",
},
``,
},
}
for _, testCase := range testCases {
c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
resp, err := c.UnblockUser(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.StatusBadRequest {
if resp.Error != "Bad Request" {
t.Errorf("expected error to be \"%s\", got \"%s\"", "Bad Request", resp.Error)
}
if resp.ErrorStatus != http.StatusBadRequest {
t.Errorf("expected error status to be \"%d\", got \"%d\"", http.StatusBadRequest, resp.ErrorStatus)
}
expectedErrMsg := "Missing required parameter \"target_user_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.UnblockUser(&UnblockUserParams{})
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")
}
}