2010-03-26 06:27:19 -04:00
|
|
|
require 'test_helper'
|
2010-01-13 12:12:13 -05:00
|
|
|
|
|
|
|
class MyController < ApplicationController
|
|
|
|
include Devise::Controllers::InternalHelpers
|
|
|
|
end
|
|
|
|
|
|
|
|
class HelpersTest < ActionController::TestCase
|
|
|
|
tests MyController
|
|
|
|
|
2010-03-26 06:27:19 -04:00
|
|
|
def setup
|
|
|
|
@mock_warden = OpenStruct.new
|
|
|
|
@controller.request.env['warden'] = @mock_warden
|
2010-07-05 19:27:20 -04:00
|
|
|
@controller.request.env['devise.mapping'] = Devise.mappings[:user]
|
2010-03-26 06:27:19 -04:00
|
|
|
end
|
|
|
|
|
2010-07-05 19:27:20 -04:00
|
|
|
test 'get resource name from env' do
|
2010-01-13 12:12:13 -05:00
|
|
|
assert_equal :user, @controller.resource_name
|
|
|
|
end
|
|
|
|
|
2010-07-05 19:27:20 -04:00
|
|
|
test 'get resource class from env' do
|
2010-01-13 12:12:13 -05:00
|
|
|
assert_equal User, @controller.resource_class
|
|
|
|
end
|
|
|
|
|
2010-07-05 19:27:20 -04:00
|
|
|
test 'get resource instance variable from env' do
|
2010-07-16 05:01:36 -04:00
|
|
|
@controller.instance_variable_set(:@user, user = User.new)
|
|
|
|
assert_equal user, @controller.resource
|
2010-01-13 12:12:13 -05:00
|
|
|
end
|
|
|
|
|
2010-07-05 19:27:20 -04:00
|
|
|
test 'set resource instance variable from env' do
|
2010-07-16 05:01:36 -04:00
|
|
|
user = @controller.send(:resource_class).new
|
|
|
|
@controller.send(:resource=, user)
|
2010-01-13 12:12:13 -05:00
|
|
|
|
2010-07-16 05:01:36 -04:00
|
|
|
assert_equal user, @controller.send(:resource)
|
|
|
|
assert_equal user, @controller.instance_variable_get(:@user)
|
2010-01-13 12:12:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'resources methods are not controller actions' do
|
|
|
|
assert @controller.class.action_methods.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'require no authentication tests current mapping' do
|
2011-04-29 02:56:35 -04:00
|
|
|
@mock_warden.expects(:authenticate?).with(:rememberable, :token_authenticatable, :scope => :user).returns(true)
|
2010-08-23 07:56:10 -04:00
|
|
|
@mock_warden.expects(:user).with(:user).returns(User.new)
|
2010-01-13 12:12:13 -05:00
|
|
|
@controller.expects(:redirect_to).with(root_path)
|
|
|
|
@controller.send :require_no_authentication
|
|
|
|
end
|
2010-07-16 05:01:36 -04:00
|
|
|
|
2011-06-22 14:28:29 -04:00
|
|
|
test 'require no authentication skips if no inputs are available' do
|
|
|
|
Devise.mappings[:user].expects(:no_input_strategies).returns([])
|
|
|
|
@mock_warden.expects(:authenticate?).never
|
|
|
|
@controller.expects(:redirect_to).never
|
|
|
|
@controller.send :require_no_authentication
|
|
|
|
end
|
|
|
|
|
2011-04-12 21:18:49 -04:00
|
|
|
test 'require no authentication sets a flash message' do
|
2011-04-29 02:56:35 -04:00
|
|
|
@mock_warden.expects(:authenticate?).with(:rememberable, :token_authenticatable, :scope => :user).returns(true)
|
2011-04-12 21:18:49 -04:00
|
|
|
@mock_warden.expects(:user).with(:user).returns(User.new)
|
|
|
|
@controller.expects(:redirect_to).with(root_path)
|
|
|
|
@controller.send :require_no_authentication
|
2011-04-13 12:48:30 -04:00
|
|
|
assert flash[:alert] == I18n.t("devise.failure.already_authenticated")
|
2011-04-12 21:18:49 -04:00
|
|
|
end
|
|
|
|
|
2010-07-16 05:01:36 -04:00
|
|
|
test 'signed in resource returns signed in resource for current scope' do
|
|
|
|
@mock_warden.expects(:authenticate).with(:scope => :user).returns(User.new)
|
|
|
|
assert_kind_of User, @controller.signed_in_resource
|
|
|
|
end
|
2010-11-21 08:25:37 -05:00
|
|
|
|
2010-01-13 12:12:13 -05:00
|
|
|
test 'is a devise controller' do
|
|
|
|
assert @controller.devise_controller?
|
|
|
|
end
|
2010-11-21 08:25:37 -05:00
|
|
|
|
2010-11-10 14:04:06 -05:00
|
|
|
test 'does not issue blank flash messages' do
|
|
|
|
MyController.send(:public, :set_flash_message)
|
|
|
|
I18n.stubs(:t).returns(' ')
|
|
|
|
@controller.set_flash_message :notice, :send_instructions
|
|
|
|
assert flash[:notice].nil?
|
|
|
|
MyController.send(:protected, :set_flash_message)
|
|
|
|
end
|
2010-11-21 08:25:37 -05:00
|
|
|
|
2010-11-10 14:04:06 -05:00
|
|
|
test 'issues non-blank flash messages normally' do
|
|
|
|
MyController.send(:public, :set_flash_message)
|
|
|
|
I18n.stubs(:t).returns('non-blank')
|
|
|
|
@controller.set_flash_message :notice, :send_instructions
|
|
|
|
assert flash[:notice] == 'non-blank'
|
|
|
|
MyController.send(:protected, :set_flash_message)
|
|
|
|
end
|
2011-01-15 14:00:23 -05:00
|
|
|
|
|
|
|
test 'navigational_formats not returning a wild card' do
|
|
|
|
MyController.send(:public, :navigational_formats)
|
|
|
|
Devise.navigational_formats = [:"*/*", :html]
|
|
|
|
assert_not @controller.navigational_formats.include?(:"*/*")
|
|
|
|
MyController.send(:protected, :navigational_formats)
|
|
|
|
end
|
2010-01-13 12:12:13 -05:00
|
|
|
end
|