2014-08-23 16:16:09 +09:00
|
|
|
require File.dirname(__FILE__) + "/../test_helper"
|
2010-04-20 23:05:11 +00:00
|
|
|
|
|
|
|
class CommentControllerTest < ActionController::TestCase
|
|
|
|
fixtures :users, :posts
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@post_number = 1
|
|
|
|
end
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def create_comment(post_id, body, params = {})
|
2014-08-23 16:44:43 +09:00
|
|
|
Comment.create({ :post_id => post_id, :user_id => 2, :body => body, :ip_addr => "127.0.0.1", :is_spam => false }.merge(params))
|
2010-04-20 23:05:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_update
|
|
|
|
comment = create_comment(1, "hi there")
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2014-08-23 16:44:43 +09:00
|
|
|
get :edit, { :id => comment.id }
|
2010-04-20 23:05:11 +00:00
|
|
|
assert_response :success
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2014-08-23 16:44:43 +09:00
|
|
|
post :update, { :id => comment.id, :comment => { :body => "muggle" } }, { :user_id => 1 }
|
2010-04-20 23:05:11 +00:00
|
|
|
assert_redirected_to :controller => "comment", :action => "index"
|
|
|
|
comment.reload
|
|
|
|
assert_equal("muggle", comment.body)
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
# TODO: test privileges
|
|
|
|
end
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def test_destroy
|
|
|
|
comment = create_comment(1, "hi there")
|
|
|
|
|
2014-08-23 16:44:43 +09:00
|
|
|
post :destroy, { :id => comment.id }, { :user_id => 1 }
|
2010-04-20 23:05:11 +00:00
|
|
|
assert_redirected_to :controller => "post", :action => "show", :id => 1
|
|
|
|
assert_nil(Comment.find_by_id(comment.id))
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
# TODO: Test privileges
|
|
|
|
end
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def test_create_simple
|
2014-08-23 16:44:43 +09:00
|
|
|
post :create, { :comment => { :post_id => 1, :body => "hoge" } }, { :user_id => 1 }
|
2010-04-20 23:05:11 +00:00
|
|
|
post = Post.find(1)
|
|
|
|
assert_equal(1, post.comments.size)
|
|
|
|
assert_equal("hoge", post.comments[0].body)
|
|
|
|
assert_equal(1, post.comments[0].user_id)
|
|
|
|
assert_not_nil(post.last_commented_at)
|
|
|
|
end
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def test_create_throttling
|
|
|
|
old_member_comment_limit = CONFIG["member_comment_limit"]
|
|
|
|
CONFIG["member_comment_limit"] = 1
|
|
|
|
create_comment(1, "c1", :user_id => 4)
|
2014-08-23 16:44:43 +09:00
|
|
|
post :create, { :comment => { :post_id => 1, :body => "c2" }, :commit => "Post" }, { :user_id => 4 }
|
2010-09-07 01:22:42 +00:00
|
|
|
assert_redirected_to :controller => "post", :action => "show", :id => 1
|
2010-04-20 23:05:11 +00:00
|
|
|
assert_equal(1, Post.find(1).comments.size)
|
|
|
|
assert_equal("c1", Post.find(1).comments[0].body)
|
|
|
|
CONFIG["member_comment_limit"] = old_member_comment_limit
|
|
|
|
end
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def test_create_do_not_bump_post
|
2014-08-23 16:44:43 +09:00
|
|
|
post :create, { :comment => { :post_id => 1, :body => "hoge" }, :commit => "Post without bumping" }, { :user_id => 1 }
|
2010-04-20 23:05:11 +00:00
|
|
|
post = Post.find(1)
|
|
|
|
assert_equal(1, post.comments.size)
|
|
|
|
assert_nil(post.last_commented_at)
|
|
|
|
end
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def test_show
|
|
|
|
comment = create_comment(1, "hoge")
|
2014-08-23 16:44:43 +09:00
|
|
|
get :show, { :id => comment.id }, { :user_id => 4 }
|
2010-04-20 23:05:11 +00:00
|
|
|
assert_response :success
|
|
|
|
end
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def test_index
|
|
|
|
create_comment(1, "hoge")
|
|
|
|
create_comment(1, "moogle")
|
|
|
|
create_comment(3, "box")
|
|
|
|
create_comment(2, "tree")
|
2014-08-23 16:44:43 +09:00
|
|
|
get :index, {}, { :user_id => 4 }
|
2010-04-20 23:05:11 +00:00
|
|
|
assert_response :success
|
|
|
|
end
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def test_mark_as_spam
|
|
|
|
# TODO: allow janitors to mark spam
|
|
|
|
comment = create_comment(1, "hoge")
|
2014-08-23 16:44:43 +09:00
|
|
|
post :mark_as_spam, { :id => comment.id }, { :user_id => 2 }
|
2010-04-20 23:05:11 +00:00
|
|
|
comment.reload
|
|
|
|
assert(comment.is_spam?, "Comment not marked as spam")
|
|
|
|
end
|
2014-08-23 16:19:01 +09:00
|
|
|
|
2010-04-20 23:05:11 +00:00
|
|
|
def test_moderate
|
|
|
|
create_comment(1, "hoge")
|
|
|
|
create_comment(1, "moogle")
|
|
|
|
create_comment(3, "box")
|
|
|
|
create_comment(2, "tree")
|
2014-08-23 16:44:43 +09:00
|
|
|
get :moderate, {}, { :user_id => 2 }
|
2010-04-20 23:05:11 +00:00
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
end
|