2007-07-14 18:35:51 +00:00
|
|
|
require File.dirname(__FILE__) + '/../test_helper'
|
|
|
|
require 'posts_controller'
|
|
|
|
|
|
|
|
# Re-raise errors caught by the controller.
|
|
|
|
class PostsController; def rescue_action(e) raise e end; end
|
|
|
|
|
|
|
|
class PostsControllerTest < Test::Unit::TestCase
|
2008-09-02 16:49:13 -04:00
|
|
|
fixtures :all
|
2007-07-14 18:35:51 +00:00
|
|
|
|
|
|
|
def setup
|
|
|
|
@controller = PostsController.new
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
@post = Post.find(:first)
|
|
|
|
end
|
2009-01-06 13:56:16 -05:00
|
|
|
|
2008-09-13 16:41:36 -04:00
|
|
|
# autodetects the :controller
|
2008-09-13 17:13:32 -04:00
|
|
|
should_route :get, '/posts', :action => :index
|
2008-09-13 16:41:36 -04:00
|
|
|
# explicitly specify :controller
|
2008-09-13 17:13:32 -04:00
|
|
|
should_route :post, '/posts', :controller => :posts, :action => :create
|
2008-09-13 16:41:36 -04:00
|
|
|
# non-string parameter
|
2008-09-13 17:13:32 -04:00
|
|
|
should_route :get, '/posts/1', :action => :show, :id => 1
|
2008-09-13 16:41:36 -04:00
|
|
|
# string-parameter
|
2008-09-13 17:13:32 -04:00
|
|
|
should_route :put, '/posts/1', :action => :update, :id => "1"
|
|
|
|
should_route :delete, '/posts/1', :action => :destroy, :id => 1
|
|
|
|
should_route :get, '/posts/new', :action => :new
|
2009-01-06 13:56:16 -05:00
|
|
|
|
2008-09-13 17:13:32 -04:00
|
|
|
# Test the nested routes
|
|
|
|
should_route :get, '/users/5/posts', :action => :index, :user_id => 5
|
|
|
|
should_route :post, '/users/5/posts', :action => :create, :user_id => 5
|
|
|
|
should_route :get, '/users/5/posts/1', :action => :show, :id => 1, :user_id => 5
|
|
|
|
should_route :delete, '/users/5/posts/1', :action => :destroy, :id => 1, :user_id => 5
|
|
|
|
should_route :get, '/users/5/posts/new', :action => :new, :user_id => 5
|
|
|
|
should_route :put, '/users/5/posts/1', :action => :update, :id => 1, :user_id => 5
|
2007-07-14 18:35:51 +00:00
|
|
|
|
2008-09-20 22:08:45 -04:00
|
|
|
context "The public" do
|
|
|
|
setup do
|
|
|
|
@request.session[:logged_in] = false
|
|
|
|
end
|
|
|
|
|
|
|
|
should_be_restful do |resource|
|
|
|
|
resource.parent = :user
|
|
|
|
|
|
|
|
resource.denied.actions = [:index, :show, :edit, :new, :create, :update, :destroy]
|
|
|
|
resource.denied.flash = /what/i
|
|
|
|
resource.denied.redirect = '"/"'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-07-20 14:19:11 +00:00
|
|
|
context "Logged in" do
|
|
|
|
setup do
|
|
|
|
@request.session[:logged_in] = true
|
|
|
|
end
|
2008-09-02 16:49:13 -04:00
|
|
|
|
2008-09-20 22:08:45 -04:00
|
|
|
should_be_restful do |resource|
|
|
|
|
resource.parent = :user
|
|
|
|
|
|
|
|
resource.create.params = { :title => "first post", :body => 'blah blah blah'}
|
|
|
|
resource.update.params = { :title => "changed" }
|
|
|
|
end
|
|
|
|
|
2008-07-31 11:26:27 -04:00
|
|
|
context "viewing posts for a user" do
|
|
|
|
setup do
|
|
|
|
get :index, :user_id => users(:first)
|
|
|
|
end
|
|
|
|
should_respond_with :success
|
2008-09-20 16:06:28 -04:00
|
|
|
should_assign_to :user, :class => User, :equals => 'users(:first)'
|
2008-09-20 15:11:57 -04:00
|
|
|
should_fail do
|
|
|
|
should_assign_to :user, :class => Post
|
|
|
|
end
|
2008-09-20 16:06:28 -04:00
|
|
|
should_fail do
|
|
|
|
should_assign_to :user, :equals => 'posts(:first)'
|
|
|
|
end
|
2008-09-20 15:11:57 -04:00
|
|
|
should_assign_to :posts
|
2008-07-31 11:26:27 -04:00
|
|
|
should_not_assign_to :foo, :bar
|
2008-11-18 22:59:04 +00:00
|
|
|
should_render_page_with_metadata :description => /Posts/, :title => /index/
|
2009-01-06 13:56:16 -05:00
|
|
|
should_render_page_with_metadata :keywords => "posts"
|
2008-07-31 11:26:27 -04:00
|
|
|
end
|
2008-09-12 17:51:01 -04:00
|
|
|
|
|
|
|
context "viewing posts for a user with rss format" do
|
|
|
|
setup do
|
|
|
|
get :index, :user_id => users(:first), :format => 'rss'
|
2008-09-12 19:37:47 -04:00
|
|
|
@user = users(:first)
|
2008-09-12 17:51:01 -04:00
|
|
|
end
|
|
|
|
should_respond_with :success
|
|
|
|
should_respond_with_content_type 'application/rss+xml'
|
2008-09-12 20:01:29 -04:00
|
|
|
should_respond_with_content_type :rss
|
2008-09-14 01:35:58 -04:00
|
|
|
should_respond_with_content_type /rss/
|
2008-09-12 19:37:47 -04:00
|
|
|
should_return_from_session :special, "'$2 off your next purchase'"
|
|
|
|
should_return_from_session :special_user_id, '@user.id'
|
2008-09-12 17:51:01 -04:00
|
|
|
should_assign_to :user, :posts
|
|
|
|
should_not_assign_to :foo, :bar
|
|
|
|
end
|
2008-09-14 20:26:17 -04:00
|
|
|
|
|
|
|
context "viewing a post on GET to #show" do
|
|
|
|
setup { get :show, :user_id => users(:first), :id => posts(:first) }
|
|
|
|
should_render_with_layout 'wide'
|
2008-10-17 09:53:43 -05:00
|
|
|
should_render_with_layout :wide
|
2009-01-06 13:56:16 -05:00
|
|
|
should_assign_to :false_flag
|
2008-09-14 20:26:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "on GET to #new" do
|
|
|
|
setup { get :new, :user_id => users(:first) }
|
|
|
|
should_render_without_layout
|
|
|
|
end
|
2007-07-14 18:35:51 +00:00
|
|
|
end
|
2008-07-31 11:26:27 -04:00
|
|
|
|
2007-07-14 18:35:51 +00:00
|
|
|
end
|