signet 0.15.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,1036 +0,0 @@
1
- # Copyright (C) 2010 Google Inc.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- require "spec_helper"
15
- require "signet/oauth_1"
16
- require "signet/oauth_1/client"
17
- require "signet/oauth_1/credential"
18
-
19
- describe Signet::OAuth1 do
20
- it "should correctly normalize parameters" do
21
- parameters = [
22
- %w[a 1],
23
- ["c", "hi there"],
24
- %w[f 25],
25
- %w[f 50],
26
- %w[f a],
27
- %w[z p],
28
- %w[z t]
29
- ]
30
- expect(Signet::OAuth1.normalize_parameters(parameters)).to eq(
31
- "a=1&c=hi%20there&f=25&f=50&f=a&z=p&z=t"
32
- )
33
- end
34
-
35
- it "should correctly normalize parameters" do
36
- parameters = [
37
- ["b5", "=%3D"],
38
- %w[a3 a],
39
- ["c@", ""],
40
- ["a2", "r b"],
41
- %w[oauth_consumer_key 9djdj82h48djs9d2],
42
- %w[oauth_token kkk9d7dh3k39sjv7],
43
- ["oauth_signature_method", "HMAC-SHA1"],
44
- %w[oauth_timestamp 137131201],
45
- %w[oauth_nonce 7d8f3e4a],
46
- ["c2", ""],
47
- ["a3", "2 q"]
48
- ]
49
- expect(Signet::OAuth1.normalize_parameters(parameters)).to eq(
50
- "a2=r%20b&a3=2%20q&a3=a&b5=%3D%253D&c%40=&c2=&oauth_consumer_key=9dj" \
51
- "dj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1" \
52
- "&oauth_timestamp=137131201&oauth_token=kkk9d7dh3k39sjv7"
53
- )
54
- end
55
-
56
- it 'should exclude the "oauth_signature" parameter when normalizing' do
57
- parameters = [
58
- %w[a 1],
59
- %w[b 2],
60
- %w[c 3],
61
- %w[oauth_signature dpf43f3p2l4k3l03]
62
- ]
63
- expect(Signet::OAuth1.normalize_parameters(parameters)).to eq(
64
- "a=1&b=2&c=3"
65
- )
66
- end
67
-
68
- it "should raise an error if normalizing parameters with bogus values" do
69
- expect(lambda do
70
- Signet::OAuth1.normalize_parameters 42
71
- end).to raise_error(TypeError)
72
- end
73
-
74
- it "should raise an error if generating a base string with bogus values" do
75
- expect(lambda do
76
- Signet::OAuth1.generate_base_string(
77
- "GET", "http://photos.example.net/photos", 42
78
- )
79
- end).to raise_error(TypeError)
80
- end
81
-
82
- it "should correctly generate a base string" do
83
- method = "GET"
84
- uri = "http://photos.example.net/photos"
85
- parameters = {
86
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
87
- "oauth_token" => "nnch734d00sl2jdk",
88
- "oauth_signature_method" => "HMAC-SHA1",
89
- "oauth_timestamp" => "1191242096",
90
- "oauth_nonce" => "kllo9940pd9333jh",
91
- "oauth_version" => "1.0",
92
- "file" => "vacation.jpg",
93
- "size" => "original"
94
- }
95
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
96
- "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" \
97
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
98
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
99
- "oauth_signature_method%3DHMAC-SHA1%26" \
100
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
101
- "oauth_version%3D1.0%26size%3Doriginal"
102
- )
103
- end
104
-
105
- it "should correctly generate a base string with an already encoded URI" do
106
- method = "GET"
107
- uri = "http://photos.example.net/https%3A%2F%2Fwww.example.com"
108
- parameters = {
109
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
110
- "oauth_token" => "nnch734d00sl2jdk",
111
- "oauth_signature_method" => "HMAC-SHA1",
112
- "oauth_timestamp" => "1191242096",
113
- "oauth_nonce" => "kllo9940pd9333jh",
114
- "oauth_version" => "1.0",
115
- "file" => "vacation.jpg",
116
- "size" => "original"
117
- }
118
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
119
- "GET&http%3A%2F%2Fphotos.example.net%2F" \
120
- "https%253A%252F%252Fwww.example.com&file%3Dvacation.jpg%26" \
121
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
122
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
123
- "oauth_signature_method%3DHMAC-SHA1%26" \
124
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
125
- "oauth_version%3D1.0%26size%3Doriginal"
126
- )
127
- end
128
-
129
- it "should correctly generate a base string with an already encoded URI" do
130
- method = "GET"
131
- uri = "http://example.com/r%20v/X?id=123"
132
- parameters = {
133
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
134
- "oauth_token" => "nnch734d00sl2jdk",
135
- "oauth_signature_method" => "HMAC-SHA1",
136
- "oauth_timestamp" => "1191242096",
137
- "oauth_nonce" => "kllo9940pd9333jh",
138
- "oauth_version" => "1.0"
139
- }
140
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
141
- "GET&http%3A%2F%2Fexample.com%2Fr%2520v%2FX&" \
142
- "id%3D123%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
143
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
144
- "oauth_signature_method%3DHMAC-SHA1%26" \
145
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
146
- "oauth_version%3D1.0"
147
- )
148
- end
149
-
150
- it "should correctly generate a base string when port 8080 is specified" do
151
- method = "GET"
152
- uri = "http://www.example.net:8080/?q=1"
153
- parameters = {
154
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
155
- "oauth_token" => "nnch734d00sl2jdk",
156
- "oauth_signature_method" => "HMAC-SHA1",
157
- "oauth_timestamp" => "1191242096",
158
- "oauth_nonce" => "kllo9940pd9333jh",
159
- "oauth_version" => "1.0"
160
- }
161
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
162
- "GET&http%3A%2F%2Fwww.example.net%3A8080%2F&" \
163
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
164
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
165
- "oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26" \
166
- "oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26q%3D1"
167
- )
168
- end
169
-
170
- it "should correctly generate a base string when port 80 is specified" do
171
- method = "GET"
172
- uri = "http://photos.example.net:80/photos"
173
- parameters = {
174
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
175
- "oauth_token" => "nnch734d00sl2jdk",
176
- "oauth_signature_method" => "HMAC-SHA1",
177
- "oauth_timestamp" => "1191242096",
178
- "oauth_nonce" => "kllo9940pd9333jh",
179
- "oauth_version" => "1.0",
180
- "file" => "vacation.jpg",
181
- "size" => "original"
182
- }
183
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
184
- "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" \
185
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
186
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
187
- "oauth_signature_method%3DHMAC-SHA1%26" \
188
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
189
- "oauth_version%3D1.0%26size%3Doriginal"
190
- )
191
- end
192
-
193
- it "should correctly generate a base string when port 443 is specified" do
194
- method = "GET"
195
- uri = "https://photos.example.net:443/photos"
196
- parameters = {
197
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
198
- "oauth_token" => "nnch734d00sl2jdk",
199
- "oauth_signature_method" => "HMAC-SHA1",
200
- "oauth_timestamp" => "1191242096",
201
- "oauth_nonce" => "kllo9940pd9333jh",
202
- "oauth_version" => "1.0",
203
- "file" => "vacation.jpg",
204
- "size" => "original"
205
- }
206
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
207
- "GET&https%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" \
208
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
209
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
210
- "oauth_signature_method%3DHMAC-SHA1%26" \
211
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
212
- "oauth_version%3D1.0%26size%3Doriginal"
213
- )
214
- end
215
-
216
- it "should correctly generate a base signature with uppercase scheme" do
217
- method = "GET"
218
- uri =
219
- "HTTP://photos.example.net/photos?file=vacation.jpg"
220
- parameters = {
221
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
222
- "oauth_token" => "nnch734d00sl2jdk",
223
- "oauth_signature_method" => "HMAC-SHA1",
224
- "oauth_timestamp" => "1191242096",
225
- "oauth_nonce" => "kllo9940pd9333jh",
226
- "oauth_version" => "1.0",
227
- "size" => "original"
228
- }
229
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
230
- "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" \
231
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
232
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
233
- "oauth_signature_method%3DHMAC-SHA1%26" \
234
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
235
- "oauth_version%3D1.0%26size%3Doriginal"
236
- )
237
- end
238
-
239
- it "should correctly generate a base signature with mixedcase authority" do
240
- method = "GET"
241
- uri =
242
- "http://photos.eXaMpLe.NET/photos?file=vacation.jpg"
243
- parameters = {
244
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
245
- "oauth_token" => "nnch734d00sl2jdk",
246
- "oauth_signature_method" => "HMAC-SHA1",
247
- "oauth_timestamp" => "1191242096",
248
- "oauth_nonce" => "kllo9940pd9333jh",
249
- "oauth_version" => "1.0",
250
- "size" => "original"
251
- }
252
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
253
- "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" \
254
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
255
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
256
- "oauth_signature_method%3DHMAC-SHA1%26" \
257
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
258
- "oauth_version%3D1.0%26size%3Doriginal"
259
- )
260
- end
261
-
262
- it "should correctly generate a base signature with a method symbol" do
263
- method = :get
264
- uri =
265
- "http://photos.example.net/photos?file=vacation.jpg"
266
- parameters = {
267
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
268
- "oauth_token" => "nnch734d00sl2jdk",
269
- "oauth_signature_method" => "HMAC-SHA1",
270
- "oauth_timestamp" => "1191242096",
271
- "oauth_nonce" => "kllo9940pd9333jh",
272
- "oauth_version" => "1.0",
273
- "size" => "original"
274
- }
275
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
276
- "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" \
277
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
278
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
279
- "oauth_signature_method%3DHMAC-SHA1%26" \
280
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
281
- "oauth_version%3D1.0%26size%3Doriginal"
282
- )
283
- end
284
-
285
- it "should correctly generate a base signature with duplicated query params" do
286
- method = "GET"
287
- uri = "http://photos.example.net/photos?foo=bar&foo=baz&foo=qux"
288
- parameters = {
289
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
290
- "oauth_token" => "nnch734d00sl2jdk",
291
- "oauth_signature_method" => "HMAC-SHA1",
292
- "oauth_timestamp" => "1191242096",
293
- "oauth_nonce" => "kllo9940pd9333jh",
294
- "oauth_version" => "1.0",
295
- "file" => "vacation.jpg",
296
- "size" => "original"
297
- }
298
- expect(Signet::OAuth1.generate_base_string(method, uri, parameters)).to eq(
299
- "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" \
300
- "foo%3Dbar%26" \
301
- "foo%3Dbaz%26" \
302
- "foo%3Dqux%26" \
303
- "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" \
304
- "oauth_nonce%3Dkllo9940pd9333jh%26" \
305
- "oauth_signature_method%3DHMAC-SHA1%26" \
306
- "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" \
307
- "oauth_version%3D1.0%26size%3Doriginal"
308
- )
309
- end
310
-
311
- it "should correctly generate an authorization header" do
312
- parameters = [
313
- %w[oauth_consumer_key 0685bd9184jfhq22],
314
- %w[oauth_token ad180jjd733klru7],
315
- ["oauth_signature_method", "HMAC-SHA1"],
316
- ["oauth_signature", "wOJIO9A2W5mFwDgiDvZbTSMK/PY="],
317
- %w[oauth_timestamp 137131200],
318
- %w[oauth_nonce 4572616e48616d6d65724c61686176],
319
- ["oauth_version", "1.0"]
320
- ]
321
- expect(Signet::OAuth1.generate_authorization_header(
322
- parameters, "http://sp.example.com/"
323
- )).to eq(
324
- 'OAuth realm="http://sp.example.com/", ' \
325
- 'oauth_consumer_key="0685bd9184jfhq22", ' \
326
- 'oauth_token="ad180jjd733klru7", ' \
327
- 'oauth_signature_method="HMAC-SHA1", ' \
328
- 'oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D", ' \
329
- 'oauth_timestamp="137131200", ' \
330
- 'oauth_nonce="4572616e48616d6d65724c61686176", ' \
331
- 'oauth_version="1.0"'
332
- )
333
- end
334
-
335
- it "should raise an error if generating an authorization header " \
336
- "with bogus values" do
337
- expect(lambda do
338
- Signet::OAuth1.generate_authorization_header 42
339
- end).to raise_error(TypeError)
340
- end
341
-
342
- it "should raise an error if generating an authorization header " \
343
- 'with the "realm" parameter specified the wrong way' do
344
- parameters = [
345
- ["realm", "http://sp.example.com/"],
346
- %w[oauth_consumer_key 0685bd9184jfhq22],
347
- %w[oauth_token ad180jjd733klru7],
348
- ["oauth_signature_method", "HMAC-SHA1"],
349
- ["oauth_signature", "wOJIO9A2W5mFwDgiDvZbTSMK/PY="],
350
- %w[oauth_timestamp 137131200],
351
- %w[oauth_nonce 4572616e48616d6d65724c61686176],
352
- ["oauth_version", "1.0"]
353
- ]
354
- expect(lambda do
355
- Signet::OAuth1.generate_authorization_header parameters
356
- end).to raise_error(ArgumentError)
357
- end
358
-
359
- it "should correctly parse an authorization header" do
360
- parameters = Signet::OAuth1.parse_authorization_header(
361
- 'OAuth realm="http://sp.example.com/", ' \
362
- 'oauth_consumer_key="0685bd9184jfhq22", ' \
363
- 'oauth_token="ad180jjd733klru7", ' \
364
- 'oauth_signature_method="HMAC-SHA1", ' \
365
- 'oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D", ' \
366
- 'oauth_timestamp="137131200", ' \
367
- 'oauth_nonce="4572616e48616d6d65724c61686176", ' \
368
- 'oauth_version="1.0"'
369
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
370
- expect(parameters["realm"]).to eq "http://sp.example.com/"
371
- expect(parameters["oauth_consumer_key"]).to eq "0685bd9184jfhq22"
372
- expect(parameters["oauth_token"]).to eq "ad180jjd733klru7"
373
- expect(parameters["oauth_signature_method"]).to eq "HMAC-SHA1"
374
- expect(parameters["oauth_signature"]).to eq "wOJIO9A2W5mFwDgiDvZbTSMK/PY="
375
- expect(parameters["oauth_timestamp"]).to eq "137131200"
376
- expect(parameters["oauth_nonce"]).to eq "4572616e48616d6d65724c61686176"
377
- expect(parameters["oauth_version"]).to eq "1.0"
378
- end
379
-
380
- it "should not unescape a realm in an authorization header" do
381
- parameters = Signet::OAuth1.parse_authorization_header(
382
- 'OAuth realm="http%3A%2F%2Fsp.example.com%2F", ' \
383
- 'domain="http%3A%2F%2Fsp.example.com%2F", ' \
384
- 'oauth_consumer_key="0685bd9184jfhq22", ' \
385
- 'oauth_token="ad180jjd733klru7", ' \
386
- 'oauth_signature_method="HMAC-SHA1", ' \
387
- 'oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D", ' \
388
- 'oauth_timestamp="137131200", ' \
389
- 'oauth_nonce="4572616e48616d6d65724c61686176", ' \
390
- 'oauth_version="1.0"'
391
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
392
- expect(parameters["realm"]).to eq "http%3A%2F%2Fsp.example.com%2F"
393
- expect(parameters["domain"]).to eq "http://sp.example.com/"
394
- expect(parameters["oauth_consumer_key"]).to eq "0685bd9184jfhq22"
395
- expect(parameters["oauth_token"]).to eq "ad180jjd733klru7"
396
- expect(parameters["oauth_signature_method"]).to eq "HMAC-SHA1"
397
- expect(parameters["oauth_signature"]).to eq "wOJIO9A2W5mFwDgiDvZbTSMK/PY="
398
- expect(parameters["oauth_timestamp"]).to eq "137131200"
399
- expect(parameters["oauth_nonce"]).to eq "4572616e48616d6d65724c61686176"
400
- expect(parameters["oauth_version"]).to eq "1.0"
401
- end
402
-
403
- it "should raise an error if parsing an authorization header " \
404
- "with bogus values" do
405
- expect(lambda do
406
- Signet::OAuth1.parse_authorization_header 42
407
- end).to raise_error(TypeError)
408
- end
409
-
410
- it "should raise an error if parsing a non-OAuth authorization header" do
411
- expect(lambda do
412
- Signet::OAuth1.parse_authorization_header(
413
- "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
414
- )
415
- end).to raise_error(Signet::ParseError)
416
- end
417
-
418
- it "should correctly parse a form encoded credential" do
419
- credential = Signet::OAuth1.parse_form_encoded_credentials(
420
- "oauth_token=hh5s93j4hdidpola&oauth_token_secret=hdhd0244k9j7ao03"
421
- )
422
- expect(credential.key).to eq "hh5s93j4hdidpola"
423
- expect(credential.secret).to eq "hdhd0244k9j7ao03"
424
- end
425
-
426
- it "should correctly parse a form encoded credential" do
427
- credential = Signet::OAuth1.parse_form_encoded_credentials(
428
- "oauth_token=hdk48Djdsa&oauth_token_secret=xyz4992k83j47x0b&" \
429
- "oauth_callback_confirmed=true"
430
- )
431
- expect(credential.key).to eq "hdk48Djdsa"
432
- expect(credential.secret).to eq "xyz4992k83j47x0b"
433
- end
434
-
435
- it "should raise an error if parsing a form encoded credential " \
436
- "with bogus values" do
437
- expect(lambda do
438
- Signet::OAuth1.parse_form_encoded_credentials 42
439
- end).to raise_error(TypeError)
440
- end
441
-
442
- it "should correctly generate a signature for a set of parameters" do
443
- method = :get
444
- uri = "http://photos.example.net/photos"
445
- client_credential_secret = "kd94hf93k423kf44"
446
- token_credential_secret = "pfkkdhi9sl3r4s00"
447
- parameters = {
448
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
449
- "oauth_token" => "nnch734d00sl2jdk",
450
- "oauth_signature_method" => "HMAC-SHA1",
451
- "oauth_timestamp" => "1191242096",
452
- "oauth_nonce" => "kllo9940pd9333jh",
453
- "oauth_version" => "1.0",
454
- "file" => "vacation.jpg",
455
- "size" => "original"
456
- }
457
- expect(Signet::OAuth1.sign_parameters(
458
- method,
459
- uri,
460
- parameters,
461
- client_credential_secret,
462
- token_credential_secret
463
- )).to eq "tR3+Ty81lMeYAr/Fid0kMTYa/WM="
464
- end
465
-
466
- it "should raise an error when trying to sign with with unknown method" do
467
- method = :get
468
- uri = "http://photos.example.net/photos"
469
- client_credential_secret = "kd94hf93k423kf44"
470
- token_credential_secret = "pfkkdhi9sl3r4s00"
471
- parameters = {
472
- "oauth_consumer_key" => "dpf43f3p2l4k3l03",
473
- "oauth_token" => "nnch734d00sl2jdk",
474
- "oauth_signature_method" => "HMAC-BOGUS", # Unknown signature method
475
- "oauth_timestamp" => "1191242096",
476
- "oauth_nonce" => "kllo9940pd9333jh",
477
- "oauth_version" => "1.0",
478
- "file" => "vacation.jpg",
479
- "size" => "original"
480
- }
481
- expect(lambda do
482
- Signet::OAuth1.sign_parameters(
483
- method,
484
- uri,
485
- parameters,
486
- client_credential_secret,
487
- token_credential_secret
488
- )
489
- end).to raise_error(NotImplementedError)
490
- end
491
-
492
- it "should correctly generate authorization URIs" do
493
- authorization_uri = "http://photos.example.net/authorize"
494
- temporary_credential_key = "hh5s93j4hdidpola"
495
- callback = "http://printer.example.com/request_token_ready"
496
- parsed_uri = Addressable::URI.parse(
497
- Signet::OAuth1.generate_authorization_uri(
498
- authorization_uri,
499
- temporary_credential_key: temporary_credential_key,
500
- callback: callback
501
- )
502
- )
503
- expect(parsed_uri.query_values).to have_key("oauth_token")
504
- expect(parsed_uri.query_values["oauth_token"]).to eq temporary_credential_key
505
- expect(parsed_uri.query_values).to have_key("oauth_callback")
506
- expect(parsed_uri.query_values["oauth_callback"]).to eq callback
507
- end
508
- end
509
-
510
- describe Signet::OAuth1, "when generating temporary credentials parameters" do
511
- before do
512
- @client_credential_key = "dpf43f3p2l4k3l03"
513
- @callback = "http://printer.example.com/request_token_ready"
514
- @signature_method = "HMAC-SHA1"
515
- @scope = "http://photos.example.com/full_access"
516
- @additional_parameters = [["scope", @scope]]
517
- @unsigned_parameters =
518
- Signet::OAuth1.unsigned_temporary_credential_parameters(
519
- client_credential_key: @client_credential_key,
520
- callback: @callback,
521
- signature_method: @signature_method,
522
- additional_parameters: @additional_parameters
523
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
524
- end
525
-
526
- it "should raise an error if the client credential key is missing" do
527
- expect(lambda do
528
- Signet::OAuth1.unsigned_temporary_credential_parameters(
529
- client_credential_key: nil,
530
- callback: @callback,
531
- signature_method: @signature_method,
532
- additional_parameters: @additional_parameters
533
- )
534
- end).to raise_error(ArgumentError)
535
- end
536
-
537
- it "should have the correct client credential key" do
538
- expect(@unsigned_parameters).to have_key("oauth_consumer_key")
539
- expect(@unsigned_parameters["oauth_consumer_key"]).to eq @client_credential_key
540
- end
541
-
542
- it "should have the correct signature method" do
543
- expect(@unsigned_parameters).to have_key("oauth_signature_method")
544
- expect(@unsigned_parameters["oauth_signature_method"]).to eq @signature_method
545
- end
546
-
547
- it "should have a valid timestamp" do
548
- # Verify that we have a timestamp, it's in the correct format and within
549
- # a reasonable range of the current time.
550
- expect(@unsigned_parameters).to have_key("oauth_timestamp")
551
- expect(@unsigned_parameters["oauth_timestamp"]).to match(/^[0-9]+$/)
552
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be <= Time.now.to_i
553
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be >= Time.now.to_i - 1
554
- end
555
-
556
- it "should have a valid nonce" do
557
- # Verify that we have a nonce and that it has sufficient length for
558
- # uniqueness.
559
- expect(@unsigned_parameters).to have_key("oauth_nonce")
560
- expect(@unsigned_parameters["oauth_nonce"]).to match(/^[0-9a-zA-Z]{16,100}$/)
561
- end
562
-
563
- it "should have the correct callback" do
564
- expect(@unsigned_parameters).to have_key("oauth_callback")
565
- expect(@unsigned_parameters["oauth_callback"]).to eq @callback
566
- end
567
-
568
- it "should have the correct scope parameter" do
569
- expect(@unsigned_parameters).to have_key("scope")
570
- expect(@unsigned_parameters["scope"]).to eq @scope
571
- end
572
-
573
- it "should have the correct OAuth version" do
574
- expect(@unsigned_parameters).to have_key("oauth_version")
575
- expect(@unsigned_parameters["oauth_version"]).to eq "1.0"
576
- end
577
- end
578
-
579
- describe Signet::OAuth1, "when generating token credential parameters" do
580
- before do
581
- @client_credential_key = "dpf43f3p2l4k3l03"
582
- @temporary_credential_key = "hh5s93j4hdidpola"
583
- @verifier = "473f82d3"
584
- @signature_method = "HMAC-SHA1"
585
- @unsigned_parameters =
586
- Signet::OAuth1.unsigned_token_credential_parameters(
587
- client_credential_key: @client_credential_key,
588
- temporary_credential_key: @temporary_credential_key,
589
- signature_method: @signature_method,
590
- verifier: @verifier
591
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
592
- end
593
-
594
- it "should raise an error if the client credential key is missing" do
595
- expect(lambda do
596
- Signet::OAuth1.unsigned_token_credential_parameters(
597
- client_credential_key: nil,
598
- temporary_credential_key: @temporary_credential_key,
599
- signature_method: @signature_method,
600
- verifier: @verifier
601
- )
602
- end).to raise_error(ArgumentError)
603
- end
604
-
605
- it "should raise an error if the temporary credential key is missing" do
606
- expect(lambda do
607
- Signet::OAuth1.unsigned_token_credential_parameters(
608
- client_credential_key: @client_credential_key,
609
- temporary_credential_key: nil,
610
- signature_method: @signature_method,
611
- verifier: @verifier
612
- )
613
- end).to raise_error(ArgumentError)
614
- end
615
-
616
- it "should raise an error if the verifier is missing" do
617
- expect(lambda do
618
- Signet::OAuth1.unsigned_token_credential_parameters(
619
- client_credential_key: @client_credential_key,
620
- temporary_credential_key: @temporary_credential_key,
621
- signature_method: @signature_method,
622
- verifier: nil
623
- )
624
- end).to raise_error(ArgumentError)
625
- end
626
-
627
- it "should have the correct client credential key" do
628
- expect(@unsigned_parameters).to have_key("oauth_consumer_key")
629
- expect(@unsigned_parameters["oauth_consumer_key"]).to eq @client_credential_key
630
- end
631
-
632
- it "should have the correct temporary credentials key" do
633
- expect(@unsigned_parameters).to have_key("oauth_token")
634
- expect(@unsigned_parameters["oauth_token"]).to eq @temporary_credential_key
635
- end
636
-
637
- it "should have the correct signature method" do
638
- expect(@unsigned_parameters).to have_key("oauth_signature_method")
639
- expect(@unsigned_parameters["oauth_signature_method"]).to eq @signature_method
640
- end
641
-
642
- it "should have a valid timestamp" do
643
- # Verify that we have a timestamp, it's in the correct format and within
644
- # a reasonable range of the current time.
645
- expect(@unsigned_parameters).to have_key("oauth_timestamp")
646
- expect(@unsigned_parameters["oauth_timestamp"]).to match(/^[0-9]+$/)
647
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be <= Time.now.to_i
648
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be >= Time.now.to_i - 1
649
- end
650
-
651
- it "should have a valid nonce" do
652
- # Verify that we have a nonce and that it has sufficient length for
653
- # uniqueness.
654
- expect(@unsigned_parameters).to have_key("oauth_nonce")
655
- expect(@unsigned_parameters["oauth_nonce"]).to match(/^[0-9a-zA-Z]{16,100}$/)
656
- end
657
-
658
- it "should have the verifier" do
659
- expect(@unsigned_parameters).to have_key("oauth_verifier")
660
- expect(@unsigned_parameters["oauth_verifier"]).to eq @verifier
661
- end
662
-
663
- it "should have the correct OAuth version" do
664
- expect(@unsigned_parameters).to have_key("oauth_version")
665
- expect(@unsigned_parameters["oauth_version"]).to eq "1.0"
666
- end
667
- end
668
-
669
- describe Signet::OAuth1, "when generating protected resource parameters" do
670
- before do
671
- @client_credential_key = "dpf43f3p2l4k3l03"
672
- @token_credential_key = "nnch734d00sl2jdk"
673
- @signature_method = "HMAC-SHA1"
674
- @unsigned_parameters =
675
- Signet::OAuth1.unsigned_resource_parameters(
676
- client_credential_key: @client_credential_key,
677
- token_credential_key: @token_credential_key,
678
- signature_method: @signature_method
679
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
680
- end
681
-
682
- it "should raise an error if the client credential key is missing" do
683
- expect(lambda do
684
- Signet::OAuth1.unsigned_resource_parameters(
685
- client_credential_key: nil,
686
- token_credential_key: @token_credential_key,
687
- signature_method: @signature_method
688
- )
689
- end).to raise_error(ArgumentError)
690
- end
691
-
692
- it "should raise an error if the token credential key is missing" do
693
- expect(lambda do
694
- Signet::OAuth1.unsigned_resource_parameters(
695
- client_credential_key: @client_credential_key,
696
- token_credential_key: nil,
697
- signature_method: @signature_method
698
- )
699
- end).to raise_error(ArgumentError)
700
- end
701
-
702
- it "should have the correct client credential key" do
703
- expect(@unsigned_parameters).to have_key("oauth_consumer_key")
704
- expect(@unsigned_parameters["oauth_consumer_key"]).to eq @client_credential_key
705
- end
706
-
707
- it "should have the correct token credentials key" do
708
- expect(@unsigned_parameters).to have_key("oauth_token")
709
- expect(@unsigned_parameters["oauth_token"]).to eq @token_credential_key
710
- end
711
-
712
- it "should have the correct signature method" do
713
- expect(@unsigned_parameters).to have_key("oauth_signature_method")
714
- expect(@unsigned_parameters["oauth_signature_method"]).to eq @signature_method
715
- end
716
-
717
- it "should have a valid timestamp" do
718
- # Verify that we have a timestamp, it's in the correct format and within
719
- # a reasonable range of the current time.
720
- expect(@unsigned_parameters).to have_key("oauth_timestamp")
721
- expect(@unsigned_parameters["oauth_timestamp"]).to match(/^[0-9]+$/)
722
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be <= Time.now.to_i
723
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be >= Time.now.to_i - 1
724
- end
725
-
726
- it "should have a valid nonce" do
727
- # Verify that we have a nonce and that it has sufficient length for
728
- # uniqueness.
729
- expect(@unsigned_parameters).to have_key("oauth_nonce")
730
- expect(@unsigned_parameters["oauth_nonce"]).to match(/^[0-9a-zA-Z]{16,100}$/)
731
- end
732
-
733
- it "should have the correct OAuth version" do
734
- expect(@unsigned_parameters).to have_key("oauth_version")
735
- expect(@unsigned_parameters["oauth_version"]).to eq "1.0"
736
- end
737
- end
738
-
739
- describe Signet::OAuth1, "when generating token credential parameters " \
740
- "with Signet::OAuth1::Credential objects" do
741
- before do
742
- @client_credential = Signet::OAuth1::Credential.new(
743
- "dpf43f3p2l4k3l03", "kd94hf93k423kf44"
744
- )
745
- @temporary_credential = Signet::OAuth1::Credential.new(
746
- "hh5s93j4hdidpola", "hdhd0244k9j7ao03"
747
- )
748
- @verifier = "473f82d3"
749
- @signature_method = "HMAC-SHA1"
750
- @unsigned_parameters =
751
- Signet::OAuth1.unsigned_token_credential_parameters(
752
- client_credential: @client_credential,
753
- temporary_credential: @temporary_credential,
754
- signature_method: @signature_method,
755
- verifier: @verifier
756
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
757
- end
758
-
759
- it "should have the correct client credential key" do
760
- expect(@unsigned_parameters).to have_key("oauth_consumer_key")
761
- expect(@unsigned_parameters["oauth_consumer_key"]).to eq @client_credential.key
762
- end
763
-
764
- it "should have the correct temporary credentials key" do
765
- expect(@unsigned_parameters).to have_key("oauth_token")
766
- expect(@unsigned_parameters["oauth_token"]).to eq @temporary_credential.key
767
- end
768
-
769
- it "should have the correct signature method" do
770
- expect(@unsigned_parameters).to have_key("oauth_signature_method")
771
- expect(@unsigned_parameters["oauth_signature_method"]).to eq @signature_method
772
- end
773
-
774
- it "should have a valid timestamp" do
775
- # Verify that we have a timestamp, it's in the correct format and within
776
- # a reasonable range of the current time.
777
- expect(@unsigned_parameters).to have_key("oauth_timestamp")
778
- expect(@unsigned_parameters["oauth_timestamp"]).to match(/^[0-9]+$/)
779
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be <= Time.now.to_i
780
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be >= Time.now.to_i - 1
781
- end
782
-
783
- it "should have a valid nonce" do
784
- # Verify that we have a nonce and that it has sufficient length for
785
- # uniqueness.
786
- expect(@unsigned_parameters).to have_key("oauth_nonce")
787
- expect(@unsigned_parameters["oauth_nonce"]).to match(/^[0-9a-zA-Z]{16,100}$/)
788
- end
789
-
790
- it "should have the correct OAuth version" do
791
- expect(@unsigned_parameters).to have_key("oauth_version")
792
- expect(@unsigned_parameters["oauth_version"]).to eq "1.0"
793
- end
794
- end
795
-
796
- describe Signet::OAuth1, "when generating token credential parameters " \
797
- "with a Signet::OAuth1::Client object" do
798
- before do
799
- @client = Signet::OAuth1::Client.new
800
- @client.client_credential = Signet::OAuth1::Credential.new(
801
- "dpf43f3p2l4k3l03", "kd94hf93k423kf44"
802
- )
803
- @client.temporary_credential = Signet::OAuth1::Credential.new(
804
- "hh5s93j4hdidpola", "hdhd0244k9j7ao03"
805
- )
806
- @verifier = "473f82d3"
807
- @signature_method = "HMAC-SHA1"
808
- @unsigned_parameters =
809
- Signet::OAuth1.unsigned_token_credential_parameters(
810
- client: @client,
811
- signature_method: @signature_method,
812
- verifier: @verifier
813
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
814
- end
815
-
816
- it "should have the correct client credential key" do
817
- expect(@unsigned_parameters).to have_key("oauth_consumer_key")
818
- expect(@unsigned_parameters["oauth_consumer_key"]).to eq @client.client_credential_key
819
- end
820
-
821
- it "should have the correct temporary credentials key" do
822
- expect(@unsigned_parameters).to have_key("oauth_token")
823
- expect(@unsigned_parameters["oauth_token"]).to eq @client.temporary_credential_key
824
- end
825
-
826
- it "should have the correct signature method" do
827
- expect(@unsigned_parameters).to have_key("oauth_signature_method")
828
- expect(@unsigned_parameters["oauth_signature_method"]).to eq @signature_method
829
- end
830
-
831
- it "should have a valid timestamp" do
832
- # Verify that we have a timestamp, it's in the correct format and within
833
- # a reasonable range of the current time.
834
- expect(@unsigned_parameters).to have_key("oauth_timestamp")
835
- expect(@unsigned_parameters["oauth_timestamp"]).to match(/^[0-9]+$/)
836
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be <= Time.now.to_i
837
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be >= Time.now.to_i - 1
838
- end
839
-
840
- it "should have a valid nonce" do
841
- # Verify that we have a nonce and that it has sufficient length for
842
- # uniqueness.
843
- expect(@unsigned_parameters).to have_key("oauth_nonce")
844
- expect(@unsigned_parameters["oauth_nonce"]).to match(/^[0-9a-zA-Z]{16,100}$/)
845
- end
846
-
847
- it "should have the correct OAuth version" do
848
- expect(@unsigned_parameters).to have_key("oauth_version")
849
- expect(@unsigned_parameters["oauth_version"]).to eq "1.0"
850
- end
851
- end
852
-
853
- describe Signet::OAuth1, "when generating token credential parameters " \
854
- "with Signet::OAuth1::Credential objects" do
855
- before do
856
- @client_credential = Signet::OAuth1::Credential.new(
857
- "dpf43f3p2l4k3l03", "kd94hf93k423kf44"
858
- )
859
- @temporary_credential = Signet::OAuth1::Credential.new(
860
- "hh5s93j4hdidpola", "hdhd0244k9j7ao03"
861
- )
862
- @verifier = "473f82d3"
863
- @signature_method = "HMAC-SHA1"
864
- @unsigned_parameters =
865
- Signet::OAuth1.unsigned_token_credential_parameters(
866
- client_credential: @client_credential,
867
- temporary_credential: @temporary_credential,
868
- signature_method: @signature_method,
869
- verifier: @verifier
870
- ).each_with_object({}) { |(k, v), h| h[k] = v; }
871
- end
872
-
873
- it "should have the correct client credential key" do
874
- expect(@unsigned_parameters).to have_key("oauth_consumer_key")
875
- expect(@unsigned_parameters["oauth_consumer_key"]).to eq @client_credential.key
876
- end
877
-
878
- it "should have the correct temporary credentials key" do
879
- expect(@unsigned_parameters).to have_key("oauth_token")
880
- expect(@unsigned_parameters["oauth_token"]).to eq @temporary_credential.key
881
- end
882
-
883
- it "should have the correct signature method" do
884
- expect(@unsigned_parameters).to have_key("oauth_signature_method")
885
- expect(@unsigned_parameters["oauth_signature_method"]).to eq @signature_method
886
- end
887
-
888
- it "should have a valid timestamp" do
889
- # Verify that we have a timestamp, it's in the correct format and within
890
- # a reasonable range of the current time.
891
- expect(@unsigned_parameters).to have_key("oauth_timestamp")
892
- expect(@unsigned_parameters["oauth_timestamp"]).to match(/^[0-9]+$/)
893
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be <= Time.now.to_i
894
- expect(@unsigned_parameters["oauth_timestamp"].to_i).to be >= Time.now.to_i - 1
895
- end
896
-
897
- it "should have a valid nonce" do
898
- # Verify that we have a nonce and that it has sufficient length for
899
- # uniqueness.
900
- expect(@unsigned_parameters).to have_key("oauth_nonce")
901
- expect(@unsigned_parameters["oauth_nonce"]).to match(/^[0-9a-zA-Z]{16,100}$/)
902
- end
903
-
904
- it "should have the correct OAuth version" do
905
- expect(@unsigned_parameters).to have_key("oauth_version")
906
- expect(@unsigned_parameters["oauth_version"]).to eq "1.0"
907
- end
908
- end
909
-
910
- describe Signet::OAuth1, "extracting credential keys from options" do
911
- it "should raise an error for bogus credentials" do
912
- expect(lambda do
913
- Signet::OAuth1.extract_credential_key_option(
914
- :client, client_credential_key: true
915
- )
916
- end).to raise_error(TypeError)
917
- end
918
-
919
- it "should raise an error for bogus credentials" do
920
- expect(lambda do
921
- Signet::OAuth1.extract_credential_key_option(
922
- :client, client_credential: 42
923
- )
924
- end).to raise_error(TypeError)
925
- end
926
-
927
- it "should raise an error for bogus credentials" do
928
- expect(lambda do
929
- Signet::OAuth1.extract_credential_key_option(
930
- :client, client: 42
931
- )
932
- end).to raise_error(TypeError)
933
- end
934
-
935
- it "should return nil for missing credential key" do
936
- expect(Signet::OAuth1.extract_credential_key_option(:client, {})).to eq nil
937
- end
938
-
939
- it "should find the correct credential key" do
940
- expect(Signet::OAuth1.extract_credential_key_option(
941
- :client, client_credential_key: "dpf43f3p2l4k3l03"
942
- )).to eq "dpf43f3p2l4k3l03"
943
- end
944
-
945
- it "should find the correct credential key" do
946
- expect(Signet::OAuth1.extract_credential_key_option(
947
- :client, client_credential: Signet::OAuth1::Credential.new(
948
- "dpf43f3p2l4k3l03", "kd94hf93k423kf44"
949
- )
950
- )).to eq "dpf43f3p2l4k3l03"
951
- end
952
-
953
- it "should find the correct credential key" do
954
- client = Signet::OAuth1::Client.new
955
- client.client_credential = Signet::OAuth1::Credential.new(
956
- "dpf43f3p2l4k3l03", "kd94hf93k423kf44"
957
- )
958
- expect(Signet::OAuth1.extract_credential_key_option(
959
- :client, client: client
960
- )).to eq "dpf43f3p2l4k3l03"
961
- end
962
-
963
- it "should find the correct credential key" do
964
- client = Signet::OAuth1::Client.new
965
- client.temporary_credential = Signet::OAuth1::Credential.new(
966
- "hh5s93j4hdidpola", "hdhd0244k9j7ao03"
967
- )
968
- expect(Signet::OAuth1.extract_credential_key_option(
969
- :temporary, client: client
970
- )).to eq "hh5s93j4hdidpola"
971
- end
972
- end
973
-
974
- describe Signet::OAuth1, "extracting credential secrets from options" do
975
- it "should raise an error for bogus credentials" do
976
- expect(lambda do
977
- Signet::OAuth1.extract_credential_secret_option(
978
- :client, client_credential_secret: true
979
- )
980
- end).to raise_error(TypeError)
981
- end
982
-
983
- it "should raise an error for bogus credentials" do
984
- expect(lambda do
985
- Signet::OAuth1.extract_credential_secret_option(
986
- :client, client_credential: 42
987
- )
988
- end).to raise_error(TypeError)
989
- end
990
-
991
- it "should raise an error for bogus credentials" do
992
- expect(lambda do
993
- Signet::OAuth1.extract_credential_secret_option(
994
- :client, client: 42
995
- )
996
- end).to raise_error(TypeError)
997
- end
998
-
999
- it "should raise an error for missing credential secret" do
1000
- expect(Signet::OAuth1.extract_credential_secret_option(:client, {})).to eq nil
1001
- end
1002
-
1003
- it "should find the correct credential secret" do
1004
- expect(Signet::OAuth1.extract_credential_secret_option(
1005
- :client, client_credential_secret: "kd94hf93k423kf44"
1006
- )).to eq "kd94hf93k423kf44"
1007
- end
1008
-
1009
- it "should find the correct credential secret" do
1010
- expect(Signet::OAuth1.extract_credential_secret_option(
1011
- :client, client_credential: Signet::OAuth1::Credential.new(
1012
- "dpf43f3p2l4k3l03", "kd94hf93k423kf44"
1013
- )
1014
- )).to eq "kd94hf93k423kf44"
1015
- end
1016
-
1017
- it "should find the correct credential secret" do
1018
- client = Signet::OAuth1::Client.new
1019
- client.client_credential = Signet::OAuth1::Credential.new(
1020
- "dpf43f3p2l4k3l03", "kd94hf93k423kf44"
1021
- )
1022
- expect(Signet::OAuth1.extract_credential_secret_option(
1023
- :client, client: client
1024
- )).to eq "kd94hf93k423kf44"
1025
- end
1026
-
1027
- it "should find the correct credential secret" do
1028
- client = Signet::OAuth1::Client.new
1029
- client.temporary_credential = Signet::OAuth1::Credential.new(
1030
- "hh5s93j4hdidpola", "hdhd0244k9j7ao03"
1031
- )
1032
- expect(Signet::OAuth1.extract_credential_secret_option(
1033
- :temporary, client: client
1034
- )).to eq "hdhd0244k9j7ao03"
1035
- end
1036
- end