2023-05-10 11:42:47 +03:00
|
|
|
package twitterscraper_test
|
|
|
|
|
|
|
|
|
|
import (
|
2024-02-20 23:27:22 +03:00
|
|
|
"encoding/json"
|
2023-07-03 15:06:56 +03:00
|
|
|
"fmt"
|
2024-02-20 23:27:22 +03:00
|
|
|
"net/http"
|
2023-05-10 11:42:47 +03:00
|
|
|
"os"
|
2024-02-20 23:27:22 +03:00
|
|
|
"strings"
|
2023-05-10 11:42:47 +03:00
|
|
|
"testing"
|
|
|
|
|
|
2024-01-28 23:06:45 +03:00
|
|
|
twitterscraper "github.com/imperatrona/twitter-scraper"
|
2023-05-10 11:42:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2024-02-20 23:27:22 +03:00
|
|
|
proxy = os.Getenv("PROXY")
|
|
|
|
|
proxyRequired = os.Getenv("PROXY_REQUIRED") != ""
|
|
|
|
|
authToken = os.Getenv("AUTH_TOKEN")
|
|
|
|
|
ct0 = os.Getenv("CT0")
|
|
|
|
|
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)
|
2023-05-10 11:42:47 +03:00
|
|
|
)
|
|
|
|
|
|
2023-07-03 15:06:56 +03:00
|
|
|
func init() {
|
2024-02-20 23:27:22 +03:00
|
|
|
if skipAuthTest {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if authToken != "" && ct0 != "" {
|
|
|
|
|
testScraper.SetAuthToken(authToken, ct0)
|
|
|
|
|
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 != "" {
|
2023-07-03 15:06:56 +03:00
|
|
|
err := testScraper.Login(username, password, email)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Sprintf("Login() error = %v", err))
|
|
|
|
|
}
|
2024-02-20 23:27:22 +03:00
|
|
|
return
|
2023-07-03 15:06:56 +03:00
|
|
|
}
|
2024-02-20 23:27:22 +03:00
|
|
|
|
2024-03-09 04:08:35 +03:00
|
|
|
skipAuthTest = true
|
2024-03-09 04:45:03 +03:00
|
|
|
fmt.Println("None of any auth data provided, skipping all tests that requires auth")
|
2023-07-03 15:06:56 +03:00
|
|
|
}
|
|
|
|
|
|
2024-02-20 23:27:22 +03:00
|
|
|
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 == "" {
|
2023-05-21 01:29:08 +03:00
|
|
|
t.Skip("Skipping test due to environment variable")
|
|
|
|
|
}
|
2024-02-20 23:27:22 +03:00
|
|
|
scraper := newTestScraper(true)
|
2023-05-10 21:48:41 +03:00
|
|
|
if err := scraper.Login(username, password, email); err != nil {
|
2023-05-10 11:42:47 +03:00
|
|
|
t.Fatalf("Login() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
if !scraper.IsLoggedIn() {
|
2023-05-10 21:48:41 +03:00
|
|
|
t.Fatalf("Expected IsLoggedIn() = true")
|
2023-05-10 11:42:47 +03:00
|
|
|
}
|
2023-05-10 22:24:32 +03:00
|
|
|
if err := scraper.Logout(); err != nil {
|
|
|
|
|
t.Errorf("Logout() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
if scraper.IsLoggedIn() {
|
|
|
|
|
t.Error("Expected IsLoggedIn() = false")
|
|
|
|
|
}
|
2023-05-10 11:42:47 +03:00
|
|
|
}
|
2023-05-30 17:31:00 +03:00
|
|
|
|
2024-02-20 23:27:22 +03:00
|
|
|
func TestLoginToken(t *testing.T) {
|
|
|
|
|
if skipAuthTest || authToken == "" || ct0 == "" {
|
|
|
|
|
t.Skip("Skipping test due to environment variable")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scraper := newTestScraper(true)
|
|
|
|
|
|
|
|
|
|
scraper.SetAuthToken(authToken, ct0)
|
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 17:31:00 +03:00
|
|
|
func TestLoginOpenAccount(t *testing.T) {
|
2024-02-20 23:27:22 +03:00
|
|
|
if os.Getenv("TEST_OPEN_ACCOUNT") == "" {
|
|
|
|
|
t.Skip("Skipping test due to environment variable")
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 17:31:00 +03:00
|
|
|
scraper := twitterscraper.New()
|
2023-10-08 21:58:48 -03:00
|
|
|
_, err := scraper.LoginOpenAccount()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2023-05-30 17:31:00 +03:00
|
|
|
t.Fatalf("LoginOpenAccount() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|