1 require File.dirname(__FILE__) + '/../abstract_unit'
3 class VerificationTest < Test::Unit::TestCase
4 class TestController < ActionController::Base
5 verify :only => :guarded_one, :params => "one",
6 :add_flash => { :error => 'unguarded' },
7 :redirect_to => { :action => "unguarded" }
9 verify :only => :guarded_two, :params => %w( one two ),
10 :redirect_to => { :action => "unguarded" }
12 verify :only => :guarded_with_flash, :params => "one",
13 :add_flash => { :notice => "prereqs failed" },
14 :redirect_to => { :action => "unguarded" }
16 verify :only => :guarded_in_session, :session => "one",
17 :redirect_to => { :action => "unguarded" }
19 verify :only => [:multi_one, :multi_two], :session => %w( one two ),
20 :redirect_to => { :action => "unguarded" }
22 verify :only => :guarded_by_method, :method => :post,
23 :redirect_to => { :action => "unguarded" }
25 verify :only => :guarded_by_xhr, :xhr => true,
26 :redirect_to => { :action => "unguarded" }
28 verify :only => :guarded_by_not_xhr, :xhr => false,
29 :redirect_to => { :action => "unguarded" }
31 before_filter :unconditional_redirect, :only => :two_redirects
32 verify :only => :two_redirects, :method => :post,
33 :redirect_to => { :action => "unguarded" }
35 verify :only => :must_be_post, :method => :post, :render => { :status => 405, :text => "Must be post" }, :add_headers => { "Allow" => "POST" }
37 verify :only => :guarded_one_for_named_route_test, :params => "one",
38 :redirect_to => :foo_url
41 render :text => "#{params[:one]}"
44 def guarded_one_for_named_route_test
45 render :text => "#{params[:one]}"
48 def guarded_with_flash
49 render :text => "#{params[:one]}"
53 render :text => "#{params[:one]}:#{params[:two]}"
56 def guarded_in_session
57 render :text => "#{session["one"]}"
61 render :text => "#{session["one"]}:#{session["two"]}"
65 render :text => "#{session["two"]}:#{session["one"]}"
69 render :text => "#{request.method}"
73 render :text => "#{request.xhr?}"
76 def guarded_by_not_xhr
77 render :text => "#{request.xhr?}"
81 render :text => "#{params[:one]}"
85 render :nothing => true
89 render :text => "Was a post!"
93 def rescue_action(e) raise end
95 def unconditional_redirect
96 redirect_to :action => "unguarded"
101 @controller = TestController.new
102 @request = ActionController::TestRequest.new
103 @response = ActionController::TestResponse.new
104 ActionController::Routing::Routes.add_named_route :foo, '/foo', :controller => 'test', :action => 'foo'
107 def test_no_deprecation_warning_for_named_route
108 assert_not_deprecated do
109 get :guarded_one_for_named_route_test, :two => "not one"
110 assert_redirected_to '/foo'
114 def test_guarded_one_with_prereqs
115 get :guarded_one, :one => "here"
116 assert_equal "here", @response.body
119 def test_guarded_one_without_prereqs
121 assert_redirected_to :action => "unguarded"
122 assert_equal 'unguarded', flash[:error]
125 def test_guarded_with_flash_with_prereqs
126 get :guarded_with_flash, :one => "here"
127 assert_equal "here", @response.body
131 def test_guarded_with_flash_without_prereqs
132 get :guarded_with_flash
133 assert_redirected_to :action => "unguarded"
134 assert_equal "prereqs failed", flash[:notice]
137 def test_guarded_two_with_prereqs
138 get :guarded_two, :one => "here", :two => "there"
139 assert_equal "here:there", @response.body
142 def test_guarded_two_without_prereqs_one
143 get :guarded_two, :two => "there"
144 assert_redirected_to :action => "unguarded"
147 def test_guarded_two_without_prereqs_two
148 get :guarded_two, :one => "here"
149 assert_redirected_to :action => "unguarded"
152 def test_guarded_two_without_prereqs_both
154 assert_redirected_to :action => "unguarded"
157 def test_unguarded_with_params
158 get :unguarded, :one => "here"
159 assert_equal "here", @response.body
162 def test_unguarded_without_params
164 assert_equal "", @response.body
167 def test_guarded_in_session_with_prereqs
168 get :guarded_in_session, {}, "one" => "here"
169 assert_equal "here", @response.body
172 def test_guarded_in_session_without_prereqs
173 get :guarded_in_session
174 assert_redirected_to :action => "unguarded"
177 def test_multi_one_with_prereqs
178 get :multi_one, {}, "one" => "here", "two" => "there"
179 assert_equal "here:there", @response.body
182 def test_multi_one_without_prereqs
184 assert_redirected_to :action => "unguarded"
187 def test_multi_two_with_prereqs
188 get :multi_two, {}, "one" => "here", "two" => "there"
189 assert_equal "there:here", @response.body
192 def test_multi_two_without_prereqs
194 assert_redirected_to :action => "unguarded"
197 def test_guarded_by_method_with_prereqs
198 post :guarded_by_method
199 assert_equal "post", @response.body
202 def test_guarded_by_method_without_prereqs
203 get :guarded_by_method
204 assert_redirected_to :action => "unguarded"
207 def test_guarded_by_xhr_with_prereqs
208 xhr :post, :guarded_by_xhr
209 assert_equal "true", @response.body
212 def test_guarded_by_xhr_without_prereqs
214 assert_redirected_to :action => "unguarded"
217 def test_guarded_by_not_xhr_with_prereqs
218 get :guarded_by_not_xhr
219 assert_equal "false", @response.body
222 def test_guarded_by_not_xhr_without_prereqs
223 xhr :post, :guarded_by_not_xhr
224 assert_redirected_to :action => "unguarded"
227 def test_guarded_post_and_calls_render_succeeds
229 assert_equal "Was a post!", @response.body
232 def test_guarded_post_and_calls_render_fails_and_sets_allow_header
235 assert_equal "Must be post", @response.body
236 assert_equal "POST", @response.headers["Allow"]
239 def test_second_redirect
240 assert_nothing_raised { get :two_redirects }