-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
config_test.go
347 lines (312 loc) · 10.6 KB
/
config_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
package oauth1
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
const expectedVerifier = "some_verifier"
func TestNewConfig(t *testing.T) {
expectedConsumerKey := "consumer_key"
expectedConsumerSecret := "consumer_secret"
config := NewConfig(expectedConsumerKey, expectedConsumerSecret)
assert.Equal(t, expectedConsumerKey, config.ConsumerKey)
assert.Equal(t, expectedConsumerSecret, config.ConsumerSecret)
}
func TestNewClient(t *testing.T) {
expectedToken := "access_token"
expectedConsumerKey := "consumer_key"
config := NewConfig(expectedConsumerKey, "consumer_secret")
token := NewToken(expectedToken, "access_secret")
client := config.Client(NoContext, token)
server := newMockServer(func(w http.ResponseWriter, req *http.Request) {
assert.Equal(t, "GET", req.Method)
params := parseOAuthParamsOrFail(t, req.Header.Get(authorizationHeaderParam))
assert.Equal(t, expectedToken, params[oauthTokenParam])
assert.Equal(t, expectedConsumerKey, params[oauthConsumerKeyParam])
})
defer server.Close()
client.Get(server.URL)
}
func TestNewClient_DefaultTransport(t *testing.T) {
client := NewClient(NoContext, NewConfig("t", "s"), NewToken("t", "s"))
// assert that the client uses the DefaultTransport
transport, ok := client.Transport.(*Transport)
assert.True(t, ok)
assert.Equal(t, http.DefaultTransport, transport.base())
}
func TestNewClient_ContextClientTransport(t *testing.T) {
baseTransport := &http.Transport{}
baseClient := &http.Client{Transport: baseTransport}
ctx := context.WithValue(NoContext, HTTPClient, baseClient)
client := NewClient(ctx, NewConfig("t", "s"), NewToken("t", "s"))
// assert that the client uses the ctx client's Transport as its base RoundTripper
transport, ok := client.Transport.(*Transport)
assert.True(t, ok)
assert.Equal(t, baseTransport, transport.base())
}
// newRequestTokenServer returns a new httptest.Server for an OAuth1 provider
// request token endpoint.
func newRequestTokenServer(t *testing.T, data url.Values) *httptest.Server {
return newMockServer(func(w http.ResponseWriter, req *http.Request) {
assert.Equal(t, "POST", req.Method)
assert.NotEmpty(t, req.Header.Get("Authorization"))
w.Header().Set(contentType, formContentType)
// Add a newline to ensure parsing works for servers that return extra whitespace
w.Write([]byte(data.Encode() + "\n"))
})
}
// newAccessTokenServer returns a new httptest.Server for an OAuth1 provider
// access token endpoint.
func newAccessTokenServer(t *testing.T, data url.Values) *httptest.Server {
return newMockServer(func(w http.ResponseWriter, req *http.Request) {
assert.Equal(t, "POST", req.Method)
assert.NotEmpty(t, req.Header.Get("Authorization"))
params := parseOAuthParamsOrFail(t, req.Header.Get(authorizationHeaderParam))
assert.Equal(t, expectedVerifier, params[oauthVerifierParam])
w.Header().Set(contentType, formContentType)
w.Write([]byte(data.Encode()))
})
}
// newUnparseableBodyServer returns a new httptest.Server which writes
// responses with bodies that error when parsed by url.ParseQuery.
func newUnparseableBodyServer() *httptest.Server {
return newMockServer(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set(contentType, formContentType)
// url.ParseQuery will error, https://golang.org/src/net/url/url_test.go#L1107
w.Write([]byte("%gh&%ij"))
})
}
func TestConfigRequestToken(t *testing.T) {
expectedToken := "request_token"
expectedSecret := "request_secret"
data := url.Values{}
data.Add("oauth_token", expectedToken)
data.Add("oauth_token_secret", expectedSecret)
data.Add("oauth_callback_confirmed", "true")
server := newRequestTokenServer(t, data)
defer server.Close()
config := &Config{
Endpoint: Endpoint{
RequestTokenURL: server.URL,
},
}
requestToken, requestSecret, err := config.RequestToken()
assert.Nil(t, err)
assert.Equal(t, expectedToken, requestToken)
assert.Equal(t, expectedSecret, requestSecret)
}
func TestConfigRequestToken_InvalidRequestTokenURL(t *testing.T) {
config := &Config{
Endpoint: Endpoint{
RequestTokenURL: "http://wrong.com/oauth/request_token",
},
}
requestToken, requestSecret, err := config.RequestToken()
assert.NotNil(t, err)
assert.Equal(t, "", requestToken)
assert.Equal(t, "", requestSecret)
}
func TestConfigRequestToken_CallbackNotConfirmed(t *testing.T) {
const expectedToken = "request_token"
const expectedSecret = "request_secret"
data := url.Values{}
data.Add("oauth_token", expectedToken)
data.Add("oauth_token_secret", expectedSecret)
data.Add("oauth_callback_confirmed", "false")
server := newRequestTokenServer(t, data)
defer server.Close()
config := &Config{
Endpoint: Endpoint{
RequestTokenURL: server.URL,
},
}
requestToken, requestSecret, err := config.RequestToken()
if assert.Error(t, err) {
assert.Equal(t, "oauth1: oauth_callback_confirmed was not true", err.Error())
}
assert.Equal(t, "", requestToken)
assert.Equal(t, "", requestSecret)
}
func TestConfigRequestToken_CannotParseBody(t *testing.T) {
server := newUnparseableBodyServer()
defer server.Close()
config := &Config{
Endpoint: Endpoint{
RequestTokenURL: server.URL,
},
}
requestToken, requestSecret, err := config.RequestToken()
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "invalid URL escape")
}
assert.Equal(t, "", requestToken)
assert.Equal(t, "", requestSecret)
}
func TestConfigRequestToken_MissingTokenOrSecret(t *testing.T) {
data := url.Values{}
data.Add("oauth_token", "any_token")
data.Add("oauth_callback_confirmed", "true")
server := newRequestTokenServer(t, data)
defer server.Close()
config := &Config{
Endpoint: Endpoint{
RequestTokenURL: server.URL,
},
}
requestToken, requestSecret, err := config.RequestToken()
if assert.Error(t, err) {
assert.Equal(t, "oauth1: Response missing oauth_token or oauth_token_secret", err.Error())
}
assert.Equal(t, "", requestToken)
assert.Equal(t, "", requestSecret)
}
func TestAuthorizationURL(t *testing.T) {
expectedURL := "https://api.example.com/oauth/authorize?oauth_token=a%2Frequest_token"
config := &Config{
Endpoint: Endpoint{
AuthorizeURL: "https://api.example.com/oauth/authorize",
},
}
url, err := config.AuthorizationURL("a/request_token")
assert.Nil(t, err)
if assert.NotNil(t, url) {
assert.Equal(t, expectedURL, url.String())
}
}
func TestAuthorizationURL_CannotParseAuthorizeURL(t *testing.T) {
config := &Config{
Endpoint: Endpoint{
AuthorizeURL: "%gh&%ij",
},
}
url, err := config.AuthorizationURL("any_request_token")
assert.Nil(t, url)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "parse")
assert.Contains(t, err.Error(), "invalid URL")
}
}
func TestConfigAccessToken(t *testing.T) {
expectedToken := "access_token"
expectedSecret := "access_secret"
data := url.Values{}
data.Add("oauth_token", expectedToken)
data.Add("oauth_token_secret", expectedSecret)
server := newAccessTokenServer(t, data)
defer server.Close()
config := &Config{
Endpoint: Endpoint{
AccessTokenURL: server.URL,
},
}
accessToken, accessSecret, err := config.AccessToken("request_token", "request_secret", expectedVerifier)
assert.Nil(t, err)
assert.Equal(t, expectedToken, accessToken)
assert.Equal(t, expectedSecret, accessSecret)
}
func TestConfigAccessToken_InvalidAccessTokenURL(t *testing.T) {
config := &Config{
Endpoint: Endpoint{
AccessTokenURL: "http://wrong.com/oauth/access_token",
},
}
accessToken, accessSecret, err := config.AccessToken("any_token", "any_secret", "any_verifier")
assert.NotNil(t, err)
assert.Equal(t, "", accessToken)
assert.Equal(t, "", accessSecret)
}
func TestConfigAccessToken_CannotParseBody(t *testing.T) {
server := newUnparseableBodyServer()
defer server.Close()
config := &Config{
Endpoint: Endpoint{
AccessTokenURL: server.URL,
},
}
accessToken, accessSecret, err := config.AccessToken("any_token", "any_secret", "any_verifier")
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "invalid URL escape")
}
assert.Equal(t, "", accessToken)
assert.Equal(t, "", accessSecret)
}
func TestConfigAccessToken_MissingTokenOrSecret(t *testing.T) {
data := url.Values{}
data.Add("oauth_token", "any_token")
server := newAccessTokenServer(t, data)
defer server.Close()
config := &Config{
Endpoint: Endpoint{
AccessTokenURL: server.URL,
},
}
accessToken, accessSecret, err := config.AccessToken("request_token", "request_secret", expectedVerifier)
if assert.Error(t, err) {
assert.Equal(t, "oauth1: Response missing oauth_token or oauth_token_secret", err.Error())
}
assert.Equal(t, "", accessToken)
assert.Equal(t, "", accessSecret)
}
func TestParseAuthorizationCallback_GET(t *testing.T) {
expectedToken := "token"
expectedVerifier := "verifier"
server := newMockServer(func(w http.ResponseWriter, req *http.Request) {
assert.Equal(t, "GET", req.Method)
// logic under test
requestToken, verifier, err := ParseAuthorizationCallback(req)
assert.Nil(t, err)
assert.Equal(t, expectedToken, requestToken)
assert.Equal(t, expectedVerifier, verifier)
})
defer server.Close()
// OAuth1 provider calls callback url
url, err := url.Parse(server.URL)
assert.Nil(t, err)
query := url.Query()
query.Add("oauth_token", expectedToken)
query.Add("oauth_verifier", expectedVerifier)
url.RawQuery = query.Encode()
http.Get(url.String())
}
func TestParseAuthorizationCallback_POST(t *testing.T) {
expectedToken := "token"
expectedVerifier := "verifier"
server := newMockServer(func(w http.ResponseWriter, req *http.Request) {
assert.Equal(t, "POST", req.Method)
// logic under test
requestToken, verifier, err := ParseAuthorizationCallback(req)
assert.Nil(t, err)
assert.Equal(t, expectedToken, requestToken)
assert.Equal(t, expectedVerifier, verifier)
})
defer server.Close()
// OAuth1 provider calls callback url
form := url.Values{}
form.Add("oauth_token", expectedToken)
form.Add("oauth_verifier", expectedVerifier)
http.PostForm(server.URL, form)
}
func TestParseAuthorizationCallback_MissingTokenOrVerifier(t *testing.T) {
server := newMockServer(func(w http.ResponseWriter, req *http.Request) {
assert.Equal(t, "GET", req.Method)
// logic under test
requestToken, verifier, err := ParseAuthorizationCallback(req)
if assert.Error(t, err) {
assert.Equal(t, "oauth1: Request missing oauth_token or oauth_verifier", err.Error())
}
assert.Equal(t, "", requestToken)
assert.Equal(t, "", verifier)
})
defer server.Close()
// OAuth1 provider calls callback url
url, err := url.Parse(server.URL)
assert.Nil(t, err)
query := url.Query()
query.Add("oauth_token", "any_token")
query.Add("oauth_verifier", "") // missing oauth_verifier
url.RawQuery = query.Encode()
http.Get(url.String())
}