-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
194 lines (175 loc) · 5 KB
/
client_test.go
File metadata and controls
194 lines (175 loc) · 5 KB
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
package weixin
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
)
func TestLoginInteractive(t *testing.T) {
t.Parallel()
var pollCount int
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/ilink/bot/get_bot_qrcode":
_ = json.NewEncoder(w).Encode(map[string]string{
"qrcode": "qr-1",
"qrcode_img_content": "weixin://qr-content",
})
case "/ilink/bot/get_qrcode_status":
pollCount++
if pollCount == 1 {
_ = json.NewEncoder(w).Encode(map[string]string{
"status": "scaned",
})
return
}
_ = json.NewEncoder(w).Encode(map[string]string{
"status": "confirmed",
"bot_token": "bot-token",
"ilink_bot_id": "demo@im.bot",
"baseurl": "https://returned.example",
"ilink_user_id": "user@im.wechat",
})
default:
http.NotFound(w, r)
}
}))
defer server.Close()
tempDir := t.TempDir()
var output bytes.Buffer
client := NewClient(Options{
BaseURL: server.URL,
HTTPClient: server.Client(),
Output: &output,
PollInterval: 5 * time.Millisecond,
QRLongPollTimeout: 100 * time.Millisecond,
})
account, err := client.LoginInteractive(context.Background(), InteractiveLoginOptions{
Output: &output,
Timeout: time.Second,
SaveDir: tempDir,
})
if err != nil {
t.Fatalf("LoginInteractive returned error: %v", err)
}
if account.AccountID != "demo@im.bot" {
t.Fatalf("unexpected account id: %q", account.AccountID)
}
if account.BotToken != "bot-token" {
t.Fatalf("unexpected bot token: %q", account.BotToken)
}
if account.UserID != "user@im.wechat" {
t.Fatalf("unexpected user id: %q", account.UserID)
}
loaded, err := LoadAccount(tempDir, "demo@im.bot")
if err != nil {
t.Fatalf("LoadAccount returned error: %v", err)
}
if loaded.BotToken != "bot-token" {
t.Fatalf("unexpected stored token: %q", loaded.BotToken)
}
}
func TestWaitLoginRefreshesExpiredQR(t *testing.T) {
t.Parallel()
var qrFetches int
var polls int
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/ilink/bot/get_bot_qrcode":
qrFetches++
_ = json.NewEncoder(w).Encode(map[string]string{
"qrcode": "qr-refreshed",
"qrcode_img_content": "weixin://qr-refreshed",
})
case "/ilink/bot/get_qrcode_status":
polls++
if polls == 1 {
_ = json.NewEncoder(w).Encode(map[string]string{"status": "expired"})
return
}
_ = json.NewEncoder(w).Encode(map[string]string{
"status": "confirmed",
"bot_token": "bot-token",
"ilink_bot_id": "demo@im.bot",
})
default:
http.NotFound(w, r)
}
}))
defer server.Close()
client := NewClient(Options{
BaseURL: server.URL,
HTTPClient: server.Client(),
Output: &bytes.Buffer{},
PollInterval: 5 * time.Millisecond,
QRLongPollTimeout: 100 * time.Millisecond,
})
session := &LoginSession{
SessionKey: "session",
QRCode: "qr-old",
QRContent: "weixin://old",
StartedAt: time.Now(),
}
account, err := client.WaitLogin(context.Background(), session, WaitOptions{Timeout: time.Second})
if err != nil {
t.Fatalf("WaitLogin returned error: %v", err)
}
if account.AccountID != "demo@im.bot" {
t.Fatalf("unexpected account id: %q", account.AccountID)
}
if qrFetches != 1 {
t.Fatalf("expected one QR refresh, got %d", qrFetches)
}
if session.QRCode != "qr-refreshed" {
t.Fatalf("expected session QR code to refresh, got %q", session.QRCode)
}
}
func TestSaveAndListAccounts(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
if _, err := SaveAccount(tempDir, &Account{AccountID: "b@im.bot", BotToken: "b"}); err != nil {
t.Fatalf("SaveAccount returned error: %v", err)
}
if _, err := SaveAccount(tempDir, &Account{AccountID: "a@im.bot", BotToken: "a"}); err != nil {
t.Fatalf("SaveAccount returned error: %v", err)
}
accounts, err := ListAccounts(tempDir)
if err != nil {
t.Fatalf("ListAccounts returned error: %v", err)
}
if len(accounts) != 2 {
t.Fatalf("expected 2 accounts, got %d", len(accounts))
}
if accounts[0].AccountID != "a@im.bot" || accounts[1].AccountID != "b@im.bot" {
t.Fatalf("accounts not sorted: %#v", accounts)
}
}
func TestPrintQRCodeWritesOutput(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
if err := PrintQRCode(&buf, "weixin://qr-content"); err != nil {
t.Fatalf("PrintQRCode returned error: %v", err)
}
if buf.Len() == 0 {
t.Fatalf("expected QR output")
}
}
func TestSaveAccountUsesSafeFilename(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
filePath, err := SaveAccount(tempDir, &Account{AccountID: "demo@im.bot", BotToken: "token"})
if err != nil {
t.Fatalf("SaveAccount returned error: %v", err)
}
if filepath.Base(filePath) == "demo@im.bot.json" {
t.Fatalf("expected safe encoded filename, got %q", filePath)
}
if _, err := os.Stat(filePath); err != nil {
t.Fatalf("expected saved file: %v", err)
}
}