Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

Commit

Permalink
post controller test
Browse files Browse the repository at this point in the history
  • Loading branch information
bborn committed Feb 16, 2011
1 parent 94c792a commit d20457c
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 51 deletions.
7 changes: 4 additions & 3 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def cache_action?
!logged_in? && controller_name.eql?('posts')
end

before_filter :login_required, :only => [:new, :edit, :update, :destroy, :create, :manage]
before_filter :find_user, :only => [:new, :edit, :index, :show, :update_views, :manage]
before_filter :login_required, :only => [:new, :edit, :update, :destroy, :create, :manage, :preview]
before_filter :find_user, :only => [:new, :edit, :index, :show, :update_views, :manage, :preview]
before_filter :require_ownership_or_moderator, :only => [:edit, :update, :destroy, :create, :manage, :new]

skip_before_filter :verify_authenticity_token, :only => [:update_views, :send_to_friend] #called from ajax on cached pages
Expand Down Expand Up @@ -85,7 +85,8 @@ def update_views
end

def preview
@user = current_user
@post = Post.unscoped.find(params[:id])
redirect_to(:controller => 'sessions', :action => 'new') and return false unless @post.user.eql?(current_user) || admin? || moderator?
end

# GET /posts/new
Expand Down
2 changes: 1 addition & 1 deletion app/views/posts/manage.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

-box do
%h3=:search.l
= form_for @search, :html => {:class => "MainForm"} do |f|
= form_for @search, :url => manage_posts_path, :html => {:class => "MainForm"} do |f|

= f.label :title.l
= f.text_field :title_like
Expand Down
21 changes: 0 additions & 21 deletions app/views/posts/preview.html.erb

This file was deleted.

27 changes: 27 additions & 0 deletions app/views/posts/preview.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#yui-main
.yui-b
-box :class => 'hfeed' do
%h3=:users_blog.l :user=> @user.login
.hentry.main
%h4.entry-title= link_to @post.title, user_post_path(@user, @post), :rel => 'bookmark'
.vcard.author
%ul
%li.update
%a{"href"=>"#{user_post_path(@user, @post)}", "rel"=>"bookmark"}
%abbr.published{"title"=>"#{@post.published_at}"}= @post.published_at_display(:literal_date)
-if @is_current_user || admin? || moderator?
%li.edit= link_to :edit.l, edit_user_post_path(@post.user, @post)
%li.delete= link_to :delete.l, user_post_path(@post.user, @post), {:method => :delete, :confirm => :permanently_delete_this_post.l}

/ vcard
.entry-content
= @post.post
= render :partial => 'polls/poll_ui', :locals => {:poll => @post.polls.first} unless @post.polls.empty?
- unless @post.tags.empty?
%ul.tags
- @post.tags.each do |t|
%li=link_to( t.name, tag_url(t), :rel => 'tag')
/ entry-content

.yui-b
=render :partial => 'author_profile', :locals => {:user => @user}
51 changes: 34 additions & 17 deletions test/functional/posts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class PostsControllerTest < ActionController::TestCase
fixtures :all

def test_should_get_index
get :index, :user_id => users(:quentin).id
get :index, :user_id => users(:quentin)
assert_response :success
assert assigns(:posts)
end
Expand All @@ -15,8 +15,6 @@ def test_should_get_index_rss
assert assigns(:posts)
end



def test_should_not_get_index_for_private_user
get :index, :user_id => users(:privateuser).id
assert_response :redirect
Expand Down Expand Up @@ -45,38 +43,57 @@ def test_should_not_create_invalid_post_without_category
end

def test_should_show_post
get :show, :id => posts(:funny_post).id, :user_id => users(:quentin).id
get :show, :id => posts(:funny_post).id, :user_id => users(:quentin)
assert_response :success
end

def test_should_show_draft_post
posts(:funny_post).save_as_draft
get :show, :id => posts(:funny_post).id, :user_id => users(:quentin).id

login_as :quentin
get :preview, :id => posts(:funny_post).id, :user_id => users(:quentin)
assert_response :success
end


test "shouldn't show draft to logged out or non owner/admin/moderator" do
posts(:funny_post).save_as_draft
get :preview, :id => posts(:funny_post).id, :user_id => users(:quentin)
assert_response :redirect

login_as :aaron
get :preview, :id => posts(:funny_post).id, :user_id => users(:quentin)
assert_response :redirect
end

test "should show draft to admin" do
posts(:funny_post).save_as_draft

login_as :admin
get :preview, :id => posts(:funny_post).id, :user_id => users(:quentin)
assert_response :success
end

def test_should_not_show_post_for_private_user
get :show, :id => posts(:funny_post).id, :user_id => users(:privateuser).id
get :show, :id => posts(:funny_post).id, :user_id => users(:privateuser)
assert_response :redirect
end

def test_should_get_edit
login_as :quentin
get :edit, :id => posts(:funny_post).id, :user_id => users(:quentin).id
get :edit, :id => posts(:funny_post).id, :user_id => users(:quentin)
assert_response :success
end

def test_should_update_post
login_as :quentin
put :update, :id => posts(:funny_post).id, :user_id => users(:quentin).id, :post => { :title => "changed_name" }
put :update, :id => posts(:funny_post).id, :user_id => users(:quentin), :post => { :title => "changed_name" }
assert_redirected_to user_post_path(users(:quentin), assigns(:post))
assert_equal assigns(:post).title, "changed_name"
end

def test_should_fail_to_update_post
login_as :quentin
put :update, :id => posts(:funny_post).id, :user_id => users(:quentin).id, :post => { :title => nil }
put :update, :id => posts(:funny_post).id, :user_id => users(:quentin), :post => { :title => nil }
assert_response :success
assert assigns(:post).errors[:title]
end
Expand All @@ -85,39 +102,39 @@ def test_should_fail_to_update_post
def test_should_destroy_post
login_as :quentin
assert_difference Post, :count, -1 do
delete :destroy, :id => posts(:funny_post), :user_id => users(:quentin).id
delete :destroy, :id => posts(:funny_post), :user_id => users(:quentin)
end
assert_redirected_to manage_user_posts_path(:user_id => users(:quentin) )
end

def test_should_not_destroy_post
login_as :aaron
assert_difference Post, :count, 0 do
delete :destroy, :id => posts(:funny_post), :user_id => users(:aaron).id
delete :destroy, :id => posts(:funny_post), :user_id => users(:aaron)
end
assert_redirected_to login_path
end

def test_should_send_emails_to_friends
login_as :quentin
assert_difference ActionMailer::Base.deliveries, :length, 2 do
post :send_to_friend, :user_id => users(:quentin).id, :id => posts(:funny_post).to_param, :emails => 'test@example.com, test2@example.com', :message => 'you are great, friends'
post :send_to_friend, :user_id => users(:quentin), :id => posts(:funny_post).to_param, :emails => 'test@example.com, test2@example.com', :message => 'you are great, friends'
assert_response :success
end
end

def test_should_not_send_emails_to_friends
login_as :quentin
assert_no_difference ActionMailer::Base.deliveries, :length do
post :send_to_friend, :user_id => users(:quentin).id, :id => posts(:funny_post).to_param, :emails => 'test_is_and_example.com, test2@example.com', :message => 'you are great, friends'
post :send_to_friend, :user_id => users(:quentin), :id => posts(:funny_post).to_param, :emails => 'test_is_and_example.com, test2@example.com', :message => 'you are great, friends'
assert_response 500
end
end

def test_should_update_emailed_count
login_as :quentin
assert_equal posts(:funny_post).emailed_count, 0
post :send_to_friend, :user_id => users(:quentin).id, :id => posts(:funny_post).to_param, :emails => 'test@example.com, test2@example.com', :message => 'you are great, friends'
post :send_to_friend, :user_id => users(:quentin), :id => posts(:funny_post).to_param, :emails => 'test@example.com, test2@example.com', :message => 'you are great, friends'
assert_response :success
assert_equal posts(:funny_post).reload.emailed_count, 1
end
Expand Down Expand Up @@ -171,11 +188,11 @@ def test_should_not_view_another_users_manage_page

private
def create_invalid_post_without_category(options = {})
post :create, {:user_id => users(:quentin).id, :post => { :raw_post => 'rawness' }.merge(options[:post] || {}) }.merge(options || {})
post :create, {:user_id => users(:quentin), :post => { :raw_post => 'rawness' }.merge(options[:post] || {}) }.merge(options || {})
end

def create_post(options = {})
post :create, {:user_id => users(:quentin).id, :post => { :title => 'dude', :raw_post => 'rawness', :category => categories(:talk) }.merge(options[:post] || {}) }.merge(options || {})
post :create, {:user_id => users(:quentin), :post => { :title => 'dude', :raw_post => 'rawness', :category => categories(:talk) }.merge(options[:post] || {}) }.merge(options || {})
end


Expand Down
18 changes: 9 additions & 9 deletions test/functional/sb_posts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,54 +109,54 @@ def test_can_edit_other_post_as_admin
def test_should_view_recent_posts
get :index
assert_response :success
assert_equals [sb_posts(:il8n), sb_posts(:shield_reply), sb_posts(:shield), sb_posts(:silver_surfer), sb_posts(:galactus), sb_posts(:ponies), sb_posts(:pdi_rebuttal), sb_posts(:pdi_reply), sb_posts(:pdi), sb_posts(:sticky)], assigns(:posts)
assert_equal [sb_posts(:il8n), sb_posts(:shield_reply), sb_posts(:shield), sb_posts(:silver_surfer), sb_posts(:galactus), sb_posts(:ponies), sb_posts(:pdi_rebuttal), sb_posts(:pdi_reply), sb_posts(:pdi), sb_posts(:sticky)], assigns(:posts)
end

def test_should_view_posts_by_forum
get :index, :forum_id => forums(:comics).to_param
assert_response :success
assert_equals [sb_posts(:shield_reply), sb_posts(:shield), sb_posts(:silver_surfer), sb_posts(:galactus)], assigns(:posts)
assert_equal [sb_posts(:shield_reply), sb_posts(:shield), sb_posts(:silver_surfer), sb_posts(:galactus)], assigns(:posts)
end

def test_should_view_posts_by_user
get :index, :user_id => users(:sam).id
assert_response :success
assert_equals [sb_posts(:shield), sb_posts(:silver_surfer), sb_posts(:ponies), sb_posts(:pdi_reply), sb_posts(:sticky)], assigns(:posts)
assert_equal [sb_posts(:shield), sb_posts(:silver_surfer), sb_posts(:ponies), sb_posts(:pdi_reply), sb_posts(:sticky)], assigns(:posts)
end

def test_should_view_monitored_posts
get :monitored, :user_id => users(:aaron).id
assert_equals [sb_posts(:pdi_reply)], assigns(:posts)
assert_equal [sb_posts(:pdi_reply)], assigns(:posts)
end

def test_should_search_recent_posts
get :search, :q => 'pdi'
assert_response :success
assert_equals [sb_posts(:pdi_rebuttal), sb_posts(:pdi_reply), sb_posts(:pdi)], assigns(:posts)
assert_equal [sb_posts(:pdi_rebuttal), sb_posts(:pdi_reply), sb_posts(:pdi)], assigns(:posts)
end

def test_should_search_posts_by_forum
get :search, :forum_id => forums(:comics).to_param, :q => 'galactus'
assert_response :success
assert_equals [sb_posts(:silver_surfer), sb_posts(:galactus)], assigns(:posts)
assert_equal [sb_posts(:silver_surfer), sb_posts(:galactus)], assigns(:posts)
end

def test_should_view_recent_posts_as_rss
get :index, :format => 'rss'
assert_response :success
assert_equals [sb_posts(:il8n), sb_posts(:shield_reply), sb_posts(:shield), sb_posts(:silver_surfer), sb_posts(:galactus), sb_posts(:ponies), sb_posts(:pdi_rebuttal), sb_posts(:pdi_reply), sb_posts(:pdi), sb_posts(:sticky)], assigns(:posts)
assert_equal [sb_posts(:il8n), sb_posts(:shield_reply), sb_posts(:shield), sb_posts(:silver_surfer), sb_posts(:galactus), sb_posts(:ponies), sb_posts(:pdi_rebuttal), sb_posts(:pdi_reply), sb_posts(:pdi), sb_posts(:sticky)], assigns(:posts)
end

def test_should_view_posts_by_forum_as_rss
get :index, :forum_id => forums(:comics).to_param, :format => 'rss'
assert_response :success
assert_equals [sb_posts(:shield_reply), sb_posts(:shield), sb_posts(:silver_surfer), sb_posts(:galactus)], assigns(:posts)
assert_equal [sb_posts(:shield_reply), sb_posts(:shield), sb_posts(:silver_surfer), sb_posts(:galactus)], assigns(:posts)
end

def test_should_view_posts_by_user_as_rss
get :index, :user_id => users(:sam).id, :format => 'rss'
assert_response :success
assert_equals [sb_posts(:shield), sb_posts(:silver_surfer), sb_posts(:ponies), sb_posts(:pdi_reply), sb_posts(:sticky)], assigns(:posts)
assert_equal [sb_posts(:shield), sb_posts(:silver_surfer), sb_posts(:ponies), sb_posts(:pdi_reply), sb_posts(:sticky)], assigns(:posts)
end

def test_disallow_new_post_to_locked_topic
Expand Down
1 change: 1 addition & 0 deletions test/unit/post_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def setup
def test_should_find_including_unpublished
post = posts(:funny_post)
post.save_as_draft
assert Post.unscoped.find(post.id)
assert Post.unscoped.all.include?(post)
end

Expand Down

0 comments on commit d20457c

Please sign in to comment.