ghazel-erubis_rails_helper 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. data/README.rdoc +66 -0
  2. data/lib/erubis_rails_helper/erubis/generator.rb +100 -0
  3. data/lib/erubis_rails_helper/template_handlers/erubis.rb +260 -0
  4. data/lib/erubis_rails_helper.rb +20 -0
  5. data/test/abstract_unit.rb +51 -0
  6. data/test/controller/action_pack_assertions_test.rb +516 -0
  7. data/test/controller/addresses_render_test.rb +37 -0
  8. data/test/controller/assert_select_test.rb +734 -0
  9. data/test/controller/base_test.rb +217 -0
  10. data/test/controller/benchmark_test.rb +32 -0
  11. data/test/controller/caching_test.rb +690 -0
  12. data/test/controller/capture_test.rb +66 -0
  13. data/test/controller/content_type_test.rb +168 -0
  14. data/test/controller/controller_fixtures/app/controllers/admin/user_controller.rb +0 -0
  15. data/test/controller/controller_fixtures/app/controllers/user_controller.rb +0 -0
  16. data/test/controller/controller_fixtures/vendor/plugins/bad_plugin/lib/plugin_controller.rb +0 -0
  17. data/test/controller/cookie_test.rb +111 -0
  18. data/test/controller/deprecation/deprecated_base_methods_test.rb +32 -0
  19. data/test/controller/dispatcher_test.rb +103 -0
  20. data/test/controller/fake_controllers.rb +33 -0
  21. data/test/controller/fake_models.rb +19 -0
  22. data/test/controller/filter_params_test.rb +49 -0
  23. data/test/controller/filters_test.rb +885 -0
  24. data/test/controller/flash_test.rb +142 -0
  25. data/test/controller/header_test.rb +14 -0
  26. data/test/controller/helper_test.rb +224 -0
  27. data/test/controller/html-scanner/cdata_node_test.rb +15 -0
  28. data/test/controller/html-scanner/document_test.rb +148 -0
  29. data/test/controller/html-scanner/node_test.rb +89 -0
  30. data/test/controller/html-scanner/sanitizer_test.rb +273 -0
  31. data/test/controller/html-scanner/tag_node_test.rb +238 -0
  32. data/test/controller/html-scanner/text_node_test.rb +50 -0
  33. data/test/controller/html-scanner/tokenizer_test.rb +131 -0
  34. data/test/controller/http_basic_authentication_test.rb +88 -0
  35. data/test/controller/http_digest_authentication_test.rb +178 -0
  36. data/test/controller/integration_test.rb +417 -0
  37. data/test/controller/layout_test.rb +204 -0
  38. data/test/controller/logging_test.rb +46 -0
  39. data/test/controller/middleware_stack_test.rb +90 -0
  40. data/test/controller/mime_responds_test.rb +536 -0
  41. data/test/controller/mime_type_test.rb +93 -0
  42. data/test/controller/polymorphic_routes_test.rb +293 -0
  43. data/test/controller/rack_test.rb +294 -0
  44. data/test/controller/record_identifier_test.rb +139 -0
  45. data/test/controller/redirect_test.rb +282 -0
  46. data/test/controller/render_test.rb +1752 -0
  47. data/test/controller/request/json_params_parsing_test.rb +45 -0
  48. data/test/controller/request/multipart_params_parsing_test.rb +223 -0
  49. data/test/controller/request/query_string_parsing_test.rb +120 -0
  50. data/test/controller/request/url_encoded_params_parsing_test.rb +184 -0
  51. data/test/controller/request/xml_params_parsing_test.rb +88 -0
  52. data/test/controller/request_forgery_protection_test.rb +246 -0
  53. data/test/controller/request_test.rb +407 -0
  54. data/test/controller/rescue_test.rb +536 -0
  55. data/test/controller/resources_test.rb +1341 -0
  56. data/test/controller/routing_test.rb +2557 -0
  57. data/test/controller/selector_test.rb +628 -0
  58. data/test/controller/send_file_test.rb +161 -0
  59. data/test/controller/session/cookie_store_test.rb +239 -0
  60. data/test/controller/session/mem_cache_store_test.rb +127 -0
  61. data/test/controller/session/test_session_test.rb +58 -0
  62. data/test/controller/test_test.rb +692 -0
  63. data/test/controller/translation_test.rb +26 -0
  64. data/test/controller/url_rewriter_test.rb +359 -0
  65. data/test/controller/verification_test.rb +270 -0
  66. data/test/controller/view_paths_test.rb +141 -0
  67. data/test/controller/webservice_test.rb +260 -0
  68. data/test/fixtures/alternate_helpers/foo_helper.rb +3 -0
  69. data/test/fixtures/company.rb +10 -0
  70. data/test/fixtures/developer.rb +9 -0
  71. data/test/fixtures/helpers/abc_helper.rb +5 -0
  72. data/test/fixtures/helpers/fun/games_helper.rb +3 -0
  73. data/test/fixtures/helpers/fun/pdf_helper.rb +3 -0
  74. data/test/fixtures/mascot.rb +3 -0
  75. data/test/fixtures/project.rb +3 -0
  76. data/test/fixtures/reply.rb +7 -0
  77. data/test/fixtures/topic.rb +3 -0
  78. data/test/template/asset_tag_helper_test.rb +713 -0
  79. data/test/template/atom_feed_helper_test.rb +286 -0
  80. data/test/template/benchmark_helper_test.rb +86 -0
  81. data/test/template/compiled_templates_test.rb +203 -0
  82. data/test/template/date_helper_i18n_test.rb +120 -0
  83. data/test/template/date_helper_test.rb +2469 -0
  84. data/test/template/erb_util_test.rb +24 -0
  85. data/test/template/form_helper_test.rb +1176 -0
  86. data/test/template/form_options_helper_test.rb +751 -0
  87. data/test/template/form_tag_helper_test.rb +327 -0
  88. data/test/template/javascript_helper_test.rb +106 -0
  89. data/test/template/number_helper_i18n_test.rb +69 -0
  90. data/test/template/number_helper_test.rb +128 -0
  91. data/test/template/prototype_helper_test.rb +639 -0
  92. data/test/template/record_tag_helper_test.rb +58 -0
  93. data/test/template/render_test.rb +291 -0
  94. data/test/template/sanitize_helper_test.rb +48 -0
  95. data/test/template/scriptaculous_helper_test.rb +90 -0
  96. data/test/template/tag_helper_test.rb +97 -0
  97. data/test/template/test_test.rb +54 -0
  98. data/test/template/text_helper_test.rb +520 -0
  99. data/test/template/translation_helper_test.rb +32 -0
  100. data/test/template/url_helper_test.rb +614 -0
  101. data/test/test_erubis_rails_helper.rb +4 -0
  102. data/test/testing_sandbox.rb +15 -0
  103. data/test/view/test_case_test.rb +8 -0
  104. metadata +174 -0
@@ -0,0 +1,1341 @@
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 < ActionController::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_raise(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_actions_and_name_prefix_and_member_action_with_same_name
179
+ actions = { 'a' => :get }
180
+
181
+ with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions, :member => 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), :path => "/threads/1/messages/#{action}", :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}", "#{action}_thread_messages_path", :action => action
191
+ end
192
+ end
193
+ end
194
+ end
195
+
196
+ def test_with_collection_action_and_name_prefix_and_formatted
197
+ actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
198
+
199
+ with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions do
200
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
201
+ actions.each do |action, method|
202
+ assert_recognizes(options.merge(:action => action, :format => 'xml'), :path => "/threads/1/messages/#{action}.xml", :method => method)
203
+ end
204
+ end
205
+
206
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
207
+ actions.keys.each do |action|
208
+ assert_named_route "/threads/1/messages/#{action}.xml", "#{action}_thread_messages_path", :action => action, :format => 'xml'
209
+ end
210
+ end
211
+ end
212
+ end
213
+
214
+ def test_with_member_action
215
+ [:put, :post].each do |method|
216
+ with_restful_routing :messages, :member => { :mark => method } do
217
+ mark_options = {:action => 'mark', :id => '1'}
218
+ mark_path = "/messages/1/mark"
219
+ assert_restful_routes_for :messages do |options|
220
+ assert_recognizes(options.merge(mark_options), :path => mark_path, :method => method)
221
+ end
222
+
223
+ assert_restful_named_routes_for :messages do |options|
224
+ assert_named_route mark_path, :mark_message_path, mark_options
225
+ end
226
+ end
227
+ end
228
+ end
229
+
230
+ def test_with_member_action_and_requirement
231
+ expected_options = {:controller => 'messages', :action => 'mark', :id => '1.1.1'}
232
+
233
+ with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}, :member => { :mark => :get }) do
234
+ assert_recognizes(expected_options, :path => 'messages/1.1.1/mark', :method => :get)
235
+ end
236
+ end
237
+
238
+ def test_member_when_override_paths_for_default_restful_actions_with
239
+ [:put, :post].each do |method|
240
+ with_restful_routing :messages, :member => { :mark => method }, :path_names => {:new => 'nuevo'} do
241
+ mark_options = {:action => 'mark', :id => '1', :controller => "messages"}
242
+ mark_path = "/messages/1/mark"
243
+
244
+ assert_restful_routes_for :messages, :path_names => {:new => 'nuevo'} do |options|
245
+ assert_recognizes(options.merge(mark_options), :path => mark_path, :method => method)
246
+ end
247
+
248
+ assert_restful_named_routes_for :messages, :path_names => {:new => 'nuevo'} do |options|
249
+ assert_named_route mark_path, :mark_message_path, mark_options
250
+ end
251
+ end
252
+ end
253
+ end
254
+
255
+ def test_member_when_changed_default_restful_actions_and_path_names_not_specified
256
+ default_path_names = ActionController::Base.resources_path_names
257
+ ActionController::Base.resources_path_names = {:new => 'nuevo', :edit => 'editar'}
258
+
259
+ with_restful_routing :messages do
260
+ new_options = { :action => 'new', :controller => 'messages' }
261
+ new_path = "/messages/nuevo"
262
+ edit_options = { :action => 'edit', :id => '1', :controller => 'messages' }
263
+ edit_path = "/messages/1/editar"
264
+
265
+ assert_restful_routes_for :messages do |options|
266
+ assert_recognizes(options.merge(new_options), :path => new_path, :method => :get)
267
+ end
268
+
269
+ assert_restful_routes_for :messages do |options|
270
+ assert_recognizes(options.merge(edit_options), :path => edit_path, :method => :get)
271
+ end
272
+ end
273
+ ensure
274
+ ActionController::Base.resources_path_names = default_path_names
275
+ end
276
+
277
+ def test_with_two_member_actions_with_same_method
278
+ [:put, :post].each do |method|
279
+ with_restful_routing :messages, :member => { :mark => method, :unmark => method } do
280
+ %w(mark unmark).each do |action|
281
+ action_options = {:action => action, :id => '1'}
282
+ action_path = "/messages/1/#{action}"
283
+ assert_restful_routes_for :messages do |options|
284
+ assert_recognizes(options.merge(action_options), :path => action_path, :method => method)
285
+ end
286
+
287
+ assert_restful_named_routes_for :messages do |options|
288
+ assert_named_route action_path, "#{action}_message_path".to_sym, action_options
289
+ end
290
+ end
291
+ end
292
+ end
293
+ end
294
+
295
+ def test_array_as_collection_or_member_method_value
296
+ with_restful_routing :messages, :collection => { :search => [:get, :post] }, :member => { :toggle => [:get, :post] } do
297
+ assert_restful_routes_for :messages do |options|
298
+ [:get, :post].each do |method|
299
+ assert_recognizes(options.merge(:action => 'search'), :path => "/messages/search", :method => method)
300
+ end
301
+ [:get, :post].each do |method|
302
+ assert_recognizes(options.merge(:action => 'toggle', :id => '1'), :path => '/messages/1/toggle', :method => method)
303
+ end
304
+ end
305
+ end
306
+ end
307
+
308
+ def test_with_new_action
309
+ with_restful_routing :messages, :new => { :preview => :post } do
310
+ preview_options = {:action => 'preview'}
311
+ preview_path = "/messages/new/preview"
312
+ assert_restful_routes_for :messages do |options|
313
+ assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
314
+ end
315
+
316
+ assert_restful_named_routes_for :messages do |options|
317
+ assert_named_route preview_path, :preview_new_message_path, preview_options
318
+ end
319
+ end
320
+ end
321
+
322
+ def test_with_new_action_with_name_prefix
323
+ with_restful_routing :messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
324
+ preview_options = {:action => 'preview', :thread_id => '1'}
325
+ preview_path = "/threads/1/messages/new/preview"
326
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
327
+ assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
328
+ end
329
+
330
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
331
+ assert_named_route preview_path, :preview_new_thread_message_path, preview_options
332
+ end
333
+ end
334
+ end
335
+
336
+ def test_with_formatted_new_action_with_name_prefix
337
+ with_restful_routing :messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
338
+ preview_options = {:action => 'preview', :thread_id => '1', :format => 'xml'}
339
+ preview_path = "/threads/1/messages/new/preview.xml"
340
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
341
+ assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
342
+ end
343
+
344
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
345
+ assert_named_route preview_path, :preview_new_thread_message_path, preview_options
346
+ end
347
+ end
348
+ end
349
+
350
+ def test_override_new_method
351
+ with_restful_routing :messages do
352
+ assert_restful_routes_for :messages do |options|
353
+ assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
354
+ assert_raise(ActionController::MethodNotAllowed) do
355
+ ActionController::Routing::Routes.recognize_path("/messages/new", :method => :post)
356
+ end
357
+ end
358
+ end
359
+
360
+ with_restful_routing :messages, :new => { :new => :any } do
361
+ assert_restful_routes_for :messages do |options|
362
+ assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :post)
363
+ assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
364
+ end
365
+ end
366
+ end
367
+
368
+ def test_nested_restful_routes
369
+ with_routing do |set|
370
+ set.draw do |map|
371
+ map.resources :threads do |map|
372
+ map.resources :messages do |map|
373
+ map.resources :comments
374
+ end
375
+ end
376
+ end
377
+
378
+ assert_simply_restful_for :threads
379
+ assert_simply_restful_for :messages,
380
+ :name_prefix => 'thread_',
381
+ :path_prefix => 'threads/1/',
382
+ :options => { :thread_id => '1' }
383
+ assert_simply_restful_for :comments,
384
+ :name_prefix => 'thread_message_',
385
+ :path_prefix => 'threads/1/messages/2/',
386
+ :options => { :thread_id => '1', :message_id => '2' }
387
+ end
388
+ end
389
+
390
+ def test_nested_restful_routes_with_overwritten_defaults
391
+ with_routing do |set|
392
+ set.draw do |map|
393
+ map.resources :threads do |map|
394
+ map.resources :messages, :name_prefix => nil do |map|
395
+ map.resources :comments, :name_prefix => nil
396
+ end
397
+ end
398
+ end
399
+
400
+ assert_simply_restful_for :threads
401
+ assert_simply_restful_for :messages,
402
+ :path_prefix => 'threads/1/',
403
+ :options => { :thread_id => '1' }
404
+ assert_simply_restful_for :comments,
405
+ :path_prefix => 'threads/1/messages/2/',
406
+ :options => { :thread_id => '1', :message_id => '2' }
407
+ end
408
+ end
409
+
410
+ def test_shallow_nested_restful_routes
411
+ with_routing do |set|
412
+ set.draw do |map|
413
+ map.resources :threads, :shallow => true do |map|
414
+ map.resources :messages do |map|
415
+ map.resources :comments
416
+ end
417
+ end
418
+ end
419
+
420
+ assert_simply_restful_for :threads,
421
+ :shallow => true
422
+ assert_simply_restful_for :messages,
423
+ :name_prefix => 'thread_',
424
+ :path_prefix => 'threads/1/',
425
+ :shallow => true,
426
+ :options => { :thread_id => '1' }
427
+ assert_simply_restful_for :comments,
428
+ :name_prefix => 'message_',
429
+ :path_prefix => 'messages/2/',
430
+ :shallow => true,
431
+ :options => { :message_id => '2' }
432
+ end
433
+ end
434
+
435
+ def test_shallow_nested_restful_routes_with_namespaces
436
+ with_routing do |set|
437
+ set.draw do |map|
438
+ map.namespace :backoffice do |map|
439
+ map.namespace :admin do |map|
440
+ map.resources :products, :shallow => true do |map|
441
+ map.resources :images
442
+ end
443
+ end
444
+ end
445
+ end
446
+
447
+ assert_simply_restful_for :products,
448
+ :controller => 'backoffice/admin/products',
449
+ :namespace => 'backoffice/admin/',
450
+ :name_prefix => 'backoffice_admin_',
451
+ :path_prefix => 'backoffice/admin/',
452
+ :shallow => true
453
+ assert_simply_restful_for :images,
454
+ :controller => 'backoffice/admin/images',
455
+ :namespace => 'backoffice/admin/',
456
+ :name_prefix => 'backoffice_admin_product_',
457
+ :path_prefix => 'backoffice/admin/products/1/',
458
+ :shallow => true,
459
+ :options => { :product_id => '1' }
460
+ end
461
+ end
462
+
463
+ def test_restful_routes_dont_generate_duplicates
464
+ with_restful_routing :messages do
465
+ routes = ActionController::Routing::Routes.routes
466
+ routes.each do |route|
467
+ routes.each do |r|
468
+ next if route === r # skip the comparison instance
469
+ assert distinct_routes?(route, r), "Duplicate Route: #{route}"
470
+ end
471
+ end
472
+ end
473
+ end
474
+
475
+ def test_should_create_singleton_resource_routes
476
+ with_singleton_resources :account do
477
+ assert_singleton_restful_for :account
478
+ end
479
+ end
480
+
481
+ def test_should_create_multiple_singleton_resource_routes
482
+ with_singleton_resources :account, :logo do
483
+ assert_singleton_restful_for :account
484
+ assert_singleton_restful_for :logo
485
+ end
486
+ end
487
+
488
+ def test_should_create_nested_singleton_resource_routes
489
+ with_routing do |set|
490
+ set.draw do |map|
491
+ map.resource :admin, :controller => 'admin' do |admin|
492
+ admin.resource :account
493
+ end
494
+ end
495
+
496
+ assert_singleton_restful_for :admin, :controller => 'admin'
497
+ assert_singleton_restful_for :account, :name_prefix => "admin_", :path_prefix => 'admin/'
498
+ end
499
+ end
500
+
501
+ def test_resource_has_many_should_become_nested_resources
502
+ with_routing do |set|
503
+ set.draw do |map|
504
+ map.resources :messages, :has_many => [ :comments, :authors ]
505
+ end
506
+
507
+ assert_simply_restful_for :messages
508
+ assert_simply_restful_for :comments, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
509
+ assert_simply_restful_for :authors, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
510
+ end
511
+ end
512
+
513
+ def test_resources_has_many_hash_should_become_nested_resources
514
+ with_routing do |set|
515
+ set.draw do |map|
516
+ map.resources :threads, :has_many => { :messages => [ :comments, { :authors => :threads } ] }
517
+ end
518
+
519
+ assert_simply_restful_for :threads
520
+ assert_simply_restful_for :messages, :name_prefix => "thread_", :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
521
+ assert_simply_restful_for :comments, :name_prefix => "thread_message_", :path_prefix => 'threads/1/messages/1/', :options => { :thread_id => '1', :message_id => '1' }
522
+ assert_simply_restful_for :authors, :name_prefix => "thread_message_", :path_prefix => 'threads/1/messages/1/', :options => { :thread_id => '1', :message_id => '1' }
523
+ 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' }
524
+ end
525
+ end
526
+
527
+ def test_shallow_resource_has_many_should_become_shallow_nested_resources
528
+ with_routing do |set|
529
+ set.draw do |map|
530
+ map.resources :messages, :has_many => [ :comments, :authors ], :shallow => true
531
+ end
532
+
533
+ assert_simply_restful_for :messages, :shallow => true
534
+ assert_simply_restful_for :comments, :name_prefix => "message_", :path_prefix => 'messages/1/', :shallow => true, :options => { :message_id => '1' }
535
+ assert_simply_restful_for :authors, :name_prefix => "message_", :path_prefix => 'messages/1/', :shallow => true, :options => { :message_id => '1' }
536
+ end
537
+ end
538
+
539
+ def test_resource_has_one_should_become_nested_resources
540
+ with_routing do |set|
541
+ set.draw do |map|
542
+ map.resources :messages, :has_one => :logo
543
+ end
544
+
545
+ assert_simply_restful_for :messages
546
+ assert_singleton_restful_for :logo, :name_prefix => 'message_', :path_prefix => 'messages/1/', :options => { :message_id => '1' }
547
+ end
548
+ end
549
+
550
+ def test_shallow_resource_has_one_should_become_shallow_nested_resources
551
+ with_routing do |set|
552
+ set.draw do |map|
553
+ map.resources :messages, :has_one => :logo, :shallow => true
554
+ end
555
+
556
+ assert_simply_restful_for :messages, :shallow => true
557
+ assert_singleton_restful_for :logo, :name_prefix => 'message_', :path_prefix => 'messages/1/', :shallow => true, :options => { :message_id => '1' }
558
+ end
559
+ end
560
+
561
+ def test_singleton_resource_with_member_action
562
+ [:put, :post].each do |method|
563
+ with_singleton_resources :account, :member => { :reset => method } do
564
+ reset_options = {:action => 'reset'}
565
+ reset_path = "/account/reset"
566
+ assert_singleton_routes_for :account do |options|
567
+ assert_recognizes(options.merge(reset_options), :path => reset_path, :method => method)
568
+ end
569
+
570
+ assert_singleton_named_routes_for :account do |options|
571
+ assert_named_route reset_path, :reset_account_path, reset_options
572
+ end
573
+ end
574
+ end
575
+ end
576
+
577
+ def test_singleton_resource_with_two_member_actions_with_same_method
578
+ [:put, :post].each do |method|
579
+ with_singleton_resources :account, :member => { :reset => method, :disable => method } do
580
+ %w(reset disable).each do |action|
581
+ action_options = {:action => action}
582
+ action_path = "/account/#{action}"
583
+ assert_singleton_routes_for :account do |options|
584
+ assert_recognizes(options.merge(action_options), :path => action_path, :method => method)
585
+ end
586
+
587
+ assert_singleton_named_routes_for :account do |options|
588
+ assert_named_route action_path, "#{action}_account_path".to_sym, action_options
589
+ end
590
+ end
591
+ end
592
+ end
593
+ end
594
+
595
+ def test_should_nest_resources_in_singleton_resource
596
+ with_routing do |set|
597
+ set.draw do |map|
598
+ map.resource :account do |account|
599
+ account.resources :messages
600
+ end
601
+ end
602
+
603
+ assert_singleton_restful_for :account
604
+ assert_simply_restful_for :messages, :name_prefix => "account_", :path_prefix => 'account/'
605
+ end
606
+ end
607
+
608
+ def test_should_nest_resources_in_singleton_resource_with_path_prefix
609
+ with_routing do |set|
610
+ set.draw do |map|
611
+ map.resource(:account, :path_prefix => ':site_id') do |account|
612
+ account.resources :messages
613
+ end
614
+ end
615
+
616
+ assert_singleton_restful_for :account, :path_prefix => '7/', :options => { :site_id => '7' }
617
+ assert_simply_restful_for :messages, :name_prefix => "account_", :path_prefix => '7/account/', :options => { :site_id => '7' }
618
+ end
619
+ end
620
+
621
+ def test_should_nest_singleton_resource_in_resources
622
+ with_routing do |set|
623
+ set.draw do |map|
624
+ map.resources :threads do |thread|
625
+ thread.resource :admin, :controller => 'admin'
626
+ end
627
+ end
628
+
629
+ assert_simply_restful_for :threads
630
+ assert_singleton_restful_for :admin, :controller => 'admin', :name_prefix => 'thread_', :path_prefix => 'threads/5/', :options => { :thread_id => '5' }
631
+ end
632
+ end
633
+
634
+ def test_should_not_allow_delete_or_put_on_collection_path
635
+ controller_name = :messages
636
+ with_restful_routing controller_name do
637
+ options = { :controller => controller_name.to_s }
638
+ collection_path = "/#{controller_name}"
639
+
640
+ assert_raise(ActionController::MethodNotAllowed) do
641
+ assert_recognizes(options.merge(:action => 'update'), :path => collection_path, :method => :put)
642
+ end
643
+
644
+ assert_raise(ActionController::MethodNotAllowed) do
645
+ assert_recognizes(options.merge(:action => 'destroy'), :path => collection_path, :method => :delete)
646
+ end
647
+ end
648
+ end
649
+
650
+ def test_should_not_allow_invalid_head_method_for_member_routes
651
+ with_routing do |set|
652
+ set.draw do |map|
653
+ assert_raise(ArgumentError) do
654
+ map.resources :messages, :member => {:something => :head}
655
+ end
656
+ end
657
+ end
658
+ end
659
+
660
+ def test_should_not_allow_invalid_http_methods_for_member_routes
661
+ with_routing do |set|
662
+ set.draw do |map|
663
+ assert_raise(ArgumentError) do
664
+ map.resources :messages, :member => {:something => :invalid}
665
+ end
666
+ end
667
+ end
668
+ end
669
+
670
+ def test_resource_action_separator
671
+ with_routing do |set|
672
+ set.draw do |map|
673
+ map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
674
+ map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
675
+ end
676
+
677
+ action_separator = ActionController::Base.resource_action_separator
678
+
679
+ assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
680
+ assert_named_route "/threads/1/messages#{action_separator}search", "search_thread_messages_path", {}
681
+ assert_named_route "/threads/1/messages/new", "new_thread_message_path", {}
682
+ assert_named_route "/threads/1/messages/new#{action_separator}preview", "preview_new_thread_message_path", {}
683
+ assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/'
684
+ assert_named_route "/admin/account#{action_separator}login", "login_admin_account_path", {}
685
+ assert_named_route "/admin/account/new", "new_admin_account_path", {}
686
+ assert_named_route "/admin/account/new#{action_separator}preview", "preview_new_admin_account_path", {}
687
+ end
688
+ end
689
+
690
+ def test_new_style_named_routes_for_resource
691
+ with_routing do |set|
692
+ set.draw do |map|
693
+ map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
694
+ end
695
+ assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
696
+ assert_named_route "/threads/1/messages/search", "search_thread_messages_path", {}
697
+ assert_named_route "/threads/1/messages/new", "new_thread_message_path", {}
698
+ assert_named_route "/threads/1/messages/new/preview", "preview_new_thread_message_path", {}
699
+ end
700
+ end
701
+
702
+ def test_new_style_named_routes_for_singleton_resource
703
+ with_routing do |set|
704
+ set.draw do |map|
705
+ map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
706
+ end
707
+ assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/'
708
+ assert_named_route "/admin/account/login", "login_admin_account_path", {}
709
+ assert_named_route "/admin/account/new", "new_admin_account_path", {}
710
+ assert_named_route "/admin/account/new/preview", "preview_new_admin_account_path", {}
711
+ end
712
+ end
713
+
714
+ def test_resources_in_namespace
715
+ with_routing do |set|
716
+ set.draw do |map|
717
+ map.namespace :backoffice do |backoffice|
718
+ backoffice.resources :products
719
+ end
720
+ end
721
+
722
+ assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
723
+ end
724
+ end
725
+
726
+ def test_resource_has_many_in_namespace
727
+ with_routing do |set|
728
+ set.draw do |map|
729
+ map.namespace :backoffice do |backoffice|
730
+ backoffice.resources :products, :has_many => :tags
731
+ end
732
+ end
733
+
734
+ assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
735
+ assert_simply_restful_for :tags, :controller => "backoffice/tags", :name_prefix => "backoffice_product_", :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
736
+ end
737
+ end
738
+
739
+ def test_resource_has_one_in_namespace
740
+ with_routing do |set|
741
+ set.draw do |map|
742
+ map.namespace :backoffice do |backoffice|
743
+ backoffice.resources :products, :has_one => :manufacturer
744
+ end
745
+ end
746
+
747
+ assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
748
+ assert_singleton_restful_for :manufacturer, :controller => "backoffice/manufacturers", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
749
+ end
750
+ end
751
+
752
+ def test_resources_in_nested_namespace
753
+ with_routing do |set|
754
+ set.draw do |map|
755
+ map.namespace :backoffice do |backoffice|
756
+ backoffice.namespace :admin do |admin|
757
+ admin.resources :products
758
+ end
759
+ end
760
+ end
761
+
762
+ assert_simply_restful_for :products, :controller => "backoffice/admin/products", :name_prefix => 'backoffice_admin_', :path_prefix => 'backoffice/admin/'
763
+ end
764
+ end
765
+
766
+ def test_resources_using_namespace
767
+ with_routing do |set|
768
+ set.draw do |map|
769
+ map.resources :products, :namespace => "backoffice/"
770
+ end
771
+
772
+ assert_simply_restful_for :products, :controller => "backoffice/products"
773
+ end
774
+ end
775
+
776
+ def test_nested_resources_using_namespace
777
+ with_routing do |set|
778
+ set.draw do |map|
779
+ map.namespace :backoffice do |backoffice|
780
+ backoffice.resources :products do |products|
781
+ products.resources :images
782
+ end
783
+ end
784
+ end
785
+
786
+ assert_simply_restful_for :images, :controller => "backoffice/images", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => {:product_id => '1'}
787
+ end
788
+ end
789
+
790
+ def test_nested_resources_in_nested_namespace
791
+ with_routing do |set|
792
+ set.draw do |map|
793
+ map.namespace :backoffice do |backoffice|
794
+ backoffice.namespace :admin do |admin|
795
+ admin.resources :products do |products|
796
+ products.resources :images
797
+ end
798
+ end
799
+ end
800
+ end
801
+
802
+ assert_simply_restful_for :images, :controller => "backoffice/admin/images", :name_prefix => 'backoffice_admin_product_', :path_prefix => 'backoffice/admin/products/1/', :options => {:product_id => '1'}
803
+ end
804
+ end
805
+
806
+ def test_with_path_segment
807
+ with_restful_routing :messages do
808
+ assert_simply_restful_for :messages
809
+ assert_recognizes({:controller => "messages", :action => "index"}, "/messages")
810
+ assert_recognizes({:controller => "messages", :action => "index"}, "/messages/")
811
+ end
812
+
813
+ with_restful_routing :messages, :as => 'reviews' do
814
+ assert_simply_restful_for :messages, :as => 'reviews'
815
+ assert_recognizes({:controller => "messages", :action => "index"}, "/reviews")
816
+ assert_recognizes({:controller => "messages", :action => "index"}, "/reviews/")
817
+ end
818
+ end
819
+
820
+ def test_multiple_with_path_segment_and_controller
821
+ with_routing do |set|
822
+ set.draw do |map|
823
+ map.resources :products do |products|
824
+ products.resources :product_reviews, :as => 'reviews', :controller => 'messages'
825
+ end
826
+ map.resources :tutors do |tutors|
827
+ tutors.resources :tutor_reviews, :as => 'reviews', :controller => 'comments'
828
+ end
829
+ end
830
+
831
+ assert_simply_restful_for :product_reviews, :controller=>'messages', :as => 'reviews', :name_prefix => 'product_', :path_prefix => 'products/1/', :options => {:product_id => '1'}
832
+ assert_simply_restful_for :tutor_reviews,:controller=>'comments', :as => 'reviews', :name_prefix => 'tutor_', :path_prefix => 'tutors/1/', :options => {:tutor_id => '1'}
833
+ end
834
+ end
835
+
836
+ def test_with_path_segment_path_prefix_requirements
837
+ expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
838
+ with_restful_routing :messages, :as => 'comments',:path_prefix => '/thread/:thread_id', :requirements => { :thread_id => /[0-9]\.[0-9]\.[0-9]/ } do
839
+ assert_recognizes(expected_options, :path => 'thread/1.1.1/comments/1', :method => :get)
840
+ end
841
+ end
842
+
843
+ def test_resource_has_only_show_action
844
+ with_routing do |set|
845
+ set.draw do |map|
846
+ map.resources :products, :only => :show
847
+ end
848
+
849
+ assert_resource_allowed_routes('products', {}, { :id => '1' }, :show, [:index, :new, :create, :edit, :update, :destroy])
850
+ assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :show, [:index, :new, :create, :edit, :update, :destroy])
851
+ end
852
+ end
853
+
854
+ def test_singleton_resource_has_only_show_action
855
+ with_routing do |set|
856
+ set.draw do |map|
857
+ map.resource :account, :only => :show
858
+ end
859
+
860
+ assert_singleton_resource_allowed_routes('accounts', {}, :show, [:index, :new, :create, :edit, :update, :destroy])
861
+ assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :show, [:index, :new, :create, :edit, :update, :destroy])
862
+ end
863
+ end
864
+
865
+ def test_resource_does_not_have_destroy_action
866
+ with_routing do |set|
867
+ set.draw do |map|
868
+ map.resources :products, :except => :destroy
869
+ end
870
+
871
+ assert_resource_allowed_routes('products', {}, { :id => '1' }, [:index, :new, :create, :show, :edit, :update], :destroy)
872
+ assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, [:index, :new, :create, :show, :edit, :update], :destroy)
873
+ end
874
+ end
875
+
876
+ def test_singleton_resource_does_not_have_destroy_action
877
+ with_routing do |set|
878
+ set.draw do |map|
879
+ map.resource :account, :except => :destroy
880
+ end
881
+
882
+ assert_singleton_resource_allowed_routes('accounts', {}, [:new, :create, :show, :edit, :update], :destroy)
883
+ assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, [:new, :create, :show, :edit, :update], :destroy)
884
+ end
885
+ end
886
+
887
+ def test_resource_has_only_create_action_and_named_route
888
+ with_routing do |set|
889
+ set.draw do |map|
890
+ map.resources :products, :only => :create
891
+ end
892
+
893
+ assert_resource_allowed_routes('products', {}, { :id => '1' }, :create, [:index, :new, :show, :edit, :update, :destroy])
894
+ assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :create, [:index, :new, :show, :edit, :update, :destroy])
895
+
896
+ assert_not_nil set.named_routes[:products]
897
+ end
898
+ end
899
+
900
+ def test_resource_has_only_update_action_and_named_route
901
+ with_routing do |set|
902
+ set.draw do |map|
903
+ map.resources :products, :only => :update
904
+ end
905
+
906
+ assert_resource_allowed_routes('products', {}, { :id => '1' }, :update, [:index, :new, :create, :show, :edit, :destroy])
907
+ assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :update, [:index, :new, :create, :show, :edit, :destroy])
908
+
909
+ assert_not_nil set.named_routes[:product]
910
+ end
911
+ end
912
+
913
+ def test_resource_has_only_destroy_action_and_named_route
914
+ with_routing do |set|
915
+ set.draw do |map|
916
+ map.resources :products, :only => :destroy
917
+ end
918
+
919
+ assert_resource_allowed_routes('products', {}, { :id => '1' }, :destroy, [:index, :new, :create, :show, :edit, :update])
920
+ assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :destroy, [:index, :new, :create, :show, :edit, :update])
921
+
922
+ assert_not_nil set.named_routes[:product]
923
+ end
924
+ end
925
+
926
+ def test_singleton_resource_has_only_create_action_and_named_route
927
+ with_routing do |set|
928
+ set.draw do |map|
929
+ map.resource :account, :only => :create
930
+ end
931
+
932
+ assert_singleton_resource_allowed_routes('accounts', {}, :create, [:new, :show, :edit, :update, :destroy])
933
+ assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :create, [:new, :show, :edit, :update, :destroy])
934
+
935
+ assert_not_nil set.named_routes[:account]
936
+ end
937
+ end
938
+
939
+ def test_singleton_resource_has_only_update_action_and_named_route
940
+ with_routing do |set|
941
+ set.draw do |map|
942
+ map.resource :account, :only => :update
943
+ end
944
+
945
+ assert_singleton_resource_allowed_routes('accounts', {}, :update, [:new, :create, :show, :edit, :destroy])
946
+ assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :update, [:new, :create, :show, :edit, :destroy])
947
+
948
+ assert_not_nil set.named_routes[:account]
949
+ end
950
+ end
951
+
952
+ def test_singleton_resource_has_only_destroy_action_and_named_route
953
+ with_routing do |set|
954
+ set.draw do |map|
955
+ map.resource :account, :only => :destroy
956
+ end
957
+
958
+ assert_singleton_resource_allowed_routes('accounts', {}, :destroy, [:new, :create, :show, :edit, :update])
959
+ assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :destroy, [:new, :create, :show, :edit, :update])
960
+
961
+ assert_not_nil set.named_routes[:account]
962
+ end
963
+ end
964
+
965
+ def test_resource_has_only_collection_action
966
+ with_routing do |set|
967
+ set.draw do |map|
968
+ map.resources :products, :except => :all, :collection => { :sale => :get }
969
+ end
970
+
971
+ assert_resource_allowed_routes('products', {}, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
972
+ assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
973
+
974
+ assert_recognizes({ :controller => 'products', :action => 'sale' }, :path => 'products/sale', :method => :get)
975
+ assert_recognizes({ :controller => 'products', :action => 'sale', :format => 'xml' }, :path => 'products/sale.xml', :method => :get)
976
+ end
977
+ end
978
+
979
+ def test_resource_has_only_member_action
980
+ with_routing do |set|
981
+ set.draw do |map|
982
+ map.resources :products, :except => :all, :member => { :preview => :get }
983
+ end
984
+
985
+ assert_resource_allowed_routes('products', {}, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
986
+ assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
987
+
988
+ assert_recognizes({ :controller => 'products', :action => 'preview', :id => '1' }, :path => 'products/1/preview', :method => :get)
989
+ assert_recognizes({ :controller => 'products', :action => 'preview', :id => '1', :format => 'xml' }, :path => 'products/1/preview.xml', :method => :get)
990
+ end
991
+ end
992
+
993
+ def test_singleton_resource_has_only_member_action
994
+ with_routing do |set|
995
+ set.draw do |map|
996
+ map.resource :account, :except => :all, :member => { :signup => :get }
997
+ end
998
+
999
+ assert_singleton_resource_allowed_routes('accounts', {}, [], [:new, :create, :show, :edit, :update, :destroy])
1000
+ assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, [], [:new, :create, :show, :edit, :update, :destroy])
1001
+
1002
+ assert_recognizes({ :controller => 'accounts', :action => 'signup' }, :path => 'account/signup', :method => :get)
1003
+ assert_recognizes({ :controller => 'accounts', :action => 'signup', :format => 'xml' }, :path => 'account/signup.xml', :method => :get)
1004
+ end
1005
+ end
1006
+
1007
+ def test_nested_resource_has_only_show_and_member_action
1008
+ with_routing do |set|
1009
+ set.draw do |map|
1010
+ map.resources :products, :only => [:index, :show] do |product|
1011
+ product.resources :images, :member => { :thumbnail => :get }, :only => :show
1012
+ end
1013
+ end
1014
+
1015
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
1016
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
1017
+
1018
+ assert_recognizes({ :controller => 'images', :action => 'thumbnail', :product_id => '1', :id => '2' }, :path => 'products/1/images/2/thumbnail', :method => :get)
1019
+ assert_recognizes({ :controller => 'images', :action => 'thumbnail', :product_id => '1', :id => '2', :format => 'jpg' }, :path => 'products/1/images/2/thumbnail.jpg', :method => :get)
1020
+ end
1021
+ end
1022
+
1023
+ def test_nested_resource_does_not_inherit_only_option
1024
+ with_routing do |set|
1025
+ set.draw do |map|
1026
+ map.resources :products, :only => :show do |product|
1027
+ product.resources :images, :except => :destroy
1028
+ end
1029
+ end
1030
+
1031
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update], :destroy, 'products/1/images')
1032
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update], :destroy, 'products/1/images')
1033
+ end
1034
+ end
1035
+
1036
+ def test_nested_resource_does_not_inherit_only_option_by_default
1037
+ with_routing do |set|
1038
+ set.draw do |map|
1039
+ map.resources :products, :only => :show do |product|
1040
+ product.resources :images
1041
+ end
1042
+ end
1043
+
1044
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destory], [], 'products/1/images')
1045
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
1046
+ end
1047
+ end
1048
+
1049
+ def test_nested_resource_does_not_inherit_except_option
1050
+ with_routing do |set|
1051
+ set.draw do |map|
1052
+ map.resources :products, :except => :show do |product|
1053
+ product.resources :images, :only => :destroy
1054
+ end
1055
+ end
1056
+
1057
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, :destroy, [:index, :new, :create, :show, :edit, :update], 'products/1/images')
1058
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, :destroy, [:index, :new, :create, :show, :edit, :update], 'products/1/images')
1059
+ end
1060
+ end
1061
+
1062
+ def test_nested_resource_does_not_inherit_except_option_by_default
1063
+ with_routing do |set|
1064
+ set.draw do |map|
1065
+ map.resources :products, :except => :show do |product|
1066
+ product.resources :images
1067
+ end
1068
+ end
1069
+
1070
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
1071
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
1072
+ end
1073
+ end
1074
+
1075
+ def test_default_singleton_restful_route_uses_get
1076
+ with_routing do |set|
1077
+ set.draw do |map|
1078
+ map.resource :product
1079
+ end
1080
+
1081
+ assert_equal :get, set.named_routes.routes[:product].conditions[:method]
1082
+ end
1083
+ end
1084
+
1085
+ protected
1086
+ def with_restful_routing(*args)
1087
+ with_routing do |set|
1088
+ set.draw { |map| map.resources(*args) }
1089
+ yield
1090
+ end
1091
+ end
1092
+
1093
+ def with_singleton_resources(*args)
1094
+ with_routing do |set|
1095
+ set.draw { |map| map.resource(*args) }
1096
+ yield
1097
+ end
1098
+ end
1099
+
1100
+ # runs assert_restful_routes_for and assert_restful_named_routes for on the controller_name and options, without passing a block.
1101
+ def assert_simply_restful_for(controller_name, options = {})
1102
+ assert_restful_routes_for controller_name, options
1103
+ assert_restful_named_routes_for controller_name, nil, options
1104
+ end
1105
+
1106
+ def assert_singleton_restful_for(singleton_name, options = {})
1107
+ assert_singleton_routes_for singleton_name, options
1108
+ assert_singleton_named_routes_for singleton_name, options
1109
+ end
1110
+
1111
+ def assert_restful_routes_for(controller_name, options = {})
1112
+ options[:options] ||= {}
1113
+ options[:options][:controller] = options[:controller] || controller_name.to_s
1114
+
1115
+ if options[:shallow]
1116
+ options[:shallow_options] ||= {}
1117
+ options[:shallow_options][:controller] = options[:options][:controller]
1118
+ else
1119
+ options[:shallow_options] = options[:options]
1120
+ end
1121
+
1122
+ new_action = ActionController::Base.resources_path_names[:new] || "new"
1123
+ edit_action = ActionController::Base.resources_path_names[:edit] || "edit"
1124
+ if options[:path_names]
1125
+ new_action = options[:path_names][:new] if options[:path_names][:new]
1126
+ edit_action = options[:path_names][:edit] if options[:path_names][:edit]
1127
+ end
1128
+
1129
+ path = "#{options[:as] || controller_name}"
1130
+ collection_path = "/#{options[:path_prefix]}#{path}"
1131
+ shallow_path = "/#{options[:shallow] ? options[:namespace] : options[:path_prefix]}#{path}"
1132
+ member_path = "#{shallow_path}/1"
1133
+ new_path = "#{collection_path}/#{new_action}"
1134
+ edit_member_path = "#{member_path}/#{edit_action}"
1135
+ formatted_edit_member_path = "#{member_path}/#{edit_action}.xml"
1136
+
1137
+ with_options(options[:options]) do |controller|
1138
+ controller.assert_routing collection_path, :action => 'index'
1139
+ controller.assert_routing new_path, :action => 'new'
1140
+ controller.assert_routing "#{collection_path}.xml", :action => 'index', :format => 'xml'
1141
+ controller.assert_routing "#{new_path}.xml", :action => 'new', :format => 'xml'
1142
+ end
1143
+
1144
+ with_options(options[:shallow_options]) do |controller|
1145
+ controller.assert_routing member_path, :action => 'show', :id => '1'
1146
+ controller.assert_routing edit_member_path, :action => 'edit', :id => '1'
1147
+ controller.assert_routing "#{member_path}.xml", :action => 'show', :id => '1', :format => 'xml'
1148
+ controller.assert_routing formatted_edit_member_path, :action => 'edit', :id => '1', :format => 'xml'
1149
+ end
1150
+
1151
+ assert_recognizes(options[:options].merge(:action => 'index'), :path => collection_path, :method => :get)
1152
+ assert_recognizes(options[:options].merge(:action => 'new'), :path => new_path, :method => :get)
1153
+ assert_recognizes(options[:options].merge(:action => 'create'), :path => collection_path, :method => :post)
1154
+ assert_recognizes(options[:shallow_options].merge(:action => 'show', :id => '1'), :path => member_path, :method => :get)
1155
+ assert_recognizes(options[:shallow_options].merge(:action => 'edit', :id => '1'), :path => edit_member_path, :method => :get)
1156
+ assert_recognizes(options[:shallow_options].merge(:action => 'update', :id => '1'), :path => member_path, :method => :put)
1157
+ assert_recognizes(options[:shallow_options].merge(:action => 'destroy', :id => '1'), :path => member_path, :method => :delete)
1158
+
1159
+ assert_recognizes(options[:options].merge(:action => 'index', :format => 'xml'), :path => "#{collection_path}.xml", :method => :get)
1160
+ assert_recognizes(options[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
1161
+ assert_recognizes(options[:options].merge(:action => 'create', :format => 'xml'), :path => "#{collection_path}.xml", :method => :post)
1162
+ assert_recognizes(options[:shallow_options].merge(:action => 'show', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :get)
1163
+ assert_recognizes(options[:shallow_options].merge(:action => 'edit', :id => '1', :format => 'xml'), :path => formatted_edit_member_path, :method => :get)
1164
+ assert_recognizes(options[:shallow_options].merge(:action => 'update', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :put)
1165
+ assert_recognizes(options[:shallow_options].merge(:action => 'destroy', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :delete)
1166
+
1167
+ yield options[:options] if block_given?
1168
+ end
1169
+
1170
+ # test named routes like foo_path and foos_path map to the correct options.
1171
+ def assert_restful_named_routes_for(controller_name, singular_name = nil, options = {})
1172
+ if singular_name.is_a?(Hash)
1173
+ options = singular_name
1174
+ singular_name = nil
1175
+ end
1176
+ singular_name ||= controller_name.to_s.singularize
1177
+
1178
+ options[:options] ||= {}
1179
+ options[:options][:controller] = options[:controller] || controller_name.to_s
1180
+
1181
+ if options[:shallow]
1182
+ options[:shallow_options] ||= {}
1183
+ options[:shallow_options][:controller] = options[:options][:controller]
1184
+ else
1185
+ options[:shallow_options] = options[:options]
1186
+ end
1187
+
1188
+ @controller = "#{options[:options][:controller].camelize}Controller".constantize.new
1189
+ @request = ActionController::TestRequest.new
1190
+ @response = ActionController::TestResponse.new
1191
+ get :index, options[:options]
1192
+ options[:options].delete :action
1193
+
1194
+ path = "#{options[:as] || controller_name}"
1195
+ shallow_path = "/#{options[:shallow] ? options[:namespace] : options[:path_prefix]}#{path}"
1196
+ full_path = "/#{options[:path_prefix]}#{path}"
1197
+ name_prefix = options[:name_prefix]
1198
+ shallow_prefix = options[:shallow] ? options[:namespace].try(:gsub, /\//, '_') : options[:name_prefix]
1199
+
1200
+ new_action = "new"
1201
+ edit_action = "edit"
1202
+ if options[:path_names]
1203
+ new_action = options[:path_names][:new] || "new"
1204
+ edit_action = options[:path_names][:edit] || "edit"
1205
+ end
1206
+
1207
+ assert_named_route "#{full_path}", "#{name_prefix}#{controller_name}_path", options[:options]
1208
+ assert_named_route "#{full_path}.xml", "#{name_prefix}#{controller_name}_path", options[:options].merge(:format => 'xml')
1209
+ assert_named_route "#{shallow_path}/1", "#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1')
1210
+ assert_named_route "#{shallow_path}/1.xml", "#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1', :format => 'xml')
1211
+
1212
+ assert_named_route "#{full_path}/#{new_action}", "new_#{name_prefix}#{singular_name}_path", options[:options]
1213
+ assert_named_route "#{full_path}/#{new_action}.xml", "new_#{name_prefix}#{singular_name}_path", options[:options].merge(:format => 'xml')
1214
+ assert_named_route "#{shallow_path}/1/#{edit_action}", "edit_#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1')
1215
+ assert_named_route "#{shallow_path}/1/#{edit_action}.xml", "edit_#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1', :format => 'xml')
1216
+
1217
+ yield options[:options] if block_given?
1218
+ end
1219
+
1220
+ def assert_singleton_routes_for(singleton_name, options = {})
1221
+ options[:options] ||= {}
1222
+ options[:options][:controller] = options[:controller] || singleton_name.to_s.pluralize
1223
+
1224
+ full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
1225
+ new_path = "#{full_path}/new"
1226
+ edit_path = "#{full_path}/edit"
1227
+ formatted_edit_path = "#{full_path}/edit.xml"
1228
+
1229
+ with_options options[:options] do |controller|
1230
+ controller.assert_routing full_path, :action => 'show'
1231
+ controller.assert_routing new_path, :action => 'new'
1232
+ controller.assert_routing edit_path, :action => 'edit'
1233
+ controller.assert_routing "#{full_path}.xml", :action => 'show', :format => 'xml'
1234
+ controller.assert_routing "#{new_path}.xml", :action => 'new', :format => 'xml'
1235
+ controller.assert_routing formatted_edit_path, :action => 'edit', :format => 'xml'
1236
+ end
1237
+
1238
+ assert_recognizes(options[:options].merge(:action => 'show'), :path => full_path, :method => :get)
1239
+ assert_recognizes(options[:options].merge(:action => 'new'), :path => new_path, :method => :get)
1240
+ assert_recognizes(options[:options].merge(:action => 'edit'), :path => edit_path, :method => :get)
1241
+ assert_recognizes(options[:options].merge(:action => 'create'), :path => full_path, :method => :post)
1242
+ assert_recognizes(options[:options].merge(:action => 'update'), :path => full_path, :method => :put)
1243
+ assert_recognizes(options[:options].merge(:action => 'destroy'), :path => full_path, :method => :delete)
1244
+
1245
+ assert_recognizes(options[:options].merge(:action => 'show', :format => 'xml'), :path => "#{full_path}.xml", :method => :get)
1246
+ assert_recognizes(options[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
1247
+ assert_recognizes(options[:options].merge(:action => 'edit', :format => 'xml'), :path => formatted_edit_path, :method => :get)
1248
+ assert_recognizes(options[:options].merge(:action => 'create', :format => 'xml'), :path => "#{full_path}.xml", :method => :post)
1249
+ assert_recognizes(options[:options].merge(:action => 'update', :format => 'xml'), :path => "#{full_path}.xml", :method => :put)
1250
+ assert_recognizes(options[:options].merge(:action => 'destroy', :format => 'xml'), :path => "#{full_path}.xml", :method => :delete)
1251
+
1252
+ yield options[:options] if block_given?
1253
+ end
1254
+
1255
+ def assert_singleton_named_routes_for(singleton_name, options = {})
1256
+ (options[:options] ||= {})[:controller] ||= singleton_name.to_s.pluralize
1257
+ @controller = "#{options[:options][:controller].camelize}Controller".constantize.new
1258
+ @request = ActionController::TestRequest.new
1259
+ @response = ActionController::TestResponse.new
1260
+ get :show, options[:options]
1261
+ options[:options].delete :action
1262
+
1263
+ full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
1264
+ name_prefix = options[:name_prefix]
1265
+
1266
+ assert_named_route "#{full_path}", "#{name_prefix}#{singleton_name}_path", options[:options]
1267
+ assert_named_route "#{full_path}.xml", "#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
1268
+
1269
+ assert_named_route "#{full_path}/new", "new_#{name_prefix}#{singleton_name}_path", options[:options]
1270
+ assert_named_route "#{full_path}/new.xml", "new_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
1271
+ assert_named_route "#{full_path}/edit", "edit_#{name_prefix}#{singleton_name}_path", options[:options]
1272
+ assert_named_route "#{full_path}/edit.xml", "edit_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
1273
+ end
1274
+
1275
+ def assert_named_route(expected, route, options)
1276
+ actual = @controller.send(route, options) rescue $!.class.name
1277
+ assert_equal expected, actual, "Error on route: #{route}(#{options.inspect})"
1278
+ end
1279
+
1280
+ def assert_resource_methods(expected, resource, action_method, method)
1281
+ assert_equal expected.length, resource.send("#{action_method}_methods")[method].size, "#{resource.send("#{action_method}_methods")[method].inspect}"
1282
+ expected.each do |action|
1283
+ assert resource.send("#{action_method}_methods")[method].include?(action),
1284
+ "#{method} not in #{action_method} methods: #{resource.send("#{action_method}_methods")[method].inspect}"
1285
+ end
1286
+ end
1287
+
1288
+ def assert_resource_allowed_routes(controller, options, shallow_options, allowed, not_allowed, path = controller)
1289
+ shallow_path = "#{path}/#{shallow_options[:id]}"
1290
+ format = options[:format] && ".#{options[:format]}"
1291
+ options.merge!(:controller => controller)
1292
+ shallow_options.merge!(options)
1293
+
1294
+ assert_whether_allowed(allowed, not_allowed, options, 'index', "#{path}#{format}", :get)
1295
+ assert_whether_allowed(allowed, not_allowed, options, 'new', "#{path}/new#{format}", :get)
1296
+ assert_whether_allowed(allowed, not_allowed, options, 'create', "#{path}#{format}", :post)
1297
+ assert_whether_allowed(allowed, not_allowed, shallow_options, 'show', "#{shallow_path}#{format}", :get)
1298
+ assert_whether_allowed(allowed, not_allowed, shallow_options, 'edit', "#{shallow_path}/edit#{format}", :get)
1299
+ assert_whether_allowed(allowed, not_allowed, shallow_options, 'update', "#{shallow_path}#{format}", :put)
1300
+ assert_whether_allowed(allowed, not_allowed, shallow_options, 'destroy', "#{shallow_path}#{format}", :delete)
1301
+ end
1302
+
1303
+ def assert_singleton_resource_allowed_routes(controller, options, allowed, not_allowed, path = controller.singularize)
1304
+ format = options[:format] && ".#{options[:format]}"
1305
+ options.merge!(:controller => controller)
1306
+
1307
+ assert_whether_allowed(allowed, not_allowed, options, 'new', "#{path}/new#{format}", :get)
1308
+ assert_whether_allowed(allowed, not_allowed, options, 'create', "#{path}#{format}", :post)
1309
+ assert_whether_allowed(allowed, not_allowed, options, 'show', "#{path}#{format}", :get)
1310
+ assert_whether_allowed(allowed, not_allowed, options, 'edit', "#{path}/edit#{format}", :get)
1311
+ assert_whether_allowed(allowed, not_allowed, options, 'update', "#{path}#{format}", :put)
1312
+ assert_whether_allowed(allowed, not_allowed, options, 'destroy', "#{path}#{format}", :delete)
1313
+ end
1314
+
1315
+ def assert_whether_allowed(allowed, not_allowed, options, action, path, method)
1316
+ action = action.to_sym
1317
+ options = options.merge(:action => action.to_s)
1318
+ path_options = { :path => path, :method => method }
1319
+
1320
+ if Array(allowed).include?(action)
1321
+ assert_recognizes options, path_options
1322
+ elsif Array(not_allowed).include?(action)
1323
+ assert_not_recognizes options, path_options
1324
+ end
1325
+ end
1326
+
1327
+ def assert_not_recognizes(expected_options, path)
1328
+ assert_raise ActionController::RoutingError, ActionController::MethodNotAllowed, Assertion do
1329
+ assert_recognizes(expected_options, path)
1330
+ end
1331
+ end
1332
+
1333
+ def distinct_routes? (r1, r2)
1334
+ if r1.conditions == r2.conditions and r1.requirements == r2.requirements then
1335
+ if r1.segments.collect(&:to_s) == r2.segments.collect(&:to_s) then
1336
+ return false
1337
+ end
1338
+ end
1339
+ true
1340
+ end
1341
+ end