2013-02-25 19:05:37 -05:00
require 'helper'
2011-09-03 14:08:07 -04:00
describe OmniAuth :: Builder do
2014-01-15 23:00:46 -05:00
describe '#provider' do
it 'translates a symbol to a constant' do
2019-06-26 19:19:26 -04:00
expect ( OmniAuth :: Strategies ) . to receive ( :const_get ) . with ( 'MyStrategy' , false ) . and_return ( Class . new )
2011-09-03 14:08:07 -04:00
OmniAuth :: Builder . new ( nil ) do
provider :my_strategy
end
end
2014-01-15 23:00:46 -05:00
it 'accepts a class' do
2014-03-15 15:43:19 -04:00
class ExampleClass ; end
2011-09-03 14:08:07 -04:00
2014-01-15 23:00:46 -05:00
expect do
2012-10-10 04:32:55 -04:00
OmniAuth :: Builder . new ( nil ) do
provider :: ExampleClass
end
2014-01-15 23:00:46 -05:00
end . not_to raise_error
2011-09-03 14:08:07 -04:00
end
2011-11-25 09:03:23 -05:00
2012-10-10 04:32:55 -04:00
it " raises a helpful LoadError message if it can't find the class " do
2014-01-15 23:00:46 -05:00
expect do
2011-11-25 09:03:23 -05:00
OmniAuth :: Builder . new ( nil ) do
provider :lorax
end
2014-01-15 23:00:46 -05:00
end . to raise_error ( LoadError , 'Could not find matching strategy for :lorax. You may need to install an additional gem (such as omniauth-lorax).' )
2011-11-25 09:03:23 -05:00
end
2019-06-26 19:19:26 -04:00
it " doesn't translate a symbol to a top-level constant " do
class MyStrategy ; end
expect do
OmniAuth :: Builder . new ( nil ) do
provider :my_strategy
end
end . to raise_error ( LoadError , 'Could not find matching strategy for :my_strategy. You may need to install an additional gem (such as omniauth-my_strategy).' )
end
2011-09-03 14:08:07 -04:00
end
2012-04-12 17:01:48 -04:00
2014-01-15 23:00:46 -05:00
describe '#options' do
it 'merges provided options in' do
2012-04-12 17:01:48 -04:00
k = Class . new
b = OmniAuth :: Builder . new ( nil )
2014-01-16 00:04:44 -05:00
expect ( b ) . to receive ( :use ) . with ( k , :foo = > 'bar' , :baz = > 'tik' )
2012-04-12 17:01:48 -04:00
b . options :foo = > 'bar'
b . provider k , :baz = > 'tik'
end
2014-01-15 23:00:46 -05:00
it 'adds an argument if no options are provided' do
2012-04-12 17:01:48 -04:00
k = Class . new
b = OmniAuth :: Builder . new ( nil )
2014-01-16 00:04:44 -05:00
expect ( b ) . to receive ( :use ) . with ( k , :foo = > 'bar' )
2012-04-12 17:01:48 -04:00
b . options :foo = > 'bar'
b . provider k
end
end
2019-11-10 23:46:36 -05:00
describe '#on_failure' do
it 'passes the block to the config' do
prok = proc { }
2019-11-11 00:27:36 -05:00
with_config_reset ( :on_failure ) do
OmniAuth :: Builder . new ( nil ) . on_failure ( & prok )
2019-11-10 23:46:36 -05:00
2019-11-11 00:27:36 -05:00
expect ( OmniAuth . config . on_failure ) . to eq ( prok )
end
2019-11-10 23:46:36 -05:00
end
end
describe '#before_options_phase' do
it 'passes the block to the config' do
prok = proc { }
2019-11-11 00:27:36 -05:00
with_config_reset ( :before_options_phase ) do
OmniAuth :: Builder . new ( nil ) . before_options_phase ( & prok )
2019-11-10 23:46:36 -05:00
2019-11-11 00:27:36 -05:00
expect ( OmniAuth . config . before_options_phase ) . to eq ( prok )
end
2019-11-10 23:46:36 -05:00
end
end
describe '#before_request_phase' do
it 'passes the block to the config' do
prok = proc { }
2019-11-11 00:27:36 -05:00
with_config_reset ( :before_request_phase ) do
OmniAuth :: Builder . new ( nil ) . before_request_phase ( & prok )
2019-11-10 23:46:36 -05:00
2019-11-11 00:27:36 -05:00
expect ( OmniAuth . config . before_request_phase ) . to eq ( prok )
end
2019-11-10 23:46:36 -05:00
end
end
describe '#before_callback_phase' do
it 'passes the block to the config' do
prok = proc { }
2019-11-11 00:27:36 -05:00
with_config_reset ( :before_callback_phase ) do
OmniAuth :: Builder . new ( nil ) . before_callback_phase ( & prok )
2019-11-10 23:46:36 -05:00
2019-11-11 00:27:36 -05:00
expect ( OmniAuth . config . before_callback_phase ) . to eq ( prok )
end
2019-11-10 23:46:36 -05:00
end
end
describe '#configure' do
it 'passes the block to the config' do
prok = proc { }
allow ( OmniAuth ) . to receive ( :configure ) . and_call_original
OmniAuth :: Builder . new ( nil ) . configure ( & prok )
expect ( OmniAuth ) . to have_received ( :configure ) do | & block |
expect ( block ) . to eq ( prok )
end
end
end
describe '#call' do
it 'passes env to to_app.call' do
2020-12-10 13:04:28 -05:00
app = lambda { | env | [ 200 , { } , env [ 'CUSTOM_ENV_VALUE' ] ] }
2019-11-10 23:46:36 -05:00
builder = OmniAuth :: Builder . new ( app )
2020-12-10 13:04:28 -05:00
env = { 'REQUEST_METHOD' = > 'GET' , 'PATH_INFO' = > '/some/path' , 'CUSTOM_ENV_VALUE' = > 'VALUE' }
2019-11-10 23:46:36 -05:00
2020-12-10 13:04:28 -05:00
expect ( builder . call ( env ) ) . to eq ( [ 200 , { } , 'VALUE' ] )
2019-11-10 23:46:36 -05:00
end
end
2019-11-11 00:27:36 -05:00
def with_config_reset ( option )
old_config = OmniAuth . config . send ( option )
yield
OmniAuth . config . send ( " #{ option } = " , old_config )
end
2011-09-03 14:08:07 -04:00
end