Fix up specs from bleeding into others
Merge branch 'hooks' of github.com:cromulus/omniauth into cromulus-hooks Conflicts: lib/omniauth.rb
This commit is contained in:
commit
c3942078cf
|
@ -34,6 +34,9 @@ module OmniAuth
|
|||
:path_prefix => '/auth',
|
||||
:on_failure => OmniAuth::FailureEndpoint,
|
||||
:failure_raise_out_environments => ['development'],
|
||||
:on_callback_hook =>Proc.new {|p|},
|
||||
:on_options_hook=>Proc.new {|p| },
|
||||
:on_request_hook=>Proc.new {|p| },
|
||||
:form_css => Form::DEFAULT_CSS,
|
||||
:test_mode => false,
|
||||
:logger => default_logger,
|
||||
|
@ -65,6 +68,31 @@ module OmniAuth
|
|||
end
|
||||
end
|
||||
|
||||
def on_callback_hook(&block)
|
||||
if block_given?
|
||||
@on_callback_hook = block
|
||||
else
|
||||
@on_callback_hook
|
||||
end
|
||||
end
|
||||
|
||||
def on_options_hook(&block)
|
||||
if block_given?
|
||||
@on_options_hook = block
|
||||
else
|
||||
@on_options_hook
|
||||
end
|
||||
end
|
||||
|
||||
def on_request_hook(&block)
|
||||
if block_given?
|
||||
@on_request_hook = block
|
||||
else
|
||||
@on_request_hook
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def add_mock(provider, mock={})
|
||||
# Stringify keys recursively one level.
|
||||
mock.keys.each do |key|
|
||||
|
@ -96,7 +124,7 @@ module OmniAuth
|
|||
self.camelizations[name.to_s] = camelized.to_s
|
||||
end
|
||||
|
||||
attr_writer :on_failure
|
||||
attr_writer :on_failure, :on_callback_hook,:on_options_hook,:on_request_hook
|
||||
attr_accessor :failure_raise_out_environments, :path_prefix, :allowed_request_methods, :form_css, :test_mode, :mock_auth, :full_host, :camelizations, :logger
|
||||
end
|
||||
|
||||
|
|
|
@ -21,6 +21,18 @@ module OmniAuth
|
|||
OmniAuth.config.on_failure = block
|
||||
end
|
||||
|
||||
def on_options_hook(&block)
|
||||
OmniAuth.config.on_options_hook = block
|
||||
end
|
||||
|
||||
def on_request_hook(&block)
|
||||
OmniAuth.config.on_request_hook = block
|
||||
end
|
||||
|
||||
def on_callback_hook(&block)
|
||||
OmniAuth.config.on_callback_hook = block
|
||||
end
|
||||
|
||||
def configure(&block)
|
||||
OmniAuth.configure(&block)
|
||||
end
|
||||
|
|
|
@ -186,6 +186,7 @@ module OmniAuth
|
|||
|
||||
# Responds to an OPTIONS request.
|
||||
def options_call
|
||||
OmniAuth.config.on_options_hook.call(self.env)
|
||||
verbs = OmniAuth.config.allowed_request_methods.map(&:to_s).map(&:upcase).join(', ')
|
||||
return [ 200, { 'Allow' => verbs }, [] ]
|
||||
end
|
||||
|
@ -199,6 +200,8 @@ module OmniAuth
|
|||
#store query params from the request url, extracted in the callback_phase
|
||||
session['omniauth.params'] = request.params
|
||||
|
||||
OmniAuth.config.on_request_hook.call(self.env)
|
||||
|
||||
if options.form.respond_to?(:call)
|
||||
log :info, "Rendering form from supplied Rack endpoint."
|
||||
options.form.call(env)
|
||||
|
@ -218,11 +221,11 @@ module OmniAuth
|
|||
# Performs the steps necessary to run the callback phase of a strategy.
|
||||
def callback_call
|
||||
setup_phase
|
||||
|
||||
log :info, "Callback phase initiated."
|
||||
@env['omniauth.origin'] = session.delete('omniauth.origin')
|
||||
@env['omniauth.origin'] = nil if env['omniauth.origin'] == ''
|
||||
@env['omniauth.params'] = session.delete('omniauth.params') || {}
|
||||
OmniAuth.config.on_callback_hook.call(@env)
|
||||
callback_phase
|
||||
end
|
||||
|
||||
|
@ -265,7 +268,7 @@ module OmniAuth
|
|||
setup_phase
|
||||
|
||||
session['omniauth.params'] = request.params
|
||||
|
||||
OmniAuth.config.on_request_hook.call(self.env)
|
||||
if request.params['origin']
|
||||
@env['rack.session']['omniauth.origin'] = request.params['origin']
|
||||
elsif env['HTTP_REFERER'] && !env['HTTP_REFERER'].match(/#{request_path}$/)
|
||||
|
@ -284,6 +287,7 @@ module OmniAuth
|
|||
@env['omniauth.params'] = session.delete('omniauth.params') || {}
|
||||
@env['omniauth.origin'] = session.delete('omniauth.origin')
|
||||
@env['omniauth.origin'] = nil if env['omniauth.origin'] == ''
|
||||
OmniAuth.config.on_callback_hook.call(@env)
|
||||
call_app!
|
||||
end
|
||||
end
|
||||
|
|
|
@ -596,6 +596,16 @@ describe OmniAuth::Strategy do
|
|||
expect(strategy.env['omniauth.origin']).to eq('http://example.com/origin')
|
||||
end
|
||||
|
||||
it "executes callback_hook on the callback phase" do
|
||||
OmniAuth.config.mock_auth[:test] = {}
|
||||
OmniAuth.config.on_callback_hook do |env|
|
||||
env['foobar']='baz'
|
||||
end
|
||||
strategy.call(make_env('/auth/test/callback', 'rack.session' => {'omniauth.origin' => 'http://example.com/origin'}))
|
||||
expect(strategy.env['foobar']).to eq('baz')
|
||||
end
|
||||
|
||||
|
||||
it "sets omniauth.params on the request phase" do
|
||||
OmniAuth.config.mock_auth[:test] = {}
|
||||
|
||||
|
@ -603,6 +613,15 @@ describe OmniAuth::Strategy do
|
|||
expect(strategy.env['rack.session']['omniauth.params']).to eq({'foo' => 'bar'})
|
||||
end
|
||||
|
||||
it "executes request_hook on the request phase" do
|
||||
OmniAuth.config.mock_auth[:test] = {}
|
||||
OmniAuth.config.on_request_hook do |env|
|
||||
env['foobar']='baz'
|
||||
end
|
||||
strategy.call(make_env('/auth/test', 'QUERY_STRING' => 'foo=bar'))
|
||||
expect(strategy.env['foobar']).to eq('baz')
|
||||
end
|
||||
|
||||
it "turns omniauth.params into an env variable on the callback phase" do
|
||||
OmniAuth.config.mock_auth[:test] = {}
|
||||
|
||||
|
|
|
@ -26,14 +26,20 @@ describe OmniAuth do
|
|||
end
|
||||
|
||||
before do
|
||||
@old_path_prefix = OmniAuth.config.path_prefix
|
||||
@old_on_failure = OmniAuth.config.on_failure
|
||||
@old_path_prefix = OmniAuth.config.path_prefix
|
||||
@old_on_failure = OmniAuth.config.on_failure
|
||||
@old_on_callback_hook = OmniAuth.config.on_callback_hook
|
||||
@old_on_options_hook = OmniAuth.config.on_options_hook
|
||||
@old_on_request_hook = OmniAuth.config.on_request_hook
|
||||
end
|
||||
|
||||
after do
|
||||
OmniAuth.configure do |config|
|
||||
config.path_prefix = @old_path_prefix
|
||||
config.on_failure = @old_on_failure
|
||||
config.path_prefix = @old_path_prefix
|
||||
config.on_failure = @old_on_failure
|
||||
config.on_callback_hook = @old_on_callback_hook
|
||||
config.on_options_hook = @old_on_options_hook
|
||||
config.on_request_hook = @old_on_request_hook
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -54,6 +60,34 @@ describe OmniAuth do
|
|||
|
||||
expect(OmniAuth.config.on_failure.call).to eq('yoyo')
|
||||
end
|
||||
|
||||
it "is able to set hook on option_call" do
|
||||
OmniAuth.configure do |config|
|
||||
config.on_options_hook do
|
||||
'yoyo'
|
||||
end
|
||||
end
|
||||
expect(OmniAuth.config.on_options_hook.call).to eq('yoyo')
|
||||
end
|
||||
|
||||
it "is able to set hook on request_call" do
|
||||
OmniAuth.configure do |config|
|
||||
config.on_request_hook do
|
||||
'heyhey'
|
||||
end
|
||||
end
|
||||
expect(OmniAuth.config.on_request_hook.call).to eq('heyhey')
|
||||
end
|
||||
|
||||
it "is able to set hook on callback_call" do
|
||||
OmniAuth.configure do |config|
|
||||
config.on_callback_hook do
|
||||
'heyhey'
|
||||
end
|
||||
end
|
||||
expect(OmniAuth.config.on_callback_hook.call).to eq('heyhey')
|
||||
end
|
||||
|
||||
describe "mock auth" do
|
||||
before do
|
||||
OmniAuth.config.add_mock(:facebook, :uid => '12345',:info=>{:name=>'Joe', :email=>'joe@example.com'})
|
||||
|
|
Loading…
Reference in New Issue