2006-07-31 18:59:58 +00:00
|
|
|
require File.dirname(__FILE__) + '/../abstract_unit'
|
|
|
|
|
2006-08-01 01:23:06 +00:00
|
|
|
class ResourcesController < ActionController::Base
|
2006-07-31 20:00:18 +00:00
|
|
|
def index() render :nothing => true end
|
2006-12-07 15:36:08 +00:00
|
|
|
alias_method :show, :index
|
2006-07-31 18:59:58 +00:00
|
|
|
def rescue_action(e) raise e end
|
|
|
|
end
|
|
|
|
|
2006-08-01 01:23:06 +00:00
|
|
|
class ThreadsController < ResourcesController; end
|
|
|
|
class MessagesController < ResourcesController; end
|
|
|
|
class CommentsController < ResourcesController; end
|
2007-04-26 23:11:31 +00:00
|
|
|
class AuthorsController < ResourcesController; end
|
|
|
|
class LogoController < ResourcesController; end
|
2006-08-01 01:23:06 +00:00
|
|
|
|
2006-12-07 15:36:08 +00:00
|
|
|
class AccountController < ResourcesController; end
|
|
|
|
class AdminController < ResourcesController; end
|
2006-07-31 18:59:58 +00:00
|
|
|
|
2007-04-27 16:58:36 +00:00
|
|
|
module Backoffice
|
|
|
|
class ProductsController < ResourcesController; end
|
2007-05-22 09:37:07 +00:00
|
|
|
class TagsController < ResourcesController; end
|
|
|
|
class ManufacturerController < ResourcesController; end
|
|
|
|
|
2007-05-17 01:27:54 +00:00
|
|
|
module Admin
|
2007-05-22 09:37:07 +00:00
|
|
|
class ProductsController < ResourcesController; end
|
2007-05-17 01:27:54 +00:00
|
|
|
end
|
2007-04-27 16:58:36 +00:00
|
|
|
end
|
|
|
|
|
2006-07-31 18:59:58 +00:00
|
|
|
class ResourcesTest < Test::Unit::TestCase
|
2006-07-31 21:57:59 +00:00
|
|
|
def test_should_arrange_actions
|
2006-09-14 19:53:04 +00:00
|
|
|
resource = ActionController::Resources::Resource.new(:messages,
|
2006-07-31 21:57:59 +00:00
|
|
|
:collection => { :rss => :get, :reorder => :post, :csv => :post },
|
|
|
|
:member => { :rss => :get, :atom => :get, :upload => :post, :fix => :post },
|
|
|
|
:new => { :preview => :get, :draft => :get })
|
|
|
|
|
2006-09-26 22:45:09 +00:00
|
|
|
assert_resource_methods [:rss], resource, :collection, :get
|
|
|
|
assert_resource_methods [:csv, :reorder], resource, :collection, :post
|
|
|
|
assert_resource_methods [:edit, :rss, :atom], resource, :member, :get
|
|
|
|
assert_resource_methods [:upload, :fix], resource, :member, :post
|
|
|
|
assert_resource_methods [:new, :preview, :draft], resource, :new, :get
|
2006-07-31 21:57:59 +00:00
|
|
|
end
|
2006-09-14 19:53:04 +00:00
|
|
|
|
2007-01-28 08:20:04 +00:00
|
|
|
def test_should_resource_controller_name_equal_resource_name_by_default
|
|
|
|
resource = ActionController::Resources::Resource.new(:messages, {})
|
|
|
|
assert_equal 'messages', resource.controller
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_resource_controller_name_equal_controller_option
|
|
|
|
resource = ActionController::Resources::Resource.new(:messages, :controller => 'posts')
|
|
|
|
assert_equal 'posts', resource.controller
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_all_singleton_paths_be_the_same
|
|
|
|
[ :path, :nesting_path_prefix, :member_path ].each do |method|
|
|
|
|
resource = ActionController::Resources::SingletonResource.new(:messages, :path_prefix => 'admin')
|
|
|
|
assert_equal 'admin/messages', resource.send(method)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-31 18:59:58 +00:00
|
|
|
def test_default_restful_routes
|
|
|
|
with_restful_routing :messages do
|
2006-07-31 20:00:18 +00:00
|
|
|
assert_simply_restful_for :messages
|
2006-07-31 18:59:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-31 20:32:48 +00:00
|
|
|
def test_multiple_default_restful_routes
|
|
|
|
with_restful_routing :messages, :comments do
|
|
|
|
assert_simply_restful_for :messages
|
|
|
|
assert_simply_restful_for :comments
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-02-25 20:13:19 +00:00
|
|
|
def test_irregular_id_with_no_requirements_should_raise_error
|
|
|
|
expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2007-02-25 20:13:19 +00:00
|
|
|
with_restful_routing :messages do
|
|
|
|
assert_raises(ActionController::RoutingError) do
|
|
|
|
assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2007-02-25 20:13:19 +00:00
|
|
|
def test_irregular_id_with_requirements_should_pass
|
|
|
|
expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2007-02-25 20:13:19 +00:00
|
|
|
with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}) do
|
|
|
|
assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
|
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2007-02-25 20:13:19 +00:00
|
|
|
def test_with_path_prefix_requirements
|
|
|
|
expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
|
|
|
|
with_restful_routing :messages, :path_prefix => '/thread/:thread_id', :requirements => {:thread_id => /[0-9]\.[0-9]\.[0-9]/} do
|
|
|
|
assert_recognizes(expected_options, :path => 'thread/1.1.1/messages/1', :method => :get)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-31 18:59:58 +00:00
|
|
|
def test_with_path_prefix
|
|
|
|
with_restful_routing :messages, :path_prefix => '/thread/:thread_id' do
|
2006-07-31 20:00:18 +00:00
|
|
|
assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
|
2006-07-31 18:59:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-31 20:32:48 +00:00
|
|
|
def test_multile_with_path_prefix
|
|
|
|
with_restful_routing :messages, :comments, :path_prefix => '/thread/:thread_id' do
|
|
|
|
assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
|
|
|
|
assert_simply_restful_for :comments, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
|
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2007-01-17 06:46:02 +00:00
|
|
|
def test_with_name_prefix
|
|
|
|
with_restful_routing :messages, :name_prefix => 'post_' do
|
|
|
|
assert_simply_restful_for :messages, :name_prefix => 'post_'
|
|
|
|
end
|
|
|
|
end
|
2006-07-31 20:32:48 +00:00
|
|
|
|
2006-07-31 18:59:58 +00:00
|
|
|
def test_with_collection_action
|
2006-09-14 19:53:04 +00:00
|
|
|
rss_options = {:action => 'rss'}
|
2007-03-28 21:55:40 +00:00
|
|
|
rss_path = "/messages/rss"
|
2006-09-14 19:53:04 +00:00
|
|
|
actions = { 'a' => :put, 'b' => :post, 'c' => :delete }
|
|
|
|
|
|
|
|
with_restful_routing :messages, :collection => { :rss => :get }.merge(actions) do
|
2006-07-31 18:59:58 +00:00
|
|
|
assert_restful_routes_for :messages do |options|
|
2006-07-31 20:00:18 +00:00
|
|
|
assert_routing rss_path, options.merge(rss_options)
|
2006-09-14 19:53:04 +00:00
|
|
|
|
|
|
|
actions.each do |action, method|
|
2007-03-28 21:55:40 +00:00
|
|
|
assert_recognizes(options.merge(:action => action), :path => "/messages/#{action}", :method => method)
|
2006-09-14 19:53:04 +00:00
|
|
|
end
|
2006-07-31 20:00:18 +00:00
|
|
|
end
|
2006-09-14 19:53:04 +00:00
|
|
|
|
2006-07-31 20:00:18 +00:00
|
|
|
assert_restful_named_routes_for :messages do |options|
|
|
|
|
assert_named_route rss_path, :rss_messages_path, rss_options
|
2006-09-14 19:53:04 +00:00
|
|
|
actions.keys.each do |action|
|
2007-03-28 21:55:40 +00:00
|
|
|
assert_named_route "/messages/#{action}", "#{action}_messages_path", :action => action
|
2006-09-14 19:53:04 +00:00
|
|
|
end
|
2006-07-31 18:59:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_member_action
|
|
|
|
[:put, :post].each do |method|
|
|
|
|
with_restful_routing :messages, :member => { :mark => method } do
|
2006-07-31 20:00:18 +00:00
|
|
|
mark_options = {:action => 'mark', :id => '1'}
|
2007-03-28 21:55:40 +00:00
|
|
|
mark_path = "/messages/1/mark"
|
2006-07-31 18:59:58 +00:00
|
|
|
assert_restful_routes_for :messages do |options|
|
2006-07-31 20:00:18 +00:00
|
|
|
assert_recognizes(options.merge(mark_options), :path => mark_path, :method => method)
|
|
|
|
end
|
2006-09-14 19:53:04 +00:00
|
|
|
|
2006-07-31 20:00:18 +00:00
|
|
|
assert_restful_named_routes_for :messages do |options|
|
|
|
|
assert_named_route mark_path, :mark_message_path, mark_options
|
2006-07-31 18:59:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-31 21:57:59 +00:00
|
|
|
def test_with_two_member_actions_with_same_method
|
|
|
|
[:put, :post].each do |method|
|
|
|
|
with_restful_routing :messages, :member => { :mark => method, :unmark => method } do
|
|
|
|
%w(mark unmark).each do |action|
|
|
|
|
action_options = {:action => action, :id => '1'}
|
2007-03-28 21:55:40 +00:00
|
|
|
action_path = "/messages/1/#{action}"
|
2006-07-31 21:57:59 +00:00
|
|
|
assert_restful_routes_for :messages do |options|
|
|
|
|
assert_recognizes(options.merge(action_options), :path => action_path, :method => method)
|
|
|
|
end
|
2006-09-14 19:53:04 +00:00
|
|
|
|
2006-07-31 21:57:59 +00:00
|
|
|
assert_restful_named_routes_for :messages do |options|
|
|
|
|
assert_named_route action_path, "#{action}_message_path".to_sym, action_options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-31 18:59:58 +00:00
|
|
|
def test_with_new_action
|
|
|
|
with_restful_routing :messages, :new => { :preview => :post } do
|
2006-07-31 20:00:18 +00:00
|
|
|
preview_options = {:action => 'preview'}
|
2007-03-28 21:55:40 +00:00
|
|
|
preview_path = "/messages/new/preview"
|
2006-07-31 18:59:58 +00:00
|
|
|
assert_restful_routes_for :messages do |options|
|
2006-07-31 20:00:18 +00:00
|
|
|
assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
|
|
|
|
end
|
2006-09-14 19:53:04 +00:00
|
|
|
|
2006-07-31 20:00:18 +00:00
|
|
|
assert_restful_named_routes_for :messages do |options|
|
|
|
|
assert_named_route preview_path, :preview_new_message_path, preview_options
|
2006-07-31 18:59:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-09-26 22:45:09 +00:00
|
|
|
def test_override_new_method
|
|
|
|
with_restful_routing :messages do
|
|
|
|
assert_restful_routes_for :messages do |options|
|
|
|
|
assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
|
2007-05-26 20:07:34 +00:00
|
|
|
assert_raises(ActionController::MethodNotAllowed) do
|
2006-09-26 22:45:09 +00:00
|
|
|
ActionController::Routing::Routes.recognize_path("/messages/new", :method => :post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
with_restful_routing :messages, :new => { :new => :any } do
|
|
|
|
assert_restful_routes_for :messages do |options|
|
|
|
|
assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :post)
|
|
|
|
assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-08-01 00:29:02 +00:00
|
|
|
def test_nested_restful_routes
|
2006-07-31 18:59:58 +00:00
|
|
|
with_routing do |set|
|
2006-08-01 01:23:06 +00:00
|
|
|
set.draw do |map|
|
|
|
|
map.resources :threads do |map|
|
|
|
|
map.resources :messages do |map|
|
|
|
|
map.resources :comments
|
|
|
|
end
|
2006-07-31 18:59:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-08-01 01:23:06 +00:00
|
|
|
assert_simply_restful_for :threads
|
|
|
|
assert_simply_restful_for :messages,
|
2007-04-27 05:06:38 +00:00
|
|
|
:name_prefix => 'thread_',
|
2006-08-01 01:23:06 +00:00
|
|
|
:path_prefix => 'threads/1/',
|
|
|
|
:options => { :thread_id => '1' }
|
|
|
|
assert_simply_restful_for :comments,
|
2007-04-27 05:07:56 +00:00
|
|
|
:name_prefix => 'thread_message_',
|
2006-08-01 01:23:06 +00:00
|
|
|
:path_prefix => 'threads/1/messages/2/',
|
|
|
|
:options => { :thread_id => '1', :message_id => '2' }
|
2006-07-31 18:59:58 +00:00
|
|
|
end
|
|
|
|
end
|
2006-09-14 19:53:04 +00:00
|
|
|
|
2007-04-27 05:10:23 +00:00
|
|
|
def test_nested_restful_routes_with_overwritten_defaults
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
|
|
|
map.resources :threads do |map|
|
|
|
|
map.resources :messages, :name_prefix => nil do |map|
|
|
|
|
map.resources :comments, :name_prefix => nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_simply_restful_for :threads
|
|
|
|
assert_simply_restful_for :messages,
|
|
|
|
:path_prefix => 'threads/1/',
|
|
|
|
:options => { :thread_id => '1' }
|
|
|
|
assert_simply_restful_for :comments,
|
|
|
|
:path_prefix => 'threads/1/messages/2/',
|
|
|
|
:options => { :thread_id => '1', :message_id => '2' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-08-05 20:46:49 +00:00
|
|
|
def test_restful_routes_dont_generate_duplicates
|
|
|
|
with_restful_routing :messages do
|
|
|
|
routes = ActionController::Routing::Routes.routes
|
|
|
|
routes.each do |route|
|
|
|
|
routes.each do |r|
|
2006-09-14 19:53:04 +00:00
|
|
|
next if route === r # skip the comparison instance
|
2006-08-05 20:46:49 +00:00
|
|
|
assert distinct_routes?(route, r), "Duplicate Route: #{route}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2006-07-31 18:59:58 +00:00
|
|
|
|
2006-12-07 15:36:08 +00:00
|
|
|
def test_should_create_singleton_resource_routes
|
|
|
|
with_singleton_resources :account do
|
|
|
|
assert_singleton_restful_for :account
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_create_multiple_singleton_resource_routes
|
|
|
|
with_singleton_resources :account, :admin do
|
|
|
|
assert_singleton_restful_for :account
|
|
|
|
assert_singleton_restful_for :admin
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_create_nested_singleton_resource_routes
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
|
|
|
map.resource :admin do |admin|
|
|
|
|
admin.resource :account
|
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2006-12-07 15:36:08 +00:00
|
|
|
assert_singleton_restful_for :admin
|
2007-04-27 05:06:38 +00:00
|
|
|
assert_singleton_restful_for :account, :name_prefix => "admin_", :path_prefix => 'admin/'
|
2006-12-07 15:36:08 +00:00
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2007-04-26 23:11:31 +00:00
|
|
|
def test_resource_has_many_should_become_nested_resources
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
|
|
|
map.resources :messages, :has_many => [ :comments, :authors ]
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_simply_restful_for :messages
|
2007-04-27 05:06:38 +00:00
|
|
|
assert_simply_restful_for :comments, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
|
|
|
|
assert_simply_restful_for :authors, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
|
2007-04-26 23:11:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_resource_has_one_should_become_nested_resources
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
|
|
|
map.resources :messages, :has_one => :logo
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_simply_restful_for :messages
|
2007-04-27 05:06:38 +00:00
|
|
|
assert_singleton_restful_for :logo, :name_prefix => 'message_', :path_prefix => 'messages/1/', :options => { :message_id => '1' }
|
2007-04-26 23:11:31 +00:00
|
|
|
end
|
|
|
|
end
|
2006-12-07 15:36:08 +00:00
|
|
|
|
|
|
|
def test_singleton_resource_with_member_action
|
|
|
|
[:put, :post].each do |method|
|
|
|
|
with_singleton_resources :account, :member => { :reset => method } do
|
|
|
|
reset_options = {:action => 'reset'}
|
2007-03-28 21:55:40 +00:00
|
|
|
reset_path = "/account/reset"
|
2006-12-07 15:36:08 +00:00
|
|
|
assert_singleton_routes_for :account do |options|
|
|
|
|
assert_recognizes(options.merge(reset_options), :path => reset_path, :method => method)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_singleton_named_routes_for :account do |options|
|
|
|
|
assert_named_route reset_path, :reset_account_path, reset_options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_singleton_resource_with_two_member_actions_with_same_method
|
|
|
|
[:put, :post].each do |method|
|
|
|
|
with_singleton_resources :account, :member => { :reset => method, :disable => method } do
|
|
|
|
%w(reset disable).each do |action|
|
|
|
|
action_options = {:action => action}
|
2007-03-28 21:55:40 +00:00
|
|
|
action_path = "/account/#{action}"
|
2006-12-07 15:36:08 +00:00
|
|
|
assert_singleton_routes_for :account do |options|
|
|
|
|
assert_recognizes(options.merge(action_options), :path => action_path, :method => method)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_singleton_named_routes_for :account do |options|
|
|
|
|
assert_named_route action_path, "#{action}_account_path".to_sym, action_options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2006-12-07 16:31:41 +00:00
|
|
|
|
|
|
|
def test_should_nest_resources_in_singleton_resource
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
|
|
|
map.resource :account do |account|
|
|
|
|
account.resources :messages
|
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2006-12-07 16:31:41 +00:00
|
|
|
assert_singleton_restful_for :account
|
2007-04-27 05:06:38 +00:00
|
|
|
assert_simply_restful_for :messages, :name_prefix => "account_", :path_prefix => 'account/'
|
2006-12-07 16:31:41 +00:00
|
|
|
end
|
|
|
|
end
|
2007-01-08 01:07:48 +00:00
|
|
|
|
|
|
|
def test_should_nest_resources_in_singleton_resource_with_path_prefix
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
|
|
|
map.resource(:account, :path_prefix => ':site_id') do |account|
|
|
|
|
account.resources :messages
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_singleton_restful_for :account, :path_prefix => '7/', :options => { :site_id => '7' }
|
2007-04-27 05:06:38 +00:00
|
|
|
assert_simply_restful_for :messages, :name_prefix => "account_", :path_prefix => '7/account/', :options => { :site_id => '7' }
|
2007-01-08 01:07:48 +00:00
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2006-12-07 16:31:41 +00:00
|
|
|
def test_should_nest_singleton_resource_in_resources
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
|
|
|
map.resources :threads do |thread|
|
|
|
|
thread.resource :admin
|
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2006-12-07 16:31:41 +00:00
|
|
|
assert_simply_restful_for :threads
|
2007-04-27 05:06:38 +00:00
|
|
|
assert_singleton_restful_for :admin, :name_prefix => 'thread_', :path_prefix => 'threads/5/', :options => { :thread_id => '5' }
|
2006-12-07 16:31:41 +00:00
|
|
|
end
|
|
|
|
end
|
2006-12-07 15:36:08 +00:00
|
|
|
|
2007-01-28 08:20:04 +00:00
|
|
|
def test_should_not_allow_delete_or_put_on_collection_path
|
|
|
|
controller_name = :messages
|
|
|
|
with_restful_routing controller_name do
|
|
|
|
options = { :controller => controller_name.to_s }
|
|
|
|
collection_path = "/#{controller_name}"
|
|
|
|
|
2007-05-26 20:07:34 +00:00
|
|
|
assert_raises(ActionController::MethodNotAllowed) do
|
2007-01-28 08:20:04 +00:00
|
|
|
assert_recognizes(options.merge(:action => 'update'), :path => collection_path, :method => :put)
|
|
|
|
end
|
|
|
|
|
2007-05-26 20:07:34 +00:00
|
|
|
assert_raises(ActionController::MethodNotAllowed) do
|
2007-01-28 08:20:04 +00:00
|
|
|
assert_recognizes(options.merge(:action => 'destroy'), :path => collection_path, :method => :delete)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-04-27 16:58:36 +00:00
|
|
|
def test_resources_in_namespace
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
|
|
|
map.namespace :backoffice do |backoffice|
|
|
|
|
backoffice.resources :products
|
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2007-04-27 16:58:36 +00:00
|
|
|
assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
|
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:37:07 +00:00
|
|
|
|
|
|
|
def test_resource_has_many_in_namespace
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
|
|
|
map.namespace :backoffice do |backoffice|
|
|
|
|
backoffice.resources :products, :has_many => :tags
|
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2007-05-22 09:37:07 +00:00
|
|
|
assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
|
|
|
|
assert_simply_restful_for :tags, :controller => "backoffice/tags", :name_prefix => "backoffice_product_", :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_resource_has_one_in_namespace
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
|
|
|
map.namespace :backoffice do |backoffice|
|
|
|
|
backoffice.resources :products, :has_one => :manufacturer
|
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2007-05-22 09:37:07 +00:00
|
|
|
assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
|
|
|
|
assert_singleton_restful_for :manufacturer, :controller => "backoffice/manufacturer", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
|
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2007-05-17 01:27:54 +00:00
|
|
|
def test_resources_in_nested_namespace
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
|
|
|
map.namespace :backoffice do |backoffice|
|
|
|
|
backoffice.namespace :admin do |admin|
|
|
|
|
admin.resources :products
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2007-05-17 01:27:54 +00:00
|
|
|
assert_simply_restful_for :products, :controller => "backoffice/admin/products", :name_prefix => 'backoffice_admin_', :path_prefix => 'backoffice/admin/'
|
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2007-04-27 16:58:36 +00:00
|
|
|
def test_resources_using_namespace
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
|
|
|
map.resources :products, :namespace => "backoffice/"
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2007-04-27 16:58:36 +00:00
|
|
|
assert_simply_restful_for :products, :controller => "backoffice/products"
|
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2006-07-31 18:59:58 +00:00
|
|
|
protected
|
2006-07-31 20:32:48 +00:00
|
|
|
def with_restful_routing(*args)
|
2006-07-31 18:59:58 +00:00
|
|
|
with_routing do |set|
|
2006-07-31 20:32:48 +00:00
|
|
|
set.draw { |map| map.resources(*args) }
|
2006-07-31 18:59:58 +00:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
2007-05-22 09:38:54 +00:00
|
|
|
|
2006-12-07 15:36:08 +00:00
|
|
|
def with_singleton_resources(*args)
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw { |map| map.resource(*args) }
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
2006-07-31 20:00:18 +00:00
|
|
|
|
|
|
|
# runs assert_restful_routes_for and assert_restful_named_routes for on the controller_name and options, without passing a block.
|
|
|
|
def assert_simply_restful_for(controller_name, options = {})
|
|
|
|
assert_restful_routes_for controller_name, options
|
2007-04-27 16:58:36 +00:00
|
|
|
assert_restful_named_routes_for controller_name, nil, options
|
2006-07-31 20:00:18 +00:00
|
|
|
end
|
|
|
|
|
2006-12-07 15:36:08 +00:00
|
|
|
def assert_singleton_restful_for(singleton_name, options = {})
|
|
|
|
assert_singleton_routes_for singleton_name, options
|
|
|
|
assert_singleton_named_routes_for singleton_name, options
|
|
|
|
end
|
|
|
|
|
2006-07-31 18:59:58 +00:00
|
|
|
def assert_restful_routes_for(controller_name, options = {})
|
2007-04-27 16:58:36 +00:00
|
|
|
options[:options] ||= {}
|
|
|
|
options[:options][:controller] = options[:controller] || controller_name.to_s
|
2006-07-31 18:59:58 +00:00
|
|
|
|
2007-01-28 08:20:04 +00:00
|
|
|
collection_path = "/#{options[:path_prefix]}#{controller_name}"
|
|
|
|
member_path = "#{collection_path}/1"
|
|
|
|
new_path = "#{collection_path}/new"
|
2007-03-28 21:55:40 +00:00
|
|
|
edit_member_path = "#{member_path}/edit"
|
|
|
|
formatted_edit_member_path = "#{member_path}/edit.xml"
|
2006-09-14 19:53:04 +00:00
|
|
|
|
2006-07-31 18:59:58 +00:00
|
|
|
with_options(options[:options]) do |controller|
|
2006-09-14 19:53:04 +00:00
|
|
|
controller.assert_routing collection_path, :action => 'index'
|
|
|
|
controller.assert_routing new_path, :action => 'new'
|
|
|
|
controller.assert_routing member_path, :action => 'show', :id => '1'
|
2007-01-28 08:20:04 +00:00
|
|
|
controller.assert_routing edit_member_path, :action => 'edit', :id => '1'
|
|
|
|
controller.assert_routing "#{collection_path}.xml", :action => 'index', :format => 'xml'
|
|
|
|
controller.assert_routing "#{new_path}.xml", :action => 'new', :format => 'xml'
|
2006-09-14 19:53:04 +00:00
|
|
|
controller.assert_routing "#{member_path}.xml", :action => 'show', :id => '1', :format => 'xml'
|
2007-01-28 08:20:04 +00:00
|
|
|
controller.assert_routing formatted_edit_member_path, :action => 'edit', :id => '1', :format => 'xml'
|
2006-07-31 18:59:58 +00:00
|
|
|
end
|
|
|
|
|
2007-01-28 08:20:04 +00:00
|
|
|
assert_recognizes(options[:options].merge(:action => 'index'), :path => collection_path, :method => :get)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'new'), :path => new_path, :method => :get)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'create'), :path => collection_path, :method => :post)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'show', :id => '1'), :path => member_path, :method => :get)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'edit', :id => '1'), :path => edit_member_path, :method => :get)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'update', :id => '1'), :path => member_path, :method => :put)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1'), :path => member_path, :method => :delete)
|
|
|
|
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'index', :format => 'xml'), :path => "#{collection_path}.xml", :method => :get)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'create', :format => 'xml'), :path => "#{collection_path}.xml", :method => :post)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'show', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :get)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'edit', :id => '1', :format => 'xml'), :path => formatted_edit_member_path, :method => :get)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'update', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :put)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :delete)
|
2006-07-31 20:00:18 +00:00
|
|
|
|
|
|
|
yield options[:options] if block_given?
|
|
|
|
end
|
2006-09-14 19:53:04 +00:00
|
|
|
|
2006-07-31 20:00:18 +00:00
|
|
|
# test named routes like foo_path and foos_path map to the correct options.
|
|
|
|
def assert_restful_named_routes_for(controller_name, singular_name = nil, options = {})
|
|
|
|
if singular_name.is_a?(Hash)
|
|
|
|
options = singular_name
|
|
|
|
singular_name = nil
|
|
|
|
end
|
|
|
|
singular_name ||= controller_name.to_s.singularize
|
2007-04-27 16:58:36 +00:00
|
|
|
|
|
|
|
options[:options] ||= {}
|
|
|
|
options[:options][:controller] = options[:controller] || controller_name.to_s
|
|
|
|
|
|
|
|
@controller = "#{options[:options][:controller].camelize}Controller".constantize.new
|
2006-07-31 20:00:18 +00:00
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
get :index, options[:options]
|
|
|
|
options[:options].delete :action
|
|
|
|
|
|
|
|
full_prefix = "/#{options[:path_prefix]}#{controller_name}"
|
2007-01-17 06:46:02 +00:00
|
|
|
name_prefix = options[:name_prefix]
|
|
|
|
|
2007-01-28 08:20:04 +00:00
|
|
|
assert_named_route "#{full_prefix}", "#{name_prefix}#{controller_name}_path", options[:options]
|
|
|
|
assert_named_route "#{full_prefix}/new", "#{name_prefix}new_#{singular_name}_path", options[:options]
|
|
|
|
assert_named_route "#{full_prefix}/1", "#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1')
|
2007-03-28 21:55:40 +00:00
|
|
|
assert_named_route "#{full_prefix}/1/edit", "#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1')
|
2007-01-28 08:20:04 +00:00
|
|
|
assert_named_route "#{full_prefix}.xml", "formatted_#{name_prefix}#{controller_name}_path", options[:options].merge( :format => 'xml')
|
|
|
|
assert_named_route "#{full_prefix}/new.xml", "formatted_#{name_prefix}new_#{singular_name}_path", options[:options].merge( :format => 'xml')
|
|
|
|
assert_named_route "#{full_prefix}/1.xml", "formatted_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
|
2007-03-28 21:55:40 +00:00
|
|
|
assert_named_route "#{full_prefix}/1/edit.xml", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
|
2006-07-31 18:59:58 +00:00
|
|
|
yield options[:options] if block_given?
|
|
|
|
end
|
2006-07-31 20:00:18 +00:00
|
|
|
|
2006-12-07 15:36:08 +00:00
|
|
|
def assert_singleton_routes_for(singleton_name, options = {})
|
2007-05-22 09:37:07 +00:00
|
|
|
options[:options] ||= {}
|
|
|
|
options[:options][:controller] = options[:controller] || singleton_name.to_s
|
2007-01-28 08:20:04 +00:00
|
|
|
|
|
|
|
full_path = "/#{options[:path_prefix]}#{singleton_name}"
|
|
|
|
new_path = "#{full_path}/new"
|
2007-03-28 21:55:40 +00:00
|
|
|
edit_path = "#{full_path}/edit"
|
|
|
|
formatted_edit_path = "#{full_path}/edit.xml"
|
2007-01-28 08:20:04 +00:00
|
|
|
|
2006-12-07 15:36:08 +00:00
|
|
|
with_options options[:options] do |controller|
|
2007-01-28 08:20:04 +00:00
|
|
|
controller.assert_routing full_path, :action => 'show'
|
|
|
|
controller.assert_routing new_path, :action => 'new'
|
|
|
|
controller.assert_routing edit_path, :action => 'edit'
|
|
|
|
controller.assert_routing "#{full_path}.xml", :action => 'show', :format => 'xml'
|
|
|
|
controller.assert_routing "#{new_path}.xml", :action => 'new', :format => 'xml'
|
|
|
|
controller.assert_routing formatted_edit_path, :action => 'edit', :format => 'xml'
|
2006-12-07 15:36:08 +00:00
|
|
|
end
|
|
|
|
|
2007-01-28 08:20:04 +00:00
|
|
|
assert_recognizes(options[:options].merge(:action => 'show'), :path => full_path, :method => :get)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'new'), :path => new_path, :method => :get)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'edit'), :path => edit_path, :method => :get)
|
2006-12-22 03:16:50 +00:00
|
|
|
assert_recognizes(options[:options].merge(:action => 'create'), :path => full_path, :method => :post)
|
2006-12-07 15:36:08 +00:00
|
|
|
assert_recognizes(options[:options].merge(:action => 'update'), :path => full_path, :method => :put)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'destroy'), :path => full_path, :method => :delete)
|
|
|
|
|
2007-01-28 08:20:04 +00:00
|
|
|
assert_recognizes(options[:options].merge(:action => 'show', :format => 'xml'), :path => "#{full_path}.xml", :method => :get)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'edit', :format => 'xml'), :path => formatted_edit_path, :method => :get)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'create', :format => 'xml'), :path => "#{full_path}.xml", :method => :post)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'update', :format => 'xml'), :path => "#{full_path}.xml", :method => :put)
|
|
|
|
assert_recognizes(options[:options].merge(:action => 'destroy', :format => 'xml'), :path => "#{full_path}.xml", :method => :delete)
|
|
|
|
|
2006-12-07 15:36:08 +00:00
|
|
|
yield options[:options] if block_given?
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_singleton_named_routes_for(singleton_name, options = {})
|
|
|
|
(options[:options] ||= {})[:controller] ||= singleton_name.to_s
|
|
|
|
@controller = "#{options[:options][:controller].camelize}Controller".constantize.new
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
get :show, options[:options]
|
|
|
|
options[:options].delete :action
|
2007-01-28 08:20:04 +00:00
|
|
|
|
2006-12-07 15:36:08 +00:00
|
|
|
full_path = "/#{options[:path_prefix]}#{singleton_name}"
|
2007-04-27 05:06:38 +00:00
|
|
|
name_prefix = options[:name_prefix]
|
2007-01-28 08:20:04 +00:00
|
|
|
|
2007-04-27 05:06:38 +00:00
|
|
|
assert_named_route "#{full_path}", "#{name_prefix}#{singleton_name}_path", options[:options]
|
|
|
|
assert_named_route "#{full_path}/new", "#{name_prefix}new_#{singleton_name}_path", options[:options]
|
|
|
|
assert_named_route "#{full_path}/edit", "#{name_prefix}edit_#{singleton_name}_path", options[:options]
|
|
|
|
assert_named_route "#{full_path}.xml", "formatted_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
|
|
|
|
assert_named_route "#{full_path}/new.xml", "formatted_#{name_prefix}new_#{singleton_name}_path", options[:options].merge(:format => 'xml')
|
|
|
|
assert_named_route "#{full_path}/edit.xml", "formatted_#{name_prefix}edit_#{singleton_name}_path", options[:options].merge(:format => 'xml')
|
2006-12-07 15:36:08 +00:00
|
|
|
end
|
|
|
|
|
2006-07-31 20:00:18 +00:00
|
|
|
def assert_named_route(expected, route, options)
|
|
|
|
actual = @controller.send(route, options) rescue $!.class.name
|
|
|
|
assert_equal expected, actual, "Error on route: #{route}(#{options.inspect})"
|
|
|
|
end
|
2006-07-31 21:57:59 +00:00
|
|
|
|
|
|
|
def assert_resource_methods(expected, resource, action_method, method)
|
|
|
|
assert_equal expected.length, resource.send("#{action_method}_methods")[method].size, "#{resource.send("#{action_method}_methods")[method].inspect}"
|
|
|
|
expected.each do |action|
|
2006-09-14 19:53:04 +00:00
|
|
|
assert resource.send("#{action_method}_methods")[method].include?(action),
|
2006-07-31 21:57:59 +00:00
|
|
|
"#{method} not in #{action_method} methods: #{resource.send("#{action_method}_methods")[method].inspect}"
|
|
|
|
end
|
|
|
|
end
|
2006-09-14 19:53:04 +00:00
|
|
|
|
2006-08-05 20:46:49 +00:00
|
|
|
def distinct_routes? (r1, r2)
|
|
|
|
if r1.conditions == r2.conditions and r1.requirements == r2.requirements then
|
|
|
|
if r1.segments.collect(&:to_s) == r2.segments.collect(&:to_s) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2006-07-31 18:59:58 +00:00
|
|
|
end
|