1
require 'abstract_unit'
3
class VerificationTest < ActionController::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
40
verify :only => :no_default_action, :params => "santa"
42
verify :only => :guarded_with_back, :method => :post,
46
render :text => "#{params[:one]}"
49
def guarded_one_for_named_route_test
50
render :text => "#{params[:one]}"
53
def guarded_with_flash
54
render :text => "#{params[:one]}"
58
render :text => "#{params[:one]}:#{params[:two]}"
61
def guarded_in_session
62
render :text => "#{session["one"]}"
66
render :text => "#{session["one"]}:#{session["two"]}"
70
render :text => "#{session["two"]}:#{session["one"]}"
74
render :text => "#{request.method}"
78
render :text => "#{request.xhr?}"
81
def guarded_by_not_xhr
82
render :text => "#{request.xhr?}"
86
render :text => "#{params[:one]}"
90
render :nothing => true
94
render :text => "Was a post!"
98
render :text => "#{params[:one]}"
101
def no_default_action
106
def rescue_action(e) raise end
108
def unconditional_redirect
109
redirect_to :action => "unguarded"
114
@controller = TestController.new
115
@request = ActionController::TestRequest.new
116
@response = ActionController::TestResponse.new
117
ActionController::Routing::Routes.add_named_route :foo, '/foo', :controller => 'test', :action => 'foo'
120
def test_using_symbol_back_with_no_referrer
121
assert_raise(ActionController::RedirectBackError) { get :guarded_with_back }
124
def test_using_symbol_back_redirects_to_referrer
125
@request.env["HTTP_REFERER"] = "/foo"
126
get :guarded_with_back
127
assert_redirected_to '/foo'
130
def test_no_deprecation_warning_for_named_route
131
assert_not_deprecated do
132
get :guarded_one_for_named_route_test, :two => "not one"
133
assert_redirected_to '/foo'
137
def test_guarded_one_with_prereqs
138
get :guarded_one, :one => "here"
139
assert_equal "here", @response.body
142
def test_guarded_one_without_prereqs
144
assert_redirected_to :action => "unguarded"
145
assert_equal 'unguarded', flash[:error]
148
def test_guarded_with_flash_with_prereqs
149
get :guarded_with_flash, :one => "here"
150
assert_equal "here", @response.body
154
def test_guarded_with_flash_without_prereqs
155
get :guarded_with_flash
156
assert_redirected_to :action => "unguarded"
157
assert_equal "prereqs failed", flash[:notice]
160
def test_guarded_two_with_prereqs
161
get :guarded_two, :one => "here", :two => "there"
162
assert_equal "here:there", @response.body
165
def test_guarded_two_without_prereqs_one
166
get :guarded_two, :two => "there"
167
assert_redirected_to :action => "unguarded"
170
def test_guarded_two_without_prereqs_two
171
get :guarded_two, :one => "here"
172
assert_redirected_to :action => "unguarded"
175
def test_guarded_two_without_prereqs_both
177
assert_redirected_to :action => "unguarded"
180
def test_unguarded_with_params
181
get :unguarded, :one => "here"
182
assert_equal "here", @response.body
185
def test_unguarded_without_params
187
assert_equal "", @response.body
190
def test_guarded_in_session_with_prereqs
191
get :guarded_in_session, {}, "one" => "here"
192
assert_equal "here", @response.body
195
def test_guarded_in_session_without_prereqs
196
get :guarded_in_session
197
assert_redirected_to :action => "unguarded"
200
def test_multi_one_with_prereqs
201
get :multi_one, {}, "one" => "here", "two" => "there"
202
assert_equal "here:there", @response.body
205
def test_multi_one_without_prereqs
207
assert_redirected_to :action => "unguarded"
210
def test_multi_two_with_prereqs
211
get :multi_two, {}, "one" => "here", "two" => "there"
212
assert_equal "there:here", @response.body
215
def test_multi_two_without_prereqs
217
assert_redirected_to :action => "unguarded"
220
def test_guarded_by_method_with_prereqs
221
post :guarded_by_method
222
assert_equal "post", @response.body
225
def test_guarded_by_method_without_prereqs
226
get :guarded_by_method
227
assert_redirected_to :action => "unguarded"
230
def test_guarded_by_xhr_with_prereqs
231
xhr :post, :guarded_by_xhr
232
assert_equal "true", @response.body
235
def test_guarded_by_xhr_without_prereqs
237
assert_redirected_to :action => "unguarded"
240
def test_guarded_by_not_xhr_with_prereqs
241
get :guarded_by_not_xhr
242
assert_equal "false", @response.body
245
def test_guarded_by_not_xhr_without_prereqs
246
xhr :post, :guarded_by_not_xhr
247
assert_redirected_to :action => "unguarded"
250
def test_guarded_post_and_calls_render_succeeds
252
assert_equal "Was a post!", @response.body
255
def test_default_failure_should_be_a_bad_request
256
post :no_default_action
257
assert_response :bad_request
260
def test_guarded_post_and_calls_render_fails_and_sets_allow_header
263
assert_equal "Must be post", @response.body
264
assert_equal "POST", @response.headers["Allow"]
267
def test_second_redirect
268
assert_nothing_raised { get :two_redirects }