signet 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,883 @@
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
+
15
+ require 'spec_helper'
16
+
17
+ require 'signet/oauth_1'
18
+ require 'signet/oauth_1/client'
19
+ require 'signet/oauth_1/credential'
20
+
21
+ describe Signet::OAuth1 do
22
+ it 'should correctly normalize parameters' do
23
+ parameters = [
24
+ ["a", "1"],
25
+ ["c", "hi there"],
26
+ ["f", "25"],
27
+ ["f", "50"],
28
+ ["f", "a"],
29
+ ["z", "p"],
30
+ ["z", "t"]
31
+ ]
32
+ Signet::OAuth1.normalize_parameters(parameters).should ==
33
+ 'a=1&c=hi%20there&f=25&f=50&f=a&z=p&z=t'
34
+ end
35
+
36
+ it 'should correctly normalize parameters' do
37
+ parameters = [
38
+ ["b5", "=%3D"],
39
+ ["a3", "a"],
40
+ ["c@", ""],
41
+ ["a2", "r b"],
42
+ ["oauth_consumer_key", "9djdj82h48djs9d2"],
43
+ ["oauth_token", "kkk9d7dh3k39sjv7"],
44
+ ["oauth_signature_method", "HMAC-SHA1"],
45
+ ["oauth_timestamp", "137131201"],
46
+ ["oauth_nonce", "7d8f3e4a"],
47
+ ["c2", ""],
48
+ ["a3", "2 q"]
49
+ ]
50
+ Signet::OAuth1.normalize_parameters(parameters).should ==
51
+ 'a2=r%20b&a3=2%20q&a3=a&b5=%3D%253D&c%40=&c2=&oauth_consumer_key=9dj' +
52
+ 'dj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1' +
53
+ '&oauth_timestamp=137131201&oauth_token=kkk9d7dh3k39sjv7'
54
+ end
55
+
56
+ it 'should exclude the "oauth_signature" parameter when normalizing' do
57
+ parameters = [
58
+ ["a", "1"],
59
+ ["b", "2"],
60
+ ["c", "3"],
61
+ ["oauth_signature", "dpf43f3p2l4k3l03"]
62
+ ]
63
+ Signet::OAuth1.normalize_parameters(parameters).should ==
64
+ "a=1&b=2&c=3"
65
+ end
66
+
67
+ it 'should raise an error if normalizing parameters with bogus values' do
68
+ (lambda do
69
+ Signet::OAuth1.normalize_parameters(42)
70
+ end).should raise_error(TypeError)
71
+ end
72
+
73
+ it 'should raise an error if generating a base string with bogus values' do
74
+ (lambda do
75
+ Signet::OAuth1.generate_base_string(
76
+ "GET", "http://photos.example.net/photos", 42
77
+ )
78
+ end).should raise_error(TypeError)
79
+ end
80
+
81
+ it 'should correctly generate a base string' do
82
+ method = "GET"
83
+ uri = "http://photos.example.net/photos"
84
+ parameters = {
85
+ "oauth_consumer_key" => "dpf43f3p2l4k3l03",
86
+ "oauth_token" => "nnch734d00sl2jdk",
87
+ "oauth_signature_method" => "HMAC-SHA1",
88
+ "oauth_timestamp" => "1191242096",
89
+ "oauth_nonce" => "kllo9940pd9333jh",
90
+ "oauth_version" => "1.0",
91
+ "file" => "vacation.jpg",
92
+ "size" => "original"
93
+ }
94
+ Signet::OAuth1.generate_base_string(method, uri, parameters).should == (
95
+ "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" +
96
+ "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" +
97
+ "oauth_nonce%3Dkllo9940pd9333jh%26" +
98
+ "oauth_signature_method%3DHMAC-SHA1%26" +
99
+ "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" +
100
+ "oauth_version%3D1.0%26size%3Doriginal"
101
+ )
102
+ end
103
+
104
+ it 'should correctly generate a base string with normalized ports' do
105
+ method = "GET"
106
+ uri = "http://photos.example.net:80/photos"
107
+ parameters = {
108
+ "oauth_consumer_key" => "dpf43f3p2l4k3l03",
109
+ "oauth_token" => "nnch734d00sl2jdk",
110
+ "oauth_signature_method" => "HMAC-SHA1",
111
+ "oauth_timestamp" => "1191242096",
112
+ "oauth_nonce" => "kllo9940pd9333jh",
113
+ "oauth_version" => "1.0",
114
+ "file" => "vacation.jpg",
115
+ "size" => "original"
116
+ }
117
+ Signet::OAuth1.generate_base_string(method, uri, parameters).should == (
118
+ "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" +
119
+ "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" +
120
+ "oauth_nonce%3Dkllo9940pd9333jh%26" +
121
+ "oauth_signature_method%3DHMAC-SHA1%26" +
122
+ "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" +
123
+ "oauth_version%3D1.0%26size%3Doriginal"
124
+ )
125
+ end
126
+
127
+ it 'should correctly generate a base string with normalized ports' do
128
+ method = "GET"
129
+ uri = "https://photos.example.net:443/photos"
130
+ parameters = {
131
+ "oauth_consumer_key" => "dpf43f3p2l4k3l03",
132
+ "oauth_token" => "nnch734d00sl2jdk",
133
+ "oauth_signature_method" => "HMAC-SHA1",
134
+ "oauth_timestamp" => "1191242096",
135
+ "oauth_nonce" => "kllo9940pd9333jh",
136
+ "oauth_version" => "1.0",
137
+ "file" => "vacation.jpg",
138
+ "size" => "original"
139
+ }
140
+ Signet::OAuth1.generate_base_string(method, uri, parameters).should == (
141
+ "GET&https%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" +
142
+ "oauth_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%26size%3Doriginal"
147
+ )
148
+ end
149
+
150
+ it 'should correctly generate a base signature' do
151
+ method = :get
152
+ uri =
153
+ "HTTP://photos.EXAMPLE.net:80/photos?file=vacation.jpg"
154
+ parameters = {
155
+ "oauth_consumer_key" => "dpf43f3p2l4k3l03",
156
+ "oauth_token" => "nnch734d00sl2jdk",
157
+ "oauth_signature_method" => "HMAC-SHA1",
158
+ "oauth_timestamp" => "1191242096",
159
+ "oauth_nonce" => "kllo9940pd9333jh",
160
+ "oauth_version" => "1.0",
161
+ "size" => "original"
162
+ }
163
+ Signet::OAuth1.generate_base_string(method, uri, parameters).should == (
164
+ "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" +
165
+ "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26" +
166
+ "oauth_nonce%3Dkllo9940pd9333jh%26" +
167
+ "oauth_signature_method%3DHMAC-SHA1%26" +
168
+ "oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26" +
169
+ "oauth_version%3D1.0%26size%3Doriginal"
170
+ )
171
+ end
172
+
173
+ it 'should correctly generate an authorization header' do
174
+ parameters = [
175
+ ["oauth_consumer_key", "0685bd9184jfhq22"],
176
+ ["oauth_token", "ad180jjd733klru7"],
177
+ ["oauth_signature_method", "HMAC-SHA1"],
178
+ ["oauth_signature", "wOJIO9A2W5mFwDgiDvZbTSMK/PY="],
179
+ ["oauth_timestamp", "137131200"],
180
+ ["oauth_nonce", "4572616e48616d6d65724c61686176"],
181
+ ["oauth_version", "1.0"]
182
+ ]
183
+ Signet::OAuth1.generate_authorization_header(
184
+ parameters, "http://sp.example.com/"
185
+ ).should == (
186
+ 'OAuth realm="http://sp.example.com/", ' +
187
+ 'oauth_consumer_key="0685bd9184jfhq22", ' +
188
+ 'oauth_token="ad180jjd733klru7", ' +
189
+ 'oauth_signature_method="HMAC-SHA1", ' +
190
+ 'oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D", ' +
191
+ 'oauth_timestamp="137131200", ' +
192
+ 'oauth_nonce="4572616e48616d6d65724c61686176", ' +
193
+ 'oauth_version="1.0"'
194
+ )
195
+ end
196
+
197
+ it 'should raise an error if generating an authorization header ' +
198
+ 'with bogus values' do
199
+ (lambda do
200
+ Signet::OAuth1.generate_authorization_header(42)
201
+ end).should raise_error(TypeError)
202
+ end
203
+
204
+ it 'should raise an error if generating an authorization header ' +
205
+ 'with the "realm" parameter specified the wrong way' do
206
+ parameters = [
207
+ ["realm", "http://sp.example.com/"],
208
+ ["oauth_consumer_key", "0685bd9184jfhq22"],
209
+ ["oauth_token", "ad180jjd733klru7"],
210
+ ["oauth_signature_method", "HMAC-SHA1"],
211
+ ["oauth_signature", "wOJIO9A2W5mFwDgiDvZbTSMK/PY="],
212
+ ["oauth_timestamp", "137131200"],
213
+ ["oauth_nonce", "4572616e48616d6d65724c61686176"],
214
+ ["oauth_version", "1.0"]
215
+ ]
216
+ (lambda do
217
+ Signet::OAuth1.generate_authorization_header(parameters)
218
+ end).should raise_error(ArgumentError)
219
+ end
220
+
221
+ it 'should correctly parse an authorization header' do
222
+ parameters = Hash[Signet::OAuth1.parse_authorization_header(
223
+ 'OAuth realm="http://sp.example.com/", ' +
224
+ 'oauth_consumer_key="0685bd9184jfhq22", ' +
225
+ 'oauth_token="ad180jjd733klru7", ' +
226
+ 'oauth_signature_method="HMAC-SHA1", ' +
227
+ 'oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D", ' +
228
+ 'oauth_timestamp="137131200", ' +
229
+ 'oauth_nonce="4572616e48616d6d65724c61686176", ' +
230
+ 'oauth_version="1.0"'
231
+ )]
232
+ parameters['realm'].should == 'http://sp.example.com/'
233
+ parameters['oauth_consumer_key'].should == '0685bd9184jfhq22'
234
+ parameters['oauth_token'].should == 'ad180jjd733klru7'
235
+ parameters['oauth_signature_method'].should == 'HMAC-SHA1'
236
+ parameters['oauth_signature'].should == 'wOJIO9A2W5mFwDgiDvZbTSMK/PY='
237
+ parameters['oauth_timestamp'].should == '137131200'
238
+ parameters['oauth_nonce'].should == '4572616e48616d6d65724c61686176'
239
+ parameters['oauth_version'].should == '1.0'
240
+ end
241
+
242
+ it 'should raise an error if parsing an authorization header ' +
243
+ 'with bogus values' do
244
+ (lambda do
245
+ Signet::OAuth1.parse_authorization_header(42)
246
+ end).should raise_error(TypeError)
247
+ end
248
+
249
+ it 'should raise an error if parsing a non-OAuth authorization header' do
250
+ (lambda do
251
+ Signet::OAuth1.parse_authorization_header(
252
+ 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='
253
+ )
254
+ end).should raise_error(ArgumentError)
255
+ end
256
+
257
+ it 'should correctly parse a form encoded credential' do
258
+ credential = Signet::OAuth1.parse_form_encoded_credentials(
259
+ 'oauth_token=hh5s93j4hdidpola&oauth_token_secret=hdhd0244k9j7ao03'
260
+ )
261
+ credential.key.should == 'hh5s93j4hdidpola'
262
+ credential.secret.should == 'hdhd0244k9j7ao03'
263
+ end
264
+
265
+ it 'should correctly parse a form encoded credential' do
266
+ credential = Signet::OAuth1.parse_form_encoded_credentials(
267
+ 'oauth_token=hdk48Djdsa&oauth_token_secret=xyz4992k83j47x0b&' +
268
+ 'oauth_callback_confirmed=true'
269
+ )
270
+ credential.key.should == 'hdk48Djdsa'
271
+ credential.secret.should == 'xyz4992k83j47x0b'
272
+ end
273
+
274
+ it 'should raise an error if parsing a form encoded credential ' +
275
+ 'with bogus values' do
276
+ (lambda do
277
+ Signet::OAuth1.parse_form_encoded_credentials(42)
278
+ end).should raise_error(TypeError)
279
+ end
280
+
281
+ it 'should correctly generate a signature for a set of parameters' do
282
+ method = :get
283
+ uri = "http://photos.example.net/photos"
284
+ client_credential_secret = 'kd94hf93k423kf44'
285
+ token_credential_secret = 'pfkkdhi9sl3r4s00'
286
+ parameters = {
287
+ "oauth_consumer_key" => "dpf43f3p2l4k3l03",
288
+ "oauth_token" => "nnch734d00sl2jdk",
289
+ "oauth_signature_method" => "HMAC-SHA1",
290
+ "oauth_timestamp" => "1191242096",
291
+ "oauth_nonce" => "kllo9940pd9333jh",
292
+ "oauth_version" => "1.0",
293
+ "file" => "vacation.jpg",
294
+ "size" => "original"
295
+ }
296
+ Signet::OAuth1.sign_parameters(
297
+ method,
298
+ uri,
299
+ parameters,
300
+ client_credential_secret,
301
+ token_credential_secret
302
+ ).should == "tR3+Ty81lMeYAr/Fid0kMTYa/WM="
303
+ end
304
+
305
+ it 'should raise an error when trying to sign with with unknown method' do
306
+ method = :get
307
+ uri = "http://photos.example.net/photos"
308
+ client_credential_secret = 'kd94hf93k423kf44'
309
+ token_credential_secret = 'pfkkdhi9sl3r4s00'
310
+ parameters = {
311
+ "oauth_consumer_key" => "dpf43f3p2l4k3l03",
312
+ "oauth_token" => "nnch734d00sl2jdk",
313
+ "oauth_signature_method" => "HMAC-BOGUS",
314
+ "oauth_timestamp" => "1191242096",
315
+ "oauth_nonce" => "kllo9940pd9333jh",
316
+ "oauth_version" => "1.0",
317
+ "file" => "vacation.jpg",
318
+ "size" => "original"
319
+ }
320
+ (lambda do
321
+ Signet::OAuth1.sign_parameters(
322
+ method,
323
+ uri,
324
+ parameters,
325
+ client_credential_secret,
326
+ token_credential_secret
327
+ )
328
+ end).should raise_error(NotImplementedError)
329
+ end
330
+
331
+ it 'should correctly generate authorization URIs' do
332
+ authorization_uri = 'http://photos.example.net/authorize'
333
+ temporary_credential_key = 'hh5s93j4hdidpola'
334
+ callback = 'http://printer.example.com/request_token_ready'
335
+ parsed_uri = Addressable::URI.parse(
336
+ Signet::OAuth1.generate_authorization_uri(
337
+ authorization_uri,
338
+ :temporary_credential_key => temporary_credential_key,
339
+ :callback => callback
340
+ )
341
+ )
342
+ parsed_uri.query_values.should have_key('oauth_token')
343
+ parsed_uri.query_values['oauth_token'].should == temporary_credential_key
344
+ parsed_uri.query_values.should have_key('oauth_callback')
345
+ parsed_uri.query_values['oauth_callback'].should == callback
346
+ end
347
+ end
348
+
349
+ describe Signet::OAuth1, 'when generating temporary credentials parameters' do
350
+ before do
351
+ @client_credential_key = 'dpf43f3p2l4k3l03'
352
+ @callback = 'http://printer.example.com/request_token_ready'
353
+ @signature_method = 'HMAC-SHA1'
354
+ @scope = 'http://photos.example.com/full_access'
355
+ @additional_parameters = [['scope', @scope]]
356
+ @unsigned_parameters = Hash[
357
+ Signet::OAuth1.unsigned_temporary_credential_parameters(
358
+ :client_credential_key => @client_credential_key,
359
+ :callback => @callback,
360
+ :signature_method => @signature_method,
361
+ :additional_parameters => @additional_parameters
362
+ )
363
+ ]
364
+ end
365
+
366
+ it 'should raise an error if the client credential key is missing' do
367
+ (lambda do
368
+ Signet::OAuth1.unsigned_temporary_credential_parameters(
369
+ :client_credential_key => nil,
370
+ :callback => @callback,
371
+ :signature_method => @signature_method,
372
+ :additional_parameters => @additional_parameters
373
+ )
374
+ end).should raise_error(ArgumentError)
375
+ end
376
+
377
+ it 'should have the correct client credential key' do
378
+ @unsigned_parameters.should have_key('oauth_consumer_key')
379
+ @unsigned_parameters['oauth_consumer_key'].should == @client_credential_key
380
+ end
381
+
382
+ it 'should have the correct signature method' do
383
+ @unsigned_parameters.should have_key('oauth_signature_method')
384
+ @unsigned_parameters['oauth_signature_method'].should == @signature_method
385
+ end
386
+
387
+ it 'should have a valid timestamp' do
388
+ # Verify that we have a timestamp, it's in the correct format and within
389
+ # a reasonable range of the current time.
390
+ @unsigned_parameters.should have_key('oauth_timestamp')
391
+ @unsigned_parameters['oauth_timestamp'].should =~ /^[0-9]+$/
392
+ @unsigned_parameters['oauth_timestamp'].to_i.should <= Time.now.to_i
393
+ @unsigned_parameters['oauth_timestamp'].to_i.should >= Time.now.to_i - 1
394
+ end
395
+
396
+ it 'should have a valid nonce' do
397
+ # Verify that we have a nonce and that it has sufficient length for
398
+ # uniqueness.
399
+ @unsigned_parameters.should have_key('oauth_nonce')
400
+ @unsigned_parameters['oauth_nonce'].should =~ /^[0-9a-zA-Z]{16,100}$/
401
+ end
402
+
403
+ it 'should have the correct callback' do
404
+ @unsigned_parameters.should have_key('oauth_callback')
405
+ @unsigned_parameters['oauth_callback'].should == @callback
406
+ end
407
+
408
+ it 'should have the correct scope parameter' do
409
+ @unsigned_parameters.should have_key('scope')
410
+ @unsigned_parameters['scope'].should == @scope
411
+ end
412
+
413
+ it 'should have the correct OAuth version' do
414
+ @unsigned_parameters.should have_key('oauth_version')
415
+ @unsigned_parameters['oauth_version'].should == '1.0'
416
+ end
417
+ end
418
+
419
+ describe Signet::OAuth1, 'when generating token credential parameters' do
420
+ before do
421
+ @client_credential_key = 'dpf43f3p2l4k3l03'
422
+ @temporary_credential_key = 'hh5s93j4hdidpola'
423
+ @verifier = '473f82d3'
424
+ @signature_method = 'HMAC-SHA1'
425
+ @unsigned_parameters = Hash[
426
+ Signet::OAuth1.unsigned_token_credential_parameters(
427
+ :client_credential_key => @client_credential_key,
428
+ :temporary_credential_key => @temporary_credential_key,
429
+ :signature_method => @signature_method,
430
+ :verifier => @verifier
431
+ )
432
+ ]
433
+ end
434
+
435
+ it 'should raise an error if the client credential key is missing' do
436
+ (lambda do
437
+ Signet::OAuth1.unsigned_token_credential_parameters(
438
+ :client_credential_key => nil,
439
+ :temporary_credential_key => @temporary_credential_key,
440
+ :signature_method => @signature_method,
441
+ :verifier => @verifier
442
+ )
443
+ end).should raise_error(ArgumentError)
444
+ end
445
+
446
+ it 'should raise an error if the temporary credential key is missing' do
447
+ (lambda do
448
+ Signet::OAuth1.unsigned_token_credential_parameters(
449
+ :client_credential_key => @client_credential_key,
450
+ :temporary_credential_key => nil,
451
+ :signature_method => @signature_method,
452
+ :verifier => @verifier
453
+ )
454
+ end).should raise_error(ArgumentError)
455
+ end
456
+
457
+ it 'should raise an error if the verifier is missing' do
458
+ (lambda do
459
+ Signet::OAuth1.unsigned_token_credential_parameters(
460
+ :client_credential_key => @client_credential_key,
461
+ :temporary_credential_key => @temporary_credential_key,
462
+ :signature_method => @signature_method,
463
+ :verifier => nil
464
+ )
465
+ end).should raise_error(ArgumentError)
466
+ end
467
+
468
+ it 'should have the correct client credential key' do
469
+ @unsigned_parameters.should have_key('oauth_consumer_key')
470
+ @unsigned_parameters['oauth_consumer_key'].should == @client_credential_key
471
+ end
472
+
473
+ it 'should have the correct temporary credentials key' do
474
+ @unsigned_parameters.should have_key('oauth_token')
475
+ @unsigned_parameters['oauth_token'].should == @temporary_credential_key
476
+ end
477
+
478
+ it 'should have the correct signature method' do
479
+ @unsigned_parameters.should have_key('oauth_signature_method')
480
+ @unsigned_parameters['oauth_signature_method'].should == @signature_method
481
+ end
482
+
483
+ it 'should have a valid timestamp' do
484
+ # Verify that we have a timestamp, it's in the correct format and within
485
+ # a reasonable range of the current time.
486
+ @unsigned_parameters.should have_key('oauth_timestamp')
487
+ @unsigned_parameters['oauth_timestamp'].should =~ /^[0-9]+$/
488
+ @unsigned_parameters['oauth_timestamp'].to_i.should <= Time.now.to_i
489
+ @unsigned_parameters['oauth_timestamp'].to_i.should >= Time.now.to_i - 1
490
+ end
491
+
492
+ it 'should have a valid nonce' do
493
+ # Verify that we have a nonce and that it has sufficient length for
494
+ # uniqueness.
495
+ @unsigned_parameters.should have_key('oauth_nonce')
496
+ @unsigned_parameters['oauth_nonce'].should =~ /^[0-9a-zA-Z]{16,100}$/
497
+ end
498
+
499
+ it 'should have the verifier' do
500
+ @unsigned_parameters.should have_key('oauth_verifier')
501
+ @unsigned_parameters['oauth_verifier'].should == @verifier
502
+ end
503
+
504
+ it 'should have the correct OAuth version' do
505
+ @unsigned_parameters.should have_key('oauth_version')
506
+ @unsigned_parameters['oauth_version'].should == '1.0'
507
+ end
508
+ end
509
+
510
+ describe Signet::OAuth1, 'when generating protected resource parameters' do
511
+ before do
512
+ @client_credential_key = 'dpf43f3p2l4k3l03'
513
+ @token_credential_key = 'nnch734d00sl2jdk'
514
+ @signature_method = 'HMAC-SHA1'
515
+ @unsigned_parameters = Hash[
516
+ Signet::OAuth1.unsigned_resource_parameters(
517
+ :client_credential_key => @client_credential_key,
518
+ :token_credential_key => @token_credential_key,
519
+ :signature_method => @signature_method
520
+ )
521
+ ]
522
+ end
523
+
524
+ it 'should raise an error if the client credential key is missing' do
525
+ (lambda do
526
+ Signet::OAuth1.unsigned_resource_parameters(
527
+ :client_credential_key => nil,
528
+ :token_credential_key => @token_credential_key,
529
+ :signature_method => @signature_method
530
+ )
531
+ end).should raise_error(ArgumentError)
532
+ end
533
+
534
+ it 'should raise an error if the token credential key is missing' do
535
+ (lambda do
536
+ Signet::OAuth1.unsigned_resource_parameters(
537
+ :client_credential_key => @client_credential_key,
538
+ :token_credential_key => nil,
539
+ :signature_method => @signature_method
540
+ )
541
+ end).should raise_error(ArgumentError)
542
+ end
543
+
544
+ it 'should have the correct client credential key' do
545
+ @unsigned_parameters.should have_key('oauth_consumer_key')
546
+ @unsigned_parameters['oauth_consumer_key'].should == @client_credential_key
547
+ end
548
+
549
+ it 'should have the correct token credentials key' do
550
+ @unsigned_parameters.should have_key('oauth_token')
551
+ @unsigned_parameters['oauth_token'].should == @token_credential_key
552
+ end
553
+
554
+ it 'should have the correct signature method' do
555
+ @unsigned_parameters.should have_key('oauth_signature_method')
556
+ @unsigned_parameters['oauth_signature_method'].should == @signature_method
557
+ end
558
+
559
+ it 'should have a valid timestamp' do
560
+ # Verify that we have a timestamp, it's in the correct format and within
561
+ # a reasonable range of the current time.
562
+ @unsigned_parameters.should have_key('oauth_timestamp')
563
+ @unsigned_parameters['oauth_timestamp'].should =~ /^[0-9]+$/
564
+ @unsigned_parameters['oauth_timestamp'].to_i.should <= Time.now.to_i
565
+ @unsigned_parameters['oauth_timestamp'].to_i.should >= Time.now.to_i - 1
566
+ end
567
+
568
+ it 'should have a valid nonce' do
569
+ # Verify that we have a nonce and that it has sufficient length for
570
+ # uniqueness.
571
+ @unsigned_parameters.should have_key('oauth_nonce')
572
+ @unsigned_parameters['oauth_nonce'].should =~ /^[0-9a-zA-Z]{16,100}$/
573
+ end
574
+
575
+ it 'should have the correct OAuth version' do
576
+ @unsigned_parameters.should have_key('oauth_version')
577
+ @unsigned_parameters['oauth_version'].should == '1.0'
578
+ end
579
+ end
580
+
581
+ describe Signet::OAuth1, 'when generating token credential parameters ' +
582
+ 'with Signet::OAuth1::Credential objects' do
583
+ before do
584
+ @client_credential = Signet::OAuth1::Credential.new(
585
+ 'dpf43f3p2l4k3l03', 'kd94hf93k423kf44'
586
+ )
587
+ @temporary_credential = Signet::OAuth1::Credential.new(
588
+ 'hh5s93j4hdidpola', 'hdhd0244k9j7ao03'
589
+ )
590
+ @verifier = '473f82d3'
591
+ @signature_method = 'HMAC-SHA1'
592
+ @unsigned_parameters = Hash[
593
+ Signet::OAuth1.unsigned_token_credential_parameters(
594
+ :client_credential => @client_credential,
595
+ :temporary_credential => @temporary_credential,
596
+ :signature_method => @signature_method,
597
+ :verifier => @verifier
598
+ )
599
+ ]
600
+ end
601
+
602
+ it 'should have the correct client credential key' do
603
+ @unsigned_parameters.should have_key('oauth_consumer_key')
604
+ @unsigned_parameters['oauth_consumer_key'].should == @client_credential.key
605
+ end
606
+
607
+ it 'should have the correct temporary credentials key' do
608
+ @unsigned_parameters.should have_key('oauth_token')
609
+ @unsigned_parameters['oauth_token'].should == @temporary_credential.key
610
+ end
611
+
612
+ it 'should have the correct signature method' do
613
+ @unsigned_parameters.should have_key('oauth_signature_method')
614
+ @unsigned_parameters['oauth_signature_method'].should == @signature_method
615
+ end
616
+
617
+ it 'should have a valid timestamp' do
618
+ # Verify that we have a timestamp, it's in the correct format and within
619
+ # a reasonable range of the current time.
620
+ @unsigned_parameters.should have_key('oauth_timestamp')
621
+ @unsigned_parameters['oauth_timestamp'].should =~ /^[0-9]+$/
622
+ @unsigned_parameters['oauth_timestamp'].to_i.should <= Time.now.to_i
623
+ @unsigned_parameters['oauth_timestamp'].to_i.should >= Time.now.to_i - 1
624
+ end
625
+
626
+ it 'should have a valid nonce' do
627
+ # Verify that we have a nonce and that it has sufficient length for
628
+ # uniqueness.
629
+ @unsigned_parameters.should have_key('oauth_nonce')
630
+ @unsigned_parameters['oauth_nonce'].should =~ /^[0-9a-zA-Z]{16,100}$/
631
+ end
632
+
633
+ it 'should have the correct OAuth version' do
634
+ @unsigned_parameters.should have_key('oauth_version')
635
+ @unsigned_parameters['oauth_version'].should == '1.0'
636
+ end
637
+ end
638
+
639
+ describe Signet::OAuth1, 'when generating token credential parameters ' +
640
+ 'with a Signet::OAuth1::Client object' do
641
+ before do
642
+ @client = Signet::OAuth1::Client.new
643
+ @client.client_credential = Signet::OAuth1::Credential.new(
644
+ 'dpf43f3p2l4k3l03', 'kd94hf93k423kf44'
645
+ )
646
+ @client.temporary_credential = Signet::OAuth1::Credential.new(
647
+ 'hh5s93j4hdidpola', 'hdhd0244k9j7ao03'
648
+ )
649
+ @verifier = '473f82d3'
650
+ @signature_method = 'HMAC-SHA1'
651
+ @unsigned_parameters = Hash[
652
+ Signet::OAuth1.unsigned_token_credential_parameters(
653
+ :client => @client,
654
+ :signature_method => @signature_method,
655
+ :verifier => @verifier
656
+ )
657
+ ]
658
+ end
659
+
660
+ it 'should have the correct client credential key' do
661
+ @unsigned_parameters.should have_key('oauth_consumer_key')
662
+ @unsigned_parameters['oauth_consumer_key'].should ==
663
+ @client.client_credential_key
664
+ end
665
+
666
+ it 'should have the correct temporary credentials key' do
667
+ @unsigned_parameters.should have_key('oauth_token')
668
+ @unsigned_parameters['oauth_token'].should ==
669
+ @client.temporary_credential_key
670
+ end
671
+
672
+ it 'should have the correct signature method' do
673
+ @unsigned_parameters.should have_key('oauth_signature_method')
674
+ @unsigned_parameters['oauth_signature_method'].should == @signature_method
675
+ end
676
+
677
+ it 'should have a valid timestamp' do
678
+ # Verify that we have a timestamp, it's in the correct format and within
679
+ # a reasonable range of the current time.
680
+ @unsigned_parameters.should have_key('oauth_timestamp')
681
+ @unsigned_parameters['oauth_timestamp'].should =~ /^[0-9]+$/
682
+ @unsigned_parameters['oauth_timestamp'].to_i.should <= Time.now.to_i
683
+ @unsigned_parameters['oauth_timestamp'].to_i.should >= Time.now.to_i - 1
684
+ end
685
+
686
+ it 'should have a valid nonce' do
687
+ # Verify that we have a nonce and that it has sufficient length for
688
+ # uniqueness.
689
+ @unsigned_parameters.should have_key('oauth_nonce')
690
+ @unsigned_parameters['oauth_nonce'].should =~ /^[0-9a-zA-Z]{16,100}$/
691
+ end
692
+
693
+ it 'should have the correct OAuth version' do
694
+ @unsigned_parameters.should have_key('oauth_version')
695
+ @unsigned_parameters['oauth_version'].should == '1.0'
696
+ end
697
+ end
698
+
699
+ describe Signet::OAuth1, 'when generating token credential parameters ' +
700
+ 'with Signet::OAuth1::Credential objects' do
701
+ before do
702
+ @client_credential = Signet::OAuth1::Credential.new(
703
+ 'dpf43f3p2l4k3l03', 'kd94hf93k423kf44'
704
+ )
705
+ @temporary_credential = Signet::OAuth1::Credential.new(
706
+ 'hh5s93j4hdidpola', 'hdhd0244k9j7ao03'
707
+ )
708
+ @verifier = '473f82d3'
709
+ @signature_method = 'HMAC-SHA1'
710
+ @unsigned_parameters = Hash[
711
+ Signet::OAuth1.unsigned_token_credential_parameters(
712
+ :client_credential => @client_credential,
713
+ :temporary_credential => @temporary_credential,
714
+ :signature_method => @signature_method,
715
+ :verifier => @verifier
716
+ )
717
+ ]
718
+ end
719
+
720
+ it 'should have the correct client credential key' do
721
+ @unsigned_parameters.should have_key('oauth_consumer_key')
722
+ @unsigned_parameters['oauth_consumer_key'].should == @client_credential.key
723
+ end
724
+
725
+ it 'should have the correct temporary credentials key' do
726
+ @unsigned_parameters.should have_key('oauth_token')
727
+ @unsigned_parameters['oauth_token'].should == @temporary_credential.key
728
+ end
729
+
730
+ it 'should have the correct signature method' do
731
+ @unsigned_parameters.should have_key('oauth_signature_method')
732
+ @unsigned_parameters['oauth_signature_method'].should == @signature_method
733
+ end
734
+
735
+ it 'should have a valid timestamp' do
736
+ # Verify that we have a timestamp, it's in the correct format and within
737
+ # a reasonable range of the current time.
738
+ @unsigned_parameters.should have_key('oauth_timestamp')
739
+ @unsigned_parameters['oauth_timestamp'].should =~ /^[0-9]+$/
740
+ @unsigned_parameters['oauth_timestamp'].to_i.should <= Time.now.to_i
741
+ @unsigned_parameters['oauth_timestamp'].to_i.should >= Time.now.to_i - 1
742
+ end
743
+
744
+ it 'should have a valid nonce' do
745
+ # Verify that we have a nonce and that it has sufficient length for
746
+ # uniqueness.
747
+ @unsigned_parameters.should have_key('oauth_nonce')
748
+ @unsigned_parameters['oauth_nonce'].should =~ /^[0-9a-zA-Z]{16,100}$/
749
+ end
750
+
751
+ it 'should have the correct OAuth version' do
752
+ @unsigned_parameters.should have_key('oauth_version')
753
+ @unsigned_parameters['oauth_version'].should == '1.0'
754
+ end
755
+ end
756
+
757
+ describe Signet::OAuth1, 'extracting credential keys from options' do
758
+ it 'should raise an error for bogus credentials' do
759
+ (lambda do
760
+ Signet::OAuth1.extract_credential_key_option(
761
+ :client, {:client_credential_key => true}
762
+ )
763
+ end).should raise_error(TypeError)
764
+ end
765
+
766
+ it 'should raise an error for bogus credentials' do
767
+ (lambda do
768
+ Signet::OAuth1.extract_credential_key_option(
769
+ :client, {:client_credential => 42}
770
+ )
771
+ end).should raise_error(TypeError)
772
+ end
773
+
774
+ it 'should raise an error for bogus credentials' do
775
+ (lambda do
776
+ Signet::OAuth1.extract_credential_key_option(
777
+ :client, {:client => 42}
778
+ )
779
+ end).should raise_error(TypeError)
780
+ end
781
+
782
+ it 'should return nil for missing credential key' do
783
+ Signet::OAuth1.extract_credential_key_option(:client, {}).should == nil
784
+ end
785
+
786
+ it 'should find the correct credential key' do
787
+ Signet::OAuth1.extract_credential_key_option(
788
+ :client, {:client_credential_key => 'dpf43f3p2l4k3l03'}
789
+ ).should == 'dpf43f3p2l4k3l03'
790
+ end
791
+
792
+ it 'should find the correct credential key' do
793
+ Signet::OAuth1.extract_credential_key_option(
794
+ :client, {:client_credential => Signet::OAuth1::Credential.new(
795
+ 'dpf43f3p2l4k3l03', 'kd94hf93k423kf44'
796
+ )}
797
+ ).should == 'dpf43f3p2l4k3l03'
798
+ end
799
+
800
+ it 'should find the correct credential key' do
801
+ client = Signet::OAuth1::Client.new
802
+ client.client_credential = Signet::OAuth1::Credential.new(
803
+ 'dpf43f3p2l4k3l03', 'kd94hf93k423kf44'
804
+ )
805
+ Signet::OAuth1.extract_credential_key_option(
806
+ :client, {:client => client}
807
+ ).should == 'dpf43f3p2l4k3l03'
808
+ end
809
+
810
+ it 'should find the correct credential key' do
811
+ client = Signet::OAuth1::Client.new
812
+ client.temporary_credential = Signet::OAuth1::Credential.new(
813
+ 'hh5s93j4hdidpola', 'hdhd0244k9j7ao03'
814
+ )
815
+ Signet::OAuth1.extract_credential_key_option(
816
+ :temporary, {:client => client}
817
+ ).should == 'hh5s93j4hdidpola'
818
+ end
819
+ end
820
+
821
+ describe Signet::OAuth1, 'extracting credential secrets from options' do
822
+ it 'should raise an error for bogus credentials' do
823
+ (lambda do
824
+ Signet::OAuth1.extract_credential_secret_option(
825
+ :client, {:client_credential_secret => true}
826
+ )
827
+ end).should raise_error(TypeError)
828
+ end
829
+
830
+ it 'should raise an error for bogus credentials' do
831
+ (lambda do
832
+ Signet::OAuth1.extract_credential_secret_option(
833
+ :client, {:client_credential => 42}
834
+ )
835
+ end).should raise_error(TypeError)
836
+ end
837
+
838
+ it 'should raise an error for bogus credentials' do
839
+ (lambda do
840
+ Signet::OAuth1.extract_credential_secret_option(
841
+ :client, {:client => 42}
842
+ )
843
+ end).should raise_error(TypeError)
844
+ end
845
+
846
+ it 'should raise an error for missing credential secret' do
847
+ Signet::OAuth1.extract_credential_secret_option(:client, {}).should == nil
848
+ end
849
+
850
+ it 'should find the correct credential secret' do
851
+ Signet::OAuth1.extract_credential_secret_option(
852
+ :client, {:client_credential_secret => 'kd94hf93k423kf44'}
853
+ ).should == 'kd94hf93k423kf44'
854
+ end
855
+
856
+ it 'should find the correct credential secret' do
857
+ Signet::OAuth1.extract_credential_secret_option(
858
+ :client, {:client_credential => Signet::OAuth1::Credential.new(
859
+ 'dpf43f3p2l4k3l03', 'kd94hf93k423kf44'
860
+ )}
861
+ ).should == 'kd94hf93k423kf44'
862
+ end
863
+
864
+ it 'should find the correct credential secret' do
865
+ client = Signet::OAuth1::Client.new
866
+ client.client_credential = Signet::OAuth1::Credential.new(
867
+ 'dpf43f3p2l4k3l03', 'kd94hf93k423kf44'
868
+ )
869
+ Signet::OAuth1.extract_credential_secret_option(
870
+ :client, {:client => client}
871
+ ).should == 'kd94hf93k423kf44'
872
+ end
873
+
874
+ it 'should find the correct credential secret' do
875
+ client = Signet::OAuth1::Client.new
876
+ client.temporary_credential = Signet::OAuth1::Credential.new(
877
+ 'hh5s93j4hdidpola', 'hdhd0244k9j7ao03'
878
+ )
879
+ Signet::OAuth1.extract_credential_secret_option(
880
+ :temporary, {:client => client}
881
+ ).should == 'hdhd0244k9j7ao03'
882
+ end
883
+ end