From 561833e06084092a3a7301f106db2bad91bfb163 Mon Sep 17 00:00:00 2001 From: "Carlos A. da Silva" Date: Sat, 10 Oct 2009 16:47:11 -0300 Subject: [PATCH] Adding warden scopes to helper methods. --- lib/devise/controllers/authenticable.rb | 16 ++++++++-------- test/controllers/authenticable_test.rb | 9 +++++++++ test/integration/authentication_test.rb | 10 +++++----- test/integration/password_recovery_test.rb | 6 +++--- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/devise/controllers/authenticable.rb b/lib/devise/controllers/authenticable.rb index e049b420..b895b410 100644 --- a/lib/devise/controllers/authenticable.rb +++ b/lib/devise/controllers/authenticable.rb @@ -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 diff --git a/test/controllers/authenticable_test.rb b/test/controllers/authenticable_test.rb index 4940b557..99d9ae28 100644 --- a/test/controllers/authenticable_test.rb +++ b/test/controllers/authenticable_test.rb @@ -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? diff --git a/test/integration/authentication_test.rb b/test/integration/authentication_test.rb index bed7defb..afa4e8c5 100644 --- a/test/integration/authentication_test.rb +++ b/test/integration/authentication_test.rb @@ -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 diff --git a/test/integration/password_recovery_test.rb b/test/integration/password_recovery_test.rb index 2b858d4b..b9e15fff 100644 --- a/test/integration/password_recovery_test.rb +++ b/test/integration/password_recovery_test.rb @@ -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