1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Adding warden scopes to helper methods.

This commit is contained in:
Carlos A. da Silva 2009-10-10 16:47:11 -03:00
parent 4e263b96c9
commit 561833e060
4 changed files with 25 additions and 16 deletions

View file

@ -24,33 +24,33 @@ module Devise
# Proxy to the authenticated? method on warden
#
def authenticated?(*args)
warden.authenticated?(*args)
def authenticated?(scope=resource_name)
warden.authenticated?(scope)
end
alias_method :logged_in?, :authenticated?
# Access the currently logged in user
#
def user(*args)
warden.user(*args)
def user
warden.user(resource_name)
end
alias_method :current_user, :user
def user=(user)
warden.set_user user
warden.set_user(user, :scope => resource_name)
end
alias_method :current_user=, :user=
# Logout the current user
#
def logout(*args)
def logout
warden.raw_session.inspect # Without this inspect here. The session does not clear :|
warden.logout(*args)
warden.logout(resource_name)
end
# Verify authenticated user and redirect to sign in if no authentication is found
#
def authenticate!(*args)
def authenticate!
redirect_to new_session_path unless authenticated?
end

View file

@ -6,6 +6,10 @@ class MockController < ApplicationController
def request
self
end
def path
''
end
end
class ControllerAuthenticableTest < ActionController::TestCase
@ -29,6 +33,11 @@ class ControllerAuthenticableTest < ActionController::TestCase
@controller.authenticated?
end
test 'run authenticate? with scope on warden' do
@mock_warden.expects(:authenticated?).with(:my_scope).returns(true)
@controller.authenticated?(:my_scope)
end
test 'proxy logged_in? to authenticated' do
@mock_warden.expects(:authenticated?).returns(true)
@controller.logged_in?

View file

@ -16,7 +16,7 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_response :success
assert_template 'sessions/new'
assert_contain 'Invalid email or password'
assert !warden.authenticated?
assert !warden.authenticated?(:user)
end
test 'signing in with invalid pasword should return to sign in form with error message' do
@ -44,7 +44,7 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_template 'home/index'
assert_contain 'Signed in successfully'
assert_not_contain 'Sign In'
assert warden.authenticated?
assert warden.authenticated?(:user)
end
test 'not authenticated user should not be able to sign out' do
@ -52,16 +52,16 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_response :redirect
assert_redirected_to new_user_session_path
assert !warden.authenticated?
assert !warden.authenticated?(:user)
end
test 'authenticated user should be able to sign out' do
sign_in
assert warden.authenticated?
assert warden.authenticated?(:user)
delete 'users/session'
assert_response :redirect
assert_redirected_to new_user_session_path
assert !warden.authenticated?
assert !warden.authenticated?(:user)
end
end

View file

@ -22,7 +22,7 @@ class PasswordRecoveryTest < ActionController::IntegrationTest
assert_response :redirect
assert_redirected_to root_path
assert warden.authenticated?
assert warden.authenticated?(:user)
end
test 'not authenticated user should be able to visit forgot password page' do
@ -30,7 +30,7 @@ class PasswordRecoveryTest < ActionController::IntegrationTest
assert_response :success
assert_template 'passwords/new'
assert !warden.authenticated?
assert !warden.authenticated?(:user)
end
test 'not authenticated user should be able to request a forgot password' do
@ -62,7 +62,7 @@ class PasswordRecoveryTest < ActionController::IntegrationTest
assert_response :redirect
assert_redirected_to root_path
assert warden.authenticated?
assert warden.authenticated?(:user)
end
test 'not authenticated with invalid perishable token should not be able to change his password' do