diff --git a/app/models/comment.rb b/app/models/comment.rb index 3da6c01f61..04ebb18a63 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -514,6 +514,8 @@ def spam? end def submit_spam + return unless approved && !is_deleted + Rails.env.production? && Akismetor.submit_spam(akismet_attributes) end @@ -522,9 +524,9 @@ def submit_ham end def mark_as_spam! + submit_spam update_attribute(:approved, false) update_attribute(:spam, true) - submit_spam end def mark_as_ham! diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 699dfe792c..d45a34d175 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -919,20 +919,33 @@ def queue_adapter_for_test end describe "#mark_as_spam!" do - let(:comment) { create(:comment, approved: true, spam: false) } + context "when the comment is not marked as spam" do + let(:comment) { create(:comment, approved: true, spam: false) } - it "flags the comment as spam." do - comment.mark_as_spam! - comment.reload - expect(comment.approved).to be_falsey - expect(comment.spam).to be_truthy + it "flags the comment as spam" do + comment.mark_as_spam! + comment.reload + expect(comment.approved).to be_falsey + expect(comment.spam).to be_truthy + end + end + + context "when the comment is already marked as spam" do + let(:comment) { create(:comment, approved: false, spam: false) } + + it "flags the comment as spam" do + comment.mark_as_spam! + comment.reload + expect(comment.approved).to be_falsey + expect(comment.spam).to be_truthy + end end end describe "#mark_as_ham!" do let(:comment) { create(:comment, approved: false, spam: true) } - it "flags the comment as legitimate." do + it "flags the comment as legitimate" do comment.mark_as_ham! comment.reload expect(comment.approved).to be_truthy