From 13bd4e382c89f97df818bbac14de13d7a2f61c61 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Thu, 24 Oct 2013 18:14:45 -0300 Subject: [PATCH] anti_spam_plugin: refactor wrappers logic --- plugins/anti_spam/lib/anti_spam_plugin.rb | 31 ++++++++----------------------- plugins/anti_spam/lib/anti_spam_plugin/comment_wrapper.rb | 18 ++++++++---------- plugins/anti_spam/lib/anti_spam_plugin/suggest_article_wrapper.rb | 20 +++++++++----------- plugins/anti_spam/lib/anti_spam_plugin/wrapper.rb | 18 ++++++++++++++++++ plugins/anti_spam/test/unit/anti_spam_plugin/comment_wrapper_test.rb | 6 +----- plugins/anti_spam/test/unit/anti_spam_plugin/suggest_article_wrapper_test.rb | 6 +----- plugins/anti_spam/test/unit/anti_spam_plugin/wrapper_test.rb | 25 +++++++++++++++++++++++++ plugins/anti_spam/test/unit/anti_spam_plugin_test.rb | 63 +++++++++++++++++++++------------------------------------------ 8 files changed, 91 insertions(+), 96 deletions(-) create mode 100644 plugins/anti_spam/lib/anti_spam_plugin/wrapper.rb create mode 100644 plugins/anti_spam/test/unit/anti_spam_plugin/wrapper_test.rb diff --git a/plugins/anti_spam/lib/anti_spam_plugin.rb b/plugins/anti_spam/lib/anti_spam_plugin.rb index fbb370c..78e246e 100644 --- a/plugins/anti_spam/lib/anti_spam_plugin.rb +++ b/plugins/anti_spam/lib/anti_spam_plugin.rb @@ -12,34 +12,19 @@ class AntiSpamPlugin < Noosfero::Plugin 'api.antispam.typepad.com' end - def check_comment_for_spam(comment) - if rakismet_call AntiSpamPlugin::CommentWrapper.new(comment), comment.environment, :spam? - comment.spam = true - comment.save! + def check_for_spam(object) + if rakismet_call AntiSpamPlugin::Wrapper.wrap(object), object.environment, :spam? + object.spam = true + object.save! end end - def comment_marked_as_spam(comment) - rakismet_call AntiSpamPlugin::CommentWrapper.new(comment), comment.environment, :spam! + def marked_as_spam(object) + rakismet_call AntiSpamPlugin::Wrapper.wrap(object), object.environment, :spam! end - def comment_marked_as_ham(comment) - rakismet_call AntiSpamPlugin::CommentWrapper.new(comment), comment.environment, :ham! - end - - def check_suggest_article_for_spam(suggest_article) - if rakismet_call AntiSpamPlugin::SuggestArticleWrapper.new(suggest_article), suggest_article.environment, :spam? - suggest_article.spam = true - suggest_article.save! - end - end - - def suggest_article_marked_as_spam(suggest_article) - rakismet_call AntiSpamPlugin::SuggestArticleWrapper.new(suggest_article), suggest_article.environment, :spam! - end - - def suggest_article_marked_as_ham(suggest_article) - rakismet_call AntiSpamPlugin::SuggestArticleWrapper.new(suggest_article), suggest_article.environment, :ham! + def marked_as_ham(object) + rakismet_call AntiSpamPlugin::Wrapper.wrap(object), object.environment, :ham! end protected diff --git a/plugins/anti_spam/lib/anti_spam_plugin/comment_wrapper.rb b/plugins/anti_spam/lib/anti_spam_plugin/comment_wrapper.rb index e4833dc..dd87d8b 100644 --- a/plugins/anti_spam/lib/anti_spam_plugin/comment_wrapper.rb +++ b/plugins/anti_spam/lib/anti_spam_plugin/comment_wrapper.rb @@ -1,11 +1,9 @@ -class AntiSpamPlugin::CommentWrapper < Struct.new(:comment) - - delegate :author_name, :author_email, :title, :body, :ip_address, :user_agent, :referrer, :to => :comment - - include Rakismet::Model - - alias :author :author_name - alias :user_ip :ip_address - alias :content :body - +class AntiSpamPlugin::CommentWrapper < AntiSpamPlugin::Wrapper + alias_attribute :author, :author_name + alias_attribute :user_ip, :ip_address + alias_attribute :content, :body + + def self.wraps?(object) + object.kind_of?(Comment) + end end diff --git a/plugins/anti_spam/lib/anti_spam_plugin/suggest_article_wrapper.rb b/plugins/anti_spam/lib/anti_spam_plugin/suggest_article_wrapper.rb index 5bb01e2..1be13a1 100644 --- a/plugins/anti_spam/lib/anti_spam_plugin/suggest_article_wrapper.rb +++ b/plugins/anti_spam/lib/anti_spam_plugin/suggest_article_wrapper.rb @@ -1,12 +1,10 @@ -class AntiSpamPlugin::SuggestArticleWrapper < Struct.new(:suggest_article) - - delegate :name, :email, :article_body, :ip_address, :user_agent, :referrer, :to => :suggest_article - - include Rakismet::Model - - alias :author :name - alias :author_email :email - alias :user_ip :ip_address - alias :content :article_body - +class AntiSpamPlugin::SuggestArticleWrapper < AntiSpamPlugin::Wrapper + alias_attribute :author, :name + alias_attribute :author_email, :email + alias_attribute :user_ip, :ip_address + alias_attribute :content, :article_body + + def self.wraps?(object) + object.kind_of?(SuggestArticle) + end end diff --git a/plugins/anti_spam/lib/anti_spam_plugin/wrapper.rb b/plugins/anti_spam/lib/anti_spam_plugin/wrapper.rb new file mode 100644 index 0000000..8781b8d --- /dev/null +++ b/plugins/anti_spam/lib/anti_spam_plugin/wrapper.rb @@ -0,0 +1,18 @@ +class AntiSpamPlugin::Wrapper < SimpleDelegator + include Rakismet::Model + + @@wrappers = [] + + def self.wrap(object) + wrapper = @@wrappers.find { |wrapper| wrapper.wraps?(object) } + wrapper ? wrapper.new(object) : object + end + + def self.wraps?(object) + false + end + + def self.inherited(child) + @@wrappers << child + end +end diff --git a/plugins/anti_spam/test/unit/anti_spam_plugin/comment_wrapper_test.rb b/plugins/anti_spam/test/unit/anti_spam_plugin/comment_wrapper_test.rb index 1da0295..7b2b6d0 100644 --- a/plugins/anti_spam/test/unit/anti_spam_plugin/comment_wrapper_test.rb +++ b/plugins/anti_spam/test/unit/anti_spam_plugin/comment_wrapper_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class AntiSpamPluginCommentWrapperTest < ActiveSupport::TestCase +class AntiSpamPlugin::CommentWrapperTest < ActiveSupport::TestCase def setup @comment = Comment.new( @@ -15,10 +15,6 @@ class AntiSpamPluginCommentWrapperTest < ActiveSupport::TestCase @wrapper = AntiSpamPlugin::CommentWrapper.new(@comment) end - should 'use Rakismet::Model' do - assert_includes @wrapper.class.included_modules, Rakismet::Model - end - should 'get contents' do assert_equal @comment.body, @wrapper.content end diff --git a/plugins/anti_spam/test/unit/anti_spam_plugin/suggest_article_wrapper_test.rb b/plugins/anti_spam/test/unit/anti_spam_plugin/suggest_article_wrapper_test.rb index 6b0b68c..c751506 100644 --- a/plugins/anti_spam/test/unit/anti_spam_plugin/suggest_article_wrapper_test.rb +++ b/plugins/anti_spam/test/unit/anti_spam_plugin/suggest_article_wrapper_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class AntiSpamPluginCommentWrapperTest < ActiveSupport::TestCase +class AntiSpamPlugin::SuggestArticleWrapperTest < ActiveSupport::TestCase def setup @suggest_article = SuggestArticle.new( @@ -14,10 +14,6 @@ class AntiSpamPluginCommentWrapperTest < ActiveSupport::TestCase @wrapper = AntiSpamPlugin::SuggestArticleWrapper.new(@suggest_article) end - should 'use Rakismet::Model' do - assert_includes @wrapper.class.included_modules, Rakismet::Model - end - should 'get contents' do assert_equal @suggest_article.article_body, @wrapper.content end diff --git a/plugins/anti_spam/test/unit/anti_spam_plugin/wrapper_test.rb b/plugins/anti_spam/test/unit/anti_spam_plugin/wrapper_test.rb new file mode 100644 index 0000000..fd0a3d4 --- /dev/null +++ b/plugins/anti_spam/test/unit/anti_spam_plugin/wrapper_test.rb @@ -0,0 +1,25 @@ +require 'test_helper' +require 'anti_spam_plugin/wrapper' + +class AntiSpamPluginWrapperTest < ActiveSupport::TestCase + should 'use Rakismet::Model' do + wrapped = AntiSpamPlugin::Wrapper.new(mock) + assert_includes wrapped.class.included_modules, Rakismet::Model + end + + should 'wrap object according to wraps? method' do + class EvenWrapper < AntiSpamPlugin::Wrapper + def self.wraps?(object) + object % 2 == 0 + end + end + class OddWrapper < AntiSpamPlugin::Wrapper + def self.wraps?(object) + object % 2 != 0 + end + end + + assert AntiSpamPlugin::Wrapper.wrap(5).kind_of?(OddWrapper) + assert AntiSpamPlugin::Wrapper.wrap(6).kind_of?(EvenWrapper) + end +end diff --git a/plugins/anti_spam/test/unit/anti_spam_plugin_test.rb b/plugins/anti_spam/test/unit/anti_spam_plugin_test.rb index 22ce6d5..d2dcefe 100644 --- a/plugins/anti_spam/test/unit/anti_spam_plugin_test.rb +++ b/plugins/anti_spam/test/unit/anti_spam_plugin_test.rb @@ -2,58 +2,37 @@ require 'test_helper' class AntiSpamPluginTest < ActiveSupport::TestCase - def setup - profile = fast_create(Profile) - article = fast_create(TextileArticle, :profile_id => profile.id) - @comment = fast_create(Comment, :source_id => article.id, :source_type => 'Article') - - - @suggest_article = SuggestArticle.new(:target_id => profile.id, :target_type => 'Profile', :article_name => 'article', :article_body => 'lorem ipsum', :email => 'invalid@example.com', :name => 'article') - - @suggest_article.save! - - @settings = Noosfero::Plugin::Settings.new(@comment.environment, AntiSpamPlugin) - @settings.api_key = 'b8b80ddb8084062d0c9119c945ce3bc3' - @settings.save! + class Spammable + attr_accessor :spam + + def save!; end + def spam!; end + def ham!; end + def spam?; true; end + def environment; Environment.default; end + end + def setup + @spammable = Spammable.new @plugin = AntiSpamPlugin.new - @plugin.context = @comment end - should 'check for spam and mark comment as spam if server says it is spam' do - AntiSpamPlugin::CommentWrapper.any_instance.expects(:spam?).returns(true) - @comment.expects(:save!) + attr_accessor :spammable + + should 'check for spam and mark as spam if server says it is spam' do + spammable.expects(:save!) - @plugin.check_comment_for_spam(@comment) - assert @comment.spam + @plugin.check_for_spam(spammable) + assert spammable.spam end should 'report comment spam' do - AntiSpamPlugin::CommentWrapper.any_instance.expects(:spam!) - @plugin.comment_marked_as_spam(@comment) + spammable.expects(:spam!) + @plugin.marked_as_spam(spammable) end should 'report comment ham' do - AntiSpamPlugin::CommentWrapper.any_instance.expects(:ham!) - @plugin.comment_marked_as_ham(@comment) + spammable.expects(:ham!) + @plugin.marked_as_ham(spammable) end - - should 'check for spam and mark suggest_article as spam if server says it is spam' do - AntiSpamPlugin::SuggestArticleWrapper.any_instance.expects(:spam?).returns(true) - @suggest_article.expects(:save!) - - @plugin.check_suggest_article_for_spam(@suggest_article) - assert @suggest_article.spam - end - - should 'report suggest_article spam' do - AntiSpamPlugin::SuggestArticleWrapper.any_instance.expects(:spam!) - @plugin.suggest_article_marked_as_spam(@suggest_article) - end - - should 'report suggest_article ham' do - AntiSpamPlugin::SuggestArticleWrapper.any_instance.expects(:ham!) - @plugin.suggest_article_marked_as_ham(@suggest_article) - end - end -- libgit2 0.21.2