Skip to content

Commit

Permalink
simi#343. add access token params in the profile picture url
Browse files Browse the repository at this point in the history
  • Loading branch information
anklos committed Sep 28, 2020
1 parent 8a487b2 commit a38de34
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
10 changes: 6 additions & 4 deletions lib/omniauth/strategies/facebook.rb
Expand Up @@ -161,13 +161,15 @@ def image_url(uid, options)
uri_class = options[:secure_image_url] ? URI::HTTPS : URI::HTTP
site_uri = URI.parse(client.site)
url = uri_class.build({host: site_uri.host, path: "#{site_uri.path}/#{uid}/picture"})
query = { access_token: access_token.token }

query = if options[:image_size].is_a?(String) || options[:image_size].is_a?(Symbol)
{ type: options[:image_size] }
if options[:image_size].is_a?(String) || options[:image_size].is_a?(Symbol)
query[:type] = options[:image_size]
elsif options[:image_size].is_a?(Hash)
options[:image_size]
query.merge!(options[:image_size])
end
url.query = Rack::Utils.build_query(query) if query

url.query = Rack::Utils.build_query(query)

url.to_s
end
Expand Down
31 changes: 25 additions & 6 deletions test/strategy_test.rb
Expand Up @@ -95,41 +95,52 @@ def setup
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 specified' do
@options = { secure_image_url: true }
raw_info = { 'name' => 'Fred Smith', 'id' => '321' }
strategy.stubs(:raw_info).returns(raw_info)
assert_equal 'https://graph.facebook.com/v4.0/321/picture', strategy.info['image']
strategy.stubs(:access_token).returns(@access_token)
assert_equal 'https://graph.facebook.com/v4.0/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)
assert_equal 'https://blah.facebook.com/v2.2/321/picture', strategy.info['image']
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)
assert_equal 'http://graph.facebook.com/v4.0/321/picture?type=normal', strategy.info['image']
strategy.stubs(:access_token).returns(@access_token)
assert_equal 'http://graph.facebook.com/v4.0/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)
assert_equal 'http://graph.facebook.com/v4.0/321/picture?type=normal', strategy.info['image']
strategy.stubs(:access_token).returns(@access_token)
assert_equal 'http://graph.facebook.com/v4.0/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 'http://graph.facebook.com/v4.0/321/picture?', strategy.info['image']
assert_match 'http://graph.facebook.com/v4.0/321/picture?access_token=test_access_token', strategy.info['image']
end
end

Expand All @@ -138,6 +149,10 @@ 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
Expand Down Expand Up @@ -176,7 +191,7 @@ def setup

test 'returns the facebook avatar url' do
@raw_info['id'] = '321'
assert_equal 'http://graph.facebook.com/v4.0/321/picture', strategy.info['image']
assert_equal 'http://graph.facebook.com/v4.0/321/picture?access_token=test_access_token', strategy.info['image']
end

test 'returns the Facebook link as the Facebook url' do
Expand Down Expand Up @@ -215,6 +230,10 @@ 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
Expand Down

0 comments on commit a38de34

Please sign in to comment.