PageRenderTime 64ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/ischroedi/petracommunity
Ruby | 227 lines | 177 code | 50 blank | 0 comment | 0 complexity | a1594d9f24dd1687ce39e104035d70eb MD5 | raw file
  1. require File.dirname(__FILE__) + '/../abstract_unit'
  2. class VerificationTest < Test::Unit::TestCase
  3. class TestController < ActionController::Base
  4. verify :only => :guarded_one, :params => "one",
  5. :add_flash => { :error => 'unguarded' },
  6. :redirect_to => { :action => "unguarded" }
  7. verify :only => :guarded_two, :params => %w( one two ),
  8. :redirect_to => { :action => "unguarded" }
  9. verify :only => :guarded_with_flash, :params => "one",
  10. :add_flash => { :notice => "prereqs failed" },
  11. :redirect_to => { :action => "unguarded" }
  12. verify :only => :guarded_in_session, :session => "one",
  13. :redirect_to => { :action => "unguarded" }
  14. verify :only => [:multi_one, :multi_two], :session => %w( one two ),
  15. :redirect_to => { :action => "unguarded" }
  16. verify :only => :guarded_by_method, :method => :post,
  17. :redirect_to => { :action => "unguarded" }
  18. verify :only => :guarded_by_xhr, :xhr => true,
  19. :redirect_to => { :action => "unguarded" }
  20. verify :only => :guarded_by_not_xhr, :xhr => false,
  21. :redirect_to => { :action => "unguarded" }
  22. before_filter :unconditional_redirect, :only => :two_redirects
  23. verify :only => :two_redirects, :method => :post,
  24. :redirect_to => { :action => "unguarded" }
  25. verify :only => :must_be_post, :method => :post, :render => { :status => 405, :text => "Must be post" }, :add_headers => { "Allow" => "POST" }
  26. def guarded_one
  27. render :text => "#{params[:one]}"
  28. end
  29. def guarded_with_flash
  30. render :text => "#{params[:one]}"
  31. end
  32. def guarded_two
  33. render :text => "#{params[:one]}:#{params[:two]}"
  34. end
  35. def guarded_in_session
  36. render :text => "#{session["one"]}"
  37. end
  38. def multi_one
  39. render :text => "#{session["one"]}:#{session["two"]}"
  40. end
  41. def multi_two
  42. render :text => "#{session["two"]}:#{session["one"]}"
  43. end
  44. def guarded_by_method
  45. render :text => "#{request.method}"
  46. end
  47. def guarded_by_xhr
  48. render :text => "#{request.xhr?}"
  49. end
  50. def guarded_by_not_xhr
  51. render :text => "#{request.xhr?}"
  52. end
  53. def unguarded
  54. render :text => "#{params[:one]}"
  55. end
  56. def two_redirects
  57. render :nothing => true
  58. end
  59. def must_be_post
  60. render :text => "Was a post!"
  61. end
  62. protected
  63. def rescue_action(e) raise end
  64. def unconditional_redirect
  65. redirect_to :action => "unguarded"
  66. end
  67. end
  68. def setup
  69. @controller = TestController.new
  70. @request = ActionController::TestRequest.new
  71. @response = ActionController::TestResponse.new
  72. end
  73. def test_guarded_one_with_prereqs
  74. get :guarded_one, :one => "here"
  75. assert_equal "here", @response.body
  76. end
  77. def test_guarded_one_without_prereqs
  78. get :guarded_one
  79. assert_redirected_to :action => "unguarded"
  80. assert_equal 'unguarded', flash[:error]
  81. end
  82. def test_guarded_with_flash_with_prereqs
  83. get :guarded_with_flash, :one => "here"
  84. assert_equal "here", @response.body
  85. assert flash.empty?
  86. end
  87. def test_guarded_with_flash_without_prereqs
  88. get :guarded_with_flash
  89. assert_redirected_to :action => "unguarded"
  90. assert_equal "prereqs failed", flash[:notice]
  91. end
  92. def test_guarded_two_with_prereqs
  93. get :guarded_two, :one => "here", :two => "there"
  94. assert_equal "here:there", @response.body
  95. end
  96. def test_guarded_two_without_prereqs_one
  97. get :guarded_two, :two => "there"
  98. assert_redirected_to :action => "unguarded"
  99. end
  100. def test_guarded_two_without_prereqs_two
  101. get :guarded_two, :one => "here"
  102. assert_redirected_to :action => "unguarded"
  103. end
  104. def test_guarded_two_without_prereqs_both
  105. get :guarded_two
  106. assert_redirected_to :action => "unguarded"
  107. end
  108. def test_unguarded_with_params
  109. get :unguarded, :one => "here"
  110. assert_equal "here", @response.body
  111. end
  112. def test_unguarded_without_params
  113. get :unguarded
  114. assert_equal "", @response.body
  115. end
  116. def test_guarded_in_session_with_prereqs
  117. get :guarded_in_session, {}, "one" => "here"
  118. assert_equal "here", @response.body
  119. end
  120. def test_guarded_in_session_without_prereqs
  121. get :guarded_in_session
  122. assert_redirected_to :action => "unguarded"
  123. end
  124. def test_multi_one_with_prereqs
  125. get :multi_one, {}, "one" => "here", "two" => "there"
  126. assert_equal "here:there", @response.body
  127. end
  128. def test_multi_one_without_prereqs
  129. get :multi_one
  130. assert_redirected_to :action => "unguarded"
  131. end
  132. def test_multi_two_with_prereqs
  133. get :multi_two, {}, "one" => "here", "two" => "there"
  134. assert_equal "there:here", @response.body
  135. end
  136. def test_multi_two_without_prereqs
  137. get :multi_two
  138. assert_redirected_to :action => "unguarded"
  139. end
  140. def test_guarded_by_method_with_prereqs
  141. post :guarded_by_method
  142. assert_equal "post", @response.body
  143. end
  144. def test_guarded_by_method_without_prereqs
  145. get :guarded_by_method
  146. assert_redirected_to :action => "unguarded"
  147. end
  148. def test_guarded_by_xhr_with_prereqs
  149. xhr :post, :guarded_by_xhr
  150. assert_equal "true", @response.body
  151. end
  152. def test_guarded_by_xhr_without_prereqs
  153. get :guarded_by_xhr
  154. assert_redirected_to :action => "unguarded"
  155. end
  156. def test_guarded_by_not_xhr_with_prereqs
  157. get :guarded_by_not_xhr
  158. assert_equal "false", @response.body
  159. end
  160. def test_guarded_by_not_xhr_without_prereqs
  161. xhr :post, :guarded_by_not_xhr
  162. assert_redirected_to :action => "unguarded"
  163. end
  164. def test_guarded_post_and_calls_render_succeeds
  165. post :must_be_post
  166. assert_equal "Was a post!", @response.body
  167. end
  168. def test_guarded_post_and_calls_render_fails_and_sets_allow_header
  169. get :must_be_post
  170. assert_response 405
  171. assert_equal "Must be post", @response.body
  172. assert_equal "POST", @response.headers["Allow"]
  173. end
  174. def test_second_redirect
  175. assert_nothing_raised { get :two_redirects }
  176. end
  177. end