From d357e22206a3d8b1f8c148bfe7a4722e851281c8 Mon Sep 17 00:00:00 2001 From: Gaurav Sachdeva Date: Wed, 21 Feb 2018 03:36:19 +0530 Subject: [PATCH] Comment spam moderation Part 2 (#2305) * Controller and model method for marking * Publish comment method added * test for marking comment * tests for publish comment * Small changes * controller changed * updated test as per new comment system --- app/controllers/admin_controller.rb | 34 ++++++- app/models/comment.rb | 13 +++ test/fixtures/comments.yml | 8 ++ test/functional/admin_controller_test.rb | 114 +++++++++++++++++++++++ test/functional/notes_controller_test.rb | 2 +- test/unit/node_test.rb | 2 +- 6 files changed, 170 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index e081a612b96..bcbe6d6e072 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -1,5 +1,5 @@ class AdminController < ApplicationController - before_filter :require_user, only: %i(spam spam_revisions) + before_filter :require_user, only: %i(spam spam_revisions mark_comment_spam publish_comment) def promote_admin @user = User.find params[:id] @@ -123,6 +123,38 @@ def mark_spam end end + def mark_comment_spam + @comment = Comment.find params[:id] + if current_user && (current_user.role == 'moderator' || current_user.role == 'admin') + if @comment.status == 1 + @comment.spam + flash[:notice] = "Comment has been marked as spam." + else + flash[:notice] = "Comment already marked as spam." + end + else + flash[:error] = 'Only moderators can moderate comments.' + end + redirect_to '/dashboard' + end + + def publish_comment + if current_user && (current_user.role == 'moderator' || current_user.role == 'admin') + @comment = Comment.find params[:id] + if @comment.status == 1 + flash[:notice] = 'Comment already published.' + else + @comment.publish + flash[:notice] = 'Comment published.' + end + @node = @comment.node + redirect_to @node.path + else + flash[:error] = 'Only moderators can publish comments.' + redirect_to '/dashboard' + end + end + def publish if current_user && (current_user.role == 'moderator' || current_user.role == 'admin') @node = Node.find params[:id] diff --git a/app/models/comment.rb b/app/models/comment.rb index ebe8b1120e3..15112a07ae5 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -142,4 +142,17 @@ def answer_comment_notify(current_user) notify_users(uids, current_user) notify_tag_followers(already + uids) end + + def spam + self.status = 0 + save + self + end + + def publish + self.status = 1 + save + self + end + end diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml index de8cbe1e6df..d3d3d0e0312 100644 --- a/test/fixtures/comments.yml +++ b/test/fixtures/comments.yml @@ -112,3 +112,11 @@ question_tag: comment: 'Question #everything' timestamp: <%= Time.now.to_i + 13 %> thread: /04 + +spam_comment: + uid: 1 + nid: 1 + status: 0 + comment: This thing is spam. + timestamp: <%= Time.now.to_i + 1 %> + thread: /01 diff --git a/test/functional/admin_controller_test.rb b/test/functional/admin_controller_test.rb index b1285123a3e..6453ae46bbf 100644 --- a/test/functional/admin_controller_test.rb +++ b/test/functional/admin_controller_test.rb @@ -396,4 +396,118 @@ def teardown assert_equal 1, node.author.status assert_redirected_to node.path(:question) end + + test 'should mark comment as spam if moderator' do + UserSession.create(users(:moderator)) + comment = comments(:first) + + post :mark_comment_spam, id: comment.id + + comment = assigns(:comment) + assert_equal 0, comment.status + assert_equal "Comment has been marked as spam.", flash[:notice] + assert_redirected_to '/dashboard' + end + + test 'should mark comment as spam if admin' do + UserSession.create(users(:admin)) + comment = comments(:first) + + post :mark_comment_spam, id: comment.id + + comment = assigns(:comment) + assert_equal 0, comment.status + assert_equal "Comment has been marked as spam.", flash[:notice] + assert_redirected_to '/dashboard' + end + + test 'should not mark comment as spam if no user' do + comment = comments(:first) + + post :mark_comment_spam, id: comment.id + + assert_redirected_to '/login' + end + + test 'should not mark comment as spam if normal user' do + UserSession.create(users(:bob)) + comment = comments(:first) + + post :mark_comment_spam, id: comment.id + + comment = assigns(:comment) + assert_equal 1, comment.status + assert_equal "Only moderators can moderate comments.", flash[:error] + assert_redirected_to '/dashboard' + end + + test 'should not mark comment as spam if it is already marked as spam' do + UserSession.create(users(:admin)) + comment = comments(:spam_comment) + + post :mark_comment_spam, id: comment.id + + comment = assigns(:comment) + assert_equal 0, comment.status + assert_equal "Comment already marked as spam.", flash[:notice] + assert_redirected_to '/dashboard' + end + + test 'should publish comment from spam if admin' do + UserSession.create(users(:admin)) + comment = comments(:spam_comment) + node = comment.node + post :publish_comment, id: comment.id + + comment = assigns(:comment) + assert_equal 1, comment.status + assert_equal "Comment published.", flash[:notice] + assert_redirected_to node.path + end + + test 'should publish comment from spam if moderator' do + UserSession.create(users(:moderator)) + comment = comments(:spam_comment) + node = comment.node + post :publish_comment, id: comment.id + + comment = assigns(:comment) + assert_equal 1, comment.status + assert_equal "Comment published.", flash[:notice] + assert_redirected_to node.path + end + + test 'should login if want to publish comment from spam' do + comment = comments(:spam_comment) + + post :publish_comment, id: comment.id + + assert_equal 0, comment.status + assert_redirected_to '/login' + end + + test 'should not publish comment from spam if any other user' do + UserSession.create(users(:newcomer)) + comment = comments(:spam_comment) + node = comment.node + + post :publish_comment, id: comment.id + + assert_equal 0, comment.status + assert_equal "Only moderators can publish comments.", flash[:error] + assert_redirected_to '/dashboard' + end + + test 'should not publish comment from spam if already published' do + UserSession.create(users(:admin)) + comment = comments(:first) + node = comment.node + + post :publish_comment, id: comment.id + + assert_equal 1, comment.status + assert_equal "Comment already published.", flash[:notice] + assert_redirected_to node.path + end + end diff --git a/test/functional/notes_controller_test.rb b/test/functional/notes_controller_test.rb index ef642c7b9f8..9957b1c3f17 100644 --- a/test/functional/notes_controller_test.rb +++ b/test/functional/notes_controller_test.rb @@ -405,7 +405,7 @@ def teardown author: node.author.username, date: node.created_at.strftime('%m-%d-%Y'), id: node.title.parameterize - assert_select '.fa-fire', 3 + assert_select '.fa-fire', 4 end test 'should redirect to questions show page after creating a new question' do diff --git a/test/unit/node_test.rb b/test/unit/node_test.rb index 122b4d263ce..db985d6e2fb 100644 --- a/test/unit/node_test.rb +++ b/test/unit/node_test.rb @@ -350,7 +350,7 @@ class NodeTest < ActiveSupport::TestCase test 'should delete associated comments when a node is deleted' do node = nodes(:one) - assert_equal node.comments.count, 4 + assert_equal node.comments.count, 5 deleted_node = node.destroy assert_equal node.comments.count, 0 end