From 7bf5e344f6c5750f8d8a32f114301fadbaee9dc3 Mon Sep 17 00:00:00 2001 From: Kenji Koshikawa Date: Wed, 5 Aug 2020 00:06:39 +0900 Subject: [PATCH 1/3] update README --- README.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 38ec4a3..d2548e9 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,34 @@ end The auth hash `request.env['omniauth.auth']` would look like this: ```js -TODO +{ + "result": { + "provider": "calendly", + "uid": "U12345678", + "info": {}, + "credentials": { + "token": "ACCESS_TOKEN", + "refresh_token": "REFRESH_TOKEN", + "expires_at": 1596529477, + "expires": true + }, + "extra": { + "raw_info": { + "resource": { + "avatar_url": "https://xxx.cloudfront.net/uploads/user/avatar/xxx/xxx.gif", + "created_at": "2020-07-17T05:36:18.596606Z", + "email": "hoge@example.com", + "name": "USER_NAME", + "scheduling_url": "https://calendly.com/hogehoge", + "slug": "hogehoge", + "timezone": "Asia/Tokyo", + "updated_at": "2020-08-03T15:28:55.101449Z", + "uri": "https://api.calendly.com/users/U12345678" + } + } + } + } +} ``` ## Contributing From ead68a34ad15fa4f5f9d88f00ea43c0c1f1a660d Mon Sep 17 00:00:00 2001 From: Kenji Koshikawa Date: Wed, 5 Aug 2020 15:37:11 +0900 Subject: [PATCH 2/3] refs #1 #2 extract uid from user api response. make coverage 100%. --- lib/omniauth/strategies/calendly.rb | 24 ++++++--- omniauth-calendly.gemspec | 1 + test/omniauth/calendly_test.rb | 35 ++++++++++++- test/test_helper.rb | 77 +++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 7 deletions(-) diff --git a/lib/omniauth/strategies/calendly.rb b/lib/omniauth/strategies/calendly.rb index 006d7e4..4fc944e 100644 --- a/lib/omniauth/strategies/calendly.rb +++ b/lib/omniauth/strategies/calendly.rb @@ -9,7 +9,9 @@ class Calendly < OmniAuth::Strategies::OAuth2 option :name, 'calendly' option :client_options, :site => 'https://auth.calendly.com' - uid { raw_info['id'] } + USER_API_URL = 'https://api.calendly.com/users/' + + uid { extract_uid } extra { {:raw_info => raw_info} } private @@ -17,16 +19,26 @@ class Calendly < OmniAuth::Strategies::OAuth2 def raw_info return @raw_info if defined?(@raw_info) - endpoint = 'https://api.calendly.com/users/me' - @raw_info = access_token.get(endpoint).parsed - rescue ::OAuth2::Error => e - log(:error, "#{e.class} occured. message:#{e.message}") - @raw_info = {} + @raw_info = access_token.get("#{USER_API_URL}me").parsed end def callback_url full_host + script_name + callback_path end + + def extract_uid + user_info = raw_info + return unless user_info + return unless raw_info['resource'] + return unless raw_info['resource']['uri'] + + uri = raw_info['resource']['uri'] + re = /\A#{USER_API_URL}(.+)\z/ + m = re.match uri + return if m.nil? + + m[1] + end end end end diff --git a/omniauth-calendly.gemspec b/omniauth-calendly.gemspec index 81eaa68..55e3925 100644 --- a/omniauth-calendly.gemspec +++ b/omniauth-calendly.gemspec @@ -35,6 +35,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'minitest', '~> 5.14.1' spec.add_development_dependency 'minitest-reporters', '~> 1.4.2' spec.add_development_dependency 'omniauth', '~> 1.9.1' + spec.add_development_dependency 'rack-test', '~> 1.1.0' spec.add_development_dependency 'rake', '~> 13.0' spec.add_development_dependency 'simplecov', '~> 0.18.5' spec.add_development_dependency 'webmock', '~> 3.7.6' diff --git a/test/omniauth/calendly_test.rb b/test/omniauth/calendly_test.rb index 84bcef2..bf6a862 100644 --- a/test/omniauth/calendly_test.rb +++ b/test/omniauth/calendly_test.rb @@ -2,8 +2,41 @@ require 'test_helper' -class OmniAuth::CalendlyTest < Minitest::Test +class StrategyCalendlyTest < StrategyTest def test_that_it_has_a_version_number refute_nil ::OmniAuth::Calendly::VERSION end + + def test_that_it_has_a_client_options + args = [@client_id, @client_secret, @options] + strat = OmniAuth::Strategies::Calendly.new(nil, *args) + assert_equal(@client_id, strat.options[:client_id]) + assert_equal(@client_secret, strat.options[:client_secret]) + assert_equal('https://auth.calendly.com', strat.options[:client_options][:site]) + end + + def test_that_it_returns_auth_hash_in_callback_phase + add_mock_exchange_token + add_mock_user_info + post '/auth/calendly/callback', :code => @authorization_code, :state => 'state123' + + actual_auth = auth_hash.to_hash + assert(!actual_auth['credentials'].delete('expires_at').nil?) + expected_auth = { + 'provider' => 'calendly', + 'uid' => @uid, + 'info' => {'name' => nil}, + 'credentials' => {'token' => @access_token, 'refresh_token' => @refresh_token, 'expires' => true}, + 'extra' => { + 'raw_info' => JSON.parse(dummy_user_info_response.to_json) + } + } + assert_equal(expected_auth, actual_auth) + end + +private + + def auth_hash + last_request.env['omniauth.auth'] + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 4adc7f4..1ef7148 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,3 +13,80 @@ require 'webmock/minitest' require 'omniauth-calendly' +require 'omniauth' +require 'rack/test' + +class StrategyTest < Minitest::Test + include OmniAuth::Test::StrategyTestCase + include Rack::Test::Methods + + def setup + # ENV['OAUTH_DEBUG'] = 'true' + @logger = Logger.new STDOUT + OmniAuth.config.logger = @logger + @client_id = 'DUMMY_CLIENT_ID' + @client_secret = 'DUMMY_CLIENT_SECRET' + @options = {:provider_ignores_state => true} + @authorization_code = 'DUMMY_AUTH_CODE' + @access_token = 'DUMMY_TOKEN' + @refresh_token = 'DUMMY_REFRESH_TOKEN' + @uid = 'DUMMY_CALENDLY_UID' + @now = Time.new + end + +protected + + def strategy + [OmniAuth::Strategies::Calendly, @client_id, @client_secret, @options] + end + + def add_mock_exchange_token + WebMock.enable! + url = 'https://auth.calendly.com/oauth/token' + body = { + :client_id => @client_id, + :client_secret => @client_secret, + :code => @authorization_code, + :grant_type => 'authorization_code', + :redirect_uri => 'http://example.org/auth/calendly/callback' + } + res_headers = {'Content-Type' => 'application/json'} + stub_request(:post, url).with(:body => URI.encode_www_form(body)).to_return(:status => 200, :body => dummy_token_response.to_json, :headers => res_headers) + end + + def dummy_token_response + { + :token_type => 'Bearer', + :expires_in => 7200, + :created_at => @now.to_i, + :refresh_token => @refresh_token, + :access_token => @access_token, + :scope => 'default', + :owner => "https://api.calendly.com/users/#{@uid}" + } + end + + def add_mock_user_info + WebMock.enable! + url = "#{OmniAuth::Strategies::Calendly::USER_API_URL}me" + headers = {'Authorization' => "Bearer #{@access_token}"} + res_headers = {'Content-Type' => 'application/json'} + stub_request(:get, url).with(:headers => headers).to_return(:status => 200, :body => dummy_user_info_response.to_json, :headers => res_headers) + end + + def dummy_user_info_response + { + :resource => { + :avatar_url => 'https://xxx.cloudfront.net/uploads/user/avatar/xxx/xxx.gif', + :created_at => '2020-07-17T05:36:18.596606Z', + :email => 'hoge@example.com', + :name => 'USER_NAME', + :scheduling_url => 'https://calendly.com/hogehoge', + :slug => 'hogehoge', + :timezone => 'Asia/Tokyo', + :updated_at => '2020-08-03T15:28:55.101449Z', + :uri => "https://api.calendly.com/users/#{@uid}" + } + } + end +end From 80c0b2a5f80d80d500f129cad48fa5b6bd5a22be Mon Sep 17 00:00:00 2001 From: Kenji Koshikawa Date: Wed, 5 Aug 2020 15:44:39 +0900 Subject: [PATCH 3/3] set version to 0.1.0 --- CHANGELOG.md | 5 +++++ lib/omniauth-calendly/version.rb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d06413e..83e28a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 0.1.0 + +- #1 extract uid from user api response. +- #2 make coverage 100%. + # 0.0.1.alpha - Initial release diff --git a/lib/omniauth-calendly/version.rb b/lib/omniauth-calendly/version.rb index d7aabc7..3e556f4 100644 --- a/lib/omniauth-calendly/version.rb +++ b/lib/omniauth-calendly/version.rb @@ -2,6 +2,6 @@ module OmniAuth module Calendly - VERSION = '0.0.1.alpha' + VERSION = '0.1.0' end end