Skip to content
This repository was archived by the owner on Jun 29, 2024. It is now read-only.

Commit 08517f5

Browse files
authored
Merge pull request #5 from koshilife/feature/#4_make_code_coverage_100
refs #4 add tests
2 parents ffbc825 + 9083467 commit 08517f5

File tree

6 files changed

+244
-34
lines changed

6 files changed

+244
-34
lines changed

.rubocop.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
AllCops:
2+
TargetRubyVersion: 2.6
3+
4+
Layout/AccessModifierIndentation:
5+
EnforcedStyle: outdent
6+
7+
Layout/HashAlignment:
8+
Enabled: false
9+
10+
Layout/DotPosition:
11+
EnforcedStyle: trailing
12+
13+
Layout/SpaceInsideHashLiteralBraces:
14+
EnforcedStyle: no_space
15+
16+
Lint/SuppressedException:
17+
Enabled: false
18+
19+
Metrics/BlockLength:
20+
Enabled: false
21+
22+
Metrics/BlockNesting:
23+
Max: 2
24+
25+
Layout/LineLength:
26+
AllowURI: true
27+
Enabled: false
28+
29+
Metrics/MethodLength:
30+
CountComments: false
31+
Max: 15
32+
33+
Metrics/ParameterLists:
34+
Max: 4
35+
CountKeywordArgs: true
36+
37+
Metrics/AbcSize:
38+
Enabled: false
39+
40+
Style/CollectionMethods:
41+
PreferredMethods:
42+
map: 'collect'
43+
reduce: 'inject'
44+
find: 'detect'
45+
find_all: 'select'
46+
47+
Style/Documentation:
48+
Enabled: false
49+
50+
Style/DoubleNegation:
51+
Enabled: false
52+
53+
Style/EachWithObject:
54+
Enabled: false
55+
56+
Style/Encoding:
57+
Enabled: false
58+
59+
Style/ExpandPathArguments:
60+
Enabled: false
61+
62+
Style/HashSyntax:
63+
EnforcedStyle: hash_rockets
64+
65+
Style/Lambda:
66+
Enabled: false
67+
68+
Style/RaiseArgs:
69+
EnforcedStyle: compact

lib/omniauth-zoom/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
module Omniauth
3+
module OmniAuth
44
module Zoom
55
VERSION = '0.1.0'
66
end

lib/omniauth/strategies/zoom.rb

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,44 @@
66

77
module OmniAuth
88
module Strategies
9+
# OmniAuth strategy for zoom.us
910
class Zoom < OmniAuth::Strategies::OAuth2
1011
option :name, 'zoom'
11-
option :client_options, site: 'https://zoom.us'
12+
option :client_options, :site => 'https://zoom.us'
1213

13-
uid { raw_info[:id] }
14+
uid { raw_info['id'] }
15+
extra { {:raw_info => raw_info} }
1416

15-
extra do
16-
{ raw_info: raw_info }
17-
end
18-
19-
protected
17+
protected
2018

2119
def build_access_token
2220
params = {
23-
grant_type: 'authorization_code',
24-
code: request.params['code'],
25-
redirect_uri: callback_url
21+
:grant_type => 'authorization_code',
22+
:code => request.params['code'],
23+
:redirect_uri => callback_url
2624
}
27-
path = "#{client.options[:token_url]}?#{params.to_query}"
28-
token = Base64.strict_encode64("#{client.id}:#{client.secret}")
29-
opts = { headers: { Authorization: "Basic #{token}" } }
30-
31-
response = client.request(:post, path, opts)
32-
access_token_opts = response.parsed.merge(deep_symbolize(options.auth_token_params))
33-
::OAuth2::AccessToken.from_hash(client, access_token_opts).tap do |access_token|
34-
if access_token.respond_to?(:response=)
35-
access_token.response = response
36-
end
37-
end
38-
end
25+
path = "#{client.options[:token_url]}?#{URI.encode_www_form(params)}"
26+
headers_secret = Base64.strict_encode64("#{client.id}:#{client.secret}")
27+
opts = {:headers => {:Authorization => "Basic #{headers_secret}"}}
3928

40-
def callback_url
41-
full_host + script_name + callback_path
29+
res = client.request(:post, path, opts)
30+
::OAuth2::AccessToken.from_hash(client, res.parsed)
4231
end
4332

33+
private
34+
4435
def raw_info
45-
return @raw_info unless @raw_info.nil?
36+
return @raw_info if defined?(@raw_info)
4637

47-
res = access_token.get('/v2/users/me')
48-
@raw_info = JSON.parse(res.body, symbolize_names: true)
38+
@raw_info = access_token.get('/v2/users/me').parsed || {}
4939
rescue StandardError => e
50-
logger = OmniAuth.config.logger
51-
logger&.debug("#{self.class}.#{__method__} #{e.class} occured. message:#{e.message}")
40+
log(:error, "#{e.class} occured. message:#{e.message}")
5241
@raw_info = {}
5342
end
43+
44+
def callback_url
45+
full_host + script_name + callback_path
46+
end
5447
end
5548
end
5649
end

omniauth-zoom.gemspec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ require_relative 'lib/omniauth-zoom/version'
44

55
Gem::Specification.new do |spec|
66
spec.name = 'omniauth-zoom'
7-
spec.version = Omniauth::Zoom::VERSION
7+
spec.version = OmniAuth::Zoom::VERSION
88
spec.authors = ['Kenji Koshikawa']
99
spec.email = ['koshikawa2009@gmail.com']
1010

@@ -35,6 +35,9 @@ Gem::Specification.new do |spec|
3535
spec.add_development_dependency 'codecov', '~> 0.1.17'
3636
spec.add_development_dependency 'minitest', '~> 5.14.1'
3737
spec.add_development_dependency 'minitest-reporters', '~> 1.4.2'
38+
spec.add_development_dependency 'omniauth', '~> 1.9.1'
39+
spec.add_development_dependency 'rack-test', '~> 1.1.0'
3840
spec.add_development_dependency 'rake', '~> 13.0'
3941
spec.add_development_dependency 'simplecov', '~> 0.18.5'
42+
spec.add_development_dependency 'webmock', '~> 3.7.6'
4043
end

test/omniauth/zoom_test.rb

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,59 @@
22

33
require 'test_helper'
44

5-
class Omniauth::ZoomTest < Minitest::Test
6-
def test_that_it_has_a_version_number
7-
refute_nil ::Omniauth::Zoom::VERSION
5+
class StrategyZoomTest < StrategyTest
6+
def test_it_has_a_version_number
7+
refute_nil ::OmniAuth::Zoom::VERSION
8+
end
9+
10+
def test_it_has_a_client_options
11+
args = [@client_id, @client_secret, @options]
12+
strat = OmniAuth::Strategies::Zoom.new(nil, *args)
13+
assert_equal(@client_id, strat.options[:client_id])
14+
assert_equal(@client_secret, strat.options[:client_secret])
15+
assert_equal(@scope, strat.options[:scope])
16+
assert_equal('https://zoom.us', strat.options[:client_options][:site])
17+
end
18+
19+
def test_it_returns_auth_hash_in_callback_phase
20+
add_mock_exchange_token
21+
add_mock_user_info
22+
post '/auth/zoom/callback', :code => @authorization_code, :state => 'state123'
23+
24+
actual_auth = auth_hash.to_hash
25+
assert(!actual_auth['credentials'].delete('expires_at').nil?)
26+
expected_auth = {
27+
'provider' => 'zoom',
28+
'uid' => dummy_user_info_response[:id],
29+
'info' => {'name' => nil},
30+
'credentials' => {'token' => 'DUMMY_TOKEN', 'refresh_token' => 'DUMMY_REFRESH_TOKEN', 'expires' => true},
31+
'extra' => {
32+
'raw_info' => JSON.parse(dummy_user_info_response.to_json)
33+
}
34+
}
35+
assert_equal(expected_auth, actual_auth)
36+
end
37+
38+
def test_it_returns_auth_hash_in_case_of_failure_of_get_user_info_in_callbach_phase
39+
add_mock_exchange_token
40+
add_mock_user_info_then_fail
41+
post '/auth/zoom/callback', :code => @authorization_code, :state => 'state123'
42+
43+
actual_auth = auth_hash.to_hash
44+
assert(!actual_auth['credentials'].delete('expires_at').nil?)
45+
expected_auth = {
46+
'provider' => 'zoom',
47+
'uid' => nil,
48+
'info' => {'name' => nil},
49+
'credentials' => {'token' => 'DUMMY_TOKEN', 'refresh_token' => 'DUMMY_REFRESH_TOKEN', 'expires' => true},
50+
'extra' => {'raw_info' => {}}
51+
}
52+
assert_equal(expected_auth, actual_auth)
53+
end
54+
55+
private
56+
57+
def auth_hash
58+
last_request.env['omniauth.auth']
859
end
960
end

test/test_helper.rb

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,99 @@
1111
require 'minitest/autorun'
1212
require 'minitest/reporters'
1313
Minitest::Reporters.use!
14+
require 'webmock/minitest'
1415

1516
require 'omniauth-zoom'
17+
require 'omniauth'
18+
require 'rack/test'
19+
20+
class StrategyTest < Minitest::Test
21+
include OmniAuth::Test::StrategyTestCase
22+
include Rack::Test::Methods
23+
24+
def setup
25+
ENV['OAUTH_DEBUG'] = 'true'
26+
@logger = Logger.new STDOUT
27+
OmniAuth.config.logger = @logger
28+
29+
@client_id = 'DUMMY_CLIENT_ID'
30+
@client_secret = 'DUMMY_CLIENT_SECRET'
31+
@scope = 'user_profile'
32+
@options = {:scope => @scope, :provider_ignores_state => true}
33+
@authorization_code = 'DUMMY_AUTH_CODE'
34+
@access_token = 'DUMMY_TOKEN'
35+
end
36+
37+
protected
38+
39+
def strategy
40+
[OmniAuth::Strategies::Zoom, @client_id, @client_secret, @options]
41+
end
42+
43+
def add_mock_exchange_token
44+
WebMock.enable!
45+
url = "https://zoom.us/oauth/token?code=#{@authorization_code}&grant_type=authorization_code&redirect_uri=http://example.org/auth/zoom/callback"
46+
secret = Base64.strict_encode64("#{@client_id}:#{@client_secret}")
47+
headers = {'Authorization' => "Basic #{secret}"}
48+
res_headers = {'Content-Type' => 'application/json'}
49+
stub_request(:post, url).with(:headers => headers).to_return(:status => 200, :body => dummy_token_response.to_json, :headers => res_headers)
50+
end
51+
52+
def dummy_token_response
53+
{
54+
:access_token => @access_token,
55+
:token_type => 'bearer',
56+
:refresh_token => 'DUMMY_REFRESH_TOKEN',
57+
:expires_in => 3600,
58+
:scope => 'user_profile'
59+
}
60+
end
61+
62+
def add_mock_user_info
63+
WebMock.enable!
64+
url = 'https://zoom.us/v2/users/me'
65+
headers = {'Authorization' => "Bearer #{@access_token}"}
66+
res_headers = {'Content-Type' => 'application/json'}
67+
stub_request(:get, url).with(:headers => headers).to_return(:status => 200, :body => dummy_user_info_response.to_json, :headers => res_headers)
68+
end
69+
70+
def add_mock_user_info_then_fail
71+
WebMock.enable!
72+
url = 'https://zoom.us/v2/users/me'
73+
response = {:code => 124, :message => 'Invalid access token.'}
74+
headers = {'Authorization' => "Bearer #{@access_token}"}
75+
res_headers = {'Content-Type' => 'application/json'}
76+
stub_request(:get, url).with(:headers => headers).to_return(:status => 400, :body => response.to_json, :headers => res_headers)
77+
end
78+
79+
def dummy_user_info_response
80+
{
81+
:id => 'KdYKjnimT4KPd8FFgQt9FQ',
82+
:first_name => 'Jane',
83+
:last_name => 'Dev',
84+
:email => 'jane.dev@email.com',
85+
:type => 2,
86+
:role_name => 'Owner',
87+
:pmi => 1_234_567_890,
88+
:use_pmi => false,
89+
:vanity_url => 'https://janedevinc.zoom.us/my/janedev',
90+
:personal_meeting_url => 'https://janedevinc.zoom.us/j/1234567890',
91+
:timezone => 'America/Denver',
92+
:verified => 1,
93+
:dept => '',
94+
:created_at => '2019-04-05T15:24:32Z',
95+
:last_login_time => '2019-12-16T18:02:48Z',
96+
:last_client_version => '4.6.12611.1124(mac)',
97+
:pic_url => 'https://janedev.zoom.us/p/KdYKjnimFR5Td8KKdQt9FQ/19f6430f-ca72-4154-8998-ede6be4542c7-837',
98+
:host_key => '533895',
99+
:jid => 'kdykjnimt4kpd8kkdqt9fq@xmpp.zoom.us',
100+
:group_ids => [],
101+
:im_group_ids => ['3NXCD9VFTCOUH8LD-QciGw'],
102+
:account_id => 'gVcjZnYYRLDbb_MfgHuaxg',
103+
:language => 'en-US',
104+
:phone_country => 'US',
105+
:phone_number => '+1 1234567891',
106+
:status => 'active'
107+
}
108+
end
109+
end

0 commit comments

Comments
 (0)