-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapi_test.go
91 lines (76 loc) · 2.46 KB
/
api_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
package pixiv
import (
"os"
"testing"
"time"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/require"
)
func setupAPIMockTest(code int, responseFile string) {
httpmock.Activate()
resp, _ := getMockedResponse(responseFile)
httpmock.RegisterResponder("POST", "https://oauth.secure.pixiv.net/auth/token",
httpmock.NewStringResponder(code, resp))
}
func TestAuth(t *testing.T) {
username := os.Getenv("USERNAME")
password := os.Getenv("PASSWORD")
testUID := os.Getenv("TEST_UID")
if username == "" || password == "" || testUID == "" {
t.Log("No username or password found, mock TestAuth")
testUID = "12345678"
setupAPIMockTest(200, "auth.json")
}
r := require.New(t)
account, err := Login(username, username)
r.Nil(err)
r.Equal(testUID, account.ID)
httpmock.DeactivateAndReset()
}
func TestLoadAuth(t *testing.T) {
token := os.Getenv("TOKEN")
refreshToken := os.Getenv("REFRESH_TOKEN")
testUID := os.Getenv("TEST_UID")
if token == "" || refreshToken == "" {
t.Log("No token or refresh token found, mock TestLoadAuth")
setupAPIMockTest(200, "auth_refresh_token.json")
token = "xxxxxxxx"
refreshToken = "xxxxxxxxxxxxxx"
testUID = "12345678"
}
r := require.New(t)
account, err := LoadAuth(token, refreshToken, time.Time{})
r.Nil(err)
r.Equal(testUID, account.ID)
httpmock.DeactivateAndReset()
}
func TestLoginFail(t *testing.T) {
username := os.Getenv("USERNAME")
password := os.Getenv("PASSWORD")
if username == "" || password == "" {
t.Log("No username or password found, mock TestLoginFail")
setupAPIMockTest(400, "auth_invalid_password.json")
username = "fake_username"
password = "fake_password"
}
r := require.New(t)
account, err := Login(username[:5], password[:5])
r.Nil(account)
r.EqualError(err, "login failed: Login system error: 103:pixiv ID、またはメールアドレス、パスワードが正しいかチェックしてください。")
httpmock.DeactivateAndReset()
}
func TestRefreshTokenFail(t *testing.T) {
token := os.Getenv("TOKEN")
refreshToken := os.Getenv("REFRESH_TOKEN")
if token == "" || refreshToken == "" {
t.Log("No token or refresh token found, mock TestRefreshTokenFail")
setupAPIMockTest(400, "auth_invalid_token.json")
token = "xxxxxxxx"
refreshToken = "xxxxxxxxxxxxxx"
}
r := require.New(t)
account, err := LoadAuth(token, refreshToken[:10], time.Time{})
r.Nil(account)
r.EqualError(err, "refresh token: Login system error: Invalid refresh token")
httpmock.DeactivateAndReset()
}