From dff781c579f22c8ebf56a988b4ddcfcdcdfce262 Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Fri, 24 Aug 2012 10:48:06 -0300 Subject: [PATCH] Pluggable SPAM handling system --- app/models/comment.rb | 16 ++++++++++++++++ app/models/comment_handler.rb | 2 +- test/unit/comment_handler_test.rb | 2 +- test/unit/comment_test.rb | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 92 insertions(+), 4 deletions(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index 21ca41e..34cf5d3 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -94,6 +94,18 @@ class Comment < ActiveRecord::Base Delayed::Job.enqueue CommentHandler.new(self.id) end + delegate :environment, :to => :profile + delegate :profile, :to => :source + + include Noosfero::Plugin::HotSpot + + def verify_and_notify + plugins.dispatch(:check_comment_for_spam, self) + unless spam? + notify_by_mail + end + end + def notify_by_mail if source.kind_of?(Article) && article.notify_comments? if !article.profile.notification_emails.empty? @@ -202,11 +214,15 @@ class Comment < ActiveRecord::Base def spam! self.spam = true self.save! + plugins.dispatch(:comment_marked_as_spam, self) + self end def ham! self.spam = false self.save! + plugins.dispatch(:comment_marked_as_ham, self) + self end end diff --git a/app/models/comment_handler.rb b/app/models/comment_handler.rb index 577ea1d..130bc50 100644 --- a/app/models/comment_handler.rb +++ b/app/models/comment_handler.rb @@ -2,7 +2,7 @@ class CommentHandler < Struct.new(:comment_id) def perform comment = Comment.find(comment_id) - comment.notify_by_mail + comment.verify_and_notify rescue ActiveRecord::RecordNotFound # just ignore non-existing comments end diff --git a/test/unit/comment_handler_test.rb b/test/unit/comment_handler_test.rb index 3b4dbc8..d6f7b9b 100644 --- a/test/unit/comment_handler_test.rb +++ b/test/unit/comment_handler_test.rb @@ -16,7 +16,7 @@ class CommentHandlerTest < ActiveSupport::TestCase handler = CommentHandler.new(-1) comment = Comment.new Comment.stubs(:find).with(-1).returns(comment) - comment.expects(:notify_by_mail) + comment.expects(:verify_and_notify) handler.perform end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 7eb2d85..ac68b1d 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -446,7 +446,7 @@ class CommentTest < ActiveSupport::TestCase end should 'be able to mark as spam atomically' do - c1 = fast_create(Comment, :name => 'foo', :email => 'foo@example.com') + c1 = create_comment c1.spam! c1.reload assert c1.spam? @@ -461,10 +461,82 @@ class CommentTest < ActiveSupport::TestCase end should 'be able to mark as ham atomically' do - c1 = fast_create(Comment, :name => 'foo', :email => 'foo@example.com', :spam => true) + c1 = create_comment c1.ham! c1.reload assert c1.ham? end + should 'notify by email' do + c1 = create_comment + c1.expects(:notify_by_mail) + c1.verify_and_notify + end + + should 'not notify by email when comment is spam' do + c1 = create_comment(:spam => true) + c1.expects(:notify_by_mail).never + c1.verify_and_notify + end + + class EverythingIsSpam < Noosfero::Plugin + def check_comment_for_spam(comment) + comment.spam! + end + end + + + should 'delegate spam detection to plugins' do + Environment.default.enable_plugin(EverythingIsSpam) + + c1 = create_comment + + c1.expects(:notify_by_mail).never + + c1.verify_and_notify + end + + class SpamNotification < Noosfero::Plugin + class << self + attr_accessor :marked_as_spam + attr_accessor :marked_as_ham + end + + def comment_marked_as_spam(c) + self.class.marked_as_spam = c + end + + def comment_marked_as_ham(c) + self.class.marked_as_ham = c + end + end + + should 'notify plugins of comments being marked as spam' do + Environment.default.enable_plugin(SpamNotification) + + c = create_comment + + c.spam! + + assert_equal c, SpamNotification.marked_as_spam + end + + should 'notify plugins of comments being marked as ham' do + Environment.default.enable_plugin(SpamNotification) + + c = create_comment + + c.ham! + + assert_equal c, SpamNotification.marked_as_ham + end + + private + + def create_comment(args = {}) + owner = create_user('testuser').person + article = create(TextileArticle, :profile_id => owner.id) + create(Comment, { :name => 'foo', :email => 'foo@example.com', :source => article }.merge(args)) + end + end -- libgit2 0.21.2