diff options
Diffstat (limited to 'actionpack/test/controller/filters_test.rb')
-rw-r--r-- | actionpack/test/controller/filters_test.rb | 286 |
1 files changed, 128 insertions, 158 deletions
diff --git a/actionpack/test/controller/filters_test.rb b/actionpack/test/controller/filters_test.rb index 08271012e9..a34878cee8 100644 --- a/actionpack/test/controller/filters_test.rb +++ b/actionpack/test/controller/filters_test.rb @@ -1,8 +1,8 @@ -require 'abstract_unit' +require "abstract_unit" class ActionController::Base class << self - %w(append_around_action prepend_after_action prepend_around_action prepend_before_action skip_after_action skip_before_action skip_action_callback).each do |pending| + %w(append_around_action prepend_after_action prepend_around_action prepend_before_action skip_after_action skip_before_action).each do |pending| define_method(pending) do |*args| $stderr.puts "#{pending} unimplemented: #{args.inspect}" end unless method_defined?(pending) @@ -21,7 +21,7 @@ class FilterTest < ActionController::TestCase after_action :clean_up def show - render :inline => "ran action" + render inline: "ran action" end private @@ -37,7 +37,7 @@ class FilterTest < ActionController::TestCase end class ChangingTheRequirementsController < TestController - before_action :ensure_login, :except => [:go_wild] + before_action :ensure_login, except: [:go_wild] def go_wild render plain: "gobble" @@ -56,14 +56,14 @@ class FilterTest < ActionController::TestCase end protected - (1..3).each do |i| - define_method "try_#{i}" do - instance_variable_set :@try, i - if action_name == "fail_#{i}" - head(404) + (1..3).each do |i| + define_method "try_#{i}" do + instance_variable_set :@try, i + if action_name == "fail_#{i}" + head(404) + end end end - end end class RenderingController < ActionController::Base @@ -72,14 +72,14 @@ class FilterTest < ActionController::TestCase def show @ran_action = true - render :inline => "ran action" + render inline: "ran action" end private def before_action_rendering @ran_filter ||= [] @ran_filter << "before_action_rendering" - render :inline => "something else" + render inline: "something else" end def unreached_after_action @@ -102,19 +102,19 @@ class FilterTest < ActionController::TestCase def show @ran_action = true - render :inline => "ran show action" + render inline: "ran show action" end def target_of_redirection @ran_target_of_redirection = true - render :inline => "ran target_of_redirection action" + render inline: "ran target_of_redirection action" end private def before_action_redirects @ran_filter ||= [] @ran_filter << "before_action_redirects" - redirect_to(:action => 'target_of_redirection') + redirect_to(action: "target_of_redirection") end def unreached_after_action @@ -133,15 +133,15 @@ class FilterTest < ActionController::TestCase class ConditionalFilterController < ActionController::Base def show - render :inline => "ran action" + render inline: "ran action" end def another_action - render :inline => "ran action" + render inline: "ran action" end def show_without_action - render :inline => "ran action without action" + render inline: "ran action without action" end private @@ -157,28 +157,28 @@ class FilterTest < ActionController::TestCase end class ConditionalCollectionFilterController < ConditionalFilterController - before_action :ensure_login, :except => [ :show_without_action, :another_action ] + before_action :ensure_login, except: [ :show_without_action, :another_action ] end class OnlyConditionSymController < ConditionalFilterController - before_action :ensure_login, :only => :show + before_action :ensure_login, only: :show end class ExceptConditionSymController < ConditionalFilterController - before_action :ensure_login, :except => :show_without_action + before_action :ensure_login, except: :show_without_action end class BeforeAndAfterConditionController < ConditionalFilterController - before_action :ensure_login, :only => :show - after_action :clean_up_tmp, :only => :show + before_action :ensure_login, only: :show + after_action :clean_up_tmp, only: :show end class OnlyConditionProcController < ConditionalFilterController - before_action(:only => :show) {|c| c.instance_variable_set(:"@ran_proc_action", true) } + before_action(only: :show) { |c| c.instance_variable_set(:"@ran_proc_action", true) } end class ExceptConditionProcController < ConditionalFilterController - before_action(:except => :show_without_action) {|c| c.instance_variable_set(:"@ran_proc_action", true) } + before_action(except: :show_without_action) { |c| c.instance_variable_set(:"@ran_proc_action", true) } end class ConditionalClassFilter @@ -186,24 +186,24 @@ class FilterTest < ActionController::TestCase end class OnlyConditionClassController < ConditionalFilterController - before_action ConditionalClassFilter, :only => :show + before_action ConditionalClassFilter, only: :show end class ExceptConditionClassController < ConditionalFilterController - before_action ConditionalClassFilter, :except => :show_without_action + before_action ConditionalClassFilter, except: :show_without_action end class AnomolousYetValidConditionController < ConditionalFilterController - before_action(ConditionalClassFilter, :ensure_login, Proc.new {|c| c.instance_variable_set(:"@ran_proc_action1", true)}, :except => :show_without_action) { |c| c.instance_variable_set(:"@ran_proc_action2", true)} + before_action(ConditionalClassFilter, :ensure_login, Proc.new { |c| c.instance_variable_set(:"@ran_proc_action1", true) }, except: :show_without_action) { |c| c.instance_variable_set(:"@ran_proc_action2", true) } end class OnlyConditionalOptionsFilter < ConditionalFilterController - before_action :ensure_login, :only => :index, :if => Proc.new {|c| c.instance_variable_set(:"@ran_conditional_index_proc", true) } + before_action :ensure_login, only: :index, if: Proc.new { |c| c.instance_variable_set(:"@ran_conditional_index_proc", true) } end class ConditionalOptionsFilter < ConditionalFilterController - before_action :ensure_login, :if => Proc.new { |c| true } - before_action :clean_up_tmp, :if => Proc.new { |c| false } + before_action :ensure_login, if: Proc.new { |c| true } + before_action :clean_up_tmp, if: Proc.new { |c| false } end class ConditionalOptionsSkipFilter < ConditionalFilterController @@ -222,7 +222,7 @@ class FilterTest < ActionController::TestCase skip_before_action :clean_up_tmp, only: :login, if: -> { true } def login - render plain: 'ok' + render plain: "ok" end end @@ -234,7 +234,7 @@ class FilterTest < ActionController::TestCase skip_before_action :clean_up_tmp, if: -> { true }, except: :login def login - render plain: 'ok' + render plain: "ok" end end @@ -255,14 +255,14 @@ class FilterTest < ActionController::TestCase class SkippingAndLimitedController < TestController skip_before_action :ensure_login - before_action :ensure_login, :only => :index + before_action :ensure_login, only: :index def index - render plain: 'ok' + render plain: "ok" end def public - render plain: 'ok' + render plain: "ok" end end @@ -272,7 +272,7 @@ class FilterTest < ActionController::TestCase before_action :ensure_login def index - render plain: 'ok' + render plain: "ok" end private @@ -283,17 +283,17 @@ class FilterTest < ActionController::TestCase end class ConditionalSkippingController < TestController - skip_before_action :ensure_login, :only => [ :login ] - skip_after_action :clean_up, :only => [ :login ] + skip_before_action :ensure_login, only: [ :login ] + skip_after_action :clean_up, only: [ :login ] - before_action :find_user, :only => [ :change_password ] + before_action :find_user, only: [ :change_password ] def login - render :inline => "ran action" + render inline: "ran action" end def change_password - render :inline => "ran action" + render inline: "ran action" end protected @@ -304,29 +304,29 @@ class FilterTest < ActionController::TestCase end class ConditionalParentOfConditionalSkippingController < ConditionalFilterController - before_action :conditional_in_parent_before, :only => [:show, :another_action] - after_action :conditional_in_parent_after, :only => [:show, :another_action] + before_action :conditional_in_parent_before, only: [:show, :another_action] + after_action :conditional_in_parent_after, only: [:show, :another_action] private def conditional_in_parent_before @ran_filter ||= [] - @ran_filter << 'conditional_in_parent_before' + @ran_filter << "conditional_in_parent_before" end def conditional_in_parent_after @ran_filter ||= [] - @ran_filter << 'conditional_in_parent_after' + @ran_filter << "conditional_in_parent_after" end end class ChildOfConditionalParentController < ConditionalParentOfConditionalSkippingController - skip_before_action :conditional_in_parent_before, :only => :another_action - skip_after_action :conditional_in_parent_after, :only => :another_action + skip_before_action :conditional_in_parent_before, only: :another_action + skip_after_action :conditional_in_parent_after, only: :another_action end class AnotherChildOfConditionalParentController < ConditionalParentOfConditionalSkippingController - skip_before_action :conditional_in_parent_before, :only => :show + skip_before_action :conditional_in_parent_before, only: :show end class ProcController < PrependingController @@ -418,14 +418,14 @@ class FilterTest < ActionController::TestCase class OutOfOrder < StandardError; end before_action :first - before_action :second, :only => :foo + before_action :second, only: :foo def foo - render plain: 'foo' + render plain: "foo" end def bar - render plain: 'bar' + render plain: "bar" end protected @@ -458,20 +458,20 @@ class FilterTest < ActionController::TestCase def before_all @ran_filter ||= [] - @ran_filter << 'before_all' + @ran_filter << "before_all" end def after_all @ran_filter ||= [] - @ran_filter << 'after_all' + @ran_filter << "after_all" end def between_before_all_and_after_all @ran_filter ||= [] - @ran_filter << 'between_before_all_and_after_all' + @ran_filter << "between_before_all_and_after_all" end def show - render plain: 'hello' + render plain: "hello" end end @@ -494,14 +494,13 @@ class FilterTest < ActionController::TestCase end class NonYieldingAroundFilterController < ActionController::Base - before_action :filter_one around_action :non_yielding_action before_action :action_two after_action :action_three def index - render :inline => "index" + render inline: "index" end private @@ -522,22 +521,21 @@ class FilterTest < ActionController::TestCase def action_three @filters << "action_three" end - end class ImplicitActionsController < ActionController::Base - before_action :find_only, :only => :edit - before_action :find_except, :except => :edit + before_action :find_only, only: :edit + before_action :find_except, except: :edit private - def find_only - @only = 'Only' - end + def find_only + @only = "Only" + end - def find_except - @except = 'Except' - end + def find_except + @except = "Except" + end end def test_non_yielding_around_actions_do_not_raise @@ -611,12 +609,12 @@ class FilterTest < ActionController::TestCase end def test_if_is_ignored_when_used_with_only - test_process(SkipFilterUsingOnlyAndIf, 'login') + test_process(SkipFilterUsingOnlyAndIf, "login") assert_not @controller.instance_variable_defined?(:@ran_filter) end def test_except_is_ignored_when_used_with_if - test_process(SkipFilterUsingIfAndExcept, 'login') + test_process(SkipFilterUsingIfAndExcept, "login") assert_equal %w(ensure_login), @controller.instance_variable_get(:@ran_filter) end @@ -745,13 +743,13 @@ class FilterTest < ActionController::TestCase def test_actions_with_mixed_specialization_run_in_order assert_nothing_raised do - response = test_process(MixedSpecializationController, 'bar') - assert_equal 'bar', response.body + response = test_process(MixedSpecializationController, "bar") + assert_equal "bar", response.body end assert_nothing_raised do - response = test_process(MixedSpecializationController, 'foo') - assert_equal 'foo', response.body + response = test_process(MixedSpecializationController, "foo") + assert_equal "foo", response.body end end @@ -795,7 +793,7 @@ class FilterTest < ActionController::TestCase def test_conditional_skipping_of_actions_when_parent_action_is_also_conditional test_process(ChildOfConditionalParentController) assert_equal %w( conditional_in_parent_before conditional_in_parent_after ), @controller.instance_variable_get(:@ran_filter) - test_process(ChildOfConditionalParentController, 'another_action') + test_process(ChildOfConditionalParentController, "another_action") assert_not @controller.instance_variable_defined?(:@ran_filter) end @@ -824,15 +822,15 @@ class FilterTest < ActionController::TestCase end def test_actions_obey_only_and_except_for_implicit_actions - test_process(ImplicitActionsController, 'show') - assert_equal 'Except', @controller.instance_variable_get(:@except) + test_process(ImplicitActionsController, "show") + assert_equal "Except", @controller.instance_variable_get(:@except) assert_not @controller.instance_variable_defined?(:@only) - assert_equal 'show', response.body + assert_equal "show", response.body - test_process(ImplicitActionsController, 'edit') - assert_equal 'Only', @controller.instance_variable_get(:@only) + test_process(ImplicitActionsController, "edit") + assert_equal "Only", @controller.instance_variable_get(:@only) assert_not @controller.instance_variable_defined?(:@except) - assert_equal 'edit', response.body + assert_equal "edit", response.body end private @@ -859,14 +857,14 @@ class PostsController < ActionController::Base private def default_action - render :inline => "#{action_name} called" + render inline: "#{action_name} called" end end class ControllerWithSymbolAsFilter < PostsController - around_action :raise_before, :only => :raises_before - around_action :raise_after, :only => :raises_after - around_action :without_exception, :only => :no_raise + around_action :raise_before, only: :raises_before + around_action :raise_after, only: :raises_after + around_action :without_exception, only: :no_raise private def raise_before @@ -898,7 +896,7 @@ class ControllerWithFilterClass < PostsController end end - around_action YieldingFilter, :only => :raises_after + around_action YieldingFilter, only: :raises_after end class ControllerWithFilterInstance < PostsController @@ -909,11 +907,11 @@ class ControllerWithFilterInstance < PostsController end end - around_action YieldingFilter.new, :only => :raises_after + around_action YieldingFilter.new, only: :raises_after end class ControllerWithProcFilter < PostsController - around_action(:only => :no_raise) do |c,b| + around_action(only: :no_raise) do |c,b| c.instance_variable_set(:"@before", true) b.call c.instance_variable_set(:"@after", true) @@ -921,7 +919,7 @@ class ControllerWithProcFilter < PostsController end class ControllerWithNestedFilters < ControllerWithSymbolAsFilter - around_action :raise_before, :raise_after, :without_exception, :only => :raises_both + around_action :raise_before, :raise_after, :without_exception, only: :raises_both end class ControllerWithAllTypesOfFilters < PostsController @@ -931,26 +929,26 @@ class ControllerWithAllTypesOfFilters < PostsController around_action :around_again private - def before - @ran_filter ||= [] - @ran_filter << 'before' - end + def before + @ran_filter ||= [] + @ran_filter << "before" + end - def around - @ran_filter << 'around (before yield)' - yield - @ran_filter << 'around (after yield)' - end + def around + @ran_filter << "around (before yield)" + yield + @ran_filter << "around (after yield)" + end - def after - @ran_filter << 'after' - end + def after + @ran_filter << "after" + end - def around_again - @ran_filter << 'around_again (before yield)' - yield - @ran_filter << 'around_again (after yield)' - end + def around_again + @ran_filter << "around_again (before yield)" + yield + @ran_filter << "around_again (after yield)" + end end class ControllerWithTwoLessFilters < ControllerWithAllTypesOfFilters @@ -958,46 +956,39 @@ class ControllerWithTwoLessFilters < ControllerWithAllTypesOfFilters skip_after_action :after end -class SkipFilterUsingSkipActionCallback < ControllerWithAllTypesOfFilters - ActiveSupport::Deprecation.silence do - skip_action_callback :around_again - skip_action_callback :after - end -end - class YieldingAroundFiltersTest < ActionController::TestCase include PostsController::AroundExceptions def test_base controller = PostsController - assert_nothing_raised { test_process(controller,'no_raise') } - assert_nothing_raised { test_process(controller,'raises_before') } - assert_nothing_raised { test_process(controller,'raises_after') } - assert_nothing_raised { test_process(controller,'no_action') } + assert_nothing_raised { test_process(controller,"no_raise") } + assert_nothing_raised { test_process(controller,"raises_before") } + assert_nothing_raised { test_process(controller,"raises_after") } + assert_nothing_raised { test_process(controller,"no_action") } end def test_with_symbol controller = ControllerWithSymbolAsFilter - assert_nothing_raised { test_process(controller,'no_raise') } - assert_raise(Before) { test_process(controller,'raises_before') } - assert_raise(After) { test_process(controller,'raises_after') } - assert_nothing_raised { test_process(controller,'no_raise') } + assert_nothing_raised { test_process(controller,"no_raise") } + assert_raise(Before) { test_process(controller,"raises_before") } + assert_raise(After) { test_process(controller,"raises_after") } + assert_nothing_raised { test_process(controller,"no_raise") } end def test_with_class controller = ControllerWithFilterClass - assert_nothing_raised { test_process(controller,'no_raise') } - assert_raise(After) { test_process(controller,'raises_after') } + assert_nothing_raised { test_process(controller,"no_raise") } + assert_raise(After) { test_process(controller,"raises_after") } end def test_with_instance controller = ControllerWithFilterInstance - assert_nothing_raised { test_process(controller,'no_raise') } - assert_raise(After) { test_process(controller,'raises_after') } + assert_nothing_raised { test_process(controller,"no_raise") } + assert_raise(After) { test_process(controller,"raises_after") } end def test_with_proc - test_process(ControllerWithProcFilter,'no_raise') + test_process(ControllerWithProcFilter,"no_raise") assert @controller.instance_variable_get(:@before) assert @controller.instance_variable_get(:@after) end @@ -1006,70 +997,49 @@ class YieldingAroundFiltersTest < ActionController::TestCase controller = ControllerWithNestedFilters assert_nothing_raised do begin - test_process(controller,'raises_both') + test_process(controller,"raises_both") rescue Before, After end end assert_raise Before do begin - test_process(controller,'raises_both') + test_process(controller,"raises_both") rescue After end end end def test_action_order_with_all_action_types - test_process(ControllerWithAllTypesOfFilters,'no_raise') - assert_equal 'before around (before yield) around_again (before yield) around_again (after yield) after around (after yield)', @controller.instance_variable_get(:@ran_filter).join(' ') + test_process(ControllerWithAllTypesOfFilters,"no_raise") + assert_equal "before around (before yield) around_again (before yield) around_again (after yield) after around (after yield)", @controller.instance_variable_get(:@ran_filter).join(" ") end def test_action_order_with_skip_action_method - test_process(ControllerWithTwoLessFilters,'no_raise') - assert_equal 'before around (before yield) around (after yield)', @controller.instance_variable_get(:@ran_filter).join(' ') + test_process(ControllerWithTwoLessFilters,"no_raise") + assert_equal "before around (before yield) around (after yield)", @controller.instance_variable_get(:@ran_filter).join(" ") end def test_first_action_in_multiple_before_action_chain_halts controller = ::FilterTest::TestMultipleFiltersController.new - response = test_process(controller, 'fail_1') - assert_equal '', response.body + response = test_process(controller, "fail_1") + assert_equal "", response.body assert_equal 1, controller.instance_variable_get(:@try) end def test_second_action_in_multiple_before_action_chain_halts controller = ::FilterTest::TestMultipleFiltersController.new - response = test_process(controller, 'fail_2') - assert_equal '', response.body + response = test_process(controller, "fail_2") + assert_equal "", response.body assert_equal 2, controller.instance_variable_get(:@try) end def test_last_action_in_multiple_before_action_chain_halts controller = ::FilterTest::TestMultipleFiltersController.new - response = test_process(controller, 'fail_3') - assert_equal '', response.body + response = test_process(controller, "fail_3") + assert_equal "", response.body assert_equal 3, controller.instance_variable_get(:@try) end - def test_skipping_with_skip_action_callback - test_process(SkipFilterUsingSkipActionCallback,'no_raise') - assert_equal 'before around (before yield) around (after yield)', @controller.instance_variable_get(:@ran_filter).join(' ') - end - - def test_deprecated_skip_action_callback - assert_deprecated do - Class.new(PostsController) do - skip_action_callback :clean_up - end - end - end - - def test_deprecated_skip_filter - assert_deprecated do - Class.new(PostsController) do - skip_filter :clean_up - end - end - end - protected def test_process(controller, action = "show") @controller = controller.is_a?(Class) ? controller.new : controller |