Skip to content

Commit

Permalink
Comment spam moderation Part 2 (publiclab#2305)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
grvsachdeva authored and jywarren committed Feb 20, 2018
1 parent 32a5c7a commit d357e22
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 3 deletions.
34 changes: 33 additions & 1 deletion app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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]
Expand Down
13 changes: 13 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions test/fixtures/comments.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
114 changes: 114 additions & 0 deletions test/functional/admin_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/functional/notes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/unit/node_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d357e22

Please sign in to comment.