1 require 'abstract_unit'
3 class ResourcesController
< ActionController
::Base
4 def index() render
:nothing => true end
5 alias_method
:show, :index
6 def rescue_action(e
) raise e
end
9 class ThreadsController
< ResourcesController
; end
10 class MessagesController
< ResourcesController
; end
11 class CommentsController
< ResourcesController
; end
12 class AuthorsController
< ResourcesController
; end
13 class LogosController
< ResourcesController
; end
15 class AccountsController
< ResourcesController
; end
16 class AdminController
< ResourcesController
; end
17 class ProductsController
< ResourcesController
; end
18 class ImagesController
< ResourcesController
; end
21 class ProductsController
< ResourcesController
; end
22 class TagsController
< ResourcesController
; end
23 class ManufacturersController
< ResourcesController
; end
24 class ImagesController
< ResourcesController
; end
27 class ProductsController
< ResourcesController
; end
28 class ImagesController
< ResourcesController
; end
32 class ResourcesTest
< Test
::Unit::TestCase
33 # The assertions in these tests are incompatible with the hash method
34 # optimisation. This could indicate user level problems
36 ActionController
::Base.optimise_named_routes
= false
40 ActionController
::Base.optimise_named_routes
= true
43 def test_should_arrange_actions
44 resource
= ActionController
::Resources::Resource.new(:messages,
45 :collection => { :rss => :get, :reorder => :post, :csv => :post },
46 :member => { :rss => :get, :atom => :get, :upload => :post, :fix => :post },
47 :new => { :preview => :get, :draft => :get })
49 assert_resource_methods
[:rss], resource
, :collection, :get
50 assert_resource_methods
[:csv, :reorder], resource
, :collection, :post
51 assert_resource_methods
[:edit, :rss, :atom], resource
, :member, :get
52 assert_resource_methods
[:upload, :fix], resource
, :member, :post
53 assert_resource_methods
[:new, :preview, :draft], resource
, :new, :get
56 def test_should_resource_controller_name_equal_resource_name_by_default
57 resource
= ActionController
::Resources::Resource.new(:messages, {})
58 assert_equal
'messages', resource
.controller
61 def test_should_resource_controller_name_equal_controller_option
62 resource
= ActionController
::Resources::Resource.new(:messages, :controller => 'posts')
63 assert_equal
'posts', resource
.controller
66 def test_should_all_singleton_paths_be_the_same
67 [ :path, :nesting_path_prefix, :member_path ].each
do |method
|
68 resource
= ActionController
::Resources::SingletonResource.new(:messages, :path_prefix => 'admin')
69 assert_equal
'admin/messages', resource
.send(method
)
73 def test_default_restful_routes
74 with_restful_routing
:messages do
75 assert_simply_restful_for
:messages
79 def test_override_paths_for_default_restful_actions
80 resource
= ActionController
::Resources::Resource.new(:messages,
81 :path_names => {:new => 'nuevo', :edit => 'editar'})
82 assert_equal resource
.new_path
, "#{resource.path}/nuevo"
85 def test_multiple_default_restful_routes
86 with_restful_routing
:messages, :comments do
87 assert_simply_restful_for
:messages
88 assert_simply_restful_for
:comments
92 def test_with_custom_conditions
93 with_restful_routing
:messages, :conditions => { :subdomain => 'app' } do
94 assert_equal
'app', ActionController
::Routing::Routes.named_routes
.routes
[:messages].conditions
[:subdomain]
98 def test_irregular_id_with_no_requirements_should_raise_error
99 expected_options
= {:controller => 'messages', :action => 'show', :id => '1.1.1'}
101 with_restful_routing
:messages do
102 assert_raises(ActionController
::RoutingError) do
103 assert_recognizes(expected_options
, :path => 'messages/1.1.1', :method => :get)
108 def test_irregular_id_with_requirements_should_pass
109 expected_options
= {:controller => 'messages', :action => 'show', :id => '1.1.1'}
111 with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}) do
112 assert_recognizes(expected_options
, :path => 'messages/1.1.1', :method => :get)
116 def test_with_path_prefix_requirements
117 expected_options
= {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
118 with_restful_routing
:messages, :path_prefix => '/thread/:thread_id', :requirements => {:thread_id => /[0-9]\.[0-9]\.[0-9]/} do
119 assert_recognizes(expected_options
, :path => 'thread/1.1.1/messages/1', :method => :get)
123 def test_with_path_prefix
124 with_restful_routing
:messages, :path_prefix => '/thread/:thread_id' do
125 assert_simply_restful_for
:messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
129 def test_multiple_with_path_prefix
130 with_restful_routing
:messages, :comments, :path_prefix => '/thread/:thread_id' do
131 assert_simply_restful_for
:messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
132 assert_simply_restful_for
:comments, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
136 def test_with_name_prefix
137 with_restful_routing
:messages, :name_prefix => 'post_' do
138 assert_simply_restful_for
:messages, :name_prefix => 'post_'
142 def test_with_collection_actions
143 actions
= { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
145 with_restful_routing
:messages, :collection => actions
do
146 assert_restful_routes_for
:messages do |options
|
147 actions
.each
do |action
, method
|
148 assert_recognizes(options
.merge(:action => action
), :path => "/messages/#{action}", :method => method
)
152 assert_restful_named_routes_for
:messages do |options
|
153 actions
.keys
.each
do |action
|
154 assert_named_route
"/messages/#{action}", "#{action}_messages_path", :action => action
160 def test_with_collection_actions_and_name_prefix
161 actions
= { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
163 with_restful_routing
:messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions
do
164 assert_restful_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
165 actions
.each
do |action
, method
|
166 assert_recognizes(options
.merge(:action => action
), :path => "/threads/1/messages/#{action}", :method => method
)
170 assert_restful_named_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
171 actions
.keys
.each
do |action
|
172 assert_named_route
"/threads/1/messages/#{action}", "#{action}_thread_messages_path", :action => action
178 def test_with_collection_action_and_name_prefix_and_formatted
179 actions
= { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
181 with_restful_routing
:messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions
do
182 assert_restful_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
183 actions
.each
do |action
, method
|
184 assert_recognizes(options
.merge(:action => action
, :format => 'xml'), :path => "/threads/1/messages/#{action}.xml", :method => method
)
188 assert_restful_named_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
189 actions
.keys
.each
do |action
|
190 assert_named_route
"/threads/1/messages/#{action}.xml", "formatted_#{action}_thread_messages_path", :action => action
, :format => 'xml'
196 def test_with_member_action
197 [:put, :post].each
do |method
|
198 with_restful_routing
:messages, :member => { :mark => method
} do
199 mark_options
= {:action => 'mark', :id => '1'}
200 mark_path
= "/messages/1/mark"
201 assert_restful_routes_for
:messages do |options
|
202 assert_recognizes(options
.merge(mark_options
), :path => mark_path
, :method => method
)
205 assert_restful_named_routes_for
:messages do |options
|
206 assert_named_route mark_path
, :mark_message_path, mark_options
212 def test_member_when_override_paths_for_default_restful_actions_with
213 [:put, :post].each
do |method
|
214 with_restful_routing
:messages, :member => { :mark => method
}, :path_names => {:new => 'nuevo'} do
215 mark_options
= {:action => 'mark', :id => '1', :controller => "messages"}
216 mark_path
= "/messages/1/mark"
218 assert_restful_routes_for
:messages, :path_names => {:new => 'nuevo'} do |options
|
219 assert_recognizes(options
.merge(mark_options
), :path => mark_path
, :method => method
)
222 assert_restful_named_routes_for
:messages, :path_names => {:new => 'nuevo'} do |options
|
223 assert_named_route mark_path
, :mark_message_path, mark_options
229 def test_member_when_changed_default_restful_actions_and_path_names_not_specified
230 default_path_names
= ActionController
::Base.resources_path_names
231 ActionController
::Base.resources_path_names
= {:new => 'nuevo', :edit => 'editar'}
233 with_restful_routing
:messages do
234 new_options
= { :action => 'new', :controller => 'messages' }
235 new_path
= "/messages/nuevo"
236 edit_options
= { :action => 'edit', :id => '1', :controller => 'messages' }
237 edit_path
= "/messages/1/editar"
239 assert_restful_routes_for
:messages do |options
|
240 assert_recognizes(options
.merge(new_options
), :path => new_path
, :method => :get)
243 assert_restful_routes_for
:messages do |options
|
244 assert_recognizes(options
.merge(edit_options
), :path => edit_path
, :method => :get)
248 ActionController
::Base.resources_path_names
= default_path_names
251 def test_with_two_member_actions_with_same_method
252 [:put, :post].each
do |method
|
253 with_restful_routing
:messages, :member => { :mark => method
, :unmark => method
} do
254 %w(mark unmark
).each
do |action
|
255 action_options
= {:action => action
, :id => '1'}
256 action_path
= "/messages/1/#{action}"
257 assert_restful_routes_for
:messages do |options
|
258 assert_recognizes(options
.merge(action_options
), :path => action_path
, :method => method
)
261 assert_restful_named_routes_for
:messages do |options
|
262 assert_named_route action_path
, "#{action}_message_path".to_sym
, action_options
269 def test_array_as_collection_or_member_method_value
270 with_restful_routing
:messages, :collection => { :search => [:get, :post] }, :member => { :toggle => [:get, :post] } do
271 assert_restful_routes_for
:messages do |options
|
272 [:get, :post].each
do |method
|
273 assert_recognizes(options
.merge(:action => 'search'), :path => "/messages/search", :method => method
)
275 [:get, :post].each
do |method
|
276 assert_recognizes(options
.merge(:action => 'toggle', :id => '1'), :path => '/messages/1/toggle', :method => method
)
282 def test_with_new_action
283 with_restful_routing
:messages, :new => { :preview => :post } do
284 preview_options
= {:action => 'preview'}
285 preview_path
= "/messages/new/preview"
286 assert_restful_routes_for
:messages do |options
|
287 assert_recognizes(options
.merge(preview_options
), :path => preview_path
, :method => :post)
290 assert_restful_named_routes_for
:messages do |options
|
291 assert_named_route preview_path
, :preview_new_message_path, preview_options
296 def test_with_new_action_with_name_prefix
297 with_restful_routing
:messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
298 preview_options
= {:action => 'preview', :thread_id => '1'}
299 preview_path
= "/threads/1/messages/new/preview"
300 assert_restful_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
301 assert_recognizes(options
.merge(preview_options
), :path => preview_path
, :method => :post)
304 assert_restful_named_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
305 assert_named_route preview_path
, :preview_new_thread_message_path, preview_options
310 def test_with_formatted_new_action_with_name_prefix
311 with_restful_routing
:messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
312 preview_options
= {:action => 'preview', :thread_id => '1', :format => 'xml'}
313 preview_path
= "/threads/1/messages/new/preview.xml"
314 assert_restful_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
315 assert_recognizes(options
.merge(preview_options
), :path => preview_path
, :method => :post)
318 assert_restful_named_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
319 assert_named_route preview_path
, :formatted_preview_new_thread_message_path, preview_options
324 def test_override_new_method
325 with_restful_routing
:messages do
326 assert_restful_routes_for
:messages do |options
|
327 assert_recognizes(options
.merge(:action => "new"), :path => "/messages/new", :method => :get)
328 assert_raises(ActionController
::MethodNotAllowed) do
329 ActionController
::Routing::Routes.recognize_path("/messages/new", :method => :post)
334 with_restful_routing
:messages, :new => { :new => :any } do
335 assert_restful_routes_for
:messages do |options
|
336 assert_recognizes(options
.merge(:action => "new"), :path => "/messages/new", :method => :post)
337 assert_recognizes(options
.merge(:action => "new"), :path => "/messages/new", :method => :get)
342 def test_nested_restful_routes
343 with_routing
do |set
|
345 map
.resources
:threads do |map
|
346 map
.resources
:messages do |map
|
347 map
.resources
:comments
352 assert_simply_restful_for
:threads
353 assert_simply_restful_for
:messages,
354 :name_prefix => 'thread_',
355 :path_prefix => 'threads/1/',
356 :options => { :thread_id => '1' }
357 assert_simply_restful_for
:comments,
358 :name_prefix => 'thread_message_',
359 :path_prefix => 'threads/1/messages/2/',
360 :options => { :thread_id => '1', :message_id => '2' }
364 def test_nested_restful_routes_with_overwritten_defaults
365 with_routing
do |set
|
367 map
.resources
:threads do |map
|
368 map
.resources
:messages, :name_prefix => nil do |map
|
369 map
.resources
:comments, :name_prefix => nil
374 assert_simply_restful_for
:threads
375 assert_simply_restful_for
:messages,
376 :path_prefix => 'threads/1/',
377 :options => { :thread_id => '1' }
378 assert_simply_restful_for
:comments,
379 :path_prefix => 'threads/1/messages/2/',
380 :options => { :thread_id => '1', :message_id => '2' }
384 def test_shallow_nested_restful_routes
385 with_routing
do |set
|
387 map
.resources
:threads, :shallow => true do |map
|
388 map
.resources
:messages do |map
|
389 map
.resources
:comments
394 assert_simply_restful_for
:threads,
396 assert_simply_restful_for
:messages,
397 :name_prefix => 'thread_',
398 :path_prefix => 'threads/1/',
400 :options => { :thread_id => '1' }
401 assert_simply_restful_for
:comments,
402 :name_prefix => 'message_',
403 :path_prefix => 'messages/2/',
405 :options => { :message_id => '2' }
409 def test_restful_routes_dont_generate_duplicates
410 with_restful_routing
:messages do
411 routes
= ActionController
::Routing::Routes.routes
412 routes
.each
do |route
|
414 next if route
=== r
# skip the comparison instance
415 assert distinct_routes
?(route
, r
), "Duplicate Route: #{route}"
421 def test_should_create_singleton_resource_routes
422 with_singleton_resources
:account do
423 assert_singleton_restful_for
:account
427 def test_should_create_multiple_singleton_resource_routes
428 with_singleton_resources
:account, :logo do
429 assert_singleton_restful_for
:account
430 assert_singleton_restful_for
:logo
434 def test_should_create_nested_singleton_resource_routes
435 with_routing
do |set
|
437 map
.resource
:admin, :controller => 'admin' do |admin
|
438 admin
.resource
:account
442 assert_singleton_restful_for
:admin, :controller => 'admin'
443 assert_singleton_restful_for
:account, :name_prefix => "admin_", :path_prefix => 'admin/'
447 def test_resource_has_many_should_become_nested_resources
448 with_routing
do |set
|
450 map
.resources
:messages, :has_many => [ :comments, :authors ]
453 assert_simply_restful_for
:messages
454 assert_simply_restful_for
:comments, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
455 assert_simply_restful_for
:authors, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
459 def test_resources_has_many_hash_should_become_nested_resources
460 with_routing
do |set
|
462 map
.resources
:threads, :has_many => { :messages => [ :comments, { :authors => :threads } ] }
465 assert_simply_restful_for
:threads
466 assert_simply_restful_for
:messages, :name_prefix => "thread_", :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
467 assert_simply_restful_for
:comments, :name_prefix => "thread_message_", :path_prefix => 'threads/1/messages/1/', :options => { :thread_id => '1', :message_id => '1' }
468 assert_simply_restful_for
:authors, :name_prefix => "thread_message_", :path_prefix => 'threads/1/messages/1/', :options => { :thread_id => '1', :message_id => '1' }
469 assert_simply_restful_for
:threads, :name_prefix => "thread_message_author_", :path_prefix => 'threads/1/messages/1/authors/1/', :options => { :thread_id => '1', :message_id => '1', :author_id => '1' }
473 def test_shallow_resource_has_many_should_become_shallow_nested_resources
474 with_routing
do |set
|
476 map
.resources
:messages, :has_many => [ :comments, :authors ], :shallow => true
479 assert_simply_restful_for
:messages, :shallow => true
480 assert_simply_restful_for
:comments, :name_prefix => "message_", :path_prefix => 'messages/1/', :shallow => true, :options => { :message_id => '1' }
481 assert_simply_restful_for
:authors, :name_prefix => "message_", :path_prefix => 'messages/1/', :shallow => true, :options => { :message_id => '1' }
485 def test_resource_has_one_should_become_nested_resources
486 with_routing
do |set
|
488 map
.resources
:messages, :has_one => :logo
491 assert_simply_restful_for
:messages
492 assert_singleton_restful_for
:logo, :name_prefix => 'message_', :path_prefix => 'messages/1/', :options => { :message_id => '1' }
496 def test_shallow_resource_has_one_should_become_shallow_nested_resources
497 with_routing
do |set
|
499 map
.resources
:messages, :has_one => :logo, :shallow => true
502 assert_simply_restful_for
:messages, :shallow => true
503 assert_singleton_restful_for
:logo, :name_prefix => 'message_', :path_prefix => 'messages/1/', :shallow => true, :options => { :message_id => '1' }
507 def test_singleton_resource_with_member_action
508 [:put, :post].each
do |method
|
509 with_singleton_resources
:account, :member => { :reset => method
} do
510 reset_options
= {:action => 'reset'}
511 reset_path
= "/account/reset"
512 assert_singleton_routes_for
:account do |options
|
513 assert_recognizes(options
.merge(reset_options
), :path => reset_path
, :method => method
)
516 assert_singleton_named_routes_for
:account do |options
|
517 assert_named_route reset_path
, :reset_account_path, reset_options
523 def test_singleton_resource_with_two_member_actions_with_same_method
524 [:put, :post].each
do |method
|
525 with_singleton_resources
:account, :member => { :reset => method
, :disable => method
} do
526 %w(reset disable
).each
do |action
|
527 action_options
= {:action => action
}
528 action_path
= "/account/#{action}"
529 assert_singleton_routes_for
:account do |options
|
530 assert_recognizes(options
.merge(action_options
), :path => action_path
, :method => method
)
533 assert_singleton_named_routes_for
:account do |options
|
534 assert_named_route action_path
, "#{action}_account_path".to_sym
, action_options
541 def test_should_nest_resources_in_singleton_resource
542 with_routing
do |set
|
544 map
.resource
:account do |account
|
545 account
.resources
:messages
549 assert_singleton_restful_for
:account
550 assert_simply_restful_for
:messages, :name_prefix => "account_", :path_prefix => 'account/'
554 def test_should_nest_resources_in_singleton_resource_with_path_prefix
555 with_routing
do |set
|
557 map
.resource(:account, :path_prefix => ':site_id') do |account
|
558 account
.resources
:messages
562 assert_singleton_restful_for
:account, :path_prefix => '7/', :options => { :site_id => '7' }
563 assert_simply_restful_for
:messages, :name_prefix => "account_", :path_prefix => '7/account/', :options => { :site_id => '7' }
567 def test_should_nest_singleton_resource_in_resources
568 with_routing
do |set
|
570 map
.resources
:threads do |thread
|
571 thread
.resource
:admin, :controller => 'admin'
575 assert_simply_restful_for
:threads
576 assert_singleton_restful_for
:admin, :controller => 'admin', :name_prefix => 'thread_', :path_prefix => 'threads/5/', :options => { :thread_id => '5' }
580 def test_should_not_allow_delete_or_put_on_collection_path
581 controller_name
= :messages
582 with_restful_routing controller_name
do
583 options
= { :controller => controller_name
.to_s
}
584 collection_path
= "/#{controller_name}"
586 assert_raises(ActionController
::MethodNotAllowed) do
587 assert_recognizes(options
.merge(:action => 'update'), :path => collection_path
, :method => :put)
590 assert_raises(ActionController
::MethodNotAllowed) do
591 assert_recognizes(options
.merge(:action => 'destroy'), :path => collection_path
, :method => :delete)
596 def test_should_not_allow_invalid_head_method_for_member_routes
597 with_routing
do |set
|
599 assert_raises(ArgumentError
) do
600 map
.resources
:messages, :member => {:something => :head}
606 def test_should_not_allow_invalid_http_methods_for_member_routes
607 with_routing
do |set
|
609 assert_raises(ArgumentError
) do
610 map
.resources
:messages, :member => {:something => :invalid}
616 def test_resource_action_separator
617 with_routing
do |set
|
619 map
.resources
:messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
620 map
.resource
:account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
623 action_separator
= ActionController
::Base.resource_action_separator
625 assert_simply_restful_for
:messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
626 assert_named_route
"/threads/1/messages#{action_separator}search", "search_thread_messages_path", {}
627 assert_named_route
"/threads/1/messages/new", "new_thread_message_path", {}
628 assert_named_route
"/threads/1/messages/new#{action_separator}preview", "preview_new_thread_message_path", {}
629 assert_singleton_restful_for
:account, :name_prefix => 'admin_', :path_prefix => 'admin/'
630 assert_named_route
"/admin/account#{action_separator}login", "login_admin_account_path", {}
631 assert_named_route
"/admin/account/new", "new_admin_account_path", {}
632 assert_named_route
"/admin/account/new#{action_separator}preview", "preview_new_admin_account_path", {}
636 def test_new_style_named_routes_for_resource
637 with_routing
do |set
|
639 map
.resources
:messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
641 assert_simply_restful_for
:messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
642 assert_named_route
"/threads/1/messages/search", "search_thread_messages_path", {}
643 assert_named_route
"/threads/1/messages/new", "new_thread_message_path", {}
644 assert_named_route
"/threads/1/messages/new/preview", "preview_new_thread_message_path", {}
648 def test_new_style_named_routes_for_singleton_resource
649 with_routing
do |set
|
651 map
.resource
:account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
653 assert_singleton_restful_for
:account, :name_prefix => 'admin_', :path_prefix => 'admin/'
654 assert_named_route
"/admin/account/login", "login_admin_account_path", {}
655 assert_named_route
"/admin/account/new", "new_admin_account_path", {}
656 assert_named_route
"/admin/account/new/preview", "preview_new_admin_account_path", {}
660 def test_resources_in_namespace
661 with_routing
do |set
|
663 map
.namespace
:backoffice do |backoffice
|
664 backoffice
.resources
:products
668 assert_simply_restful_for
:products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
672 def test_resource_has_many_in_namespace
673 with_routing
do |set
|
675 map
.namespace
:backoffice do |backoffice
|
676 backoffice
.resources
:products, :has_many => :tags
680 assert_simply_restful_for
:products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
681 assert_simply_restful_for
:tags, :controller => "backoffice/tags", :name_prefix => "backoffice_product_", :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
685 def test_resource_has_one_in_namespace
686 with_routing
do |set
|
688 map
.namespace
:backoffice do |backoffice
|
689 backoffice
.resources
:products, :has_one => :manufacturer
693 assert_simply_restful_for
:products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
694 assert_singleton_restful_for
:manufacturer, :controller => "backoffice/manufacturers", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
698 def test_resources_in_nested_namespace
699 with_routing
do |set
|
701 map
.namespace
:backoffice do |backoffice
|
702 backoffice
.namespace
:admin do |admin
|
703 admin
.resources
:products
708 assert_simply_restful_for
:products, :controller => "backoffice/admin/products", :name_prefix => 'backoffice_admin_', :path_prefix => 'backoffice/admin/'
712 def test_resources_using_namespace
713 with_routing
do |set
|
715 map
.resources
:products, :namespace => "backoffice/"
718 assert_simply_restful_for
:products, :controller => "backoffice/products"
722 def test_nested_resources_using_namespace
723 with_routing
do |set
|
725 map
.namespace
:backoffice do |backoffice
|
726 backoffice
.resources
:products do |products
|
727 products
.resources
:images
732 assert_simply_restful_for
:images, :controller => "backoffice/images", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => {:product_id => '1'}
736 def test_nested_resources_in_nested_namespace
737 with_routing
do |set
|
739 map
.namespace
:backoffice do |backoffice
|
740 backoffice
.namespace
:admin do |admin
|
741 admin
.resources
:products do |products
|
742 products
.resources
:images
748 assert_simply_restful_for
:images, :controller => "backoffice/admin/images", :name_prefix => 'backoffice_admin_product_', :path_prefix => 'backoffice/admin/products/1/', :options => {:product_id => '1'}
752 def test_with_path_segment
753 with_restful_routing
:messages, :as => 'reviews' do
754 assert_simply_restful_for
:messages, :as => 'reviews'
758 def test_multiple_with_path_segment_and_controller
759 with_routing
do |set
|
761 map
.resources
:products do |products
|
762 products
.resources
:product_reviews, :as => 'reviews', :controller => 'messages'
764 map
.resources
:tutors do |tutors
|
765 tutors
.resources
:tutor_reviews, :as => 'reviews', :controller => 'comments'
769 assert_simply_restful_for
:product_reviews, :controller=>'messages', :as => 'reviews', :name_prefix => 'product_', :path_prefix => 'products/1/', :options => {:product_id => '1'}
770 assert_simply_restful_for
:tutor_reviews,:controller=>'comments', :as => 'reviews', :name_prefix => 'tutor_', :path_prefix => 'tutors/1/', :options => {:tutor_id => '1'}
774 def test_with_path_segment_path_prefix_requirements
775 expected_options
= {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
776 with_restful_routing
:messages, :as => 'comments',:path_prefix => '/thread/:thread_id', :requirements => { :thread_id => /[0-9]\.[0-9]\.[0-9]/ } do
777 assert_recognizes(expected_options
, :path => 'thread/1.1.1/comments/1', :method => :get)
781 def test_resource_has_only_show_action
782 with_routing
do |set
|
784 map
.resources
:products, :only => :show
787 assert_resource_allowed_routes('products', {}, { :id => '1' }, :show, [:index, :new, :create, :edit, :update, :destroy])
788 assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :show, [:index, :new, :create, :edit, :update, :destroy])
792 def test_singleton_resource_has_only_show_action
793 with_routing
do |set
|
795 map
.resource
:account, :only => :show
798 assert_singleton_resource_allowed_routes('accounts', {}, :show, [:index, :new, :create, :edit, :update, :destroy])
799 assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :show, [:index, :new, :create, :edit, :update, :destroy])
803 def test_resource_does_not_have_destroy_action
804 with_routing
do |set
|
806 map
.resources
:products, :except => :destroy
809 assert_resource_allowed_routes('products', {}, { :id => '1' }, [:index, :new, :create, :show, :edit, :update], :destroy)
810 assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, [:index, :new, :create, :show, :edit, :update], :destroy)
814 def test_singleton_resource_does_not_have_destroy_action
815 with_routing
do |set
|
817 map
.resource
:account, :except => :destroy
820 assert_singleton_resource_allowed_routes('accounts', {}, [:new, :create, :show, :edit, :update], :destroy)
821 assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, [:new, :create, :show, :edit, :update], :destroy)
825 def test_resource_has_only_create_action_and_named_route
826 with_routing
do |set
|
828 map
.resources
:products, :only => :create
831 assert_resource_allowed_routes('products', {}, { :id => '1' }, :create, [:index, :new, :show, :edit, :update, :destroy])
832 assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :create, [:index, :new, :show, :edit, :update, :destroy])
834 assert_not_nil set
.named_routes
[:products]
838 def test_resource_has_only_update_action_and_named_route
839 with_routing
do |set
|
841 map
.resources
:products, :only => :update
844 assert_resource_allowed_routes('products', {}, { :id => '1' }, :update, [:index, :new, :create, :show, :edit, :destroy])
845 assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :update, [:index, :new, :create, :show, :edit, :destroy])
847 assert_not_nil set
.named_routes
[:product]
851 def test_resource_has_only_destroy_action_and_named_route
852 with_routing
do |set
|
854 map
.resources
:products, :only => :destroy
857 assert_resource_allowed_routes('products', {}, { :id => '1' }, :destroy, [:index, :new, :create, :show, :edit, :update])
858 assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :destroy, [:index, :new, :create, :show, :edit, :update])
860 assert_not_nil set
.named_routes
[:product]
864 def test_singleton_resource_has_only_create_action_and_named_route
865 with_routing
do |set
|
867 map
.resource
:account, :only => :create
870 assert_singleton_resource_allowed_routes('accounts', {}, :create, [:new, :show, :edit, :update, :destroy])
871 assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :create, [:new, :show, :edit, :update, :destroy])
873 assert_not_nil set
.named_routes
[:account]
877 def test_singleton_resource_has_only_update_action_and_named_route
878 with_routing
do |set
|
880 map
.resource
:account, :only => :update
883 assert_singleton_resource_allowed_routes('accounts', {}, :update, [:new, :create, :show, :edit, :destroy])
884 assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :update, [:new, :create, :show, :edit, :destroy])
886 assert_not_nil set
.named_routes
[:account]
890 def test_singleton_resource_has_only_destroy_action_and_named_route
891 with_routing
do |set
|
893 map
.resource
:account, :only => :destroy
896 assert_singleton_resource_allowed_routes('accounts', {}, :destroy, [:new, :create, :show, :edit, :update])
897 assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :destroy, [:new, :create, :show, :edit, :update])
899 assert_not_nil set
.named_routes
[:account]
903 def test_resource_has_only_collection_action
904 with_routing
do |set
|
906 map
.resources
:products, :except => :all, :collection => { :sale => :get }
909 assert_resource_allowed_routes('products', {}, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
910 assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
912 assert_recognizes({ :controller => 'products', :action => 'sale' }, :path => 'products/sale', :method => :get)
913 assert_recognizes({ :controller => 'products', :action => 'sale', :format => 'xml' }, :path => 'products/sale.xml', :method => :get)
917 def test_resource_has_only_member_action
918 with_routing
do |set
|
920 map
.resources
:products, :except => :all, :member => { :preview => :get }
923 assert_resource_allowed_routes('products', {}, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
924 assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
926 assert_recognizes({ :controller => 'products', :action => 'preview', :id => '1' }, :path => 'products/1/preview', :method => :get)
927 assert_recognizes({ :controller => 'products', :action => 'preview', :id => '1', :format => 'xml' }, :path => 'products/1/preview.xml', :method => :get)
931 def test_singleton_resource_has_only_member_action
932 with_routing
do |set
|
934 map
.resource
:account, :except => :all, :member => { :signup => :get }
937 assert_singleton_resource_allowed_routes('accounts', {}, [], [:new, :create, :show, :edit, :update, :destroy])
938 assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, [], [:new, :create, :show, :edit, :update, :destroy])
940 assert_recognizes({ :controller => 'accounts', :action => 'signup' }, :path => 'account/signup', :method => :get)
941 assert_recognizes({ :controller => 'accounts', :action => 'signup', :format => 'xml' }, :path => 'account/signup.xml', :method => :get)
945 def test_nested_resource_inherits_only_show_action
946 with_routing
do |set
|
948 map
.resources
:products, :only => :show do |product
|
949 product
.resources
:images
953 assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
954 assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
958 def test_nested_resource_has_only_show_and_member_action
959 with_routing
do |set
|
961 map
.resources
:products, :only => [:index, :show] do |product
|
962 product
.resources
:images, :member => { :thumbnail => :get }, :only => :show
966 assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
967 assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
969 assert_recognizes({ :controller => 'images', :action => 'thumbnail', :product_id => '1', :id => '2' }, :path => 'products/1/images/2/thumbnail', :method => :get)
970 assert_recognizes({ :controller => 'images', :action => 'thumbnail', :product_id => '1', :id => '2', :format => 'jpg' }, :path => 'products/1/images/2/thumbnail.jpg', :method => :get)
974 def test_nested_resource_ignores_only_option
975 with_routing
do |set
|
977 map
.resources
:products, :only => :show do |product
|
978 product
.resources
:images, :except => :destroy
982 assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update], :destroy, 'products/1/images')
983 assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update], :destroy, 'products/1/images')
987 def test_nested_resource_ignores_except_option
988 with_routing
do |set
|
990 map
.resources
:products, :except => :show do |product
|
991 product
.resources
:images, :only => :destroy
995 assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, :destroy, [:index, :new, :create, :show, :edit, :update], 'products/1/images')
996 assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, :destroy, [:index, :new, :create, :show, :edit, :update], 'products/1/images')
1001 def with_restful_routing(*args
)
1002 with_routing
do |set
|
1003 set
.draw
{ |map
| map
.resources(*args
) }
1008 def with_singleton_resources(*args
)
1009 with_routing
do |set
|
1010 set
.draw
{ |map
| map
.resource(*args
) }
1015 # runs assert_restful_routes_for and assert_restful_named_routes for on the controller_name and options, without passing a block.
1016 def assert_simply_restful_for(controller_name
, options
= {})
1017 assert_restful_routes_for controller_name
, options
1018 assert_restful_named_routes_for controller_name
, nil, options
1021 def assert_singleton_restful_for(singleton_name
, options
= {})
1022 assert_singleton_routes_for singleton_name
, options
1023 assert_singleton_named_routes_for singleton_name
, options
1026 def assert_restful_routes_for(controller_name
, options
= {})
1027 options
[:options] ||= {}
1028 options
[:options][:controller] = options
[:controller] || controller_name
.to_s
1030 if options
[:shallow]
1031 options
[:shallow_options] ||= {}
1032 options
[:shallow_options][:controller] = options
[:options][:controller]
1034 options
[:shallow_options] = options
[:options]
1037 new_action
= ActionController
::Base.resources_path_names
[:new] || "new"
1038 edit_action
= ActionController
::Base.resources_path_names
[:edit] || "edit"
1039 if options
[:path_names]
1040 new_action
= options
[:path_names][:new] if options
[:path_names][:new]
1041 edit_action
= options
[:path_names][:edit] if options
[:path_names][:edit]
1044 path
= "#{options[:as] || controller_name}"
1045 collection_path
= "/#{options[:path_prefix]}#{path}"
1046 shallow_path
= "/#{options[:path_prefix] unless options[:shallow]}#{path}"
1047 member_path
= "#{shallow_path}/1"
1048 new_path
= "#{collection_path}/#{new_action}"
1049 edit_member_path
= "#{member_path}/#{edit_action}"
1050 formatted_edit_member_path
= "#{member_path}/#{edit_action}.xml"
1052 with_options(options
[:options]) do |controller
|
1053 controller
.assert_routing collection_path
, :action => 'index'
1054 controller
.assert_routing new_path
, :action => 'new'
1055 controller
.assert_routing
"#{collection_path}.xml", :action => 'index', :format => 'xml'
1056 controller
.assert_routing
"#{new_path}.xml", :action => 'new', :format => 'xml'
1059 with_options(options
[:shallow_options]) do |controller
|
1060 controller
.assert_routing member_path
, :action => 'show', :id => '1'
1061 controller
.assert_routing edit_member_path
, :action => 'edit', :id => '1'
1062 controller
.assert_routing
"#{member_path}.xml", :action => 'show', :id => '1', :format => 'xml'
1063 controller
.assert_routing formatted_edit_member_path
, :action => 'edit', :id => '1', :format => 'xml'
1066 assert_recognizes(options
[:options].merge(:action => 'index'), :path => collection_path
, :method => :get)
1067 assert_recognizes(options
[:options].merge(:action => 'new'), :path => new_path
, :method => :get)
1068 assert_recognizes(options
[:options].merge(:action => 'create'), :path => collection_path
, :method => :post)
1069 assert_recognizes(options
[:shallow_options].merge(:action => 'show', :id => '1'), :path => member_path
, :method => :get)
1070 assert_recognizes(options
[:shallow_options].merge(:action => 'edit', :id => '1'), :path => edit_member_path
, :method => :get)
1071 assert_recognizes(options
[:shallow_options].merge(:action => 'update', :id => '1'), :path => member_path
, :method => :put)
1072 assert_recognizes(options
[:shallow_options].merge(:action => 'destroy', :id => '1'), :path => member_path
, :method => :delete)
1074 assert_recognizes(options
[:options].merge(:action => 'index', :format => 'xml'), :path => "#{collection_path}.xml", :method => :get)
1075 assert_recognizes(options
[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
1076 assert_recognizes(options
[:options].merge(:action => 'create', :format => 'xml'), :path => "#{collection_path}.xml", :method => :post)
1077 assert_recognizes(options
[:shallow_options].merge(:action => 'show', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :get)
1078 assert_recognizes(options
[:shallow_options].merge(:action => 'edit', :id => '1', :format => 'xml'), :path => formatted_edit_member_path
, :method => :get)
1079 assert_recognizes(options
[:shallow_options].merge(:action => 'update', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :put)
1080 assert_recognizes(options
[:shallow_options].merge(:action => 'destroy', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :delete)
1082 yield options
[:options] if block_given
?
1085 # test named routes like foo_path and foos_path map to the correct options.
1086 def assert_restful_named_routes_for(controller_name
, singular_name
= nil, options
= {})
1087 if singular_name
.is_a
?(Hash
)
1088 options
= singular_name
1091 singular_name
||= controller_name
.to_s
.singularize
1093 options
[:options] ||= {}
1094 options
[:options][:controller] = options
[:controller] || controller_name
.to_s
1096 if options
[:shallow]
1097 options
[:shallow_options] ||= {}
1098 options
[:shallow_options][:controller] = options
[:options][:controller]
1100 options
[:shallow_options] = options
[:options]
1103 @controller = "#{options[:options][:controller].camelize}Controller".constantize
.new
1104 @request = ActionController
::TestRequest.new
1105 @response = ActionController
::TestResponse.new
1106 get
:index, options
[:options]
1107 options
[:options].delete
:action
1109 path
= "#{options[:as] || controller_name}"
1110 shallow_path
= "/#{options[:path_prefix] unless options[:shallow]}#{path}"
1111 full_path
= "/#{options[:path_prefix]}#{path}"
1112 name_prefix
= options
[:name_prefix]
1113 shallow_prefix
= "#{options[:name_prefix] unless options[:shallow]}"
1116 edit_action
= "edit"
1117 if options
[:path_names]
1118 new_action
= options
[:path_names][:new] || "new"
1119 edit_action
= options
[:path_names][:edit] || "edit"
1122 assert_named_route
"#{full_path}", "#{name_prefix}#{controller_name}_path", options
[:options]
1123 assert_named_route
"#{full_path}.xml", "formatted_#{name_prefix}#{controller_name}_path", options
[:options].merge(:format => 'xml')
1124 assert_named_route
"#{shallow_path}/1", "#{shallow_prefix}#{singular_name}_path", options
[:shallow_options].merge(:id => '1')
1125 assert_named_route
"#{shallow_path}/1.xml", "formatted_#{shallow_prefix}#{singular_name}_path", options
[:shallow_options].merge(:id => '1', :format => 'xml')
1127 assert_named_route
"#{full_path}/#{new_action}", "new_#{name_prefix}#{singular_name}_path", options
[:options]
1128 assert_named_route
"#{full_path}/#{new_action}.xml", "formatted_new_#{name_prefix}#{singular_name}_path", options
[:options].merge(:format => 'xml')
1129 assert_named_route
"#{shallow_path}/1/#{edit_action}", "edit_#{shallow_prefix}#{singular_name}_path", options
[:shallow_options].merge(:id => '1')
1130 assert_named_route
"#{shallow_path}/1/#{edit_action}.xml", "formatted_edit_#{shallow_prefix}#{singular_name}_path", options
[:shallow_options].merge(:id => '1', :format => 'xml')
1132 yield options
[:options] if block_given
?
1135 def assert_singleton_routes_for(singleton_name
, options
= {})
1136 options
[:options] ||= {}
1137 options
[:options][:controller] = options
[:controller] || singleton_name
.to_s
.pluralize
1139 full_path
= "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
1140 new_path
= "#{full_path}/new"
1141 edit_path
= "#{full_path}/edit"
1142 formatted_edit_path
= "#{full_path}/edit.xml"
1144 with_options options
[:options] do |controller
|
1145 controller
.assert_routing full_path
, :action => 'show'
1146 controller
.assert_routing new_path
, :action => 'new'
1147 controller
.assert_routing edit_path
, :action => 'edit'
1148 controller
.assert_routing
"#{full_path}.xml", :action => 'show', :format => 'xml'
1149 controller
.assert_routing
"#{new_path}.xml", :action => 'new', :format => 'xml'
1150 controller
.assert_routing formatted_edit_path
, :action => 'edit', :format => 'xml'
1153 assert_recognizes(options
[:options].merge(:action => 'show'), :path => full_path
, :method => :get)
1154 assert_recognizes(options
[:options].merge(:action => 'new'), :path => new_path
, :method => :get)
1155 assert_recognizes(options
[:options].merge(:action => 'edit'), :path => edit_path
, :method => :get)
1156 assert_recognizes(options
[:options].merge(:action => 'create'), :path => full_path
, :method => :post)
1157 assert_recognizes(options
[:options].merge(:action => 'update'), :path => full_path
, :method => :put)
1158 assert_recognizes(options
[:options].merge(:action => 'destroy'), :path => full_path
, :method => :delete)
1160 assert_recognizes(options
[:options].merge(:action => 'show', :format => 'xml'), :path => "#{full_path}.xml", :method => :get)
1161 assert_recognizes(options
[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
1162 assert_recognizes(options
[:options].merge(:action => 'edit', :format => 'xml'), :path => formatted_edit_path
, :method => :get)
1163 assert_recognizes(options
[:options].merge(:action => 'create', :format => 'xml'), :path => "#{full_path}.xml", :method => :post)
1164 assert_recognizes(options
[:options].merge(:action => 'update', :format => 'xml'), :path => "#{full_path}.xml", :method => :put)
1165 assert_recognizes(options
[:options].merge(:action => 'destroy', :format => 'xml'), :path => "#{full_path}.xml", :method => :delete)
1167 yield options
[:options] if block_given
?
1170 def assert_singleton_named_routes_for(singleton_name
, options
= {})
1171 (options
[:options] ||= {})[:controller] ||= singleton_name
.to_s
.pluralize
1172 @controller = "#{options[:options][:controller].camelize}Controller".constantize
.new
1173 @request = ActionController
::TestRequest.new
1174 @response = ActionController
::TestResponse.new
1175 get
:show, options
[:options]
1176 options
[:options].delete
:action
1178 full_path
= "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
1179 name_prefix
= options
[:name_prefix]
1181 assert_named_route
"#{full_path}", "#{name_prefix}#{singleton_name}_path", options
[:options]
1182 assert_named_route
"#{full_path}.xml", "formatted_#{name_prefix}#{singleton_name}_path", options
[:options].merge(:format => 'xml')
1184 assert_named_route
"#{full_path}/new", "new_#{name_prefix}#{singleton_name}_path", options
[:options]
1185 assert_named_route
"#{full_path}/new.xml", "formatted_new_#{name_prefix}#{singleton_name}_path", options
[:options].merge(:format => 'xml')
1186 assert_named_route
"#{full_path}/edit", "edit_#{name_prefix}#{singleton_name}_path", options
[:options]
1187 assert_named_route
"#{full_path}/edit.xml", "formatted_edit_#{name_prefix}#{singleton_name}_path", options
[:options].merge(:format => 'xml')
1190 def assert_named_route(expected
, route
, options
)
1191 actual
= @controller.send(route
, options
) rescue $
!.class.name
1192 assert_equal expected
, actual
, "Error on route: #{route}(#{options.inspect})"
1195 def assert_resource_methods(expected
, resource
, action_method
, method
)
1196 assert_equal expected
.length
, resource
.send("#{action_method}_methods")[method
].size
, "#{resource.send("#{action_method}_methods")[method].inspect}"
1197 expected
.each
do |action
|
1198 assert resource
.send("#{action_method}_methods")[method
].include?(action
),
1199 "#{method} not in #{action_method} methods: #{resource.send("#{action_method}_methods")[method].inspect}"
1203 def assert_resource_allowed_routes(controller
, options
, shallow_options
, allowed
, not_allowed
, path
= controller
)
1204 shallow_path
= "#{path}/#{shallow_options[:id]}"
1205 format
= options
[:format] && ".#{options[:format]}"
1206 options
.merge
!(:controller => controller
)
1207 shallow_options
.merge
!(options
)
1209 assert_whether_allowed(allowed
, not_allowed
, options
, 'index', "#{path}#{format}", :get)
1210 assert_whether_allowed(allowed
, not_allowed
, options
, 'new', "#{path}/new#{format}", :get)
1211 assert_whether_allowed(allowed
, not_allowed
, options
, 'create', "#{path}#{format}", :post)
1212 assert_whether_allowed(allowed
, not_allowed
, shallow_options
, 'show', "#{shallow_path}#{format}", :get)
1213 assert_whether_allowed(allowed
, not_allowed
, shallow_options
, 'edit', "#{shallow_path}/edit#{format}", :get)
1214 assert_whether_allowed(allowed
, not_allowed
, shallow_options
, 'update', "#{shallow_path}#{format}", :put)
1215 assert_whether_allowed(allowed
, not_allowed
, shallow_options
, 'destroy', "#{shallow_path}#{format}", :delete)
1218 def assert_singleton_resource_allowed_routes(controller
, options
, allowed
, not_allowed
, path
= controller
.singularize
)
1219 format
= options
[:format] && ".#{options[:format]}"
1220 options
.merge
!(:controller => controller
)
1222 assert_whether_allowed(allowed
, not_allowed
, options
, 'new', "#{path}/new#{format}", :get)
1223 assert_whether_allowed(allowed
, not_allowed
, options
, 'create', "#{path}#{format}", :post)
1224 assert_whether_allowed(allowed
, not_allowed
, options
, 'show', "#{path}#{format}", :get)
1225 assert_whether_allowed(allowed
, not_allowed
, options
, 'edit', "#{path}/edit#{format}", :get)
1226 assert_whether_allowed(allowed
, not_allowed
, options
, 'update', "#{path}#{format}", :put)
1227 assert_whether_allowed(allowed
, not_allowed
, options
, 'destroy', "#{path}#{format}", :delete)
1230 def assert_whether_allowed(allowed
, not_allowed
, options
, action
, path
, method
)
1231 action
= action
.to_sym
1232 options
= options
.merge(:action => action
.to_s
)
1233 path_options
= { :path => path
, :method => method
}
1235 if Array(allowed
).include?(action
)
1236 assert_recognizes options
, path_options
1237 elsif Array(not_allowed
).include?(action
)
1238 assert_not_recognizes options
, path_options
1242 def assert_not_recognizes(expected_options
, path
)
1243 assert_raise ActionController
::RoutingError, ActionController
::MethodNotAllowed, Test
::Unit::AssertionFailedError do
1244 assert_recognizes(expected_options
, path
)
1248 def distinct_routes
? (r1
, r2
)
1249 if r1
.conditions
== r2
.conditions
and r1
.requirements
== r2
.requirements
then
1250 if r1
.segments
.collect(&:to_s) == r2
.segments
.collect(&:to_s) then