cirandas.net

ref: master

test/unit/suggest_article_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
require_relative "../test_helper"

class SuggestArticleTest < ActiveSupport::TestCase

  def setup
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []
    @profile = create_user('test_user').person
    Noosfero::Plugin.stubs(:all).returns(['SuggestArticleTest::EverythingIsSpam', 'SuggestArticleTest::SpamNotification'])
  end
  attr_reader :profile

  should 'have the article_name' do
    t = SuggestArticle.new
    refute t.article_object.errors[:name].present?
    t.article_object.valid?
    assert t.article_object.errors[:name].present?
  end

  should 'have the email' do
    t = SuggestArticle.new
    refute t.errors[:email.to_s].present?
    t.valid?
    assert t.errors[:email.to_s].present?
  end

  should 'have the name' do
    t = SuggestArticle.new
    refute t.errors[:name.to_s].present?
    t.valid?
    assert t.errors[:name.to_s].present?
  end

  should 'have the target_id' do
    t = SuggestArticle.new
    refute t.errors[:target_id.to_s].present?
    t.valid?
    assert t.errors[:target_id.to_s].present?
  end

  should 'have the article' do
    t = SuggestArticle.new
    assert t.respond_to?(:article_object)
    refute t.errors[:article_object].present?
    t.valid?
    assert t.errors[:article_object].present?
  end

  should 'create an article on with perfom method' do
    t = SuggestArticle.new
    name = 'some name'
    body = 'some body'
    abstract = 'some abstract'
    t.article = {:name => name, :body => body, :abstract => abstract}
    t.target = @profile
    count = TextArticle.count
    t.perform
    assert_equal count + 1, TextArticle.count
  end

  should 'fill source name and URL into created article' do
    t = build(SuggestArticle, :target => @profile)
    t.article.merge!({:source_name => 'GNU project', :source => 'http://www.gnu.org/'})
    t.perform

    article = TextArticle.last
    assert_equal 'GNU project', article.source_name
    assert_equal 'http://www.gnu.org/', article.source
  end

  should 'use name and e-mail as sender info' do
    t = build(SuggestArticle, :target => @profile)
    t.name = 'Some One'
    t.email = 'someone@example.com'
    assert_match(/.*Some One.*someone@example.com/, t.sender)
  end

  should 'highlight created article' do
    t = build(SuggestArticle, :target => @profile)
    t.article[:highlighted] = true
    t.perform

    article = TextArticle.where(name: t.article_name).last # just to be sure
    assert article.highlighted
  end

  should 'not be highlighted by default' do
    t = build(SuggestArticle, :target => @profile)
    t.perform

    article = TextArticle.where(name: t.article_name).last
    assert_equal false, article.highlighted
  end

  should 'override target notification message method from Task' do
    task = build(SuggestArticle, :target => @profile)
    assert_nothing_raised NotImplementedError do
      task.target_notification_message
    end
  end

  should 'notify community moderators after create article suggestions' do
    task = build(SuggestArticle, :target => @profile)
    task.save
  end

  should 'fill name into author_name created article' do
    t = build(SuggestArticle, :target => @profile)
    t.name = 'some name'
    t.perform

    article = TextArticle.last
    assert_equal 'some name', article.author_name
  end

  should 'have target notification message' do
    task = build(SuggestArticle, :target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe')

    assert_match(/#{task.name}.*suggested the publication of the article: #{task.subject}.*[\n]*.*to approve or reject/, task.target_notification_message)
  end

  should 'have target notification description' do
    task = build(SuggestArticle,:target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe')

    assert_match(/#{task.name}.*suggested the publication of the article: #{task.subject}/, task.target_notification_description)
  end

  should 'deliver target notification message' do
    task = build(SuggestArticle, :target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe', :email => 'johndoe@example.com')

    email = TaskMailer.target_notification(task, task.target_notification_message).deliver

    assert_match(/#{task.name}.*suggested the publication of the article: #{task.subject}/, email.subject)
  end

  class EverythingIsSpam < Noosfero::Plugin
    def check_for_spam(object)
      object.spam!
    end
  end

  should 'delegate spam detection to plugins' do
    Environment.default.enable_plugin(EverythingIsSpam)

    t1 = build(SuggestArticle, :target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe', :email => 'johndoe@example.com')

    EverythingIsSpam.any_instance.expects(:check_for_spam)

    t1.check_for_spam
  end

  class SpamNotification < Noosfero::Plugin
    class << self
      attr_accessor :marked_as_spam
      attr_accessor :marked_as_ham
    end

    def check_for_spam(c)
      # do nothing
    end

    def marked_as_spam(c)
      self.class.marked_as_spam = c
    end

    def marked_as_ham(c)
      self.class.marked_as_ham = c
    end
  end

  should 'notify plugins of suggest_articles being marked as spam' do
    Environment.default.enable_plugin(SpamNotification)

    t = SuggestArticle.create!(:target => @profile, :article => {:name => 'suggested article', :body => 'wanna feel my body? my body baaaby'}, :name => 'johndoe', :email => 'johndoe@example.com')

    t.spam!
    process_delayed_job_queue

    assert_equal t, SpamNotification.marked_as_spam
  end

  should 'notify plugins of suggest_articles being marked as ham' do
    Environment.default.enable_plugin(SpamNotification)

    t = SuggestArticle.create!(:target => @profile, :article => {:name => 'suggested article', :body => 'wanna feel my body? my body baaaby'}, :name => 'johndoe', :email => 'johndoe@example.com')

    t.ham!
    process_delayed_job_queue

    assert_equal t, SpamNotification.marked_as_ham
  end

  should 'store User-Agent' do
    t = SuggestArticle.new(:user_agent => 'foo')
    assert_equal 'foo', t.user_agent
  end

  should 'store referrer' do
    t = SuggestArticle.new(:referrer => 'bar')
    assert_equal 'bar', t.referrer
  end

  should 'log spammer ip after marking comment as spam' do
    t = SuggestArticle.create!(:target => @profile, :article => {:name => 'suggested article', :body => 'wanna feel my body? my body baaaby'}, :name => 'johndoe', :email => 'johndoe@example.com', :ip_address => '192.168.0.1')
    t.spam!
    log = File.open('log/test_spammers.log')
    assert_match "SuggestArticle-id: #{t.id} IP: 192.168.0.1", log.read
  end

  should 'not require name and email when requestor is present' do
    t = SuggestArticle.new(:requestor => fast_create(Person))
    t.valid?
    assert t.errors[:email].blank?
    assert t.errors[:name].blank?
  end

  should 'return name as sender when requestor is setted' do
    person = fast_create(Person)
    t = SuggestArticle.new(:requestor => person)
    assert_equal person.name, t.sender
  end

  should 'create an event on perfom method' do
    t = SuggestArticle.new
    t.article = {:name => 'name', :body => 'body', :type => 'Event'}
    t.target = @profile
    assert_difference "Event.count" do
      t.perform
    end
  end

  should 'accept article type parameter' do
    t = SuggestArticle.new
    t.article = {:name => 'name', :body => 'body', :type => 'TextArticle'}
    t.article_type == TextArticle
  end

  should 'fallback to tinymce when type parameter is invalid' do
    t = SuggestArticle.new
    t.article = {:name => 'name', :body => 'body', :type => 'Comment'}
    t.article_type == TextArticle
  end

  should 'fallback to tinymce when type parameter is blank' do
    t = SuggestArticle.new
    t.article = {:name => 'name', :body => 'body', :type => ''}
    t.article_type == TextArticle
  end
end