-
Notifications
You must be signed in to change notification settings - Fork 1
/
twitter_test.go
615 lines (503 loc) · 14.9 KB
/
twitter_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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
package twitter_test
import (
"encoding/json"
"net/url"
"os"
"strings"
"testing"
"time"
"github.com/cvcio/twitter"
)
var (
consumerKey = os.Getenv("TEST_TWITTER_CONSUMER_KEY")
consumerSecret = os.Getenv("TEST_TWITTER_CONSUMER_SECRET")
accessToken = os.Getenv("TEST_TWITTER_ACCESS_TOKEN")
accessTokenSecret = os.Getenv("TEST_TWITTER_ACCESS_TOKEN_SECRET")
)
// Test_API_NewAPI_Client Test New Twitter API Client
// func Test_NewTwitter_PKCE(t *testing.T) {
// api, err := twitter.NewTwitterWithPKCE(consumerKey, consumerSecret, accessToken, accessTokenSecret)
// if err != nil {
// t.Fatalf("Couldn't create Twitter API HTTP Client")
// }
// if api.GetClient() == nil {
// t.Fatalf("Twitter API HTTP Client returned nil")
// }
// }
// Test_API_NewAPI_Client Test New Twitter API Client
func Test_NewTwitter_Client(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Couldn't create Twitter API HTTP Client")
}
if api.GetClient() == nil {
t.Fatalf("Twitter API HTTP Client returned nil")
}
}
func Test_NewTwitterWithContext_Client(t *testing.T) {
api, err := twitter.NewTwitterWithContext(consumerKey, consumerSecret, accessToken, accessTokenSecret)
if err != nil {
t.Fatalf("Couldn't create Twitter API HTTP Client")
}
if api.GetClient() == nil {
t.Fatalf("Twitter API HTTP Client returned nil")
}
}
// Test_API_NewAPI_VerifyCredentials Test Twitter API VerifyCredentials
func Test_NewTwitter_VerifyCredentials(t *testing.T) {
api, _ := twitter.NewTwitter(consumerKey, consumerSecret)
ok, err := api.VerifyCredentials()
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
if !ok {
t.Fatalf("Twitter API VerifyCredentials Error: %v", ok)
}
}
func Test_NewTwitterWithContext_VerifyCredentials(t *testing.T) {
api, err := twitter.NewTwitterWithContext(consumerKey, consumerSecret, accessToken, accessTokenSecret)
ok, err := api.VerifyCredentials()
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
if !ok {
t.Fatalf("Twitter API VerifyCredentials Error: %v", ok)
}
}
func Test_GetUserFollowers(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
var data []*twitter.User
v := url.Values{}
v.Add("max_results", "50")
v.Add("user.fields", "created_at,description,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified")
v.Add("expansions", "pinned_tweet_id")
v.Add("tweet.fields", "created_at,id,lang,source,public_metrics")
res, errs := api.GetUserFollowers("44142397", v, twitter.WithRate(15*time.Minute/15), twitter.WithAuto(false)) // @andefined
for {
r, rok := <-res
if !rok {
break
}
if r != nil {
b, err := json.Marshal(r.Data)
if err != nil {
t.Errorf("json Marshal Error: %v", err)
}
json.Unmarshal(b, &data)
}
e, eok := <-errs
if !eok {
break
}
if e != nil {
t.Errorf("Twitter API Error: %v", e)
break
}
}
if len(data) != 50 {
t.Fatalf("Twitter API GetUserFollowers Error. Should have returned 50, got %d", len(data))
}
}
func Test_GetUserFollowers_Error(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
v := url.Values{}
v.Add("max_results", "5000")
res, errs := api.GetUserFollowers("44142397", v, twitter.WithRate(15*time.Minute/15), twitter.WithAuto(true)) // @andefined
for {
_, rok := <-res
if !rok {
break
}
e, ok := <-errs
if !ok {
break
}
if e == nil || !strings.Contains(e.Error(), "400") {
t.Fatalf("Should have returned 400, got %s instead", e.Error())
break
}
}
}
func Test_Channels(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
size := 0
v := url.Values{}
v.Add("max_results", "1000")
res, errs := api.GetUserFollowing("44142397", v, twitter.WithRate(time.Minute/6), twitter.WithAuto(true)) // @andefined
for {
select {
case r, ok := <-res:
if !ok {
res = nil
break
}
var d []*twitter.User
b, err := json.Marshal(r.Data)
if err != nil {
t.Fatalf("json Marshal Error: %v", err)
}
json.Unmarshal(b, &d)
size += len(d)
case e, ok := <-errs:
if !ok {
errs = nil
break
}
t.Errorf("Twitter API Error: %v", e)
}
if res == nil && errs == nil {
break
}
}
if size < 1000 {
t.Fatalf("Twitter API GetUserFollowing Error. Should have returned more than 1000, got %d", size)
}
}
func Test_GetUserFollowing(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
var data []*twitter.User
v := url.Values{}
v.Add("max_results", "50")
res, _ := api.GetUserFollowing("44142397", v, twitter.WithRate(15*time.Minute/15), twitter.WithAuto(false)) // @andefined
for {
r, ok := <-res
if !ok {
break
}
b, err := json.Marshal(r.Data)
if err != nil {
t.Fatalf("json Marshal Error: %v", err)
}
json.Unmarshal(b, &data)
}
if len(data) != 50 {
t.Fatalf("Twitter API GetUserFollowing Error. Should have returned 50, got %d", len(data))
}
}
func Test_GetUsers(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
var data []*twitter.User
v := url.Values{}
v.Add("ids", "44142397,334602996")
res, _ := api.GetUsers(v) // @andefined, @atsipras
for {
r, ok := <-res
if !ok {
break
}
b, err := json.Marshal(r.Data)
if err != nil {
t.Fatalf("json Marshal Error: %v", err)
}
json.Unmarshal(b, &data)
}
if len(data) != 2 {
t.Fatalf("Twitter API GetUsers Error. Should have returned 2, got %d", len(data))
}
}
func Test_GetUsersBy(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
var data []*twitter.User
v := url.Values{}
v.Add("usernames", "andefined,atsipras")
res, _ := api.GetUsersBy(v) // @andefined, @atsipras
for {
r, ok := <-res
if !ok {
break
}
b, err := json.Marshal(r.Data)
if err != nil {
t.Fatalf("json Marshal Error: %v", err)
}
json.Unmarshal(b, &data)
}
if len(data) != 2 {
t.Fatalf("Twitter API GetUsersBy Error. Should have returned 2, got %d", len(data))
}
}
func Test_GetUserByID(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
var data *twitter.User
v := url.Values{}
res, _ := api.GetUserByID("44142397", v) // @andefined
for {
r, ok := <-res
if !ok {
break
}
b, err := json.Marshal(r.Data)
if err != nil {
t.Fatalf("json Marshal Error: %v", err)
}
json.Unmarshal(b, &data)
}
if data.UserName != "andefined" {
t.Fatalf("Twitter API GetUserByID Error. Should have returned andefined, got %s", data.UserName)
}
}
func Test_GetUsersByUserName(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
var data *twitter.User
v := url.Values{}
res, _ := api.GetUsersByUserName("andefined", v) // @andefined
for {
r, ok := <-res
if !ok {
break
}
b, err := json.Marshal(r.Data)
if err != nil {
t.Fatalf("json Marshal Error: %v", err)
}
json.Unmarshal(b, &data)
}
if data.UserName != "andefined" {
t.Fatalf("Twitter API GetUsersByUserName Error. Should have returned andefined, got %s", data.UserName)
}
}
func Test_GetUserTweets(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
var data []*twitter.Tweet
v := url.Values{}
v.Add("max_results", "50")
res, _ := api.GetUserTweets("44142397", v, twitter.WithRate(15*time.Minute/1500), twitter.WithAuto(false)) // @andefined
for {
r, ok := <-res
if !ok {
break
}
b, err := json.Marshal(r.Data)
if err != nil {
t.Fatalf("json Marshal Error: %v", err)
}
json.Unmarshal(b, &data)
}
if len(data) < 1 {
t.Fatalf("Twitter API GetUserTweets Error. Should have returned >= 1, got %d", len(data))
}
}
func Test_GetUsesMentions(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
var data []*twitter.Tweet
v := url.Values{}
v.Add("max_results", "10")
res, _ := api.GetUserMentions("44142397", v, twitter.WithRate(15*time.Minute/450), twitter.WithAuto(false)) // @andefined
for {
r, ok := <-res
if !ok {
break
}
b, err := json.Marshal(r.Data)
if err != nil {
t.Fatalf("json Marshal Error: %v", err)
}
json.Unmarshal(b, &data)
}
if len(data) != 10 {
t.Fatalf("Twitter API GetUserMentions Error. Should have returned 10, got %d", len(data))
}
}
func Test_GetTweets(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
var data []*twitter.Tweet
v := url.Values{}
v.Add("ids", "1370136892432322569,1370704815983038469")
res, _ := api.GetTweets(v, twitter.WithRate(15*time.Minute/300), twitter.WithAuto(false)) // https://twitter.com/andefined/status/1370136892432322569, https://twitter.com/andefined/status/1370704815983038469
for {
r, ok := <-res
if !ok {
break
}
b, err := json.Marshal(r.Data)
if err != nil {
t.Fatalf("json Marshal Error: %v", err)
}
json.Unmarshal(b, &data)
}
if len(data) != 2 {
t.Fatalf("Twitter API GetTweets Error. Should have returned 2, got %d", len(data))
}
if data[0].Text != "interesting comparison @kmitsotakis vs @atsipras https://t.co/WWcQjOgAtz" {
t.Fatalf("Twitter API GetTweets Error. Should have returned `interesting comparison @kmitsotakis vs @atsipras https://t.co/WWcQjOgAtz`, got %s", data[0].Text)
}
}
func Test_GetTweetByID(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
var data *twitter.Tweet
v := url.Values{}
res, _ := api.GetTweetByID("1370136892432322569", v) // https://twitter.com/andefined/status/1370136892432322569
for {
r, ok := <-res
if !ok {
break
}
b, err := json.Marshal(r.Data)
if err != nil {
t.Fatalf("json Marshal Error: %v", err)
}
json.Unmarshal(b, &data)
}
if data.Text != "interesting comparison @kmitsotakis vs @atsipras https://t.co/WWcQjOgAtz" {
t.Fatalf("Twitter API GetTweets Error. Should have returned `interesting comparison @kmitsotakis vs @atsipras https://t.co/WWcQjOgAtz`, got %s", data.Text)
}
}
func Test_GetTweetsSearchRecent(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
var data []*twitter.Tweet
v := url.Values{}
v.Add("query", "covid")
v.Add("max_results", "100")
res, _ := api.GetTweetsSearchRecent(v, twitter.WithAuto(false))
for {
r, ok := <-res
if !ok {
break
}
b, err := json.Marshal(r.Data)
if err != nil {
t.Fatalf("json Marshal Error: %v", err)
}
json.Unmarshal(b, &data)
}
if len(data) != 100 {
t.Fatalf("Twitter API GetTweetsSearchRecent Error. Should have returned 100, got %d", len(data))
}
}
func Test_PostFilterStreamRules(t *testing.T) {
api, _ := twitter.NewTwitter(consumerKey, consumerSecret)
rules := new(twitter.Rules)
rules.Add = append(rules.Add, &twitter.RulesData{
Value: "greece",
Tag: "test-client",
})
v := url.Values{}
res, err := api.PostFilterStreamRules(v, rules)
if err != nil {
t.Fatalf("Twitter API PostFilterStreamRules Error: %s", err.Error())
}
if len(res.Data) != 1 {
t.Fatalf("Twitter API PostFilterStreamRules Error. Should have returned 1, got %d", len(res.Data))
}
if len(res.Errors) != 0 {
t.Fatalf("Twitter API PostFilterStreamRules Error. Should have returned 0 error Error(), got %d", len(res.Errors))
}
}
func Test_GetFilterStreamRules(t *testing.T) {
api, _ := twitter.NewTwitter(consumerKey, consumerSecret)
_, err := api.GetFilterStreamRules(nil)
if err != nil {
t.Fatalf("Twitter API GetFilterStreamRules Error: %s", err.Error())
}
}
func Test_GetFilterStream(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
v := url.Values{}
v.Add("user.fields", "created_at,description,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified")
v.Add("expansions", "author_id,in_reply_to_user_id")
v.Add("tweet.fields", "created_at,id,lang,source,public_metrics")
var f twitter.StreamData
s, serr := api.GetFilterStream(v)
if serr != nil {
t.Fatalf("Twitter API GetFilterStream Error: %s", serr.Error())
}
for x := range s.C {
f, _ = x.(twitter.StreamData)
t.Log(f)
break
}
s.Stop()
if f.Data.ID == "" {
t.Fatalf("Twitter API GetFilterStream Error. Should have return a valid tweet struct, got %v", f.Data)
}
}
func Test_GetSampleStream(t *testing.T) {
api, err := twitter.NewTwitter(consumerKey, consumerSecret)
if err != nil {
t.Fatalf("Twitter API VerifyCredentials Error: %s", err.Error())
}
v := url.Values{}
v.Add("user.fields", "created_at,description,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified")
v.Add("expansions", "author_id,in_reply_to_user_id")
v.Add("tweet.fields", "created_at,id,lang,source,public_metrics")
var f twitter.StreamData
s, serr := api.GetSampleStream(v)
if serr != nil {
t.Fatalf("Twitter API GetSampleStream Error: %s", serr.Error())
}
for t := range s.C {
f, _ = t.(twitter.StreamData)
break
}
s.Stop()
if f.Data.ID == "" {
t.Fatalf("Twitter API GetSampleStream Error. Should have return a valid tweet struct, got %v", f.Data)
}
}
func Test_DeleteFilterStreamRules(t *testing.T) {
api, _ := twitter.NewTwitter(consumerKey, consumerSecret)
rulesToDelete, err := api.GetFilterStreamRules(nil)
if err != nil {
t.Fatalf("Twitter API GetFilterStreamRules Error: %s", err.Error())
}
var ids []string
for _, v := range rulesToDelete.Data {
ids = append(ids, v.ID)
}
rules := new(twitter.Rules)
rules.Delete = &twitter.RulesDelete{
Ids: ids,
}
res, err := api.PostFilterStreamRules(nil, rules)
if err != nil {
t.Fatalf("Twitter API PostFilterStreamRules Error: %s", err.Error())
}
if res.Meta.Summary.Deleted != 1 {
t.Fatalf("Twitter API PostFilterStreamRules Error. Should have returned 1, got %d", res.Meta.Summary.Deleted)
}
if len(res.Errors) > 0 {
t.Fatalf("Twitter API PostFilterStreamRules Error. Should have returned 0 error Error(), got %d", len(res.Errors))
}
}