Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from koshilife/develop
Browse files Browse the repository at this point in the history
release 0.1.0
  • Loading branch information
koshilife authored Aug 5, 2020
2 parents bb64a50 + 80c0b2a commit 4f4b8c3
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.1.0

- #1 extract uid from user api response.
- #2 make coverage 100%.

# 0.0.1.alpha

- Initial release
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]",
"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
Expand Down
2 changes: 1 addition & 1 deletion lib/omniauth-calendly/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module OmniAuth
module Calendly
VERSION = '0.0.1.alpha'
VERSION = '0.1.0'
end
end
24 changes: 18 additions & 6 deletions lib/omniauth/strategies/calendly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,36 @@ 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

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
1 change: 1 addition & 0 deletions omniauth-calendly.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
35 changes: 34 additions & 1 deletion test/omniauth/calendly_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
77 changes: 77 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 => '[email protected]',
: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

0 comments on commit 4f4b8c3

Please sign in to comment.