Codebase list ruby-omniauth-facebook / upstream/9.0.0+git20220125.1.250bb67 test / strategy_test.rb
upstream/9.0.0+git20220125.1.250bb67

Tree @upstream/9.0.0+git20220125.1.250bb67 (Download .tar.gz)

strategy_test.rb @upstream/9.0.0+git20220125.1.250bb67raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
require 'helper'
require 'omniauth-facebook'
require 'openssl'
require 'base64'

class StrategyTest < StrategyTestCase
  include OAuth2StrategyTests
end

class ClientTest < StrategyTestCase
  test 'has correct Facebook site' do
    assert_equal "https://graph.facebook.com/#{@facebook_api_version}", strategy.client.site
  end

  test 'has correct authorize url' do
    assert_equal "https://www.facebook.com/#{@facebook_api_version}/dialog/oauth", strategy.client.options[:authorize_url]
  end

  test 'has correct token url with versioning' do
    @options = {client_options: {site: 'https://graph.facebook.net/v2.2'}}
    assert_equal 'oauth/access_token', strategy.client.options[:token_url]
    assert_equal 'https://graph.facebook.net/v2.2/oauth/access_token', strategy.client.token_url
  end
end

class CallbackUrlTest < StrategyTestCase
  test "returns the default callback url (omitting querystring)" do
    url_base = 'http://auth.request.com'
    @request.stubs(:url).returns("#{url_base}/some/page")
    strategy.stubs(:script_name).returns('') # as not to depend on Rack env
    strategy.stubs(:query_string).returns('?foo=bar')
    assert_equal "#{url_base}/auth/facebook/callback", strategy.callback_url
  end

  test "returns path from callback_path option (omitting querystring)" do
    @options = { callback_path: "/auth/FB/done"}
    url_base = 'http://auth.request.com'
    @request.stubs(:url).returns("#{url_base}/page/path")
    strategy.stubs(:script_name).returns('') # as not to depend on Rack env
    strategy.stubs(:query_string).returns('?foo=bar')
    assert_equal "#{url_base}/auth/FB/done", strategy.callback_url
  end

  test "returns url from callback_url option" do
    url = 'https://auth.myapp.com/auth/fb/callback'
    @options = { callback_url: url }
    assert_equal url, strategy.callback_url
  end
end

class AuthorizeParamsTest < StrategyTestCase
  test 'includes default scope for email' do
    assert strategy.authorize_params.is_a?(Hash)
    assert_equal 'email', strategy.authorize_params[:scope]
  end

  test 'includes display parameter from request when present' do
    @request.stubs(:params).returns({ 'display' => 'touch' })
    assert strategy.authorize_params.is_a?(Hash)
    assert_equal 'touch', strategy.authorize_params[:display]
  end

  test 'includes auth_type parameter from request when present' do
    @request.stubs(:params).returns({ 'auth_type' => 'reauthenticate' })
    assert strategy.authorize_params.is_a?(Hash)
    assert_equal 'reauthenticate', strategy.authorize_params[:auth_type]
  end

  test 'overrides default scope with parameter passed from request' do
    @request.stubs(:params).returns({ 'scope' => 'email' })
    assert strategy.authorize_params.is_a?(Hash)
    assert_equal 'email', strategy.authorize_params[:scope]
  end
end

class AccessTokenOptionsTest < StrategyTestCase
  test 'has correct param name by default' do
    assert_equal 'access_token', strategy.access_token_options[:param_name]
  end

  test 'has correct header format by default' do
    assert_equal 'OAuth %s', strategy.access_token_options[:header_format]
  end
end

class UidTest < StrategyTestCase
  def setup
    super
    strategy.stubs(:raw_info).returns({ 'id' => '123' })
  end

  test 'returns the id from raw_info' do
    assert_equal '123', strategy.uid
  end
end

class InfoTest < StrategyTestCase
  def setup
    super
    @access_token = stub('OAuth2::AccessToken')
    @access_token.stubs(:token).returns('test_access_token')
  end

  test 'returns the secure facebook avatar url when `secure_image_url` option is set to true' do
    @options = { secure_image_url: true }
    raw_info = { 'name' => 'Fred Smith', 'id' => '321' }
    strategy.stubs(:raw_info).returns(raw_info)
    strategy.stubs(:access_token).returns(@access_token)
    assert_equal "https://graph.facebook.com/#{@facebook_api_version}/321/picture?access_token=test_access_token", strategy.info['image']
  end

  test 'returns the non-ssl facebook avatar url when `secure_image_url` option is set to false' do
    @options = { secure_image_url: false }
    raw_info = { 'name' => 'Fred Smith', 'id' => '321' }
    strategy.stubs(:raw_info).returns(raw_info)
    strategy.stubs(:access_token).returns(@access_token)
    assert_equal "http://graph.facebook.com/#{@facebook_api_version}/321/picture?access_token=test_access_token", strategy.info['image']
  end

  test 'returns the secure facebook avatar url when `secure_image_url` option is omitted' do
    raw_info = { 'name' => 'Fred Smith', 'id' => '321' }
    strategy.stubs(:raw_info).returns(raw_info)
    strategy.stubs(:access_token).returns(@access_token)
    assert_equal "https://graph.facebook.com/#{@facebook_api_version}/321/picture?access_token=test_access_token", strategy.info['image']
  end

  test 'returns the image_url based of the client site' do
    @options = { secure_image_url: true, client_options: {site: "https://blah.facebook.com/v2.2"}}
    raw_info = { 'name' => 'Fred Smith', 'id' => '321' }
    strategy.stubs(:raw_info).returns(raw_info)
    strategy.stubs(:access_token).returns(@access_token)
    assert_equal "https://blah.facebook.com/v2.2/321/picture?access_token=test_access_token", strategy.info['image']
  end

  test 'returns the image with size specified in the `image_size` option' do
    @options = { image_size: 'normal' }
    raw_info = { 'name' => 'Fred Smith', 'id' => '321' }
    strategy.stubs(:raw_info).returns(raw_info)
    strategy.stubs(:access_token).returns(@access_token)
    assert_equal "https://graph.facebook.com/#{@facebook_api_version}/321/picture?access_token=test_access_token&type=normal", strategy.info['image']
  end

  test 'returns the image with size specified as a symbol in the `image_size` option' do
    @options = { image_size: :normal }
    raw_info = { 'name' => 'Fred Smith', 'id' => '321' }
    strategy.stubs(:raw_info).returns(raw_info)
    strategy.stubs(:access_token).returns(@access_token)
    assert_equal "https://graph.facebook.com/#{@facebook_api_version}/321/picture?access_token=test_access_token&type=normal", strategy.info['image']
  end

  test 'returns the image with width and height specified in the `image_size` option' do
    @options = { image_size: { width: 123, height: 987 } }
    raw_info = { 'name' => 'Fred Smith', 'id' => '321' }
    strategy.stubs(:raw_info).returns(raw_info)
    strategy.stubs(:access_token).returns(@access_token)
    assert_match 'width=123', strategy.info['image']
    assert_match 'height=987', strategy.info['image']
    assert_match "https://graph.facebook.com/#{@facebook_api_version}/321/picture?access_token=test_access_token", strategy.info['image']
  end
end

class InfoTestOptionalDataPresent < StrategyTestCase
  def setup
    super
    @raw_info ||= { 'name' => 'Fred Smith' }
    strategy.stubs(:raw_info).returns(@raw_info)

    access_token = stub('OAuth2::AccessToken')
    access_token.stubs(:token).returns('test_access_token')
    strategy.stubs(:access_token).returns(access_token)
  end

  test 'returns the name' do
    assert_equal 'Fred Smith', strategy.info['name']
  end

  test 'returns the email' do
    @raw_info['email'] = 'fred@smith.com'
    assert_equal 'fred@smith.com', strategy.info['email']
  end

  test 'returns the username as nickname' do
    @raw_info['username'] = 'fredsmith'
    assert_equal 'fredsmith', strategy.info['nickname']
  end

  test 'returns the first name' do
    @raw_info['first_name'] = 'Fred'
    assert_equal 'Fred', strategy.info['first_name']
  end

  test 'returns the last name' do
    @raw_info['last_name'] = 'Smith'
    assert_equal 'Smith', strategy.info['last_name']
  end

  test 'returns the location name as location' do
    @raw_info['location'] = { 'id' => '104022926303756', 'name' => 'Palo Alto, California' }
    assert_equal 'Palo Alto, California', strategy.info['location']
  end

  test 'returns bio as description' do
    @raw_info['bio'] = 'I am great'
    assert_equal 'I am great', strategy.info['description']
  end

  test 'returns the facebook avatar url' do
    @raw_info['id'] = '321'
    assert_equal "https://graph.facebook.com/#{@facebook_api_version}/321/picture?access_token=test_access_token", strategy.info['image']
  end

  test 'returns the Facebook link as the Facebook url' do
    @raw_info['link'] = 'http://www.facebook.com/fredsmith'
    assert_kind_of Hash, strategy.info['urls']
    assert_equal 'http://www.facebook.com/fredsmith', strategy.info['urls']['Facebook']
  end

  test 'returns website url' do
    @raw_info['website'] = 'https://my-wonderful-site.com'
    assert_kind_of Hash, strategy.info['urls']
    assert_equal 'https://my-wonderful-site.com', strategy.info['urls']['Website']
  end

  test 'return both Facebook link and website urls' do
    @raw_info['link'] = 'http://www.facebook.com/fredsmith'
    @raw_info['website'] = 'https://my-wonderful-site.com'
    assert_kind_of Hash, strategy.info['urls']
    assert_equal 'http://www.facebook.com/fredsmith', strategy.info['urls']['Facebook']
    assert_equal 'https://my-wonderful-site.com', strategy.info['urls']['Website']
  end

  test 'returns the positive verified status' do
    @raw_info['verified'] = true
    assert strategy.info['verified']
  end

  test 'returns the negative verified status' do
    @raw_info['verified'] = false
    refute strategy.info['verified']
  end
end

class InfoTestOptionalDataNotPresent < StrategyTestCase
  def setup
    super
    @raw_info ||= { 'name' => 'Fred Smith' }
    strategy.stubs(:raw_info).returns(@raw_info)

    access_token = stub('OAuth2::AccessToken')
    access_token.stubs(:token).returns('test_access_token')
    strategy.stubs(:access_token).returns(access_token)
  end

  test 'has no email key' do
    refute_has_key 'email', strategy.info
  end

  test 'has no nickname key' do
    refute_has_key 'nickname', strategy.info
  end

  test 'has no first name key' do
    refute_has_key 'first_name', strategy.info
  end

  test 'has no last name key' do
    refute_has_key 'last_name', strategy.info
  end

  test 'has no location key' do
    refute_has_key 'location', strategy.info
  end

  test 'has no description key' do
    refute_has_key 'description', strategy.info
  end

  test 'has no urls' do
    refute_has_key 'urls', strategy.info
  end

  test 'has no verified key' do
    refute_has_key 'verified', strategy.info
  end
end

class RawInfoTest < StrategyTestCase
  def setup
    super
    @access_token = stub('OAuth2::AccessToken')
    @appsecret_proof = 'appsecret_proof'
    @options = {appsecret_proof: @appsecret_proof, fields: 'name,email'}
  end

  test "performs a GET to https://graph.facebook.com/#{@facebook_api_version}/me" do
    strategy.stubs(:appsecret_proof).returns(@appsecret_proof)
    strategy.stubs(:access_token).returns(@access_token)
    params = {params: @options}
    @access_token.expects(:get).with('me', params).returns(stub_everything('OAuth2::Response'))
    strategy.raw_info
  end

  test "performs a GET to https://graph.facebook.com/#{@facebook_api_version}/me with locale" do
    @options.merge!({ locale: 'cs_CZ' })
    strategy.stubs(:access_token).returns(@access_token)
    strategy.stubs(:appsecret_proof).returns(@appsecret_proof)
    params = {params: @options}
    @access_token.expects(:get).with('me', params).returns(stub_everything('OAuth2::Response'))
    strategy.raw_info
  end

  test "performs a GET to https://graph.facebook.com/#{@facebook_api_version}/me with info_fields" do
    @options.merge!({info_fields: 'about'})
    strategy.stubs(:access_token).returns(@access_token)
    strategy.stubs(:appsecret_proof).returns(@appsecret_proof)
    params = {params: {appsecret_proof: @appsecret_proof, fields: 'about'}}
    @access_token.expects(:get).with('me', params).returns(stub_everything('OAuth2::Response'))
    strategy.raw_info
  end

  test "performs a GET to https://graph.facebook.com/#{@facebook_api_version}/me with default info_fields" do
    strategy.stubs(:access_token).returns(@access_token)
    strategy.stubs(:appsecret_proof).returns(@appsecret_proof)
    params = {params: {appsecret_proof: @appsecret_proof, fields: 'name,email'}}
    @access_token.expects(:get).with('me', params).returns(stub_everything('OAuth2::Response'))
    strategy.raw_info
  end

  test 'returns a Hash' do
    strategy.stubs(:access_token).returns(@access_token)
    strategy.stubs(:appsecret_proof).returns(@appsecret_proof)
    raw_response = stub('Faraday::Response')
    raw_response.stubs(:body).returns('{ "ohai": "thar" }')
    raw_response.stubs(:status).returns(200)
    raw_response.stubs(:headers).returns({'Content-Type' => 'application/json' })
    oauth2_response = OAuth2::Response.new(raw_response)
    params = {params: @options}
    @access_token.stubs(:get).with('me', params).returns(oauth2_response)
    assert_kind_of Hash, strategy.raw_info
    assert_equal 'thar', strategy.raw_info['ohai']
  end

  test 'returns an empty hash when the response is false' do
    strategy.stubs(:access_token).returns(@access_token)
    strategy.stubs(:appsecret_proof).returns(@appsecret_proof)
    oauth2_response = stub('OAuth2::Response', parsed: false)
    params = {params: @options}
    @access_token.stubs(:get).with('me', params).returns(oauth2_response)
    assert_kind_of Hash, strategy.raw_info
    assert_equal({}, strategy.raw_info)
  end

  test 'should not include raw_info in extras hash when skip_info is specified' do
    @options = { skip_info: true }
    strategy.stubs(:raw_info).returns({foo: 'bar' })
    refute_has_key 'raw_info', strategy.extra
  end
end

class CredentialsTest < StrategyTestCase
  def setup
    super
    @access_token = stub('OAuth2::AccessToken')
    @access_token.stubs(:token)
    @access_token.stubs(:expires?)
    @access_token.stubs(:expires_at)
    @access_token.stubs(:refresh_token)
    strategy.stubs(:access_token).returns(@access_token)
  end

  test 'returns a Hash' do
    assert_kind_of Hash, strategy.credentials
  end

  test 'returns the token' do
    @access_token.stubs(:token).returns('123')
    assert_equal '123', strategy.credentials['token']
  end

  test 'returns the expiry status' do
    @access_token.stubs(:expires?).returns(true)
    assert strategy.credentials['expires']

    @access_token.stubs(:expires?).returns(false)
    refute strategy.credentials['expires']
  end

  test 'returns the refresh token and expiry time when expiring' do
    ten_mins_from_now = (Time.now + 600).to_i
    @access_token.stubs(:expires?).returns(true)
    @access_token.stubs(:refresh_token).returns('321')
    @access_token.stubs(:expires_at).returns(ten_mins_from_now)
    assert_equal '321', strategy.credentials['refresh_token']
    assert_equal ten_mins_from_now, strategy.credentials['expires_at']
  end

  test 'does not return the refresh token when test is nil and expiring' do
    @access_token.stubs(:expires?).returns(true)
    @access_token.stubs(:refresh_token).returns(nil)
    assert_nil strategy.credentials['refresh_token']
    refute_has_key 'refresh_token', strategy.credentials
  end

  test 'does not return the refresh token when not expiring' do
    @access_token.stubs(:expires?).returns(false)
    @access_token.stubs(:refresh_token).returns('XXX')
    assert_nil strategy.credentials['refresh_token']
    refute_has_key 'refresh_token', strategy.credentials
  end
end

class ExtraTest < StrategyTestCase
  def setup
    super
    @raw_info = { 'name' => 'Fred Smith' }
    strategy.stubs(:raw_info).returns(@raw_info)
  end

  test 'returns a Hash' do
    assert_kind_of Hash, strategy.extra
  end

  test 'contains raw info' do
    assert_equal({ 'raw_info' => @raw_info }, strategy.extra)
  end
end

module SignedRequestHelpers
  def signed_request(payload, secret)
    encoded_payload = base64_encode_url(MultiJson.encode(payload))
    encoded_signature = base64_encode_url(signature(encoded_payload, secret))
    [encoded_signature, encoded_payload].join('.')
  end

  def base64_encode_url(value)
    Base64.encode64(value).tr('+/', '-_').gsub(/\n/, '')
  end

  def signature(payload, secret, algorithm = OpenSSL::Digest::SHA256.new)
    OpenSSL::HMAC.digest(algorithm, secret, payload)
  end
end

module SignedRequestTests
  class TestCase < StrategyTestCase
    include SignedRequestHelpers
  end

  class CookieAndParamNotPresentTest < TestCase
    test 'is nil' do
      assert_nil strategy.send(:signed_request_from_cookie)
    end

    test 'throws an error on calling build_access_token' do
      assert_raises(OmniAuth::Strategies::Facebook::NoAuthorizationCodeError) { strategy.send(:with_authorization_code!) {} }
    end
  end

  class CookiePresentTest < TestCase
    def setup(algo = nil)
      super()
      @payload = {
        'algorithm' => algo || 'HMAC-SHA256',
        'code' => 'm4c0d3z',
        'issued_at' => Time.now.to_i,
        'user_id' => '123456'
      }

      @request.stubs(:cookies).returns({"fbsr_#{@client_id}" => signed_request(@payload, @client_secret)})
    end

    test 'parses the access code out from the cookie' do
      assert_equal @payload, strategy.send(:signed_request_from_cookie)
    end

    test 'throws an error if the algorithm is unknown' do
      setup('UNKNOWN-ALGO')
      assert_equal "unknown algorithm: UNKNOWN-ALGO", assert_raises(OmniAuth::Facebook::SignedRequest::UnknownSignatureAlgorithmError) { strategy.send(:signed_request_from_cookie) }.message
    end
  end

  class EmptySignedRequestTest < TestCase
    def setup
      super
      @request.stubs(:params).returns({'signed_request' => ''})
    end

    test 'empty param' do
      assert_nil strategy.send(:signed_request_from_cookie)
    end
  end

  class MissingCodeInParamsRequestTest < TestCase
    def setup
      super
      @request.stubs(:params).returns({})
    end

    test 'calls fail! when a code is not included in the params' do
      strategy.expects(:fail!).times(1).with(:no_authorization_code, kind_of(OmniAuth::Strategies::Facebook::NoAuthorizationCodeError))
      strategy.callback_phase
    end
  end

  class MissingCodeInCookieRequestTest < TestCase
    def setup(algo = nil)
      super()
      @payload = {
        'algorithm' => algo || 'HMAC-SHA256',
        'code' => nil,
        'issued_at' => Time.now.to_i,
        'user_id' => '123456'
      }

      @request.stubs(:cookies).returns({"fbsr_#{@client_id}" => signed_request(@payload, @client_secret)})
    end

    test 'calls fail! when a code is not included in the cookie' do
      strategy.expects(:fail!).times(1).with(:no_authorization_code, kind_of(OmniAuth::Strategies::Facebook::NoAuthorizationCodeError))
      strategy.callback_phase
    end
  end

  class UnknownAlgorithmInCookieRequestTest < TestCase
    def setup
      super()
      @payload = {
        'algorithm' => 'UNKNOWN-ALGO',
        'code' => nil,
        'issued_at' => Time.now.to_i,
        'user_id' => '123456'
      }

      @request.stubs(:cookies).returns({"fbsr_#{@client_id}" => signed_request(@payload, @client_secret)})
    end

    test 'calls fail! when an algorithm is unknown' do
      strategy.expects(:fail!).times(1).with(:unknown_signature_algorithm, kind_of(OmniAuth::Facebook::SignedRequest::UnknownSignatureAlgorithmError))
      strategy.callback_phase
    end
  end
end