2009-11-16 11:58:14 -05:00
|
|
|
module Devise
|
|
|
|
module TestHelpers
|
|
|
|
def self.included(base)
|
|
|
|
base.class_eval do
|
|
|
|
setup :setup_controller_for_warden, :warden if respond_to?(:setup)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# This is a Warden::Proxy customized for functional tests. It's meant to
|
2010-01-07 16:37:47 -05:00
|
|
|
# some of Warden::Manager responsibilities, as retrieving configuration
|
2009-11-16 11:58:14 -05:00
|
|
|
# options and calling the FailureApp.
|
|
|
|
class TestWarden < Warden::Proxy #:nodoc:
|
|
|
|
attr_reader :controller
|
|
|
|
|
|
|
|
def initialize(controller)
|
|
|
|
@controller = controller
|
2010-01-05 07:44:13 -05:00
|
|
|
manager = Warden::Manager.new(nil) do |config|
|
2010-03-28 08:51:03 -04:00
|
|
|
config.merge! Devise.warden_config
|
2009-11-16 11:58:14 -05:00
|
|
|
end
|
2010-01-05 07:44:13 -05:00
|
|
|
super(controller.request.env, manager)
|
2009-11-16 11:58:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate!(*args)
|
|
|
|
catch_with_redirect { super }
|
|
|
|
end
|
|
|
|
|
2010-03-26 07:26:51 -04:00
|
|
|
def user(*args)
|
|
|
|
catch_with_redirect { super }
|
|
|
|
end
|
|
|
|
|
2009-11-16 11:58:14 -05:00
|
|
|
def catch_with_redirect(&block)
|
|
|
|
result = catch(:warden, &block)
|
|
|
|
|
|
|
|
if result.is_a?(Hash) && !custom_failure? && !@controller.send(:performed?)
|
|
|
|
result[:action] ||= :unauthenticated
|
|
|
|
|
|
|
|
env = @controller.request.env
|
|
|
|
env["PATH_INFO"] = "/#{result[:action]}"
|
|
|
|
env["warden.options"] = result
|
|
|
|
Warden::Manager._before_failure.each{ |hook| hook.call(env, result) }
|
|
|
|
|
|
|
|
status, headers, body = Devise::FailureApp.call(env).to_a
|
2010-06-13 05:09:59 -04:00
|
|
|
@controller.send :render, :status => status, :text => body,
|
|
|
|
:content_type => headers["Content-Type"], :location => headers["Location"]
|
2009-11-16 11:58:14 -05:00
|
|
|
else
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# We need to setup the environment variables and the response in the controller.
|
|
|
|
def setup_controller_for_warden #:nodoc:
|
2010-02-16 11:00:36 -05:00
|
|
|
@request.env['action_controller.instance'] = @controller
|
2009-11-16 11:58:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Quick access to Warden::Proxy.
|
|
|
|
def warden #:nodoc:
|
|
|
|
@warden ||= (@request.env['warden'] = TestWarden.new(@controller))
|
|
|
|
end
|
|
|
|
|
|
|
|
# sign_in a given resource by storing its keys in the session.
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# sign_in :user, @user # sign_in(scope, resource)
|
|
|
|
# sign_in @user # sign_in(resource)
|
|
|
|
#
|
|
|
|
def sign_in(resource_or_scope, resource=nil)
|
2009-11-16 12:07:01 -05:00
|
|
|
scope ||= Devise::Mapping.find_scope!(resource_or_scope)
|
2009-11-16 11:58:14 -05:00
|
|
|
resource ||= resource_or_scope
|
2010-01-14 07:38:02 -05:00
|
|
|
warden.session_serializer.store(resource, scope)
|
2009-11-16 11:58:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Sign out a given resource or scope by calling logout on Warden.
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# sign_out :user # sign_out(scope)
|
|
|
|
# sign_out @user # sign_out(resource)
|
|
|
|
#
|
|
|
|
def sign_out(resource_or_scope)
|
2009-11-16 12:07:01 -05:00
|
|
|
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
2009-12-14 16:48:15 -05:00
|
|
|
@controller.instance_variable_set(:"@current_#{scope}", nil)
|
2009-11-16 11:58:14 -05:00
|
|
|
warden.logout(scope)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|