PageRenderTime 54ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/rails/actionpack/test/controller/filters_test.rb

http://monkeycharger.googlecode.com/
Ruby | 853 lines | 768 code | 83 blank | 2 comment | 3 complexity | 1b08a54c0bd2022e3e24f7cd2b762856 MD5 | raw file
  1. require File.dirname(__FILE__) + '/../abstract_unit'
  2. class FilterTest < Test::Unit::TestCase
  3. class TestController < ActionController::Base
  4. before_filter :ensure_login
  5. after_filter :clean_up
  6. def show
  7. render :inline => "ran action"
  8. end
  9. private
  10. def ensure_login
  11. @ran_filter ||= []
  12. @ran_filter << "ensure_login"
  13. end
  14. def clean_up
  15. @ran_after_filter ||= []
  16. @ran_after_filter << "clean_up"
  17. end
  18. end
  19. class ChangingTheRequirementsController < TestController
  20. before_filter :ensure_login, :except => [:go_wild]
  21. def go_wild
  22. render :text => "gobble"
  23. end
  24. end
  25. class TestMultipleFiltersController < ActionController::Base
  26. before_filter :try_1
  27. before_filter :try_2
  28. before_filter :try_3
  29. (1..3).each do |i|
  30. define_method "fail_#{i}" do
  31. render :text => i.to_s
  32. end
  33. end
  34. protected
  35. (1..3).each do |i|
  36. define_method "try_#{i}" do
  37. instance_variable_set :@try, i
  38. action_name != "fail_#{i}"
  39. end
  40. end
  41. end
  42. class RenderingController < ActionController::Base
  43. before_filter :render_something_else
  44. def show
  45. @ran_action = true
  46. render :inline => "ran action"
  47. end
  48. private
  49. def render_something_else
  50. render :inline => "something else"
  51. end
  52. end
  53. class ConditionalFilterController < ActionController::Base
  54. def show
  55. render :inline => "ran action"
  56. end
  57. def another_action
  58. render :inline => "ran action"
  59. end
  60. def show_without_filter
  61. render :inline => "ran action without filter"
  62. end
  63. private
  64. def ensure_login
  65. @ran_filter ||= []
  66. @ran_filter << "ensure_login"
  67. end
  68. def clean_up_tmp
  69. @ran_filter ||= []
  70. @ran_filter << "clean_up_tmp"
  71. end
  72. def rescue_action(e) raise(e) end
  73. end
  74. class ConditionalCollectionFilterController < ConditionalFilterController
  75. before_filter :ensure_login, :except => [ :show_without_filter, :another_action ]
  76. end
  77. class OnlyConditionSymController < ConditionalFilterController
  78. before_filter :ensure_login, :only => :show
  79. end
  80. class ExceptConditionSymController < ConditionalFilterController
  81. before_filter :ensure_login, :except => :show_without_filter
  82. end
  83. class BeforeAndAfterConditionController < ConditionalFilterController
  84. before_filter :ensure_login, :only => :show
  85. after_filter :clean_up_tmp, :only => :show
  86. end
  87. class OnlyConditionProcController < ConditionalFilterController
  88. before_filter(:only => :show) {|c| c.assigns["ran_proc_filter"] = true }
  89. end
  90. class ExceptConditionProcController < ConditionalFilterController
  91. before_filter(:except => :show_without_filter) {|c| c.assigns["ran_proc_filter"] = true }
  92. end
  93. class ConditionalClassFilter
  94. def self.filter(controller) controller.assigns["ran_class_filter"] = true end
  95. end
  96. class OnlyConditionClassController < ConditionalFilterController
  97. before_filter ConditionalClassFilter, :only => :show
  98. end
  99. class ExceptConditionClassController < ConditionalFilterController
  100. before_filter ConditionalClassFilter, :except => :show_without_filter
  101. end
  102. class AnomolousYetValidConditionController < ConditionalFilterController
  103. before_filter(ConditionalClassFilter, :ensure_login, Proc.new {|c| c.assigns["ran_proc_filter1"] = true }, :except => :show_without_filter) { |c| c.assigns["ran_proc_filter2"] = true}
  104. end
  105. class EmptyFilterChainController < TestController
  106. self.filter_chain.clear
  107. def show
  108. @action_executed = true
  109. render :text => "yawp!"
  110. end
  111. end
  112. class PrependingController < TestController
  113. prepend_before_filter :wonderful_life
  114. # skip_before_filter :fire_flash
  115. private
  116. def wonderful_life
  117. @ran_filter ||= []
  118. @ran_filter << "wonderful_life"
  119. end
  120. end
  121. class ConditionalSkippingController < TestController
  122. skip_before_filter :ensure_login, :only => [ :login ]
  123. skip_after_filter :clean_up, :only => [ :login ]
  124. before_filter :find_user, :only => [ :change_password ]
  125. def login
  126. render :inline => "ran action"
  127. end
  128. def change_password
  129. render :inline => "ran action"
  130. end
  131. protected
  132. def find_user
  133. @ran_filter ||= []
  134. @ran_filter << "find_user"
  135. end
  136. end
  137. class ConditionalParentOfConditionalSkippingController < ConditionalFilterController
  138. before_filter :conditional_in_parent, :only => [:show, :another_action]
  139. after_filter :conditional_in_parent, :only => [:show, :another_action]
  140. private
  141. def conditional_in_parent
  142. @ran_filter ||= []
  143. @ran_filter << 'conditional_in_parent'
  144. end
  145. end
  146. class ChildOfConditionalParentController < ConditionalParentOfConditionalSkippingController
  147. skip_before_filter :conditional_in_parent, :only => :another_action
  148. skip_after_filter :conditional_in_parent, :only => :another_action
  149. end
  150. class AnotherChildOfConditionalParentController < ConditionalParentOfConditionalSkippingController
  151. skip_before_filter :conditional_in_parent, :only => :show
  152. end
  153. class ProcController < PrependingController
  154. before_filter(proc { |c| c.assigns["ran_proc_filter"] = true })
  155. end
  156. class ImplicitProcController < PrependingController
  157. before_filter { |c| c.assigns["ran_proc_filter"] = true }
  158. end
  159. class AuditFilter
  160. def self.filter(controller)
  161. controller.assigns["was_audited"] = true
  162. end
  163. end
  164. class AroundFilter
  165. def before(controller)
  166. @execution_log = "before"
  167. controller.class.execution_log << " before aroundfilter " if controller.respond_to? :execution_log
  168. controller.assigns["before_ran"] = true
  169. end
  170. def after(controller)
  171. controller.assigns["execution_log"] = @execution_log + " and after"
  172. controller.assigns["after_ran"] = true
  173. controller.class.execution_log << " after aroundfilter " if controller.respond_to? :execution_log
  174. end
  175. end
  176. class AppendedAroundFilter
  177. def before(controller)
  178. controller.class.execution_log << " before appended aroundfilter "
  179. end
  180. def after(controller)
  181. controller.class.execution_log << " after appended aroundfilter "
  182. end
  183. end
  184. class AuditController < ActionController::Base
  185. before_filter(AuditFilter)
  186. def show
  187. render_text "hello"
  188. end
  189. end
  190. class AroundFilterController < PrependingController
  191. around_filter AroundFilter.new
  192. end
  193. class BeforeAfterClassFilterController < PrependingController
  194. begin
  195. filter = AroundFilter.new
  196. before_filter filter
  197. after_filter filter
  198. end
  199. end
  200. class MixedFilterController < PrependingController
  201. cattr_accessor :execution_log
  202. def initialize
  203. @@execution_log = ""
  204. end
  205. before_filter { |c| c.class.execution_log << " before procfilter " }
  206. prepend_around_filter AroundFilter.new
  207. after_filter { |c| c.class.execution_log << " after procfilter " }
  208. append_around_filter AppendedAroundFilter.new
  209. end
  210. class MixedSpecializationController < ActionController::Base
  211. class OutOfOrder < StandardError; end
  212. before_filter :first
  213. before_filter :second, :only => :foo
  214. def foo
  215. render_text 'foo'
  216. end
  217. def bar
  218. render_text 'bar'
  219. end
  220. protected
  221. def first
  222. @first = true
  223. end
  224. def second
  225. raise OutOfOrder unless @first
  226. end
  227. end
  228. class DynamicDispatchController < ActionController::Base
  229. before_filter :choose
  230. %w(foo bar baz).each do |action|
  231. define_method(action) { render :text => action }
  232. end
  233. private
  234. def choose
  235. self.action_name = params[:choose]
  236. end
  237. end
  238. class PrependingBeforeAndAfterController < ActionController::Base
  239. prepend_before_filter :before_all
  240. prepend_after_filter :after_all
  241. before_filter :between_before_all_and_after_all
  242. def before_all
  243. @ran_filter ||= []
  244. @ran_filter << 'before_all'
  245. end
  246. def after_all
  247. @ran_filter ||= []
  248. @ran_filter << 'after_all'
  249. end
  250. def between_before_all_and_after_all
  251. @ran_filter ||= []
  252. @ran_filter << 'between_before_all_and_after_all'
  253. end
  254. def show
  255. render :text => 'hello'
  256. end
  257. end
  258. class ErrorToRescue < Exception; end
  259. class RescuingAroundFilterWithBlock
  260. def filter(controller)
  261. begin
  262. yield
  263. rescue ErrorToRescue => ex
  264. controller.send :render, :text => "I rescued this: #{ex.inspect}"
  265. end
  266. end
  267. end
  268. class RescuedController < ActionController::Base
  269. around_filter RescuingAroundFilterWithBlock.new
  270. def show
  271. raise ErrorToRescue.new("Something made the bad noise.")
  272. end
  273. private
  274. def rescue_action(exception)
  275. raise exception
  276. end
  277. end
  278. class NonYieldingAroundFilterController < ActionController::Base
  279. before_filter :filter_one
  280. around_filter :non_yielding_filter
  281. before_filter :filter_two
  282. after_filter :filter_three
  283. def index
  284. render :inline => "index"
  285. end
  286. #make sure the controller complains
  287. def rescue_action(e); raise e; end
  288. private
  289. def filter_one
  290. @filters ||= []
  291. @filters << "filter_one"
  292. end
  293. def filter_two
  294. @filters << "filter_two"
  295. end
  296. def non_yielding_filter
  297. @filters << "zomg it didn't yield"
  298. @filter_return_value
  299. end
  300. def filter_three
  301. @filters << "filter_three"
  302. end
  303. end
  304. def test_non_yielding_around_filters_not_returning_false_do_not_raise
  305. controller = NonYieldingAroundFilterController.new
  306. controller.instance_variable_set "@filter_return_value", true
  307. assert_nothing_raised do
  308. test_process(controller, "index")
  309. end
  310. end
  311. def test_non_yielding_around_filters_returning_false_do_not_raise
  312. controller = NonYieldingAroundFilterController.new
  313. controller.instance_variable_set "@filter_return_value", false
  314. assert_nothing_raised do
  315. test_process(controller, "index")
  316. end
  317. end
  318. def test_after_filters_are_not_run_if_around_filter_returns_false
  319. controller = NonYieldingAroundFilterController.new
  320. controller.instance_variable_set "@filter_return_value", false
  321. test_process(controller, "index")
  322. assert_equal ["filter_one", "zomg it didn't yield"], controller.assigns['filters']
  323. end
  324. def test_after_filters_are_not_run_if_around_filter_does_not_yield
  325. controller = NonYieldingAroundFilterController.new
  326. controller.instance_variable_set "@filter_return_value", true
  327. test_process(controller, "index")
  328. assert_equal ["filter_one", "zomg it didn't yield"], controller.assigns['filters']
  329. end
  330. def test_empty_filter_chain
  331. assert_equal 0, EmptyFilterChainController.filter_chain.size
  332. assert test_process(EmptyFilterChainController).template.assigns['action_executed']
  333. end
  334. def test_added_filter_to_inheritance_graph
  335. assert_equal [ :ensure_login ], TestController.before_filters
  336. end
  337. def test_base_class_in_isolation
  338. assert_equal [ ], ActionController::Base.before_filters
  339. end
  340. def test_prepending_filter
  341. assert_equal [ :wonderful_life, :ensure_login ], PrependingController.before_filters
  342. end
  343. def test_running_filters
  344. assert_equal %w( wonderful_life ensure_login ), test_process(PrependingController).template.assigns["ran_filter"]
  345. end
  346. def test_running_filters_with_proc
  347. assert test_process(ProcController).template.assigns["ran_proc_filter"]
  348. end
  349. def test_running_filters_with_implicit_proc
  350. assert test_process(ImplicitProcController).template.assigns["ran_proc_filter"]
  351. end
  352. def test_running_filters_with_class
  353. assert test_process(AuditController).template.assigns["was_audited"]
  354. end
  355. def test_running_anomolous_yet_valid_condition_filters
  356. response = test_process(AnomolousYetValidConditionController)
  357. assert_equal %w( ensure_login ), response.template.assigns["ran_filter"]
  358. assert response.template.assigns["ran_class_filter"]
  359. assert response.template.assigns["ran_proc_filter1"]
  360. assert response.template.assigns["ran_proc_filter2"]
  361. response = test_process(AnomolousYetValidConditionController, "show_without_filter")
  362. assert_equal nil, response.template.assigns["ran_filter"]
  363. assert !response.template.assigns["ran_class_filter"]
  364. assert !response.template.assigns["ran_proc_filter1"]
  365. assert !response.template.assigns["ran_proc_filter2"]
  366. end
  367. def test_running_collection_condition_filters
  368. assert_equal %w( ensure_login ), test_process(ConditionalCollectionFilterController).template.assigns["ran_filter"]
  369. assert_equal nil, test_process(ConditionalCollectionFilterController, "show_without_filter").template.assigns["ran_filter"]
  370. assert_equal nil, test_process(ConditionalCollectionFilterController, "another_action").template.assigns["ran_filter"]
  371. end
  372. def test_running_only_condition_filters
  373. assert_equal %w( ensure_login ), test_process(OnlyConditionSymController).template.assigns["ran_filter"]
  374. assert_equal nil, test_process(OnlyConditionSymController, "show_without_filter").template.assigns["ran_filter"]
  375. assert test_process(OnlyConditionProcController).template.assigns["ran_proc_filter"]
  376. assert !test_process(OnlyConditionProcController, "show_without_filter").template.assigns["ran_proc_filter"]
  377. assert test_process(OnlyConditionClassController).template.assigns["ran_class_filter"]
  378. assert !test_process(OnlyConditionClassController, "show_without_filter").template.assigns["ran_class_filter"]
  379. end
  380. def test_running_except_condition_filters
  381. assert_equal %w( ensure_login ), test_process(ExceptConditionSymController).template.assigns["ran_filter"]
  382. assert_equal nil, test_process(ExceptConditionSymController, "show_without_filter").template.assigns["ran_filter"]
  383. assert test_process(ExceptConditionProcController).template.assigns["ran_proc_filter"]
  384. assert !test_process(ExceptConditionProcController, "show_without_filter").template.assigns["ran_proc_filter"]
  385. assert test_process(ExceptConditionClassController).template.assigns["ran_class_filter"]
  386. assert !test_process(ExceptConditionClassController, "show_without_filter").template.assigns["ran_class_filter"]
  387. end
  388. def test_running_before_and_after_condition_filters
  389. assert_equal %w( ensure_login clean_up_tmp), test_process(BeforeAndAfterConditionController).template.assigns["ran_filter"]
  390. assert_equal nil, test_process(BeforeAndAfterConditionController, "show_without_filter").template.assigns["ran_filter"]
  391. end
  392. def test_bad_filter
  393. bad_filter_controller = Class.new(ActionController::Base)
  394. assert_raises(ActionController::ActionControllerError) do
  395. bad_filter_controller.before_filter 2
  396. end
  397. end
  398. def test_around_filter
  399. controller = test_process(AroundFilterController)
  400. assert controller.template.assigns["before_ran"]
  401. assert controller.template.assigns["after_ran"]
  402. end
  403. def test_before_after_class_filter
  404. controller = test_process(BeforeAfterClassFilterController)
  405. assert controller.template.assigns["before_ran"]
  406. assert controller.template.assigns["after_ran"]
  407. end
  408. def test_having_properties_in_around_filter
  409. controller = test_process(AroundFilterController)
  410. assert_equal "before and after", controller.template.assigns["execution_log"]
  411. end
  412. def test_prepending_and_appending_around_filter
  413. controller = test_process(MixedFilterController)
  414. assert_equal " before aroundfilter before procfilter before appended aroundfilter " +
  415. " after appended aroundfilter after aroundfilter after procfilter ",
  416. MixedFilterController.execution_log
  417. end
  418. def test_rendering_breaks_filtering_chain
  419. response = test_process(RenderingController)
  420. assert_equal "something else", response.body
  421. assert !response.template.assigns["ran_action"]
  422. end
  423. def test_filters_with_mixed_specialization_run_in_order
  424. assert_nothing_raised do
  425. response = test_process(MixedSpecializationController, 'bar')
  426. assert_equal 'bar', response.body
  427. end
  428. assert_nothing_raised do
  429. response = test_process(MixedSpecializationController, 'foo')
  430. assert_equal 'foo', response.body
  431. end
  432. end
  433. def test_dynamic_dispatch
  434. %w(foo bar baz).each do |action|
  435. request = ActionController::TestRequest.new
  436. request.query_parameters[:choose] = action
  437. response = DynamicDispatchController.process(request, ActionController::TestResponse.new)
  438. assert_equal action, response.body
  439. end
  440. end
  441. def test_running_prepended_before_and_after_filter
  442. assert_equal 3, PrependingBeforeAndAfterController.filter_chain.length
  443. response = test_process(PrependingBeforeAndAfterController)
  444. assert_equal %w( before_all between_before_all_and_after_all after_all ), response.template.assigns["ran_filter"]
  445. end
  446. def test_conditional_skipping_of_filters
  447. assert_nil test_process(ConditionalSkippingController, "login").template.assigns["ran_filter"]
  448. assert_equal %w( ensure_login find_user ), test_process(ConditionalSkippingController, "change_password").template.assigns["ran_filter"]
  449. assert_nil test_process(ConditionalSkippingController, "login").template.controller.instance_variable_get("@ran_after_filter")
  450. assert_equal %w( clean_up ), test_process(ConditionalSkippingController, "change_password").template.controller.instance_variable_get("@ran_after_filter")
  451. end
  452. def test_conditional_skipping_of_filters_when_parent_filter_is_also_conditional
  453. assert_equal %w( conditional_in_parent conditional_in_parent ), test_process(ChildOfConditionalParentController).template.assigns['ran_filter']
  454. assert_nil test_process(ChildOfConditionalParentController, 'another_action').template.assigns['ran_filter']
  455. end
  456. def test_condition_skipping_of_filters_when_siblings_also_have_conditions
  457. assert_equal %w( conditional_in_parent conditional_in_parent ), test_process(ChildOfConditionalParentController).template.assigns['ran_filter'], "1"
  458. assert_equal nil, test_process(AnotherChildOfConditionalParentController).template.assigns['ran_filter']
  459. assert_equal %w( conditional_in_parent conditional_in_parent ), test_process(ChildOfConditionalParentController).template.assigns['ran_filter']
  460. end
  461. def test_changing_the_requirements
  462. assert_equal nil, test_process(ChangingTheRequirementsController, "go_wild").template.assigns['ran_filter']
  463. end
  464. def test_a_rescuing_around_filter
  465. response = nil
  466. assert_nothing_raised do
  467. response = test_process(RescuedController)
  468. end
  469. assert response.success?
  470. assert_equal("I rescued this: #<FilterTest::ErrorToRescue: Something made the bad noise.>", response.body)
  471. end
  472. private
  473. def test_process(controller, action = "show")
  474. request = ActionController::TestRequest.new
  475. request.action = action
  476. controller.process(request, ActionController::TestResponse.new)
  477. end
  478. end
  479. class PostsController < ActionController::Base
  480. def rescue_action(e); raise e; end
  481. module AroundExceptions
  482. class Error < StandardError ; end
  483. class Before < Error ; end
  484. class After < Error ; end
  485. end
  486. include AroundExceptions
  487. class DefaultFilter
  488. include AroundExceptions
  489. end
  490. module_eval %w(raises_before raises_after raises_both no_raise no_filter).map { |action| "def #{action}; default_action end" }.join("\n")
  491. private
  492. def default_action
  493. render :inline => "#{action_name} called"
  494. end
  495. end
  496. class ControllerWithSymbolAsFilter < PostsController
  497. around_filter :raise_before, :only => :raises_before
  498. around_filter :raise_after, :only => :raises_after
  499. around_filter :without_exception, :only => :no_raise
  500. private
  501. def raise_before
  502. raise Before
  503. yield
  504. end
  505. def raise_after
  506. yield
  507. raise After
  508. end
  509. def without_exception
  510. # Do stuff...
  511. 1 + 1
  512. yield
  513. # Do stuff...
  514. 1 + 1
  515. end
  516. end
  517. class ControllerWithFilterClass < PostsController
  518. class YieldingFilter < DefaultFilter
  519. def self.filter(controller)
  520. yield
  521. raise After
  522. end
  523. end
  524. around_filter YieldingFilter, :only => :raises_after
  525. end
  526. class ControllerWithFilterInstance < PostsController
  527. class YieldingFilter < DefaultFilter
  528. def filter(controller)
  529. yield
  530. raise After
  531. end
  532. end
  533. around_filter YieldingFilter.new, :only => :raises_after
  534. end
  535. class ControllerWithFilterMethod < PostsController
  536. class YieldingFilter < DefaultFilter
  537. def filter(controller)
  538. yield
  539. raise After
  540. end
  541. end
  542. around_filter YieldingFilter.new.method(:filter), :only => :raises_after
  543. end
  544. class ControllerWithProcFilter < PostsController
  545. around_filter(:only => :no_raise) do |c,b|
  546. c.assigns['before'] = true
  547. b.call
  548. c.assigns['after'] = true
  549. end
  550. end
  551. class ControllerWithWrongFilterType < PostsController
  552. around_filter lambda { yield }, :only => :no_raise
  553. end
  554. class ControllerWithNestedFilters < ControllerWithSymbolAsFilter
  555. around_filter :raise_before, :raise_after, :without_exception, :only => :raises_both
  556. end
  557. class ControllerWithAllTypesOfFilters < PostsController
  558. before_filter :before
  559. around_filter :around
  560. after_filter :after
  561. around_filter :around_again
  562. private
  563. def before
  564. @ran_filter ||= []
  565. @ran_filter << 'before'
  566. end
  567. def around
  568. @ran_filter << 'around (before yield)'
  569. yield
  570. @ran_filter << 'around (after yield)'
  571. end
  572. def after
  573. @ran_filter << 'after'
  574. end
  575. def around_again
  576. @ran_filter << 'around_again (before yield)'
  577. yield
  578. @ran_filter << 'around_again (after yield)'
  579. end
  580. end
  581. class ControllerWithTwoLessFilters < ControllerWithAllTypesOfFilters
  582. skip_filter :around_again
  583. skip_filter :after
  584. end
  585. class YieldingAroundFiltersTest < Test::Unit::TestCase
  586. include PostsController::AroundExceptions
  587. def test_filters_registering
  588. assert_equal 1, ControllerWithFilterMethod.filter_chain.size
  589. assert_equal 1, ControllerWithFilterClass.filter_chain.size
  590. assert_equal 1, ControllerWithFilterInstance.filter_chain.size
  591. assert_equal 3, ControllerWithSymbolAsFilter.filter_chain.size
  592. assert_equal 1, ControllerWithWrongFilterType.filter_chain.size
  593. assert_equal 6, ControllerWithNestedFilters.filter_chain.size
  594. assert_equal 4, ControllerWithAllTypesOfFilters.filter_chain.size
  595. end
  596. def test_wrong_filter_type
  597. assert_raise(ActionController::ActionControllerError) do
  598. test_process(ControllerWithWrongFilterType,'no_raise')
  599. end
  600. end
  601. def test_base
  602. controller = PostsController
  603. assert_nothing_raised { test_process(controller,'no_raise') }
  604. assert_nothing_raised { test_process(controller,'raises_before') }
  605. assert_nothing_raised { test_process(controller,'raises_after') }
  606. assert_nothing_raised { test_process(controller,'no_filter') }
  607. end
  608. def test_with_symbol
  609. controller = ControllerWithSymbolAsFilter
  610. assert_nothing_raised { test_process(controller,'no_raise') }
  611. assert_raise(Before) { test_process(controller,'raises_before') }
  612. assert_raise(After) { test_process(controller,'raises_after') }
  613. assert_nothing_raised { test_process(controller,'no_raise') }
  614. end
  615. def test_with_class
  616. controller = ControllerWithFilterClass
  617. assert_nothing_raised { test_process(controller,'no_raise') }
  618. assert_raise(After) { test_process(controller,'raises_after') }
  619. end
  620. def test_with_instance
  621. controller = ControllerWithFilterInstance
  622. assert_nothing_raised { test_process(controller,'no_raise') }
  623. assert_raise(After) { test_process(controller,'raises_after') }
  624. end
  625. def test_with_method
  626. controller = ControllerWithFilterMethod
  627. assert_nothing_raised { test_process(controller,'no_raise') }
  628. assert_raise(After) { test_process(controller,'raises_after') }
  629. end
  630. def test_with_proc
  631. controller = test_process(ControllerWithProcFilter,'no_raise')
  632. assert controller.template.assigns['before']
  633. assert controller.template.assigns['after']
  634. end
  635. def test_nested_filters
  636. controller = ControllerWithNestedFilters
  637. assert_nothing_raised do
  638. begin
  639. test_process(controller,'raises_both')
  640. rescue Before, After
  641. end
  642. end
  643. assert_raise Before do
  644. begin
  645. test_process(controller,'raises_both')
  646. rescue After
  647. end
  648. end
  649. end
  650. def test_filter_order_with_all_filter_types
  651. controller = test_process(ControllerWithAllTypesOfFilters,'no_raise')
  652. assert_equal 'before around (before yield) around_again (before yield) around_again (after yield) around (after yield) after',controller.template.assigns['ran_filter'].join(' ')
  653. end
  654. def test_filter_order_with_skip_filter_method
  655. controller = test_process(ControllerWithTwoLessFilters,'no_raise')
  656. assert_equal 'before around (before yield) around (after yield)',controller.template.assigns['ran_filter'].join(' ')
  657. end
  658. def test_first_filter_in_multiple_before_filter_chain_halts
  659. controller = ::FilterTest::TestMultipleFiltersController.new
  660. response = test_process(controller, 'fail_1')
  661. assert_equal '', response.body
  662. assert_equal 1, controller.instance_variable_get(:@try)
  663. assert controller.instance_variable_get(:@before_filter_chain_aborted)
  664. end
  665. def test_second_filter_in_multiple_before_filter_chain_halts
  666. controller = ::FilterTest::TestMultipleFiltersController.new
  667. response = test_process(controller, 'fail_2')
  668. assert_equal '', response.body
  669. assert_equal 2, controller.instance_variable_get(:@try)
  670. assert controller.instance_variable_get(:@before_filter_chain_aborted)
  671. end
  672. def test_last_filter_in_multiple_before_filter_chain_halts
  673. controller = ::FilterTest::TestMultipleFiltersController.new
  674. response = test_process(controller, 'fail_3')
  675. assert_equal '', response.body
  676. assert_equal 3, controller.instance_variable_get(:@try)
  677. assert controller.instance_variable_get(:@before_filter_chain_aborted)
  678. end
  679. protected
  680. def test_process(controller, action = "show")
  681. request = ActionController::TestRequest.new
  682. request.action = action
  683. controller.process(request, ActionController::TestResponse.new)
  684. end
  685. end