forked from imperatrona/twitter-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
145 lines (120 loc) · 3.34 KB
/
auth_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
package twitterscraper_test
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"testing"
twitterscraper "github.com/imperatrona/twitter-scraper"
)
var (
proxy = os.Getenv("PROXY")
proxyRequired = os.Getenv("PROXY_REQUIRED") != ""
authToken = os.Getenv("AUTH_TOKEN")
csrfToken = os.Getenv("CSRF_TOKEN")
cookies = os.Getenv("COOKIES")
username = os.Getenv("TWITTER_USERNAME")
password = os.Getenv("TWITTER_PASSWORD")
email = os.Getenv("TWITTER_EMAIL")
skipAuthTest = os.Getenv("SKIP_AUTH_TEST") != ""
testScraper = newTestScraper(false)
)
func init() {
if skipAuthTest {
return
}
if authToken != "" && csrfToken != "" {
testScraper.SetAuthToken(twitterscraper.AuthToken{Token: authToken, CSRFToken: csrfToken})
if !testScraper.IsLoggedIn() {
panic("Invalid AuthToken")
}
return
}
if cookies != "" {
var parsedCookies []*http.Cookie
json.NewDecoder(strings.NewReader(cookies)).Decode(&parsedCookies)
testScraper.SetCookies(parsedCookies)
if !testScraper.IsLoggedIn() {
panic("Invalid Cookies")
}
return
}
if username != "" && password != "" {
err := testScraper.Login(username, password, email)
if err != nil {
panic(fmt.Sprintf("Login() error = %v", err))
}
return
}
skipAuthTest = true
fmt.Println("None of any auth data provided, skipping all tests that requires auth")
}
func newTestScraper(skip_auth bool) *twitterscraper.Scraper {
s := twitterscraper.New()
if proxy != "" && proxyRequired {
err := s.SetProxy(proxy)
if err != nil {
panic(fmt.Sprintf("SetProxy() error = %v", err))
}
}
// Check connection by getting guest token
if err := s.GetGuestToken(); err != nil {
panic(fmt.Sprintf("cannot get guest token, can also be error with connection to twitter.\n %v", err))
}
if skip_auth == true || !skipAuthTest {
s.ClearGuestToken()
return s
}
return s
}
func TestLoginPassword(t *testing.T) {
if skipAuthTest || username == "" || password == "" {
t.Skip("Skipping test due to environment variable")
}
scraper := newTestScraper(true)
if err := scraper.Login(username, password, email); err != nil {
t.Fatalf("Login() error = %v", err)
}
if !scraper.IsLoggedIn() {
t.Fatalf("Expected IsLoggedIn() = true")
}
if err := scraper.Logout(); err != nil {
t.Errorf("Logout() error = %v", err)
}
if scraper.IsLoggedIn() {
t.Error("Expected IsLoggedIn() = false")
}
}
func TestLoginToken(t *testing.T) {
if skipAuthTest || authToken == "" || csrfToken == "" {
t.Skip("Skipping test due to environment variable")
}
scraper := newTestScraper(true)
scraper.SetAuthToken(twitterscraper.AuthToken{Token: authToken, CSRFToken: csrfToken})
if !scraper.IsLoggedIn() {
t.Error("Expected IsLoggedIn() = true")
}
}
func TestLoginCookie(t *testing.T) {
if skipAuthTest || cookies == "" {
t.Skip("Skipping test due to environment variable")
}
scraper := newTestScraper(true)
var c []*http.Cookie
json.NewDecoder(strings.NewReader(cookies)).Decode(&c)
scraper.SetCookies(c)
if !scraper.IsLoggedIn() {
t.Error("Expected IsLoggedIn() = true")
}
}
func TestLoginOpenAccount(t *testing.T) {
if os.Getenv("TEST_OPEN_ACCOUNT") == "" {
t.Skip("Skipping test due to environment variable")
}
scraper := twitterscraper.New()
_, err := scraper.LoginOpenAccount()
if err != nil {
t.Fatalf("LoginOpenAccount() error = %v", err)
}
}