forked from huandu/facebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_test.go
402 lines (312 loc) · 9.8 KB
/
session_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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// A facebook graph api client in go.
// https://github.com/huandu/facebook/
//
// Copyright 2012, Huan Du
// Licensed under the MIT license
// https://github.com/huandu/facebook/blob/master/LICENSE
package facebook
import (
"bytes"
"context"
"encoding/base64"
"net/http"
"net/http/httptest"
"testing"
)
func TestSession(t *testing.T) {
if FB_TEST_VALID_ACCESS_TOKEN == "" {
t.Skipf("skip this case as we don't have a valid access token.")
}
session := &Session{}
session.SetAccessToken(FB_TEST_VALID_ACCESS_TOKEN)
test := func(t *testing.T, session *Session) {
id, err := session.User()
if err != nil {
t.Fatalf("cannot get current user id. [e:%v]", err)
}
t.Logf("current user id is %v", id)
result, e := session.Api("/me", GET, Params{
"fields": "id,email,website",
})
if e != nil {
t.Fatalf("cannot get my extended info. [e:%v]", e)
}
if Version == "" {
t.Log("use default facebook version.")
} else {
t.Logf("global facebook version: %v", Version)
}
if session.Version == "" {
t.Log("use default session facebook version.")
} else {
t.Logf("session facebook version: %v", session.Version)
}
t.Logf("my extended info is: %v", result)
}
// Default version.
test(t, session)
// Global version overwrite default session version.
func() {
Version = FB_LATEST_VERSION
defer func() {
Version = ""
}()
test(t, session)
}()
// Session version overwrite default version.
func() {
Version = "vx.y" // an invalid version.
session.Version = FB_LATEST_VERSION
defer func() {
Version = ""
}()
test(t, session)
}()
// Session with appsecret proof enabled.
if FB_TEST_VALID_ACCESS_TOKEN != "" {
app := New(FB_TEST_APP_ID, FB_TEST_APP_SECRET)
app.EnableAppsecretProof = true
session := app.Session(FB_TEST_VALID_ACCESS_TOKEN)
_, e := session.Api("/me", GET, Params{
"fields": "id",
})
if e != nil {
t.Fatalf("cannot get my info with proof. [e:%v]", e)
}
}
}
func TestUploadingBinary(t *testing.T) {
if FB_TEST_VALID_ACCESS_TOKEN == "" {
t.Skipf("skip this case as we don't have a valid access token.")
}
t.Skipf("facebook doesn't support uploading photo to timeline.")
buf := bytes.NewBufferString(FB_TEST_BINARY_JPG_FILE)
reader := base64.NewDecoder(base64.StdEncoding, buf)
session := &Session{}
session.SetAccessToken(FB_TEST_VALID_ACCESS_TOKEN)
result, e := session.Api("/317426148998929/photos", POST, Params{
"caption": "Test photo from https://github.com/huandu/facebook",
"source": Data("attachment.jpg", reader),
"published": true,
})
if e != nil {
t.Fatalf("cannot create photo on page timeline. [e:%v]", e)
}
var id string
e = result.DecodeField("id", &id)
if e != nil {
t.Fatalf("facebook should return photo id on success. [e:%v]", e)
}
t.Logf("newly created photo id is %v", id)
}
func TestUploadBinaryWithBatch(t *testing.T) {
if FB_TEST_VALID_ACCESS_TOKEN == "" {
t.Skipf("skip this case as we don't have a valid access token.")
}
buf1 := bytes.NewBufferString(FB_TEST_BINARY_JPG_FILE)
reader1 := base64.NewDecoder(base64.StdEncoding, buf1)
buf2 := bytes.NewBufferString(FB_TEST_BINARY_JPG_FILE)
reader2 := base64.NewDecoder(base64.StdEncoding, buf2)
session := &Session{}
session.SetAccessToken(FB_TEST_VALID_ACCESS_TOKEN)
// sample comes from facebook batch api sample.
// https://developers.facebook.com/docs/reference/api/batch/
//
// curl
// -F 'access_token=…' \
// -F 'batch=[{"method":"POST","relative_url":"me/photos","body":"message=My cat photo","attached_files":"file1"},{"method":"POST","relative_url":"me/photos","body":"message=My dog photo","attached_files":"file2"},]' \
// -F '[email protected]' \
// -F '[email protected]' \
// https://graph.facebook.com
result, e := session.Batch(Params{
"file1": Data("cat.jpg", reader1),
"file2": Data("dog.jpg", reader2),
}, Params{
"method": POST,
"relative_url": "me/photos",
"body": "message=My cat photo",
"attached_files": "file1",
}, Params{
"method": POST,
"relative_url": "me/photos",
"body": "message=My dog photo",
"attached_files": "file2",
})
if e != nil {
t.Fatalf("cannot create photo on my timeline. [e:%v]", e)
}
t.Logf("batch call result. [result:%v]", result)
}
func TestGraphDebuggingAPI(t *testing.T) {
if FB_TEST_VALID_ACCESS_TOKEN == "" {
t.Skipf("cannot call batch api without access token. skip this test.")
}
test := func(t *testing.T, session *Session) {
session.SetAccessToken(FB_TEST_VALID_ACCESS_TOKEN)
defer session.SetAccessToken("")
// test app must not grant "read_friends" permission.
// otherwise there is no way to get a warning from facebook.
res, _ := session.Get("/me/friends", nil)
if res == nil {
t.Fatalf("res must not be nil.")
}
debugInfo := res.DebugInfo()
if debugInfo == nil {
t.Fatalf("debug info must exist.")
}
t.Logf("facebook response is: %v", res)
t.Logf("debug info is: %v", *debugInfo)
if len(debugInfo.Messages) == 0 {
t.Fatalf("facebook must warn me for the permission issue.")
}
msg := debugInfo.Messages[0]
if msg.Type == "" || msg.Message == "" {
t.Fatalf("facebook must say something. [msg:%v]", msg)
}
if debugInfo.FacebookApiVersion == "" {
t.Fatalf("facebook must tell me api version.")
}
if debugInfo.FacebookDebug == "" {
t.Fatalf("facebook must tell me X-FB-Debug.")
}
if debugInfo.FacebookRev == "" {
t.Fatalf("facebook must tell me x-fb-rev.")
}
}
defer func() {
Debug = DEBUG_OFF
Version = ""
}()
Version = FB_LATEST_VERSION
Debug = DEBUG_ALL
test(t, defaultSession)
session := &Session{}
session.SetDebug(DEBUG_ALL)
test(t, session)
// test changing debug mode.
old := session.SetDebug(DEBUG_OFF)
if old != DEBUG_ALL {
t.Fatalf("debug mode must be DEBUG_ALL. [debug:%v]", old)
}
if session.Debug() != DEBUG_ALL {
t.Fatalf("debug mode must be DEBUG_ALL [debug:%v]", session.Debug())
}
Debug = DEBUG_OFF
if session.Debug() != DEBUG_OFF {
t.Fatalf("debug mode must be DEBUG_OFF. [debug:%v]", session.Debug())
}
}
func TestInspectValidToken(t *testing.T) {
if FB_TEST_VALID_ACCESS_TOKEN == "" {
t.Skipf("skip this case as we don't have a valid access token.")
}
session := testGlobalApp.Session(FB_TEST_VALID_ACCESS_TOKEN)
result, err := session.Inspect()
if err != nil {
t.Fatalf("cannot inspect a valid access token. [e:%v]", err)
}
var isValid bool
err = result.DecodeField("is_valid", &isValid)
if err != nil {
t.Fatalf("cannot get 'is_valid' in inspect result. [e:%v]", err)
}
if !isValid {
t.Fatalf("inspect result shows access token is invalid. why? [result:%v]", result)
}
}
func TestInspectInvalidToken(t *testing.T) {
invalidToken := "CAACZA38ZAD8CoBAe2bDC6EdThnni3b56scyshKINjZARoC9ZAuEUTgYUkYnKdimqfA2ZAXcd2wLd7Rr8jLmMXTY9vqAhQGqObZBIUz1WwbqVoCsB3AAvLtwoWNhsxM76mK0eiJSLXHZCdPVpyhmtojvzXA7f69Bm6b5WZBBXia8iOpPZAUHTGp1UQLFMt47c7RqJTrYIl3VfAR0deN82GMFL2"
session := testGlobalApp.Session(invalidToken)
result, err := session.Inspect()
if err == nil {
t.Fatalf("facebook should indicate it's an invalid token. why not? [result:%v]", result)
}
if _, ok := err.(*Error); !ok {
t.Fatalf("inspect error should be a standard facebook error. why not? [e:%v]", err)
}
isValid := true
err = result.DecodeField("is_valid", &isValid)
if err != nil {
t.Fatalf("cannot get 'is_valid' in inspect result. [e:%v]", err)
}
if isValid {
t.Fatalf("inspect result shows access token is valid. why? [result:%v]", result)
}
}
func TestSessionCancelationWithContext(t *testing.T) {
session := &Session{}
ctx, cancel := context.WithCancel(context.Background())
newSession := session.WithContext(ctx)
if session == newSession {
t.Fatalf("session.WithContext must return a new session instance.")
}
if session.Context() != context.Background() {
t.Fatalf("default session context must be context.Background().")
}
if ctx != newSession.Context() {
t.Fatalf("ctx is not set to new session.")
}
if FB_TEST_VALID_ACCESS_TOKEN == "" {
t.Skipf("skip this case as we don't have a valid access token.")
}
cancel()
newSession.SetAccessToken(FB_TEST_VALID_ACCESS_TOKEN)
_, err := newSession.Inspect()
if err == nil {
t.Fatalf("http request must fail as cancelled.")
}
t.Logf("http request error should fail as cancelled. [e:%v]", err)
}
func TestInspectAppAccessToken(t *testing.T) {
app := New(FB_TEST_APP_ID, FB_TEST_APP_SECRET)
session := app.Session(app.AppAccessToken())
_, err := session.Inspect()
if err != nil {
t.Fatalf("fail to inspect app access token. [e:%v]", err)
}
}
func TestSessionWithCustomBaseUrl(t *testing.T) {
testMux := http.NewServeMux()
numCalls := 0
testMux.HandleFunc("/v3.0/me", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"name": "some-user"}`))
numCalls++
})
srv := httptest.NewServer(testMux)
defer srv.Close()
session := &Session{
Version: "v3.0",
BaseURL: srv.URL + "/",
}
_, err := session.Get("/me", nil)
if err != nil {
t.Fatalf("request to custom base URL failed: %v", err)
}
if numCalls != 1 {
t.Fatal("no call to mock server")
}
}
func TestSessionGetWithQueryString(t *testing.T) {
if FB_TEST_VALID_ACCESS_TOKEN == "" {
t.Skipf("skip this case as we don't have a valid access token.")
}
session := &Session{}
session.SetAccessToken(FB_TEST_VALID_ACCESS_TOKEN)
id, err := session.User()
if err != nil {
t.Fatalf("cannot get current user id. [e:%v]", err)
}
t.Logf("current user id is %v", id)
result, e := session.Api("me?fields=name,email", GET, Params{
"fields": "id,name",
})
if e != nil {
t.Fatalf("cannot get my extended info. [e:%v]", e)
}
if result.Get("name") == nil || result.Get("email") == nil {
t.Fatalf("fail to get my extend info. [result:%v]", result)
}
t.Logf("my extended info is: %v", result)
}