Some warden helpers tests taken from rails_warden
This commit is contained in:
parent
11715cac1f
commit
fff46a53eb
|
@ -16,59 +16,57 @@ module Devise
|
|||
end
|
||||
end
|
||||
|
||||
protected
|
||||
# The main accessor for the warden proxy instance
|
||||
#
|
||||
def warden
|
||||
request.env['warden']
|
||||
end
|
||||
|
||||
# The main accessor for the warden proxy instance
|
||||
#
|
||||
def warden
|
||||
request.env['warden']
|
||||
end
|
||||
# Proxy to the authenticated? method on warden
|
||||
#
|
||||
def authenticated?(*args)
|
||||
warden.authenticated?(*args)
|
||||
end
|
||||
alias_method :logged_in?, :authenticated?
|
||||
|
||||
# Proxy to the authenticated? method on warden
|
||||
#
|
||||
def authenticated?(*args)
|
||||
warden.authenticated?(*args)
|
||||
end
|
||||
alias_method :logged_in?, :authenticated?
|
||||
# Access the currently logged in user
|
||||
#
|
||||
def user(*args)
|
||||
warden.user(*args)
|
||||
end
|
||||
alias_method :current_user, :user
|
||||
|
||||
# Access the currently logged in user
|
||||
#
|
||||
def user(*args)
|
||||
warden.user(*args)
|
||||
end
|
||||
alias_method :current_user, :user
|
||||
def user=(user)
|
||||
warden.set_user user
|
||||
end
|
||||
alias_method :current_user=, :user=
|
||||
|
||||
def user=(user)
|
||||
warden.set_user user
|
||||
end
|
||||
alias_method :current_user=, :user=
|
||||
# Logout the current user
|
||||
#
|
||||
def logout(*args)
|
||||
warden.raw_session.inspect # Without this inspect here. The session does not clear :|
|
||||
warden.logout(*args)
|
||||
end
|
||||
|
||||
# Logout the current user
|
||||
#
|
||||
def logout(*args)
|
||||
warden.raw_session.inspect # Without this inspect here. The session does not clear :|
|
||||
warden.logout(*args)
|
||||
end
|
||||
# Proxy to the authenticate method on warden
|
||||
#
|
||||
def authenticate(*args)
|
||||
warden.authenticate(*args)
|
||||
end
|
||||
|
||||
# Proxy to the authenticate method on warden
|
||||
#
|
||||
def authenticate(*args)
|
||||
warden.authenticate(*args)
|
||||
end
|
||||
# Proxy to the authenticate method on warden
|
||||
#
|
||||
def authenticate!(*args)
|
||||
warden.authenticate!(*args)
|
||||
end
|
||||
|
||||
# Proxy to the authenticate method on warden
|
||||
#
|
||||
def authenticate!(*args)
|
||||
warden.authenticate!(*args)
|
||||
end
|
||||
|
||||
# Helper for use in before_filters where no authentication is required:
|
||||
# Example:
|
||||
# before_filter :require_no_authentication, :only => :new
|
||||
#
|
||||
def require_no_authentication
|
||||
redirect_to root_path if authenticated?
|
||||
end
|
||||
# Helper for use in before_filters where no authentication is required:
|
||||
# Example:
|
||||
# before_filter :require_no_authentication, :only => :new
|
||||
#
|
||||
def require_no_authentication
|
||||
redirect_to root_path if authenticated?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
require 'test/test_helper'
|
||||
require 'ostruct'
|
||||
|
||||
class MockController < ApplicationController
|
||||
attr_accessor :env
|
||||
def request
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
class AuthenticableTest < ActionController::TestCase
|
||||
|
||||
def setup
|
||||
@controller = MockController.new
|
||||
@mock_warden = OpenStruct.new
|
||||
@controller.env = { 'warden' => @mock_warden }
|
||||
end
|
||||
|
||||
test 'setup warden' do
|
||||
assert_not_nil @controller.warden
|
||||
end
|
||||
|
||||
test 'provide access to warden instance' do
|
||||
assert_equal @controller.warden, @controller.env['warden']
|
||||
end
|
||||
|
||||
test 'run authenticate on warden' do
|
||||
@mock_warden.expects(:authenticate).returns(true)
|
||||
@controller.authenticate
|
||||
end
|
||||
|
||||
test 'run authenticate! on warden' do
|
||||
@mock_warden.expects(:authenticate!).returns(true)
|
||||
@controller.authenticate!
|
||||
end
|
||||
|
||||
test 'run authenticate? on warden' do
|
||||
@mock_warden.expects(:authenticated?).returns(true)
|
||||
@controller.authenticated?
|
||||
end
|
||||
|
||||
test 'proxy logged_in? to authenticated' do
|
||||
@mock_warden.expects(:authenticated?).returns(true)
|
||||
@controller.logged_in?
|
||||
end
|
||||
|
||||
test 'run user on warden' do
|
||||
@mock_warden.expects(:user).returns(true)
|
||||
@controller.user
|
||||
end
|
||||
|
||||
test 'run current_user on warden' do
|
||||
@mock_warden.expects(:user).returns(true)
|
||||
@controller.current_user
|
||||
end
|
||||
|
||||
test 'set the user on warden' do
|
||||
@mock_warden.expects(:set_user).returns(true)
|
||||
@controller.user = User.new
|
||||
end
|
||||
|
||||
test 'proxy logout to warden' do
|
||||
@mock_warden.expects(:logout).returns(true)
|
||||
@controller.logout
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue