signet 0.14.1 → 0.16.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,810 +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 "multi_json"
16
- require "signet/oauth_1/client"
17
- require "addressable/uri"
18
- require "stringio"
19
-
20
- conn = Faraday.default_connection
21
-
22
- def merge_body chunked_body
23
- raise ArgumentError, "Expected chunked body, got nil." if chunked_body == nil
24
- merged_body = StringIO.new
25
- chunked_body.each do |chunk|
26
- merged_body.write chunk
27
- end
28
- merged_body.string
29
- end
30
-
31
- describe Signet::OAuth1::Client, "unconfigured" do
32
- before do
33
- @client = Signet::OAuth1::Client.new
34
- end
35
-
36
- it "should have no temporary_credential_uri" do
37
- expect(@client.temporary_credential_uri).to be_nil
38
- end
39
-
40
- it "should allow the temporary_credential_uri to be set to a String" do
41
- @client.temporary_credential_uri = "http://example.com/"
42
- expect(@client.temporary_credential_uri.to_s).to eq "http://example.com/"
43
- end
44
-
45
- it "should allow the temporary_credential_uri to be set to a URI" do
46
- @client.temporary_credential_uri =
47
- Addressable::URI.parse "http://example.com/"
48
- expect(@client.temporary_credential_uri.to_s).to eq "http://example.com/"
49
- end
50
-
51
- it "should have no authorization_uri" do
52
- expect(@client.authorization_uri).to be_nil
53
- end
54
-
55
- it "should allow the authorization_uri to be set to a String" do
56
- @client.authorization_uri = "http://example.com/authorize"
57
- expect(@client.authorization_uri.to_s).to include(
58
- "http://example.com/authorize"
59
- )
60
- end
61
-
62
- it "should allow the authorization_uri to be set to a Hash" do
63
- @client.authorization_uri = {
64
- scheme: "http", host: "example.com", path: "/authorize"
65
- }
66
- expect(@client.authorization_uri.to_s).to include(
67
- "http://example.com/authorize"
68
- )
69
- end
70
-
71
- it "should allow the authorization_uri to be set to a URI" do
72
- @client.authorization_uri =
73
- Addressable::URI.parse "http://example.com/authorize"
74
- expect(@client.authorization_uri.to_s).to include(
75
- "http://example.com/authorize"
76
- )
77
- end
78
-
79
- it "should have no token_credential_uri" do
80
- expect(@client.token_credential_uri).to be_nil
81
- end
82
-
83
- it "should allow the token_credential_uri to be set to a String" do
84
- @client.token_credential_uri = "http://example.com/"
85
- expect(@client.token_credential_uri.to_s).to eq "http://example.com/"
86
- end
87
-
88
- it "should allow the token_credential_uri to be set to a Hash" do
89
- @client.token_credential_uri = {
90
- scheme: "http", host: "example.com", path: "/token"
91
- }
92
- expect(@client.token_credential_uri.to_s).to eq "http://example.com/token"
93
- end
94
-
95
- it "should allow the token_credential_uri to be set to a URI" do
96
- @client.token_credential_uri =
97
- Addressable::URI.parse "http://example.com/"
98
- expect(@client.token_credential_uri.to_s).to eq "http://example.com/"
99
- end
100
-
101
- it "should have no client_credential" do
102
- expect(@client.client_credential).to be_nil
103
- end
104
-
105
- it "should raise an error for partially set client credentials" do
106
- @client.client_credential_key = "12345"
107
- @client.client_credential_secret = nil
108
- expect(lambda do
109
- @client.client_credential
110
- end).to raise_error(ArgumentError)
111
- end
112
-
113
- it "should raise an error for partially set client credentials" do
114
- @client.client_credential_key = nil
115
- @client.client_credential_secret = "54321"
116
- expect(lambda do
117
- @client.client_credential
118
- end).to raise_error(ArgumentError)
119
- end
120
-
121
- it "should allow the client_credential to be set to a " \
122
- "Signet::OAuth1::Credential" do
123
- @client.client_credential =
124
- Signet::OAuth1::Credential.new "12345", "54321"
125
- expect(@client.client_credential_key).to eq "12345"
126
- expect(@client.client_credential_secret).to eq "54321"
127
- expect(@client.client_credential).to eq Signet::OAuth1::Credential.new("12345", "54321")
128
- end
129
-
130
- it "should allow the client_credential to be set to nil" do
131
- @client.client_credential_key = "12345"
132
- @client.client_credential_secret = "54321"
133
- expect(@client.client_credential_key).to eq "12345"
134
- expect(@client.client_credential_secret).to eq "54321"
135
- @client.client_credential = nil
136
- expect(@client.client_credential).to be_nil
137
- expect(@client.client_credential_key).to be_nil
138
- expect(@client.client_credential_secret).to be_nil
139
- end
140
-
141
- it "should not allow the client_credential to be set to a bogus value" do
142
- expect(lambda do
143
- @client.client_credential = 42
144
- end).to raise_error(TypeError)
145
- end
146
-
147
- it "should have no client_credential_key" do
148
- expect(@client.client_credential_key).to be_nil
149
- end
150
-
151
- it "should allow the client_credential_key to be set to a String" do
152
- @client.client_credential_key = "12345"
153
- expect(@client.client_credential_key).to eq "12345"
154
- end
155
-
156
- it "should not allow the client_credential_key to be set to a non-String" do
157
- expect(lambda do
158
- @client.client_credential_key = 12_345
159
- end).to raise_error(TypeError)
160
- end
161
-
162
- it "should have no client_credential_secret" do
163
- expect(@client.client_credential_secret).to be_nil
164
- end
165
-
166
- it "should allow the client_credential_secret to be set to a String" do
167
- @client.client_credential_secret = "54321"
168
- expect(@client.client_credential_secret).to eq "54321"
169
- end
170
-
171
- it "should not allow the client_credential_secret " \
172
- "to be set to a non-String" do
173
- expect(lambda do
174
- @client.client_credential_secret = 54_321
175
- end).to raise_error(TypeError)
176
- end
177
-
178
- it "should have an out-of-band callback" do
179
- expect(@client.callback).to eq ::Signet::OAuth1::OUT_OF_BAND
180
- end
181
-
182
- it "should allow the callback to be set to a String" do
183
- @client.callback = "http://example.com/callback"
184
- expect(@client.callback).to eq "http://example.com/callback"
185
- end
186
-
187
- it "should allow the callback to be set to a URI" do
188
- @client.callback =
189
- Addressable::URI.parse "http://example.com/callback"
190
- expect(@client.callback).to eq "http://example.com/callback"
191
- end
192
-
193
- it "should not allow the callback to be set to a non-String" do
194
- expect(lambda do
195
- @client.callback = 12_345
196
- end).to raise_error(TypeError)
197
- end
198
-
199
- it "should raise an error if the temporary credentials URI is not set" do
200
- @client.client_credential_key = "dpf43f3p2l4k3l03"
201
- @client.client_credential_secret = "kd94hf93k423kf44"
202
- expect(lambda do
203
- @client.generate_temporary_credential_request
204
- end).to raise_error(ArgumentError)
205
- end
206
-
207
- it "should raise an error if the client credential key is not set" do
208
- @client.temporary_credential_uri =
209
- "http://example.com/temporary_credentials"
210
- @client.client_credential_secret = "kd94hf93k423kf44"
211
- expect(lambda do
212
- @client.generate_temporary_credential_request
213
- end).to raise_error(ArgumentError)
214
- end
215
-
216
- it "should raise an error if the client credential secret is not set" do
217
- @client.temporary_credential_uri =
218
- "http://example.com/temporary_credentials"
219
- @client.client_credential_key = "dpf43f3p2l4k3l03"
220
- expect(lambda do
221
- @client.generate_temporary_credential_request
222
- end).to raise_error(ArgumentError)
223
- end
224
-
225
- it "should have no temporary_credential" do
226
- expect(@client.temporary_credential).to be_nil
227
- end
228
-
229
- it "should raise an error for partially set temporary credentials" do
230
- @client.temporary_credential_key = "12345"
231
- @client.temporary_credential_secret = nil
232
- expect(lambda do
233
- @client.temporary_credential
234
- end).to raise_error(ArgumentError)
235
- end
236
-
237
- it "should raise an error for partially set temporary credentials" do
238
- @client.temporary_credential_key = nil
239
- @client.temporary_credential_secret = "54321"
240
- expect(lambda do
241
- @client.temporary_credential
242
- end).to raise_error(ArgumentError)
243
- end
244
-
245
- it "should allow the temporary_credential to be set to a " \
246
- "Signet::OAuth1::Credential" do
247
- @client.temporary_credential =
248
- Signet::OAuth1::Credential.new "12345", "54321"
249
- expect(@client.temporary_credential_key).to eq "12345"
250
- expect(@client.temporary_credential_secret).to eq "54321"
251
- expect(@client.temporary_credential).to eq Signet::OAuth1::Credential.new("12345", "54321")
252
- end
253
-
254
- it "should allow the temporary_credential to be set to nil" do
255
- @client.temporary_credential_key = "12345"
256
- @client.temporary_credential_secret = "54321"
257
- expect(@client.temporary_credential_key).to eq "12345"
258
- expect(@client.temporary_credential_secret).to eq "54321"
259
- @client.temporary_credential = nil
260
- expect(@client.temporary_credential).to be_nil
261
- expect(@client.temporary_credential_key).to be_nil
262
- expect(@client.temporary_credential_secret).to be_nil
263
- end
264
-
265
- it "should not allow the temporary_credential to be set to a bogus value" do
266
- expect(lambda do
267
- @client.temporary_credential = 42
268
- end).to raise_error(TypeError)
269
- end
270
-
271
- it "should have no temporary_credential_key" do
272
- expect(@client.temporary_credential_key).to be_nil
273
- end
274
-
275
- it "should allow the temporary_credential_key to be set to a String" do
276
- @client.temporary_credential_key = "12345"
277
- expect(@client.temporary_credential_key).to eq "12345"
278
- end
279
-
280
- it "should not allow the temporary_credential_key " \
281
- "to be set to a non-String" do
282
- expect(lambda do
283
- @client.temporary_credential_key = 12_345
284
- end).to raise_error(TypeError)
285
- end
286
-
287
- it "should have no temporary_credential_secret" do
288
- expect(@client.temporary_credential_secret).to be_nil
289
- end
290
-
291
- it "should allow the temporary_credential_secret to be set to a String" do
292
- @client.temporary_credential_secret = "54321"
293
- expect(@client.temporary_credential_secret).to eq "54321"
294
- end
295
-
296
- it "should not allow the temporary_credential_secret " \
297
- "to be set to a non-String" do
298
- expect(lambda do
299
- @client.temporary_credential_secret = 54_321
300
- end).to raise_error(TypeError)
301
- end
302
-
303
- it "should have no token_credential" do
304
- expect(@client.token_credential).to be_nil
305
- end
306
-
307
- it "should raise an error for partially set token credentials" do
308
- @client.token_credential_key = "12345"
309
- @client.token_credential_secret = nil
310
- expect(lambda do
311
- @client.token_credential
312
- end).to raise_error(ArgumentError)
313
- end
314
-
315
- it "should raise an error for partially set token credentials" do
316
- @client.token_credential_key = nil
317
- @client.token_credential_secret = "54321"
318
- expect(lambda do
319
- @client.token_credential
320
- end).to raise_error(ArgumentError)
321
- end
322
-
323
- it "should allow the token_credential to be set to a " \
324
- "Signet::OAuth1::Credential" do
325
- @client.token_credential =
326
- Signet::OAuth1::Credential.new "12345", "54321"
327
- expect(@client.token_credential_key).to eq "12345"
328
- expect(@client.token_credential_secret).to eq "54321"
329
- expect(@client.token_credential).to eq Signet::OAuth1::Credential.new("12345", "54321")
330
- end
331
-
332
- it "should allow the token_credential to be set to nil" do
333
- @client.token_credential_key = "12345"
334
- @client.token_credential_secret = "54321"
335
- expect(@client.token_credential_key).to eq "12345"
336
- expect(@client.token_credential_secret).to eq "54321"
337
- @client.token_credential = nil
338
- expect(@client.token_credential).to be_nil
339
- expect(@client.token_credential_key).to be_nil
340
- expect(@client.token_credential_secret).to be_nil
341
- end
342
-
343
- it "should not allow the token_credential to be set to a bogus value" do
344
- expect(lambda do
345
- @client.token_credential = 42
346
- end).to raise_error(TypeError)
347
- end
348
-
349
- it "should have no token_credential_key" do
350
- expect(@client.token_credential_key).to be_nil
351
- end
352
-
353
- it "should allow the token_credential_key to be set to a String" do
354
- @client.token_credential_key = "12345"
355
- expect(@client.token_credential_key).to eq "12345"
356
- end
357
-
358
- it "should not allow the token_credential_key " \
359
- "to be set to a non-String" do
360
- expect(lambda do
361
- @client.token_credential_key = 12_345
362
- end).to raise_error(TypeError)
363
- end
364
-
365
- it "should have no token_credential_secret" do
366
- expect(@client.token_credential_secret).to be_nil
367
- end
368
-
369
- it "should allow the token_credential_secret to be set to a String" do
370
- @client.token_credential_secret = "54321"
371
- expect(@client.token_credential_secret).to eq "54321"
372
- end
373
-
374
- it "should not allow the token_credential_secret " \
375
- "to be set to a non-String" do
376
- expect(lambda do
377
- @client.token_credential_secret = 54_321
378
- end).to raise_error(TypeError)
379
- end
380
-
381
- it "should not allow the two_legged flag " \
382
- "to be set to a non-Boolean" do
383
- expect(lambda do
384
- @client.two_legged = 42
385
- end).to raise_error(TypeError)
386
- end
387
- end
388
-
389
- describe Signet::OAuth1::Client, "configured" do
390
- before do
391
- @client = Signet::OAuth1::Client.new
392
- @client.temporary_credential_uri =
393
- "http://example.com/temporary_credentials"
394
- @client.authorization_uri =
395
- "http://example.com/authorize"
396
- @client.token_credential_uri =
397
- "http://example.com/token_credentials"
398
- @client.callback = "http://example.com/callback"
399
- @client.client_credential_key = "dpf43f3p2l4k3l03"
400
- @client.client_credential_secret = "kd94hf93k423kf44"
401
- @client.temporary_credential_key = "hh5s93j4hdidpola"
402
- @client.temporary_credential_secret = "hdhd0244k9j7ao03"
403
- @client.token_credential_key = "nnch734d00sl2jdk"
404
- @client.token_credential_secret = "pfkkdhi9sl3r4s00"
405
- end
406
-
407
- it "should generate a JSON representation of the client" do
408
- json = @client.to_json
409
- expect(json).not_to be_nil
410
-
411
- deserialized = MultiJson.load json
412
- expect(deserialized["temporary_credential_uri"]).to eq "http://example.com/temporary_credentials"
413
- expect(deserialized["authorization_uri"]).to include(
414
- "http://example.com/authorize"
415
- )
416
- expect(deserialized["token_credential_uri"]).to eq "http://example.com/token_credentials"
417
- expect(deserialized["callback"]).to eq "http://example.com/callback"
418
- expect(deserialized["client_credential_key"]).to eq "dpf43f3p2l4k3l03"
419
- expect(deserialized["client_credential_secret"]).to eq "kd94hf93k423kf44"
420
- expect(deserialized["temporary_credential_key"]).to eq "hh5s93j4hdidpola"
421
- expect(deserialized["temporary_credential_secret"]).to eq "hdhd0244k9j7ao03"
422
- expect(deserialized["token_credential_key"]).to eq "nnch734d00sl2jdk"
423
- expect(deserialized["token_credential_secret"]).to eq "pfkkdhi9sl3r4s00"
424
- end
425
-
426
- it "should generate an authorization URI with a callback" do
427
- @client.temporary_credential_key = nil
428
- expect(@client.authorization_uri.to_s).to eq "http://example.com/authorize?oauth_callback=http://example.com/callback"
429
- end
430
-
431
- it "should generate an authorization URI with a temporary credential" do
432
- @client.callback = nil
433
- expect(@client.authorization_uri.to_s).to include(
434
- "oauth_token=hh5s93j4hdidpola"
435
- )
436
- end
437
-
438
- it "should generate an authorization URI both a callback and " \
439
- "a temporary credential" do
440
- expect(@client.authorization_uri.to_s).to include(
441
- "oauth_callback=http://example.com/callback"
442
- )
443
- expect(@client.authorization_uri.to_s).to include(
444
- "oauth_token=hh5s93j4hdidpola"
445
- )
446
- end
447
-
448
- it "should generate an authorization URI with additional parameters" do
449
- authorization_uri = @client.authorization_uri(
450
- additional_parameters: { domain: "www.example.com" }
451
- )
452
- expect(authorization_uri.to_s).to include(
453
- "oauth_callback=http://example.com/callback"
454
- )
455
- expect(authorization_uri.to_s).to include(
456
- "oauth_token=hh5s93j4hdidpola"
457
- )
458
- expect(authorization_uri.to_s).to include(
459
- "domain=www.example.com"
460
- )
461
- end
462
-
463
- it "should raise an error if the verifier is not provided" do
464
- expect(lambda do
465
- @client.generate_token_credential_request
466
- end).to raise_error(ArgumentError)
467
- expect(lambda do
468
- @client.generate_token_credential_request verifier: nil
469
- end).to raise_error(ArgumentError)
470
- end
471
-
472
- it "should raise an error if the token credentials URI is not set" do
473
- @client.token_credential_uri = nil
474
- expect(lambda do
475
- @client.generate_token_credential_request verifier: "12345"
476
- end).to raise_error(ArgumentError)
477
- end
478
-
479
- it "should raise an error if the client credential key is not set" do
480
- @client.client_credential_key = nil
481
- expect(lambda do
482
- @client.generate_token_credential_request verifier: "12345"
483
- end).to raise_error(ArgumentError)
484
- end
485
-
486
- it "should raise an error if the client credential secret is not set" do
487
- @client.client_credential_secret = nil
488
- expect(lambda do
489
- @client.generate_token_credential_request verifier: "12345"
490
- end).to raise_error(ArgumentError)
491
- end
492
-
493
- it "should raise an error if the temporary credential key is not set" do
494
- @client.temporary_credential_key = nil
495
- expect(lambda do
496
- @client.generate_token_credential_request verifier: "12345"
497
- end).to raise_error(ArgumentError)
498
- end
499
-
500
- it "should raise an error if the temporary credential secret is not set" do
501
- @client.temporary_credential_secret = nil
502
- expect(lambda do
503
- @client.generate_token_credential_request verifier: "12345"
504
- end).to raise_error(ArgumentError)
505
- end
506
-
507
- it "should raise an error if the client credential key is not set" do
508
- @client.client_credential_key = nil
509
- expect(lambda do
510
- @client.generate_authenticated_request
511
- end).to raise_error(ArgumentError)
512
- end
513
-
514
- it "should raise an error if the client credential secret is not set" do
515
- @client.client_credential_secret = nil
516
- expect(lambda do
517
- @client.generate_authenticated_request
518
- end).to raise_error(ArgumentError)
519
- end
520
-
521
- it "should raise an error if the token credential key is not set" do
522
- @client.token_credential_key = nil
523
- expect(lambda do
524
- @client.generate_authenticated_request
525
- end).to raise_error(ArgumentError)
526
- end
527
-
528
- it "should raise an error if the token credential secret is not set" do
529
- @client.token_credential_secret = nil
530
- expect(lambda do
531
- @client.generate_authenticated_request
532
- end).to raise_error(ArgumentError)
533
- end
534
-
535
- it "should raise an error if no request is provided" do
536
- expect(lambda do
537
- @client.generate_authenticated_request
538
- end).to raise_error(ArgumentError)
539
- end
540
-
541
- it "should raise an error if a bogus request is provided" do
542
- expect do
543
- @client.generate_authenticated_request(
544
- request: []
545
- )
546
- end.to raise_error(ArgumentError)
547
- end
548
-
549
- it "should not raise an error if a request is "\
550
- "provided without a connection" do
551
- request = @client.generate_authenticated_request(
552
- request: conn.build_request(:get) do |req|
553
- req.url "http://www.example.com/"
554
- end
555
- )
556
- end
557
-
558
- it "should raise an error if no URI is provided" do
559
- expect(lambda do
560
- @client.generate_authenticated_request(
561
- :method => "GET",
562
- headers: [],
563
- :body => ""
564
- )
565
- end).to raise_error(ArgumentError)
566
- end
567
-
568
- it "should not raise an error if a request body is chunked" do
569
- request = @client.generate_authenticated_request(
570
- method: "POST",
571
- :uri => "https://photos.example.net/photos",
572
- :body => ["A chunked body."]
573
- )
574
- expect(request).to be_kind_of(Faraday::Request)
575
- expect(request.body).to eq "A chunked body."
576
- end
577
-
578
- it "should not raise an error if a request body is chunked" do
579
- chunked_body = StringIO.new
580
- chunked_body.write "A chunked body."
581
- chunked_body.rewind
582
- request = @client.generate_authenticated_request(
583
- method: "POST",
584
- :uri => "https://photos.example.net/photos",
585
- :body => chunked_body
586
- )
587
- expect(request).to be_kind_of(Faraday::Request)
588
- expect(request.body).to eq "A chunked body."
589
- end
590
-
591
- it "should raise an error if a request body is of a bogus type" do
592
- expect(lambda do
593
- @client.generate_authenticated_request(
594
- method: "POST",
595
- :uri => "https://photos.example.net/photos",
596
- :body => 42
597
- )
598
- end).to raise_error(TypeError)
599
- end
600
-
601
- it "should correctly fetch the temporary credentials" do
602
- # Repeat this because signatures change from test to test
603
- 10.times do
604
- request = @client.generate_temporary_credential_request
605
- expect(request.method).to eq :post
606
- expect(request.path).to eq "http://example.com/temporary_credentials"
607
- authorization_header = request.headers["Authorization"]
608
- parameters = ::Signet::OAuth1.parse_authorization_header(
609
- authorization_header
610
- ).inject({}) { |h, (k, v)| h[k] = v; h }
611
- expect(parameters).not_to have_key("oauth_client_credential_key")
612
- expect(parameters).not_to have_key("oauth_temporary_credential_key")
613
- expect(parameters).not_to have_key("oauth_token")
614
- expect(parameters["oauth_nonce"]).to match(/^\w+$/)
615
- expect(parameters["oauth_callback"]).to eq @client.callback
616
- expect(parameters["oauth_timestamp"]).to match(/^\d+$/)
617
- expect(parameters["oauth_signature_method"]).to eq "HMAC-SHA1"
618
- expect(parameters["oauth_consumer_key"]).to eq @client.client_credential_key
619
- expect(parameters["oauth_signature"]).to match(%r{^[a-zA-Z0-9\=/\+]+$})
620
- expect(parameters["oauth_version"]).to eq "1.0"
621
- end
622
- end
623
-
624
- it "should correctly fetch the token credentials" do
625
- # Repeat this because signatures change from test to test
626
- 10.times do
627
- request = @client.generate_token_credential_request(
628
- verifier: "473f82d3"
629
- )
630
- expect(request.method).to eq :post
631
- expect(request.path).to eq "http://example.com/token_credentials"
632
- authorization_header = request.headers["Authorization"]
633
- parameters = ::Signet::OAuth1.parse_authorization_header(
634
- authorization_header
635
- ).inject({}) { |h, (k, v)| h[k] = v; h }
636
- expect(parameters).not_to have_key("oauth_client_credential_key")
637
- expect(parameters).not_to have_key("oauth_temporary_credential_key")
638
- expect(parameters).not_to have_key("oauth_callback")
639
- expect(parameters["oauth_nonce"]).to match(/^\w+$/)
640
- expect(parameters["oauth_timestamp"]).to match(/^\d+$/)
641
- expect(parameters["oauth_signature_method"]).to eq "HMAC-SHA1"
642
- expect(parameters["oauth_consumer_key"]).to eq @client.client_credential_key
643
- expect(parameters["oauth_token"]).to eq @client.temporary_credential_key
644
- expect(parameters["oauth_signature"]).to match(%r{^[a-zA-Z0-9\=/\+]+$})
645
- expect(parameters["oauth_verifier"]).to eq "473f82d3"
646
- expect(parameters["oauth_version"]).to eq "1.0"
647
- end
648
- end
649
-
650
- it "should correctly fetch the protected resource" do
651
- # Repeat this because signatures change from test to test
652
- 10.times do
653
- original_request = [
654
- "GET",
655
- "https://photos.example.net/photos?file=vacation.jpg&size=original",
656
- [["Host", "photos.example.net"]],
657
- [""]
658
- ]
659
- signed_request = @client.generate_authenticated_request(
660
- request: original_request
661
- )
662
- expect(signed_request.method).to eq :get
663
- expect(signed_request.path).to eq "https://photos.example.net/photos"
664
- expect(signed_request.params).to eq({ "file" => "vacation.jpg", "size" => "original" })
665
- authorization_header = signed_request.headers["Authorization"]
666
- expect(signed_request.body).to eq ""
667
- parameters = ::Signet::OAuth1.parse_authorization_header(
668
- authorization_header
669
- ).inject({}) { |h, (k, v)| h[k] = v; h }
670
- expect(parameters).not_to have_key("oauth_client_credential_key")
671
- expect(parameters).not_to have_key("oauth_temporary_credential_key")
672
- expect(parameters).not_to have_key("oauth_token_credential_key")
673
- expect(parameters).not_to have_key("oauth_callback")
674
- expect(parameters["oauth_nonce"]).to match(/^\w+$/)
675
- expect(parameters["oauth_timestamp"]).to match(/^\d+$/)
676
- expect(parameters["oauth_signature_method"]).to eq "HMAC-SHA1"
677
- expect(parameters["oauth_consumer_key"]).to eq @client.client_credential_key
678
- expect(parameters["oauth_token"]).to eq @client.token_credential_key
679
- expect(parameters["oauth_signature"]).to match(%r{^[a-zA-Z0-9\=/\+]+$})
680
- expect(parameters["oauth_version"]).to eq "1.0"
681
- end
682
- end
683
-
684
- it "should correctly fetch the protected resource" do
685
- # Repeat this because signatures change from test to test
686
- 10.times do
687
- original_request = [
688
- "POST",
689
- "https://photos.example.net/photos",
690
- [
691
- ["Host", "photos.example.net"],
692
- ["Content-Type", "application/x-www-form-urlencoded; charset=utf-8"],
693
- ["Content-Length", "31"]
694
- ],
695
- ["file=vacation.jpg&size=original"]
696
- ]
697
- signed_request = @client.generate_authenticated_request(
698
- request: original_request
699
- )
700
- expect(signed_request.method).to eq :post
701
- expect(signed_request.path).to eq "https://photos.example.net/photos"
702
- authorization_header = signed_request.headers["Authorization"]
703
- expect(signed_request.body).to eq "file=vacation.jpg&size=original"
704
- parameters = ::Signet::OAuth1.parse_authorization_header(
705
- authorization_header
706
- ).inject({}) { |h, (k, v)| h[k] = v; h }
707
- expect(parameters).not_to have_key("oauth_client_credential_key")
708
- expect(parameters).not_to have_key("oauth_temporary_credential_key")
709
- expect(parameters).not_to have_key("oauth_token_credential_key")
710
- expect(parameters).not_to have_key("oauth_callback")
711
- expect(parameters["oauth_nonce"]).to match(/^\w+$/)
712
- expect(parameters["oauth_timestamp"]).to match(/^\d+$/)
713
- expect(parameters["oauth_signature_method"]).to eq "HMAC-SHA1"
714
- expect(parameters["oauth_consumer_key"]).to eq @client.client_credential_key
715
- expect(parameters["oauth_token"]).to eq @client.token_credential_key
716
- expect(parameters["oauth_signature"]).to match(%r{^[a-zA-Z0-9\=/\+]+$})
717
- expect(parameters["oauth_version"]).to eq "1.0"
718
- end
719
- end
720
-
721
- describe "with Faraday requests" do
722
- it "should correctly get the protected resource" do
723
- # Repeat this because signatures change from test to test
724
- 10.times do
725
- original_request = conn.build_request :get do |req|
726
- req.url(
727
- "https://photos.example.net/photos?file=vacation.jpg&size=original"
728
- )
729
- req.headers = Faraday::Utils::Headers.new(
730
- [["Host", "photos.example.net"]]
731
- )
732
- req.body = ""
733
- end
734
-
735
- signed_request = @client.generate_authenticated_request(
736
- request: original_request
737
- )
738
-
739
- # Should be same request object
740
- expect(original_request["Authorization"]).to eq signed_request["Authorization"]
741
-
742
- expect(signed_request.method).to eq :get
743
- expect(signed_request.path).to eq "https://photos.example.net/photos"
744
- expect(signed_request.params).to eq ({ "file" => "vacation.jpg", "size" => "original" })
745
- authorization_header = signed_request.headers["Authorization"]
746
- expect(signed_request.body).to eq ""
747
- parameters = ::Signet::OAuth1.parse_authorization_header(
748
- authorization_header
749
- ).inject({}) { |h, (k, v)| h[k] = v; h }
750
- expect(parameters).not_to have_key("oauth_client_credential_key")
751
- expect(parameters).not_to have_key("oauth_temporary_credential_key")
752
- expect(parameters).not_to have_key("oauth_token_credential_key")
753
- expect(parameters).not_to have_key("oauth_callback")
754
- expect(parameters["oauth_nonce"]).to match(/^\w+$/)
755
- expect(parameters["oauth_timestamp"]).to match(/^\d+$/)
756
- expect(parameters["oauth_signature_method"]).to eq "HMAC-SHA1"
757
- expect(parameters["oauth_consumer_key"]).to eq @client.client_credential_key
758
- expect(parameters["oauth_token"]).to eq @client.token_credential_key
759
- expect(parameters["oauth_signature"]).to match(%r{^[a-zA-Z0-9\=/\+]+$})
760
- expect(parameters["oauth_version"]).to eq "1.0"
761
- end
762
- end
763
-
764
- it "should correctly post the protected resource" do
765
- # Repeat this because signatures change from test to test
766
- 10.times do
767
- original_request = conn.build_request :post do |req|
768
- req.url "https://photos.example.net/photos"
769
- req.headers = Faraday::Utils::Headers.new([
770
- ["Host", "photos.example.net"],
771
- ["Content-Type", "application/x-www-form-urlencoded; charset=utf-8"],
772
- ["Content-Length", "31"]
773
- ])
774
- req.body = {
775
- "size" => "original",
776
- "file" => "vacation.jpg"
777
- }
778
- end
779
-
780
- signed_request = @client.generate_authenticated_request(
781
- request: original_request
782
- )
783
-
784
- # Should be same request object
785
- expect(original_request["Authorization"]).to eq signed_request["Authorization"]
786
-
787
- expect(signed_request.method).to eq :post
788
- expect(signed_request.path).to eq "https://photos.example.net/photos"
789
- authorization_header = signed_request.headers["Authorization"]
790
- # Can't rely on the order post parameters are encoded in.
791
- expect(signed_request.body).to include("file=vacation.jpg")
792
- expect(signed_request.body).to include("size=original")
793
- parameters = ::Signet::OAuth1.parse_authorization_header(
794
- authorization_header
795
- ).inject({}) { |h, (k, v)| h[k] = v; h }
796
- expect(parameters).not_to have_key("oauth_client_credential_key")
797
- expect(parameters).not_to have_key("oauth_temporary_credential_key")
798
- expect(parameters).not_to have_key("oauth_token_credential_key")
799
- expect(parameters).not_to have_key("oauth_callback")
800
- expect(parameters["oauth_nonce"]).to match(/^\w+$/)
801
- expect(parameters["oauth_timestamp"]).to match(/^\d+$/)
802
- expect(parameters["oauth_signature_method"]).to eq "HMAC-SHA1"
803
- expect(parameters["oauth_consumer_key"]).to eq @client.client_credential_key
804
- expect(parameters["oauth_token"]).to eq @client.token_credential_key
805
- expect(parameters["oauth_signature"]).to match(%r{^[a-zA-Z0-9\=/\+]+$})
806
- expect(parameters["oauth_version"]).to eq "1.0"
807
- end
808
- end
809
- end
810
- end