forked from imperatrona/twitter-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
215 lines (185 loc) · 5.13 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
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
package twitterscraper_test
import (
"encoding/json"
"fmt"
"net/http"
"os"
"testing"
"time"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
twitterscraper "github.com/masa-finance/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() {
// Set log level to Debug
logrus.SetLevel(logrus.DebugLevel)
// Optionally, set a custom formatter
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
if err := godotenv.Load(); err != nil {
logrus.WithError(err).Warn("Error loading .env file")
}
username = os.Getenv("TWITTER_USERNAME")
password = os.Getenv("TWITTER_PASSWORD")
email = os.Getenv("TWITTER_EMAIL")
skipAuthTest = os.Getenv("SKIP_AUTH_TEST") != ""
logrus.WithFields(logrus.Fields{
"username": username,
"password": password,
"email": email,
"skipAuthTest": skipAuthTest,
}).Info("Environment variables loaded")
}
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 {
err := s.ClearGuestToken()
if err != nil {
return nil
}
return s
}
return s
}
func TestBasic(t *testing.T) {
//client := httpwrap.NewClient().WithJar() // Ensure cookies are handled
scraper := twitterscraper.New()
if scraper == nil {
t.Fatalf("New() returned nil")
}
req, err := http.NewRequest("GET", "https://twitter.com", nil)
if err != nil {
fmt.Println("Error creating request:", err)
return
}
// Set a User-Agent header
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3")
resp, err := scraper.GetHTTPClient().Do(req)
if err != nil {
fmt.Println("Error making request:", err)
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
fmt.Println("Request successful")
} else {
fmt.Printf("Request failed with status: %d\n", resp.StatusCode)
}
}
func TestAuth(t *testing.T) {
if skipAuthTest {
t.Skip("Skipping test due to environment variable")
}
scraper := twitterscraper.New()
// Add a short delay before login attempt
time.Sleep(2 * time.Second)
err := scraper.Login(username, password, email)
if err != nil {
t.Fatalf("Login() error = %v", err)
}
// Add a short delay after login attempt
time.Sleep(2 * time.Second)
if !scraper.IsLoggedIn() {
t.Fatalf("Expected IsLoggedIn() = true")
}
// Save cookies
cookies := scraper.GetCookies()
err = saveCookiesToFile(cookies, "twitter_cookies.json")
if err != nil {
t.Fatalf("Failed to save cookies: %v", err)
}
// Log success
t.Log("Successfully logged in and saved cookies")
}
//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)
// }
//}
func saveCookiesToFile(cookies []*http.Cookie, filename string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
return encoder.Encode(cookies)
}