Froze rails gems
[depot.git] / vendor / rails / actionpack / test / controller / resources_test.rb
1 require 'abstract_unit'
2
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
7 end
8
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
14
15 class AccountsController < ResourcesController; end
16 class AdminController < ResourcesController; end
17 class ProductsController < ResourcesController; end
18 class ImagesController < ResourcesController; end
19
20 module Backoffice
21 class ProductsController < ResourcesController; end
22 class TagsController < ResourcesController; end
23 class ManufacturersController < ResourcesController; end
24 class ImagesController < ResourcesController; end
25
26 module Admin
27 class ProductsController < ResourcesController; end
28 class ImagesController < ResourcesController; end
29 end
30 end
31
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
35 def setup
36 ActionController::Base.optimise_named_routes = false
37 end
38
39 def teardown
40 ActionController::Base.optimise_named_routes = true
41 end
42
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 })
48
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
54 end
55
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
59 end
60
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
64 end
65
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)
70 end
71 end
72
73 def test_default_restful_routes
74 with_restful_routing :messages do
75 assert_simply_restful_for :messages
76 end
77 end
78
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"
83 end
84
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
89 end
90 end
91
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]
95 end
96 end
97
98 def test_irregular_id_with_no_requirements_should_raise_error
99 expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
100
101 with_restful_routing :messages do
102 assert_raises(ActionController::RoutingError) do
103 assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
104 end
105 end
106 end
107
108 def test_irregular_id_with_requirements_should_pass
109 expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
110
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)
113 end
114 end
115
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)
120 end
121 end
122
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' }
126 end
127 end
128
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' }
133 end
134 end
135
136 def test_with_name_prefix
137 with_restful_routing :messages, :name_prefix => 'post_' do
138 assert_simply_restful_for :messages, :name_prefix => 'post_'
139 end
140 end
141
142 def test_with_collection_actions
143 actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
144
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)
149 end
150 end
151
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
155 end
156 end
157 end
158 end
159
160 def test_with_collection_actions_and_name_prefix
161 actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
162
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)
167 end
168 end
169
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
173 end
174 end
175 end
176 end
177
178 def test_with_collection_action_and_name_prefix_and_formatted
179 actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
180
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)
185 end
186 end
187
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'
191 end
192 end
193 end
194 end
195
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)
203 end
204
205 assert_restful_named_routes_for :messages do |options|
206 assert_named_route mark_path, :mark_message_path, mark_options
207 end
208 end
209 end
210 end
211
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"
217
218 assert_restful_routes_for :messages, :path_names => {:new => 'nuevo'} do |options|
219 assert_recognizes(options.merge(mark_options), :path => mark_path, :method => method)
220 end
221
222 assert_restful_named_routes_for :messages, :path_names => {:new => 'nuevo'} do |options|
223 assert_named_route mark_path, :mark_message_path, mark_options
224 end
225 end
226 end
227 end
228
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'}
232
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"
238
239 assert_restful_routes_for :messages do |options|
240 assert_recognizes(options.merge(new_options), :path => new_path, :method => :get)
241 end
242
243 assert_restful_routes_for :messages do |options|
244 assert_recognizes(options.merge(edit_options), :path => edit_path, :method => :get)
245 end
246 end
247 ensure
248 ActionController::Base.resources_path_names = default_path_names
249 end
250
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)
259 end
260
261 assert_restful_named_routes_for :messages do |options|
262 assert_named_route action_path, "#{action}_message_path".to_sym, action_options
263 end
264 end
265 end
266 end
267 end
268
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)
274 end
275 [:get, :post].each do |method|
276 assert_recognizes(options.merge(:action => 'toggle', :id => '1'), :path => '/messages/1/toggle', :method => method)
277 end
278 end
279 end
280 end
281
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)
288 end
289
290 assert_restful_named_routes_for :messages do |options|
291 assert_named_route preview_path, :preview_new_message_path, preview_options
292 end
293 end
294 end
295
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)
302 end
303
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
306 end
307 end
308 end
309
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)
316 end
317
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
320 end
321 end
322 end
323
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)
330 end
331 end
332 end
333
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)
338 end
339 end
340 end
341
342 def test_nested_restful_routes
343 with_routing do |set|
344 set.draw do |map|
345 map.resources :threads do |map|
346 map.resources :messages do |map|
347 map.resources :comments
348 end
349 end
350 end
351
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' }
361 end
362 end
363
364 def test_nested_restful_routes_with_overwritten_defaults
365 with_routing do |set|
366 set.draw do |map|
367 map.resources :threads do |map|
368 map.resources :messages, :name_prefix => nil do |map|
369 map.resources :comments, :name_prefix => nil
370 end
371 end
372 end
373
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' }
381 end
382 end
383
384 def test_shallow_nested_restful_routes
385 with_routing do |set|
386 set.draw do |map|
387 map.resources :threads, :shallow => true do |map|
388 map.resources :messages do |map|
389 map.resources :comments
390 end
391 end
392 end
393
394 assert_simply_restful_for :threads,
395 :shallow => true
396 assert_simply_restful_for :messages,
397 :name_prefix => 'thread_',
398 :path_prefix => 'threads/1/',
399 :shallow => true,
400 :options => { :thread_id => '1' }
401 assert_simply_restful_for :comments,
402 :name_prefix => 'message_',
403 :path_prefix => 'messages/2/',
404 :shallow => true,
405 :options => { :message_id => '2' }
406 end
407 end
408
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|
413 routes.each do |r|
414 next if route === r # skip the comparison instance
415 assert distinct_routes?(route, r), "Duplicate Route: #{route}"
416 end
417 end
418 end
419 end
420
421 def test_should_create_singleton_resource_routes
422 with_singleton_resources :account do
423 assert_singleton_restful_for :account
424 end
425 end
426
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
431 end
432 end
433
434 def test_should_create_nested_singleton_resource_routes
435 with_routing do |set|
436 set.draw do |map|
437 map.resource :admin, :controller => 'admin' do |admin|
438 admin.resource :account
439 end
440 end
441
442 assert_singleton_restful_for :admin, :controller => 'admin'
443 assert_singleton_restful_for :account, :name_prefix => "admin_", :path_prefix => 'admin/'
444 end
445 end
446
447 def test_resource_has_many_should_become_nested_resources
448 with_routing do |set|
449 set.draw do |map|
450 map.resources :messages, :has_many => [ :comments, :authors ]
451 end
452
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' }
456 end
457 end
458
459 def test_resources_has_many_hash_should_become_nested_resources
460 with_routing do |set|
461 set.draw do |map|
462 map.resources :threads, :has_many => { :messages => [ :comments, { :authors => :threads } ] }
463 end
464
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' }
470 end
471 end
472
473 def test_shallow_resource_has_many_should_become_shallow_nested_resources
474 with_routing do |set|
475 set.draw do |map|
476 map.resources :messages, :has_many => [ :comments, :authors ], :shallow => true
477 end
478
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' }
482 end
483 end
484
485 def test_resource_has_one_should_become_nested_resources
486 with_routing do |set|
487 set.draw do |map|
488 map.resources :messages, :has_one => :logo
489 end
490
491 assert_simply_restful_for :messages
492 assert_singleton_restful_for :logo, :name_prefix => 'message_', :path_prefix => 'messages/1/', :options => { :message_id => '1' }
493 end
494 end
495
496 def test_shallow_resource_has_one_should_become_shallow_nested_resources
497 with_routing do |set|
498 set.draw do |map|
499 map.resources :messages, :has_one => :logo, :shallow => true
500 end
501
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' }
504 end
505 end
506
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)
514 end
515
516 assert_singleton_named_routes_for :account do |options|
517 assert_named_route reset_path, :reset_account_path, reset_options
518 end
519 end
520 end
521 end
522
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)
531 end
532
533 assert_singleton_named_routes_for :account do |options|
534 assert_named_route action_path, "#{action}_account_path".to_sym, action_options
535 end
536 end
537 end
538 end
539 end
540
541 def test_should_nest_resources_in_singleton_resource
542 with_routing do |set|
543 set.draw do |map|
544 map.resource :account do |account|
545 account.resources :messages
546 end
547 end
548
549 assert_singleton_restful_for :account
550 assert_simply_restful_for :messages, :name_prefix => "account_", :path_prefix => 'account/'
551 end
552 end
553
554 def test_should_nest_resources_in_singleton_resource_with_path_prefix
555 with_routing do |set|
556 set.draw do |map|
557 map.resource(:account, :path_prefix => ':site_id') do |account|
558 account.resources :messages
559 end
560 end
561
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' }
564 end
565 end
566
567 def test_should_nest_singleton_resource_in_resources
568 with_routing do |set|
569 set.draw do |map|
570 map.resources :threads do |thread|
571 thread.resource :admin, :controller => 'admin'
572 end
573 end
574
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' }
577 end
578 end
579
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}"
585
586 assert_raises(ActionController::MethodNotAllowed) do
587 assert_recognizes(options.merge(:action => 'update'), :path => collection_path, :method => :put)
588 end
589
590 assert_raises(ActionController::MethodNotAllowed) do
591 assert_recognizes(options.merge(:action => 'destroy'), :path => collection_path, :method => :delete)
592 end
593 end
594 end
595
596 def test_should_not_allow_invalid_head_method_for_member_routes
597 with_routing do |set|
598 set.draw do |map|
599 assert_raises(ArgumentError) do
600 map.resources :messages, :member => {:something => :head}
601 end
602 end
603 end
604 end
605
606 def test_should_not_allow_invalid_http_methods_for_member_routes
607 with_routing do |set|
608 set.draw do |map|
609 assert_raises(ArgumentError) do
610 map.resources :messages, :member => {:something => :invalid}
611 end
612 end
613 end
614 end
615
616 def test_resource_action_separator
617 with_routing do |set|
618 set.draw do |map|
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'
621 end
622
623 action_separator = ActionController::Base.resource_action_separator
624
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", {}
633 end
634 end
635
636 def test_new_style_named_routes_for_resource
637 with_routing do |set|
638 set.draw do |map|
639 map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
640 end
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", {}
645 end
646 end
647
648 def test_new_style_named_routes_for_singleton_resource
649 with_routing do |set|
650 set.draw do |map|
651 map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
652 end
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", {}
657 end
658 end
659
660 def test_resources_in_namespace
661 with_routing do |set|
662 set.draw do |map|
663 map.namespace :backoffice do |backoffice|
664 backoffice.resources :products
665 end
666 end
667
668 assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
669 end
670 end
671
672 def test_resource_has_many_in_namespace
673 with_routing do |set|
674 set.draw do |map|
675 map.namespace :backoffice do |backoffice|
676 backoffice.resources :products, :has_many => :tags
677 end
678 end
679
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' }
682 end
683 end
684
685 def test_resource_has_one_in_namespace
686 with_routing do |set|
687 set.draw do |map|
688 map.namespace :backoffice do |backoffice|
689 backoffice.resources :products, :has_one => :manufacturer
690 end
691 end
692
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' }
695 end
696 end
697
698 def test_resources_in_nested_namespace
699 with_routing do |set|
700 set.draw do |map|
701 map.namespace :backoffice do |backoffice|
702 backoffice.namespace :admin do |admin|
703 admin.resources :products
704 end
705 end
706 end
707
708 assert_simply_restful_for :products, :controller => "backoffice/admin/products", :name_prefix => 'backoffice_admin_', :path_prefix => 'backoffice/admin/'
709 end
710 end
711
712 def test_resources_using_namespace
713 with_routing do |set|
714 set.draw do |map|
715 map.resources :products, :namespace => "backoffice/"
716 end
717
718 assert_simply_restful_for :products, :controller => "backoffice/products"
719 end
720 end
721
722 def test_nested_resources_using_namespace
723 with_routing do |set|
724 set.draw do |map|
725 map.namespace :backoffice do |backoffice|
726 backoffice.resources :products do |products|
727 products.resources :images
728 end
729 end
730 end
731
732 assert_simply_restful_for :images, :controller => "backoffice/images", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => {:product_id => '1'}
733 end
734 end
735
736 def test_nested_resources_in_nested_namespace
737 with_routing do |set|
738 set.draw do |map|
739 map.namespace :backoffice do |backoffice|
740 backoffice.namespace :admin do |admin|
741 admin.resources :products do |products|
742 products.resources :images
743 end
744 end
745 end
746 end
747
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'}
749 end
750 end
751
752 def test_with_path_segment
753 with_restful_routing :messages, :as => 'reviews' do
754 assert_simply_restful_for :messages, :as => 'reviews'
755 end
756 end
757
758 def test_multiple_with_path_segment_and_controller
759 with_routing do |set|
760 set.draw do |map|
761 map.resources :products do |products|
762 products.resources :product_reviews, :as => 'reviews', :controller => 'messages'
763 end
764 map.resources :tutors do |tutors|
765 tutors.resources :tutor_reviews, :as => 'reviews', :controller => 'comments'
766 end
767 end
768
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'}
771 end
772 end
773
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)
778 end
779 end
780
781 def test_resource_has_only_show_action
782 with_routing do |set|
783 set.draw do |map|
784 map.resources :products, :only => :show
785 end
786
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])
789 end
790 end
791
792 def test_singleton_resource_has_only_show_action
793 with_routing do |set|
794 set.draw do |map|
795 map.resource :account, :only => :show
796 end
797
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])
800 end
801 end
802
803 def test_resource_does_not_have_destroy_action
804 with_routing do |set|
805 set.draw do |map|
806 map.resources :products, :except => :destroy
807 end
808
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)
811 end
812 end
813
814 def test_singleton_resource_does_not_have_destroy_action
815 with_routing do |set|
816 set.draw do |map|
817 map.resource :account, :except => :destroy
818 end
819
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)
822 end
823 end
824
825 def test_resource_has_only_create_action_and_named_route
826 with_routing do |set|
827 set.draw do |map|
828 map.resources :products, :only => :create
829 end
830
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])
833
834 assert_not_nil set.named_routes[:products]
835 end
836 end
837
838 def test_resource_has_only_update_action_and_named_route
839 with_routing do |set|
840 set.draw do |map|
841 map.resources :products, :only => :update
842 end
843
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])
846
847 assert_not_nil set.named_routes[:product]
848 end
849 end
850
851 def test_resource_has_only_destroy_action_and_named_route
852 with_routing do |set|
853 set.draw do |map|
854 map.resources :products, :only => :destroy
855 end
856
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])
859
860 assert_not_nil set.named_routes[:product]
861 end
862 end
863
864 def test_singleton_resource_has_only_create_action_and_named_route
865 with_routing do |set|
866 set.draw do |map|
867 map.resource :account, :only => :create
868 end
869
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])
872
873 assert_not_nil set.named_routes[:account]
874 end
875 end
876
877 def test_singleton_resource_has_only_update_action_and_named_route
878 with_routing do |set|
879 set.draw do |map|
880 map.resource :account, :only => :update
881 end
882
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])
885
886 assert_not_nil set.named_routes[:account]
887 end
888 end
889
890 def test_singleton_resource_has_only_destroy_action_and_named_route
891 with_routing do |set|
892 set.draw do |map|
893 map.resource :account, :only => :destroy
894 end
895
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])
898
899 assert_not_nil set.named_routes[:account]
900 end
901 end
902
903 def test_resource_has_only_collection_action
904 with_routing do |set|
905 set.draw do |map|
906 map.resources :products, :except => :all, :collection => { :sale => :get }
907 end
908
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])
911
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)
914 end
915 end
916
917 def test_resource_has_only_member_action
918 with_routing do |set|
919 set.draw do |map|
920 map.resources :products, :except => :all, :member => { :preview => :get }
921 end
922
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])
925
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)
928 end
929 end
930
931 def test_singleton_resource_has_only_member_action
932 with_routing do |set|
933 set.draw do |map|
934 map.resource :account, :except => :all, :member => { :signup => :get }
935 end
936
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])
939
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)
942 end
943 end
944
945 def test_nested_resource_inherits_only_show_action
946 with_routing do |set|
947 set.draw do |map|
948 map.resources :products, :only => :show do |product|
949 product.resources :images
950 end
951 end
952
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')
955 end
956 end
957
958 def test_nested_resource_has_only_show_and_member_action
959 with_routing do |set|
960 set.draw do |map|
961 map.resources :products, :only => [:index, :show] do |product|
962 product.resources :images, :member => { :thumbnail => :get }, :only => :show
963 end
964 end
965
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')
968
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)
971 end
972 end
973
974 def test_nested_resource_ignores_only_option
975 with_routing do |set|
976 set.draw do |map|
977 map.resources :products, :only => :show do |product|
978 product.resources :images, :except => :destroy
979 end
980 end
981
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')
984 end
985 end
986
987 def test_nested_resource_ignores_except_option
988 with_routing do |set|
989 set.draw do |map|
990 map.resources :products, :except => :show do |product|
991 product.resources :images, :only => :destroy
992 end
993 end
994
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')
997 end
998 end
999
1000 protected
1001 def with_restful_routing(*args)
1002 with_routing do |set|
1003 set.draw { |map| map.resources(*args) }
1004 yield
1005 end
1006 end
1007
1008 def with_singleton_resources(*args)
1009 with_routing do |set|
1010 set.draw { |map| map.resource(*args) }
1011 yield
1012 end
1013 end
1014
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
1019 end
1020
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
1024 end
1025
1026 def assert_restful_routes_for(controller_name, options = {})
1027 options[:options] ||= {}
1028 options[:options][:controller] = options[:controller] || controller_name.to_s
1029
1030 if options[:shallow]
1031 options[:shallow_options] ||= {}
1032 options[:shallow_options][:controller] = options[:options][:controller]
1033 else
1034 options[:shallow_options] = options[:options]
1035 end
1036
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]
1042 end
1043
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"
1051
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'
1057 end
1058
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'
1064 end
1065
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)
1073
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)
1081
1082 yield options[:options] if block_given?
1083 end
1084
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
1089 singular_name = nil
1090 end
1091 singular_name ||= controller_name.to_s.singularize
1092
1093 options[:options] ||= {}
1094 options[:options][:controller] = options[:controller] || controller_name.to_s
1095
1096 if options[:shallow]
1097 options[:shallow_options] ||= {}
1098 options[:shallow_options][:controller] = options[:options][:controller]
1099 else
1100 options[:shallow_options] = options[:options]
1101 end
1102
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
1108
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]}"
1114
1115 new_action = "new"
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"
1120 end
1121
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')
1126
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')
1131
1132 yield options[:options] if block_given?
1133 end
1134
1135 def assert_singleton_routes_for(singleton_name, options = {})
1136 options[:options] ||= {}
1137 options[:options][:controller] = options[:controller] || singleton_name.to_s.pluralize
1138
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"
1143
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'
1151 end
1152
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)
1159
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)
1166
1167 yield options[:options] if block_given?
1168 end
1169
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
1177
1178 full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
1179 name_prefix = options[:name_prefix]
1180
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')
1183
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')
1188 end
1189
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})"
1193 end
1194
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}"
1200 end
1201 end
1202
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)
1208
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)
1216 end
1217
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)
1221
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)
1228 end
1229
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 }
1234
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
1239 end
1240 end
1241
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)
1245 end
1246 end
1247
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
1251 return false
1252 end
1253 end
1254 true
1255 end
1256 end