Commit 13bd4e382c89f97df818bbac14de13d7a2f61c61

Authored by Rodrigo Souto
1 parent 3ca8206c

anti_spam_plugin: refactor wrappers logic

plugins/anti_spam/lib/anti_spam_plugin.rb
... ... @@ -12,34 +12,19 @@ class AntiSpamPlugin < Noosfero::Plugin
12 12 'api.antispam.typepad.com'
13 13 end
14 14  
15   - def check_comment_for_spam(comment)
16   - if rakismet_call AntiSpamPlugin::CommentWrapper.new(comment), comment.environment, :spam?
17   - comment.spam = true
18   - comment.save!
  15 + def check_for_spam(object)
  16 + if rakismet_call AntiSpamPlugin::Wrapper.wrap(object), object.environment, :spam?
  17 + object.spam = true
  18 + object.save!
19 19 end
20 20 end
21 21  
22   - def comment_marked_as_spam(comment)
23   - rakismet_call AntiSpamPlugin::CommentWrapper.new(comment), comment.environment, :spam!
  22 + def marked_as_spam(object)
  23 + rakismet_call AntiSpamPlugin::Wrapper.wrap(object), object.environment, :spam!
24 24 end
25 25  
26   - def comment_marked_as_ham(comment)
27   - rakismet_call AntiSpamPlugin::CommentWrapper.new(comment), comment.environment, :ham!
28   - end
29   -
30   - def check_suggest_article_for_spam(suggest_article)
31   - if rakismet_call AntiSpamPlugin::SuggestArticleWrapper.new(suggest_article), suggest_article.environment, :spam?
32   - suggest_article.spam = true
33   - suggest_article.save!
34   - end
35   - end
36   -
37   - def suggest_article_marked_as_spam(suggest_article)
38   - rakismet_call AntiSpamPlugin::SuggestArticleWrapper.new(suggest_article), suggest_article.environment, :spam!
39   - end
40   -
41   - def suggest_article_marked_as_ham(suggest_article)
42   - rakismet_call AntiSpamPlugin::SuggestArticleWrapper.new(suggest_article), suggest_article.environment, :ham!
  26 + def marked_as_ham(object)
  27 + rakismet_call AntiSpamPlugin::Wrapper.wrap(object), object.environment, :ham!
43 28 end
44 29  
45 30 protected
... ...
plugins/anti_spam/lib/anti_spam_plugin/comment_wrapper.rb
1   -class AntiSpamPlugin::CommentWrapper < Struct.new(:comment)
2   -
3   - delegate :author_name, :author_email, :title, :body, :ip_address, :user_agent, :referrer, :to => :comment
4   -
5   - include Rakismet::Model
6   -
7   - alias :author :author_name
8   - alias :user_ip :ip_address
9   - alias :content :body
10   -
  1 +class AntiSpamPlugin::CommentWrapper < AntiSpamPlugin::Wrapper
  2 + alias_attribute :author, :author_name
  3 + alias_attribute :user_ip, :ip_address
  4 + alias_attribute :content, :body
  5 +
  6 + def self.wraps?(object)
  7 + object.kind_of?(Comment)
  8 + end
11 9 end
... ...
plugins/anti_spam/lib/anti_spam_plugin/suggest_article_wrapper.rb
1   -class AntiSpamPlugin::SuggestArticleWrapper < Struct.new(:suggest_article)
2   -
3   - delegate :name, :email, :article_body, :ip_address, :user_agent, :referrer, :to => :suggest_article
4   -
5   - include Rakismet::Model
6   -
7   - alias :author :name
8   - alias :author_email :email
9   - alias :user_ip :ip_address
10   - alias :content :article_body
11   -
  1 +class AntiSpamPlugin::SuggestArticleWrapper < AntiSpamPlugin::Wrapper
  2 + alias_attribute :author, :name
  3 + alias_attribute :author_email, :email
  4 + alias_attribute :user_ip, :ip_address
  5 + alias_attribute :content, :article_body
  6 +
  7 + def self.wraps?(object)
  8 + object.kind_of?(SuggestArticle)
  9 + end
12 10 end
... ...
plugins/anti_spam/lib/anti_spam_plugin/wrapper.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +class AntiSpamPlugin::Wrapper < SimpleDelegator
  2 + include Rakismet::Model
  3 +
  4 + @@wrappers = []
  5 +
  6 + def self.wrap(object)
  7 + wrapper = @@wrappers.find { |wrapper| wrapper.wraps?(object) }
  8 + wrapper ? wrapper.new(object) : object
  9 + end
  10 +
  11 + def self.wraps?(object)
  12 + false
  13 + end
  14 +
  15 + def self.inherited(child)
  16 + @@wrappers << child
  17 + end
  18 +end
... ...
plugins/anti_spam/test/unit/anti_spam_plugin/comment_wrapper_test.rb
1 1 require 'test_helper'
2 2  
3   -class AntiSpamPluginCommentWrapperTest < ActiveSupport::TestCase
  3 +class AntiSpamPlugin::CommentWrapperTest < ActiveSupport::TestCase
4 4  
5 5 def setup
6 6 @comment = Comment.new(
... ... @@ -15,10 +15,6 @@ class AntiSpamPluginCommentWrapperTest &lt; ActiveSupport::TestCase
15 15 @wrapper = AntiSpamPlugin::CommentWrapper.new(@comment)
16 16 end
17 17  
18   - should 'use Rakismet::Model' do
19   - assert_includes @wrapper.class.included_modules, Rakismet::Model
20   - end
21   -
22 18 should 'get contents' do
23 19 assert_equal @comment.body, @wrapper.content
24 20 end
... ...
plugins/anti_spam/test/unit/anti_spam_plugin/suggest_article_wrapper_test.rb
1 1 require 'test_helper'
2 2  
3   -class AntiSpamPluginCommentWrapperTest < ActiveSupport::TestCase
  3 +class AntiSpamPlugin::SuggestArticleWrapperTest < ActiveSupport::TestCase
4 4  
5 5 def setup
6 6 @suggest_article = SuggestArticle.new(
... ... @@ -14,10 +14,6 @@ class AntiSpamPluginCommentWrapperTest &lt; ActiveSupport::TestCase
14 14 @wrapper = AntiSpamPlugin::SuggestArticleWrapper.new(@suggest_article)
15 15 end
16 16  
17   - should 'use Rakismet::Model' do
18   - assert_includes @wrapper.class.included_modules, Rakismet::Model
19   - end
20   -
21 17 should 'get contents' do
22 18 assert_equal @suggest_article.article_body, @wrapper.content
23 19 end
... ...
plugins/anti_spam/test/unit/anti_spam_plugin/wrapper_test.rb 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +require 'test_helper'
  2 +require 'anti_spam_plugin/wrapper'
  3 +
  4 +class AntiSpamPluginWrapperTest < ActiveSupport::TestCase
  5 + should 'use Rakismet::Model' do
  6 + wrapped = AntiSpamPlugin::Wrapper.new(mock)
  7 + assert_includes wrapped.class.included_modules, Rakismet::Model
  8 + end
  9 +
  10 + should 'wrap object according to wraps? method' do
  11 + class EvenWrapper < AntiSpamPlugin::Wrapper
  12 + def self.wraps?(object)
  13 + object % 2 == 0
  14 + end
  15 + end
  16 + class OddWrapper < AntiSpamPlugin::Wrapper
  17 + def self.wraps?(object)
  18 + object % 2 != 0
  19 + end
  20 + end
  21 +
  22 + assert AntiSpamPlugin::Wrapper.wrap(5).kind_of?(OddWrapper)
  23 + assert AntiSpamPlugin::Wrapper.wrap(6).kind_of?(EvenWrapper)
  24 + end
  25 +end
... ...
plugins/anti_spam/test/unit/anti_spam_plugin_test.rb
... ... @@ -2,58 +2,37 @@ require &#39;test_helper&#39;
2 2  
3 3 class AntiSpamPluginTest < ActiveSupport::TestCase
4 4  
5   - def setup
6   - profile = fast_create(Profile)
7   - article = fast_create(TextileArticle, :profile_id => profile.id)
8   - @comment = fast_create(Comment, :source_id => article.id, :source_type => 'Article')
9   -
10   -
11   - @suggest_article = SuggestArticle.new(:target_id => profile.id, :target_type => 'Profile', :article_name => 'article', :article_body => 'lorem ipsum', :email => 'invalid@example.com', :name => 'article')
12   -
13   - @suggest_article.save!
14   -
15   - @settings = Noosfero::Plugin::Settings.new(@comment.environment, AntiSpamPlugin)
16   - @settings.api_key = 'b8b80ddb8084062d0c9119c945ce3bc3'
17   - @settings.save!
  5 + class Spammable
  6 + attr_accessor :spam
  7 +
  8 + def save!; end
  9 + def spam!; end
  10 + def ham!; end
  11 + def spam?; true; end
  12 + def environment; Environment.default; end
  13 + end
18 14  
  15 + def setup
  16 + @spammable = Spammable.new
19 17 @plugin = AntiSpamPlugin.new
20   - @plugin.context = @comment
21 18 end
22 19  
23   - should 'check for spam and mark comment as spam if server says it is spam' do
24   - AntiSpamPlugin::CommentWrapper.any_instance.expects(:spam?).returns(true)
25   - @comment.expects(:save!)
  20 + attr_accessor :spammable
  21 +
  22 + should 'check for spam and mark as spam if server says it is spam' do
  23 + spammable.expects(:save!)
26 24  
27   - @plugin.check_comment_for_spam(@comment)
28   - assert @comment.spam
  25 + @plugin.check_for_spam(spammable)
  26 + assert spammable.spam
29 27 end
30 28  
31 29 should 'report comment spam' do
32   - AntiSpamPlugin::CommentWrapper.any_instance.expects(:spam!)
33   - @plugin.comment_marked_as_spam(@comment)
  30 + spammable.expects(:spam!)
  31 + @plugin.marked_as_spam(spammable)
34 32 end
35 33  
36 34 should 'report comment ham' do
37   - AntiSpamPlugin::CommentWrapper.any_instance.expects(:ham!)
38   - @plugin.comment_marked_as_ham(@comment)
  35 + spammable.expects(:ham!)
  36 + @plugin.marked_as_ham(spammable)
39 37 end
40   -
41   - should 'check for spam and mark suggest_article as spam if server says it is spam' do
42   - AntiSpamPlugin::SuggestArticleWrapper.any_instance.expects(:spam?).returns(true)
43   - @suggest_article.expects(:save!)
44   -
45   - @plugin.check_suggest_article_for_spam(@suggest_article)
46   - assert @suggest_article.spam
47   - end
48   -
49   - should 'report suggest_article spam' do
50   - AntiSpamPlugin::SuggestArticleWrapper.any_instance.expects(:spam!)
51   - @plugin.suggest_article_marked_as_spam(@suggest_article)
52   - end
53   -
54   - should 'report suggest_article ham' do
55   - AntiSpamPlugin::SuggestArticleWrapper.any_instance.expects(:ham!)
56   - @plugin.suggest_article_marked_as_ham(@suggest_article)
57   - end
58   -
59 38 end
... ...