mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Some warden helpers tests taken from rails_warden
This commit is contained in:
parent
11715cac1f
commit
fff46a53eb
2 changed files with 110 additions and 46 deletions
|
@ -16,59 +16,57 @@ module Devise
|
||||||
end
|
end
|
||||||
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
|
# Proxy to the authenticated? method on warden
|
||||||
#
|
#
|
||||||
def warden
|
def authenticated?(*args)
|
||||||
request.env['warden']
|
warden.authenticated?(*args)
|
||||||
end
|
end
|
||||||
|
alias_method :logged_in?, :authenticated?
|
||||||
|
|
||||||
# Proxy to the authenticated? method on warden
|
# Access the currently logged in user
|
||||||
#
|
#
|
||||||
def authenticated?(*args)
|
def user(*args)
|
||||||
warden.authenticated?(*args)
|
warden.user(*args)
|
||||||
end
|
end
|
||||||
alias_method :logged_in?, :authenticated?
|
alias_method :current_user, :user
|
||||||
|
|
||||||
# Access the currently logged in user
|
def user=(user)
|
||||||
#
|
warden.set_user user
|
||||||
def user(*args)
|
end
|
||||||
warden.user(*args)
|
alias_method :current_user=, :user=
|
||||||
end
|
|
||||||
alias_method :current_user, :user
|
|
||||||
|
|
||||||
def user=(user)
|
# Logout the current user
|
||||||
warden.set_user user
|
#
|
||||||
end
|
def logout(*args)
|
||||||
alias_method :current_user=, :user=
|
warden.raw_session.inspect # Without this inspect here. The session does not clear :|
|
||||||
|
warden.logout(*args)
|
||||||
|
end
|
||||||
|
|
||||||
# Logout the current user
|
# Proxy to the authenticate method on warden
|
||||||
#
|
#
|
||||||
def logout(*args)
|
def authenticate(*args)
|
||||||
warden.raw_session.inspect # Without this inspect here. The session does not clear :|
|
warden.authenticate(*args)
|
||||||
warden.logout(*args)
|
end
|
||||||
end
|
|
||||||
|
|
||||||
# Proxy to the authenticate method on warden
|
# Proxy to the authenticate method on warden
|
||||||
#
|
#
|
||||||
def authenticate(*args)
|
def authenticate!(*args)
|
||||||
warden.authenticate(*args)
|
warden.authenticate!(*args)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Proxy to the authenticate method on warden
|
# Helper for use in before_filters where no authentication is required:
|
||||||
#
|
# Example:
|
||||||
def authenticate!(*args)
|
# before_filter :require_no_authentication, :only => :new
|
||||||
warden.authenticate!(*args)
|
#
|
||||||
end
|
def require_no_authentication
|
||||||
|
redirect_to root_path if authenticated?
|
||||||
# Helper for use in before_filters where no authentication is required:
|
end
|
||||||
# Example:
|
|
||||||
# before_filter :require_no_authentication, :only => :new
|
|
||||||
#
|
|
||||||
def require_no_authentication
|
|
||||||
redirect_to root_path if authenticated?
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
66
test/controllers/authenticable_test.rb
Normal file
66
test/controllers/authenticable_test.rb
Normal file
|
@ -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…
Add table
Reference in a new issue