forked from n0madic/twitter-scraper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth_test.go
58 lines (52 loc) · 1.36 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
package twitterscraper_test
import (
"fmt"
"os"
"testing"
twitterscraper "github.com/RockX-SG/twitter-scraper"
)
var (
username = os.Getenv("TWITTER_USERNAME")
password = os.Getenv("TWITTER_PASSWORD")
email = os.Getenv("TWITTER_EMAIL")
skipAuthTest = os.Getenv("SKIP_AUTH_TEST") != ""
testScraper = twitterscraper.New()
)
func init() {
if username != "" && password != "" && !skipAuthTest {
err := testScraper.Login(username, password, email)
if err != nil {
panic(fmt.Sprintf("Login() error = %v", err))
}
}
}
func TestAuth(t *testing.T) {
if skipAuthTest {
t.Skip("Skipping test due to environment variable")
}
scraper := twitterscraper.New()
if err := scraper.Login(username, password, email); err != nil {
t.Fatalf("Login() error = %v", err)
}
if !scraper.IsLoggedIn() {
t.Fatalf("Expected IsLoggedIn() = true")
}
cookies := scraper.GetCookies()
scraper2 := twitterscraper.New()
scraper2.SetCookies(cookies)
if !scraper2.IsLoggedIn() {
t.Error("Expected restored IsLoggedIn() = true")
}
if err := scraper.Logout(); err != nil {
t.Errorf("Logout() error = %v", err)
}
if scraper.IsLoggedIn() {
t.Error("Expected IsLoggedIn() = false")
}
}
func TestLoginOpenAccount(t *testing.T) {
scraper := twitterscraper.New()
if err := scraper.LoginOpenAccount(); err != nil {
t.Fatalf("LoginOpenAccount() error = %v", err)
}
}