ginjo-omniauth-slack 2.4.1 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,123 @@
1
+ require 'helper'
2
+
3
+ describe OmniAuth::Slack::OAuth2::AccessToken do
4
+ def setup
5
+ @access_token = OmniAuth::Slack::OAuth2::AccessToken.from_hash(
6
+ OmniAuth::Slack::OAuth2::Client.new('key','secret'),
7
+ {
8
+ 'ok' => true,
9
+ 'access_token' => 'xoxp-abcdef-123456789',
10
+ 'user' => {'id' => '11', 'name' => 'bill'},
11
+ 'team_id' => 33,
12
+ 'team_name' => 'my team',
13
+ 'scope' => 'users:read'
14
+ }
15
+ )
16
+
17
+ @at_class = OmniAuth::Slack::OAuth2::AccessToken
18
+
19
+ @scope_base = YAML.load_file(File.join(File.dirname(__FILE__), 'support/scope_base.yml'))
20
+ end
21
+
22
+ it 'defines getter methods for basic user data' do
23
+ assert_equal 'users:read', @access_token.scope
24
+ assert_equal 33, @access_token.team_id
25
+ assert_equal 'my team', @access_token.team_name
26
+ end
27
+
28
+ describe 'user_id' do
29
+ it "gets data from params['user'].to_h['id']" do
30
+ assert_equal '11', @access_token.user_id
31
+ end
32
+
33
+ it "gets data from params['user_id']" do
34
+ @access_token.params.replace({'user_id' => 'user-id-01'})
35
+ assert_equal 'user-id-01', @access_token.user_id
36
+ end
37
+
38
+ it "gets data from params['authorizing_user'].to_h['user_id']" do
39
+ @access_token.params.replace({'authorizing_user' => {'user_id' => 'user-id-02'}})
40
+ assert_equal 'user-id-02', @access_token.user_id
41
+ end
42
+ end
43
+
44
+ describe 'ok?' do
45
+ it 'returns true or false' do
46
+ assert_equal true, @access_token.ok?
47
+ @access_token.params['ok'] = 'false'
48
+ assert_equal false, @access_token.ok?
49
+ @access_token.params['ok'] = nil
50
+ assert_equal false, @access_token.ok?
51
+ end
52
+ end
53
+
54
+ describe 'token_type?' do
55
+ it 'compares token_type with any number of arguments and returns true if any match' do
56
+ assert_equal true, @access_token.token_type?('user')
57
+ end
58
+ end
59
+
60
+ describe 'uid' do
61
+ it 'gets concatenated user_id-team_id' do
62
+ assert_equal '11-33', @access_token.uid
63
+ end
64
+ end
65
+
66
+ describe 'all_scopes' do
67
+ it 'retrieves compiled hash of all scopes currently visible on token' do
68
+ end
69
+ end
70
+
71
+ describe 'has_scope?' do
72
+ it 'passes array of classic scopes to class-level has_scope?' do
73
+ assert_equal true, @access_token.has_scope?('identify', 'channels:read', 'boblobla', base:@scope_base)
74
+ end
75
+
76
+ it 'passes string of classic scopes to class-level has_scope?' do
77
+ assert_equal true, @access_token.has_scope?('identify channels:read boblobla', base:@scope_base)
78
+ end
79
+ end
80
+
81
+ describe 'self.has_scope?' do
82
+ it 'accepts a string of classic scopes to be matched against base-scopes containing :classic key' do
83
+ assert_equal true, @at_class.has_scope?(scope_query:'identify channels:read chat:write:bot users.profile:read', scope_base:@scope_base)
84
+ end
85
+
86
+ it 'accepts an array of classic scopes to be matched against base-scopes containing :classic key' do
87
+ assert_equal true, @at_class.has_scope?(scope_query:['identify','channels:read','chat:write:bot','users.profile:read'], scope_base:@scope_base)
88
+ end
89
+
90
+ it 'accepts a scope_query hash' do
91
+ assert_equal true, @at_class.has_scope?(scope_query:{channel:'channels:read im:read', group:'chat:write'}, scope_base:@scope_base)
92
+ end
93
+
94
+ it 'accepts an array of scope_query hashes' do
95
+ assert_equal true, @at_class.has_scope?(scope_query:[{channel:'channels:read im:read', group:'chat:write'}, {app_home:'chat:write'}], scope_base:@scope_base)
96
+ end
97
+
98
+ it 'accepts a scope_query hash with array of string values' do
99
+ assert_equal true, @at_class.has_scope?(scope_query:{channel: %w(channels:read im:read chat:write)}, scope_base:@scope_base)
100
+ end
101
+
102
+ it "accepts a :logic param to switch to 'and' (all) logic from 'or' (any) logic" do
103
+ assert_equal false, @at_class.has_scope?(scope_query:{channel:'channels:read im:read', group:'chat:write'}, scope_base:@scope_base, logic:'and')
104
+ assert_equal true, @at_class.has_scope?(scope_query:{channel:'channels:read im:read', group:'chat:write'}, scope_base:@scope_base, logic:'or')
105
+ assert_equal false, @at_class.has_scope?(scope_query:{channel:'im:read'}, scope_base:@scope_base)
106
+ end
107
+
108
+ it "given 'or' logic, must match at least one scope from given scope_query hash" do
109
+ assert_equal true, @at_class.has_scope?(scope_query:{channel:'channels:read im:read', group:'chat:wrong'}, scope_base:@scope_base, logic:'or')
110
+ assert_equal false, @at_class.has_scope?(scope_query:{channel:'chappels:read im:read', group:'chat:wrong'}, scope_base:@scope_base, logic:'or')
111
+ end
112
+
113
+ it "given 'and' logic, must match all scopes from given scope_query hash" do
114
+ assert_equal true, @at_class.has_scope?(scope_query:{channel:'channels:read channels:history', group:'chat:write'}, scope_base:@scope_base, logic:'and')
115
+ assert_equal false, @at_class.has_scope?(scope_query:{channel:'channels:read channels:history', group:'chat:write', team:'users:read'}, scope_base:@scope_base, logic:'and')
116
+ end
117
+
118
+ it "logic for array of query hashes is opposite of logic for query-hash" do
119
+ assert_equal true, @at_class.has_scope?(scope_query:[{channel:'channels:read im:read'}, {group:'chat:write'}], scope_base:@scope_base, logic:'and')
120
+ assert_equal false, @at_class.has_scope?(scope_query:[{channel:'channels:read im:read'}, {group:'chat:wrong chat:still.wrong'}], scope_base:@scope_base, logic:'or')
121
+ end
122
+ end
123
+ end
@@ -1,9 +1,32 @@
1
- require "bundler/setup"
2
- require "minitest/autorun"
3
- require "mocha/setup"
4
- require "omniauth/strategies/slack"
1
+ # Run all tests with
2
+ # ruby -I./test test/test.rb
3
+ # or
4
+ # rake test
5
+ # or run specific tests
6
+ # ruby -I./test test/refinements_test.rb
7
+ #
8
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
9
+ $LOAD_PATH.unshift File.expand_path('../support', __FILE__)
10
+
11
+ # Is specific require-bundler-setup needed?
12
+ #require 'bundler/setup'
13
+
14
+ require 'minitest/autorun'
15
+ # Must load after minitest-autorun.
16
+ require 'mocha/setup'
17
+
18
+ # Is explicit require-oauth2 still needed?
19
+ require 'oauth2'
20
+ require 'omniauth-slack'
21
+
22
+ # See below for require 'shared_examples' (needs to load after helpers).
23
+ #require 'shared_examples'
24
+
25
+
5
26
 
6
27
  OmniAuth.config.test_mode = true
28
+ OmniAuth.logger.level = 1
29
+ ENV['OMNIAUTH_SLACK_DEBUG']='false'
7
30
 
8
31
  module BlockTestHelper
9
32
  def test(name, &blk)
@@ -54,5 +77,17 @@ class StrategyTestCase < TestCase
54
77
  end
55
78
  end
56
79
 
57
- Dir[File.expand_path("../support/**/*", __FILE__)].each(&method(:require))
80
+
81
+ ## This needs to load after helpers.
82
+ require 'shared_examples'
83
+
84
+
85
+ ## These are not used here anymore.
86
+
87
+ # Dir[File.expand_path("../support/**/*", __FILE__)].each(&method(:require))
88
+
89
+ # Dir[File.join("../", 'test', '*.rb')].each {|file| require file }
90
+
91
+ #require_relative 'support/shared_examples.rb'
92
+ #Dir[File.expand_path("../**/*.rb", __FILE__)].each(&method(:require))
58
93
 
@@ -0,0 +1,83 @@
1
+ require 'helper'
2
+
3
+ #class TestObjectRefinements
4
+ class TestCallerMethodName
5
+ #using OmniAuth::Slack::ObjectRefinements
6
+ include OmniAuth::Slack::CallerMethodName
7
+
8
+ def test_caller_method_name_outter
9
+ test_caller_method_name_middle
10
+ end
11
+
12
+ def test_caller_method_name_middle
13
+ test_caller_method_name_inner
14
+ end
15
+
16
+ def test_caller_method_name_inner
17
+ caller_method_name
18
+ end
19
+ end
20
+
21
+ # describe OmniAuth::Slack::ObjectRefinements do
22
+ # using OmniAuth::Slack::ObjectRefinements
23
+ #
24
+ # describe 'caller_method_name' do
25
+ # instance = TestObjectRefinements.new
26
+ # it "gets the name of the method that called the current method" do
27
+ # assert_equal 'test_caller_method_name_middle', instance.test_caller_method_name_outter
28
+ # end
29
+ # end
30
+ # end
31
+
32
+ describe OmniAuth::Slack::CallerMethodName do
33
+ #using OmniAuth::Slack::ObjectRefinements
34
+
35
+ describe 'caller_method_name' do
36
+ instance = TestCallerMethodName.new
37
+ it "gets the name of the method that called the current method" do
38
+ assert_equal 'test_caller_method_name_middle', instance.test_caller_method_name_outter
39
+ end
40
+ end
41
+ end
42
+
43
+ describe OmniAuth::Slack::ArrayRefinements do
44
+ using OmniAuth::Slack::ArrayRefinements
45
+
46
+ describe 'sort_with' do
47
+ it 'sorts one array according the order of another array' do
48
+ assert_equal [5,2,2,"three","three",1,:four,:four], [1,2,"three",:four,5,:four,2,"three"].sort_with([5,2,"three",1,:four])
49
+ end
50
+
51
+ it 'takes an argument for :beginning or :ending to specify where to put un-matched source items' do
52
+ assert_equal [:c, :d, :b, :e, :a], [:a, :b, :c, :d, :e].sort_with([:b, :e, :a], :beginning)
53
+ assert_equal [:b, :e, :a, :c, :d], [:a, :b, :c, :d, :e].sort_with([:b, :e, :a], :ending)
54
+ end
55
+ end
56
+ end
57
+
58
+ describe OmniAuth::Slack::OAuth2Refinements do
59
+ using OmniAuth::Slack::OAuth2Refinements
60
+
61
+ describe OAuth2::Response do
62
+ describe 'to_auth_hash' do
63
+ it 'returns parsed response as OmniAuth::Slack::AuthHash' do
64
+ resp = OAuth2::Response.new(a:'data')
65
+ resp.stubs(:parsed).returns({a:'data'})
66
+ assert_kind_of OmniAuth::Slack::AuthHash, resp.to_auth_hash
67
+ assert_equal({a:'data'}[:a], resp.to_auth_hash.a)
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ describe OmniAuth::Slack::StringRefinements do
74
+ using OmniAuth::Slack::StringRefinements
75
+
76
+ describe 'words' do
77
+ it 'splits string into array of words based on white-space or commas' do
78
+ assert_equal('scope:one,scope.two scope.three ,scope@four, ,scope-five scope_six,,,scope/seven , scope#eight'.words,
79
+ ["scope:one", "scope.two", "scope.three", "scope@four", "scope-five", "scope_six", "scope/seven", "scope#eight"]
80
+ )
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,249 @@
1
+ require 'helper'
2
+
3
+ class StrategyTest < StrategyTestCase
4
+ include OAuth2StrategyTests
5
+
6
+ test 'uses custom AuthHash subclass for auth_hash object' do
7
+ ::OmniAuth::Strategy.class_eval do
8
+ def auth_hash; {a:1, b:2}; end
9
+ end
10
+ assert_equal true, strategy.auth_hash.is_a?(OmniAuth::Slack::AuthHash)
11
+ end
12
+ end
13
+
14
+ class ClientTest < StrategyTestCase
15
+ test "has correct Slack site" do
16
+ assert_equal "https://slack.com", strategy.client.site
17
+ end
18
+
19
+ test "has correct authorize url" do
20
+ assert_equal "/oauth/v2/authorize", strategy.client.instance_eval{
21
+ options[:authorize_url].is_a?(Proc) ? instance_eval(&options[:authorize_url]) : options[:authorize_url]
22
+ }
23
+ end
24
+
25
+ test "has correct token url" do
26
+ assert_equal "/api/oauth.v2.access", strategy.client.instance_eval{
27
+ options[:token_url].is_a?(Proc) ? instance_eval(&options[:token_url]) : options[:token_url]
28
+ }
29
+ end
30
+
31
+ test "has correct auth_scheme" do
32
+ assert_equal :basic_auth, strategy.client.options[:auth_scheme]
33
+ end
34
+
35
+ test 'request logs api call' do
36
+ # We need to manually stub the base #request method,
37
+ # since we're testing the override in the subclassed Client.
38
+ ::OAuth2::Client.class_eval do
39
+ def request(*args)
40
+ {'simple' => 'hash'}
41
+ end
42
+ end
43
+ @client = strategy.client
44
+ OmniAuth.logger.expects(:debug).with(){|*params| assert_match(/http:\/\/host\/api\/test.action/, params[0])}
45
+ @client.request(:get, 'http://host/api/test.action')
46
+ end
47
+
48
+ test 'request adds api response to @history array' do
49
+ # We need to manually stub the base #request method,
50
+ # since we're testing the override in the subclassed Client.
51
+ ::OAuth2::Client.class_eval do
52
+ def request(*args)
53
+ {'simple' => 'hash'}
54
+ end
55
+ end
56
+ @client = strategy.client
57
+ @client.history = []
58
+ @client.request(:get, 'http://host/api/test.action')
59
+ #assert_equal( {'test.action' => {'simple' => 'hash'}}, @client.history )
60
+ #assert_equal( {'test.action' => {'simple' => 'hash'}}, strategy.send(:raw_info) )
61
+ assert_equal( {'api_call' => 'test.action', 'response' => {'simple' => 'hash'}}, @client.history[0].to_h.tap{|r| r.delete('time')} )
62
+
63
+ end
64
+
65
+ test "transfers team_domain from strategy options to client.site uri" do
66
+ @options = { :team_domain => 'subdomain' }
67
+ assert_equal "https://subdomain.slack.com", strategy.client.site
68
+ end
69
+
70
+ test "transfers team_domain from request.params to client.site uri" do
71
+ @options = {pass_through_params: 'team_domain' }
72
+ @request.stubs(:params).returns({ 'team_domain' => 'subdomain2' })
73
+ assert_equal "https://subdomain2.slack.com", strategy.client.site
74
+ end
75
+
76
+ test "transfers history option from strategy client_options to client.history" do
77
+ @options = { :client_options => {history:[5]} }
78
+ assert_equal [5], strategy.client.history
79
+ end
80
+ end
81
+
82
+ class CallbackUrlTest < StrategyTestCase
83
+ test "returns the default callback url" do
84
+ url_base = "http://auth.request.com"
85
+ @request.stubs(:url).returns("#{url_base}/some/page")
86
+ strategy.stubs(:script_name).returns("") # as not to depend on Rack env
87
+ assert_equal "#{url_base}/auth/slack/callback", strategy.callback_url
88
+ end
89
+
90
+ test "returns path from callback_path option" do
91
+ @options = { :callback_path => "/auth/slack/done"}
92
+ url_base = "http://auth.request.com"
93
+ @request.stubs(:url).returns("#{url_base}/page/path")
94
+ strategy.stubs(:script_name).returns("") # as not to depend on Rack env
95
+ assert_equal "#{url_base}/auth/slack/done", strategy.callback_url
96
+ end
97
+ end
98
+
99
+ class UidTest < StrategyTestCase
100
+ def setup
101
+ super
102
+ @access_token = stub("OmniAuth::Slack::OAuth2::AccessToken")
103
+ #@access_token.stubs(:user_id).returns('U123')
104
+ #@access_token.stubs(:team_id).returns('T456')
105
+ strategy.stubs(:access_token).returns(@access_token)
106
+ end
107
+
108
+ test "returns the uid from access_token" do
109
+ @access_token.stubs(:uid).returns("U123-T456")
110
+ assert_equal "U123-T456", strategy.uid
111
+ end
112
+ end
113
+
114
+ class CredentialsTest < StrategyTestCase
115
+ def setup
116
+ super
117
+ @access_token = stub("OmniAuth::Slack::OAuth2::AccessToken")
118
+ @access_token.stubs(:token)
119
+ @access_token.stubs(:token_type)
120
+ @access_token.stubs(:expires?)
121
+ @access_token.stubs(:expires_at)
122
+ @access_token.stubs(:refresh_token)
123
+ @access_token.stubs(:[])
124
+ @access_token.stubs(:params)
125
+ @access_token.stubs(:is_app_token?)
126
+ @access_token.stubs(:scope)
127
+ @access_token.stubs(:scopes)
128
+ @access_token.stubs(:all_scopes)
129
+ @access_token.stubs(:user_token)
130
+ strategy.stubs(:access_token).returns(@access_token)
131
+ end
132
+
133
+ test "returns a Hash" do
134
+ assert_kind_of Hash, strategy.credentials
135
+ end
136
+
137
+ test "returns the token" do
138
+ @access_token.stubs(:token).returns("123")
139
+ assert_equal "123", strategy.credentials["token"]
140
+ end
141
+
142
+ test "returns the token_type" do
143
+ @access_token.stubs(:token_type).returns('bot')
144
+ assert_equal 'bot', strategy.credentials[:token_type]
145
+ end
146
+
147
+ test "returns the expiry status" do
148
+ @access_token.stubs(:expires?).returns(true)
149
+ assert strategy.credentials["expires"]
150
+
151
+ @access_token.stubs(:expires?).returns(false)
152
+ refute strategy.credentials["expires"]
153
+ end
154
+
155
+ test "returns the refresh token and expiry time when expiring" do
156
+ ten_mins_from_now = (Time.now + 600).to_i
157
+ @access_token.stubs(:expires?).returns(true)
158
+ @access_token.stubs(:refresh_token).returns("321")
159
+ @access_token.stubs(:expires_at).returns(ten_mins_from_now)
160
+ assert_equal "321", strategy.credentials["refresh_token"]
161
+ assert_equal ten_mins_from_now, strategy.credentials["expires_at"]
162
+ end
163
+
164
+ test "does not return the refresh token when test is nil and expiring" do
165
+ @access_token.stubs(:expires?).returns(true)
166
+ @access_token.stubs(:refresh_token).returns(nil)
167
+ assert_nil strategy.credentials["refresh_token"]
168
+ refute_has_key "refresh_token", strategy.credentials
169
+ end
170
+
171
+ test "does not return the refresh token when not expiring" do
172
+ @access_token.stubs(:expires?).returns(false)
173
+ @access_token.stubs(:refresh_token).returns("XXX")
174
+ assert_nil strategy.credentials["refresh_token"]
175
+ refute_has_key "refresh_token", strategy.credentials
176
+ end
177
+ end
178
+
179
+ # This test is no longer relevant, but if modified, it might still be useful.
180
+ #
181
+ # class SkipInfoTest < StrategyTestCase
182
+ #
183
+ # test 'info should not include extended info when skip_info is specified' do
184
+ # @access_token = stub_everything("OmniAuth::Slack::OAuth2::AccessToken")
185
+ # @options = { skip_info: true }
186
+ # strategy.stubs(:access_token).returns(@access_token)
187
+ # assert_equal %w(name email user_id team_name team_id image), strategy.info.keys.map(&:to_s)
188
+ # end
189
+ #
190
+ # end
191
+
192
+ class AuthorizeParamsTest < StrategyTestCase
193
+
194
+ test 'returns OmniAuth::Strategy::Options hash' do
195
+ assert_kind_of OmniAuth::Strategy::Options, strategy.authorize_params
196
+ end
197
+
198
+ test 'forwards oauth request params (redirect_uri scope team) to slack' do
199
+ @options = {pass_through_params: %w(redirect_uri scope team)}
200
+ strategy.request.params['scope'] = 'test-scope'
201
+ strategy.request.params['team'] = 'test-team'
202
+ strategy.request.params['redirect_uri'] = 'http://my-test-uri/auth/callback'
203
+ assert_equal 'test-scope', strategy.authorize_params['scope']
204
+ assert_equal 'test-team', strategy.authorize_params['team']
205
+ assert_equal 'http://my-test-uri/auth/callback', strategy.authorize_params['redirect_uri']
206
+ end
207
+
208
+ end
209
+
210
+ # class InitializeTest < StrategyTestCase
211
+ # end
212
+
213
+ class CallbackPhaseTest < StrategyTestCase
214
+ def setup
215
+ super
216
+ strategy.stubs(:session).returns({'omniauth.authorize_params'=>{'team'=>'ABC123'}})
217
+ strategy.stubs(:env).returns({})
218
+ # Without this, OAuth2#callback_phase fails during these tests with csrf detected.
219
+ strategy.stubs('fail!').returns(true)
220
+ end
221
+
222
+ test "sets env['omniauth.authorize_params'] with session['omniauth.authorize_params']" do
223
+ strategy.callback_phase
224
+ assert_equal( {'team'=>'ABC123'}, strategy.env['omniauth.authorize_params'] )
225
+ end
226
+ end
227
+
228
+ class PassThroughParamsTest < StrategyTestCase
229
+ test 'returns all AUTH_OPTIONS when given :all' do
230
+ strategy.options.pass_through_params = :all
231
+ assert_equal strategy.class::AUTH_OPTIONS, strategy.send(:pass_through_params)
232
+ end
233
+
234
+ test 'returns empty array when given :none' do
235
+ strategy.options.pass_through_params = :none
236
+ assert_equal [], strategy.send(:pass_through_params)
237
+ end
238
+
239
+ test "returns empty array when given nil" do
240
+ strategy.options.pass_through_params = nil
241
+ assert_equal [], strategy.send(:pass_through_params)
242
+ end
243
+
244
+ test "returns specific items when given specific items" do
245
+ strategy.options.pass_through_params = %w(scope team_domain)
246
+ assert_equal %w(scope team_domain), strategy.send(:pass_through_params)
247
+ end
248
+ end
249
+