2010-03-26 06:27:19 -04:00
|
|
|
require 'test_helper'
|
2010-01-13 12:12:13 -05:00
|
|
|
require 'ostruct'
|
2009-10-09 20:11:58 -04:00
|
|
|
|
2010-01-13 12:12:13 -05:00
|
|
|
class ControllerAuthenticableTest < ActionController::TestCase
|
2010-07-16 05:01:36 -04:00
|
|
|
tests ApplicationController
|
2010-01-13 12:12:13 -05:00
|
|
|
|
|
|
|
def setup
|
|
|
|
@mock_warden = OpenStruct.new
|
2010-07-16 05:01:36 -04:00
|
|
|
@controller.request.env['warden'] = @mock_warden
|
2010-01-13 12:12:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'provide access to warden instance' do
|
2010-07-16 05:01:36 -04:00
|
|
|
assert_equal @mock_warden, @controller.warden
|
2010-01-13 12:12:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'proxy signed_in? to authenticated' do
|
2010-01-14 07:38:02 -05:00
|
|
|
@mock_warden.expects(:authenticate?).with(:scope => :my_scope)
|
2010-01-13 12:12:13 -05:00
|
|
|
@controller.signed_in?(:my_scope)
|
|
|
|
end
|
|
|
|
|
2010-06-11 10:51:48 -04:00
|
|
|
test 'proxy anybody_signed_in? to signed_in?' do
|
|
|
|
Devise.mappings.keys.each { |scope| # :user, :admin, :manager
|
|
|
|
@controller.expects(:signed_in?).with(scope)
|
|
|
|
}
|
|
|
|
@controller.anybody_signed_in?
|
|
|
|
end
|
|
|
|
|
2010-08-02 07:50:48 -04:00
|
|
|
test 'proxy current_user to authenticate with user scope' do
|
|
|
|
@mock_warden.expects(:authenticate).with(:scope => :user)
|
|
|
|
@controller.current_user
|
|
|
|
end
|
|
|
|
|
2010-01-14 07:38:02 -05:00
|
|
|
test 'proxy current_admin to authenticate with admin scope' do
|
|
|
|
@mock_warden.expects(:authenticate).with(:scope => :admin)
|
2010-01-13 12:12:13 -05:00
|
|
|
@controller.current_admin
|
2010-01-14 07:38:02 -05:00
|
|
|
end
|
2010-01-13 12:12:13 -05:00
|
|
|
|
2010-08-02 07:50:48 -04:00
|
|
|
test 'proxy current_publisher_account to authenticate with namespaced publisher account scope' do
|
|
|
|
@mock_warden.expects(:authenticate).with(:scope => :publisher_account)
|
|
|
|
@controller.current_publisher_account
|
2010-01-13 12:12:13 -05:00
|
|
|
end
|
|
|
|
|
2010-08-02 07:50:48 -04:00
|
|
|
test 'proxy authenticate_user! to authenticate with user scope' do
|
2010-01-13 12:12:13 -05:00
|
|
|
@mock_warden.expects(:authenticate!).with(:scope => :user)
|
|
|
|
@controller.authenticate_user!
|
|
|
|
end
|
|
|
|
|
2010-08-02 07:50:48 -04:00
|
|
|
test 'proxy authenticate_admin! to authenticate with admin scope' do
|
2010-01-13 12:12:13 -05:00
|
|
|
@mock_warden.expects(:authenticate!).with(:scope => :admin)
|
|
|
|
@controller.authenticate_admin!
|
|
|
|
end
|
|
|
|
|
2010-08-02 07:50:48 -04:00
|
|
|
test 'proxy authenticate_publisher_account! to authenticate with namespaced publisher account scope' do
|
|
|
|
@mock_warden.expects(:authenticate!).with(:scope => :publisher_account)
|
|
|
|
@controller.authenticate_publisher_account!
|
|
|
|
end
|
|
|
|
|
2010-08-13 08:02:51 -04:00
|
|
|
test 'proxy user_signed_in? to authenticate with user scope' do
|
2010-07-19 08:06:48 -04:00
|
|
|
@mock_warden.expects(:authenticate).with(:scope => :user).returns("user")
|
|
|
|
assert @controller.user_signed_in?
|
2010-01-13 12:12:13 -05:00
|
|
|
end
|
|
|
|
|
2010-08-13 08:02:51 -04:00
|
|
|
test 'proxy admin_signed_in? to authenticatewith admin scope' do
|
2010-07-19 08:06:48 -04:00
|
|
|
@mock_warden.expects(:authenticate).with(:scope => :admin)
|
|
|
|
assert_not @controller.admin_signed_in?
|
2010-01-13 12:12:13 -05:00
|
|
|
end
|
|
|
|
|
2010-08-13 08:02:51 -04:00
|
|
|
test 'proxy publisher_account_signed_in? to authenticate with namespaced publisher account scope' do
|
|
|
|
@mock_warden.expects(:authenticate).with(:scope => :publisher_account)
|
2010-08-02 07:50:48 -04:00
|
|
|
@controller.publisher_account_signed_in?
|
|
|
|
end
|
|
|
|
|
2010-01-13 12:12:13 -05:00
|
|
|
test 'proxy user_session to session scope in warden' do
|
2010-01-14 07:38:02 -05:00
|
|
|
@mock_warden.expects(:authenticate).with(:scope => :user).returns(true)
|
2010-01-13 12:12:13 -05:00
|
|
|
@mock_warden.expects(:session).with(:user).returns({})
|
|
|
|
@controller.user_session
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'proxy admin_session to session scope in warden' do
|
2010-01-14 07:38:02 -05:00
|
|
|
@mock_warden.expects(:authenticate).with(:scope => :admin).returns(true)
|
2010-01-13 12:12:13 -05:00
|
|
|
@mock_warden.expects(:session).with(:admin).returns({})
|
|
|
|
@controller.admin_session
|
|
|
|
end
|
|
|
|
|
2010-08-02 07:50:48 -04:00
|
|
|
test 'proxy publisher_account_session from namespaced scope to session scope in warden' do
|
|
|
|
@mock_warden.expects(:authenticate).with(:scope => :publisher_account).returns(true)
|
|
|
|
@mock_warden.expects(:session).with(:publisher_account).returns({})
|
|
|
|
@controller.publisher_account_session
|
|
|
|
end
|
|
|
|
|
2010-01-13 12:12:13 -05:00
|
|
|
test 'sign in proxy to set_user on warden' do
|
|
|
|
user = User.new
|
|
|
|
@mock_warden.expects(:set_user).with(user, :scope => :user).returns(true)
|
|
|
|
@controller.sign_in(:user, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'sign in accepts a resource as argument' do
|
|
|
|
user = User.new
|
|
|
|
@mock_warden.expects(:set_user).with(user, :scope => :user).returns(true)
|
|
|
|
@controller.sign_in(user)
|
|
|
|
end
|
2009-10-09 20:11:58 -04:00
|
|
|
|
2010-01-13 12:12:13 -05:00
|
|
|
test 'sign out proxy to logout on warden' do
|
|
|
|
@mock_warden.expects(:user).with(:user).returns(true)
|
|
|
|
@mock_warden.expects(:logout).with(:user).returns(true)
|
|
|
|
@controller.sign_out(:user)
|
2009-10-09 20:11:58 -04:00
|
|
|
end
|
|
|
|
|
2010-01-13 12:12:13 -05:00
|
|
|
test 'sign out accepts a resource as argument' do
|
|
|
|
@mock_warden.expects(:user).with(:user).returns(true)
|
|
|
|
@mock_warden.expects(:logout).with(:user).returns(true)
|
|
|
|
@controller.sign_out(User.new)
|
2009-10-11 09:49:25 -04:00
|
|
|
end
|
|
|
|
|
2010-06-23 15:03:27 -04:00
|
|
|
test 'sign out everybody proxy to logout on warden' do
|
|
|
|
Devise.mappings.keys.each { |scope|
|
|
|
|
@mock_warden.expects(:user).with(scope).returns(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
@mock_warden.expects(:logout).with(*Devise.mappings.keys).returns(true)
|
2010-06-24 09:38:49 -04:00
|
|
|
@controller.sign_out_all_scopes
|
2010-06-23 15:03:27 -04:00
|
|
|
end
|
|
|
|
|
2010-01-13 12:12:13 -05:00
|
|
|
test 'stored location for returns the location for a given scope' do
|
|
|
|
assert_nil @controller.stored_location_for(:user)
|
2010-04-03 05:43:31 -04:00
|
|
|
@controller.session[:"user_return_to"] = "/foo.bar"
|
2010-01-13 12:12:13 -05:00
|
|
|
assert_equal "/foo.bar", @controller.stored_location_for(:user)
|
2009-10-09 20:11:58 -04:00
|
|
|
end
|
2009-10-10 08:32:51 -04:00
|
|
|
|
2010-01-13 12:12:13 -05:00
|
|
|
test 'stored location for accepts a resource as argument' do
|
|
|
|
assert_nil @controller.stored_location_for(:user)
|
2010-04-03 05:43:31 -04:00
|
|
|
@controller.session[:"user_return_to"] = "/foo.bar"
|
2010-01-13 12:12:13 -05:00
|
|
|
assert_equal "/foo.bar", @controller.stored_location_for(User.new)
|
2009-10-10 08:32:51 -04:00
|
|
|
end
|
|
|
|
|
2010-01-13 12:12:13 -05:00
|
|
|
test 'stored location cleans information after reading' do
|
2010-04-03 05:43:31 -04:00
|
|
|
@controller.session[:"user_return_to"] = "/foo.bar"
|
2010-01-13 12:12:13 -05:00
|
|
|
assert_equal "/foo.bar", @controller.stored_location_for(:user)
|
2010-04-03 05:43:31 -04:00
|
|
|
assert_nil @controller.session[:"user_return_to"]
|
2010-01-13 12:12:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'after sign in path defaults to root path if none by was specified for the given scope' do
|
|
|
|
assert_equal root_path, @controller.after_sign_in_path_for(:user)
|
|
|
|
end
|
2009-10-11 22:24:57 -04:00
|
|
|
|
2010-01-13 12:12:13 -05:00
|
|
|
test 'after sign in path defaults to the scoped root path' do
|
|
|
|
assert_equal admin_root_path, @controller.after_sign_in_path_for(:admin)
|
|
|
|
end
|
|
|
|
|
2010-05-23 19:50:42 -04:00
|
|
|
test 'after update path defaults to root path if none by was specified for the given scope' do
|
|
|
|
assert_equal root_path, @controller.after_update_path_for(:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'after update path defaults to the scoped root path' do
|
|
|
|
assert_equal admin_root_path, @controller.after_update_path_for(:admin)
|
|
|
|
end
|
|
|
|
|
2010-01-13 12:12:13 -05:00
|
|
|
test 'after sign out path defaults to the root path' do
|
|
|
|
assert_equal root_path, @controller.after_sign_out_path_for(:admin)
|
|
|
|
assert_equal root_path, @controller.after_sign_out_path_for(:user)
|
|
|
|
end
|
2009-10-11 22:24:57 -04:00
|
|
|
|
2010-01-13 12:12:13 -05:00
|
|
|
test 'sign in and redirect uses the stored location' do
|
|
|
|
user = User.new
|
2010-04-03 05:43:31 -04:00
|
|
|
@controller.session[:"user_return_to"] = "/foo.bar"
|
2010-04-01 11:30:55 -04:00
|
|
|
@mock_warden.expects(:user).with(:user).returns(nil)
|
2010-01-13 12:12:13 -05:00
|
|
|
@mock_warden.expects(:set_user).with(user, :scope => :user).returns(true)
|
|
|
|
@controller.expects(:redirect_to).with("/foo.bar")
|
|
|
|
@controller.sign_in_and_redirect(user)
|
2009-10-11 22:24:57 -04:00
|
|
|
end
|
|
|
|
|
2010-01-13 12:12:13 -05:00
|
|
|
test 'sign in and redirect uses the configured after sign in path' do
|
|
|
|
admin = Admin.new
|
2010-04-01 11:30:55 -04:00
|
|
|
@mock_warden.expects(:user).with(:admin).returns(nil)
|
2010-01-13 12:12:13 -05:00
|
|
|
@mock_warden.expects(:set_user).with(admin, :scope => :admin).returns(true)
|
|
|
|
@controller.expects(:redirect_to).with(admin_root_path)
|
|
|
|
@controller.sign_in_and_redirect(admin)
|
2009-10-10 08:32:51 -04:00
|
|
|
end
|
2009-10-27 19:26:40 -04:00
|
|
|
|
2010-04-01 11:30:55 -04:00
|
|
|
test 'sign in and redirect does not sign in again if user is already signed' do
|
2010-01-16 04:56:35 -05:00
|
|
|
admin = Admin.new
|
2010-04-01 11:30:55 -04:00
|
|
|
@mock_warden.expects(:user).with(:admin).returns(admin)
|
|
|
|
@mock_warden.expects(:set_user).never
|
2010-01-13 12:12:13 -05:00
|
|
|
@controller.expects(:redirect_to).with(admin_root_path)
|
2010-04-01 11:30:55 -04:00
|
|
|
@controller.sign_in_and_redirect(admin)
|
2009-10-27 19:26:40 -04:00
|
|
|
end
|
2010-01-13 12:12:13 -05:00
|
|
|
|
|
|
|
test 'sign out and redirect uses the configured after sign out path' do
|
|
|
|
@mock_warden.expects(:user).with(:admin).returns(true)
|
|
|
|
@mock_warden.expects(:logout).with(:admin).returns(true)
|
|
|
|
@controller.expects(:redirect_to).with(admin_root_path)
|
|
|
|
@controller.instance_eval "def after_sign_out_path_for(resource); admin_root_path; end"
|
|
|
|
@controller.sign_out_and_redirect(:admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'is not a devise controller' do
|
|
|
|
assert_not @controller.devise_controller?
|
|
|
|
end
|
2009-10-09 20:11:58 -04:00
|
|
|
end
|