cirandas.net

ref: master

test/unit/person_notifier_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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
require_relative "../test_helper"

class PersonNotifierTest < ActiveSupport::TestCase

  FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures'
  CHARSET = "utf-8"

  def setup
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []
    Person.delete_all
    @admin = create_user('adminuser').person
    @member = create_user('member').person
    @admin.notification_time = 24
    @member.notification_time = 24
    @admin.save!
    @member.save!
    @community = fast_create(Community)
    @community.add_member(@admin)
    @article = fast_create(TextArticle, :name => 'Article test', :profile_id => @community.id, :notify_comments => false)
    Delayed::Job.delete_all
    ActionMailer::Base.deliveries = []
  end

  should 'deliver mail to community members' do
    @community.add_member(@member)
    process_delayed_job_queue
    notify
    sent = ActionMailer::Base.deliveries.last
    assert_equal [@member.email], sent.to
  end

  should 'do not send mail if do not have notifications' do
    @community.add_member(@member)
    ActionTracker::Record.delete_all
    assert_no_difference 'ActionMailer::Base.deliveries.count' do
      notify
    end
  end

  should 'do not send mail to people not joined to community' do
    Comment.create!(:author => @admin, :title => 'test comment 2', :body => 'body 2!', :source => @article)
    assert_no_difference 'ActionMailer::Base.deliveries.count' do
      notify
    end
  end

  should 'display author name in delivered mail' do
    @community.add_member(@member)
    User.current = @admin.user
    comment = Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article)
    comment.save!
    process_delayed_job_queue
    notify
    sent = ActionMailer::Base.deliveries.last
    assert_match /#{@admin.name}/, sent.body.to_s
  end

  should 'do not include comment created before last notification' do
    @community.add_member(@member)
    ActionTracker::Record.delete_all
    comment = Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article )
    @member.last_notification = DateTime.now.in_time_zone + 1.day
    assert_no_difference 'ActionMailer::Base.deliveries.count' do
      notify
    end
  end

  should 'update last notification date' do
    Comment.create!(:author => @admin, :title => 'test comment 2', :body => 'body 2!', :source => @article)
    notify
    initial_notification = @member.last_notification

    Comment.create!(:author => @admin, :title => 'test comment 2', :body => 'body 2!', :source => @article)
    @community.add_member(@member)
    notify
    assert @member.last_notification > initial_notification
  end

  should 'reschedule after notification' do
    Comment.create!(:author => @admin, :title => 'test comment 2', :body => 'body 2!', :source => @article)
    @community.add_member(@member)
    assert PersonNotifier::NotifyJob.find(@member.id).blank?
    notify
    assert PersonNotifier::NotifyJob.find(@member.id)
  end

  should 'schedule next mail at notification time' do
    @member.notification_time = 12
    time = Time.now.in_time_zone
    @member.notifier.schedule_next_notification_mail
    job = Delayed::Job.handler_like(PersonNotifier::NotifyJob.name).last
    assert job.run_at >= time + @member.notification_time.hours
    assert job.run_at < time + (@member.notification_time+1).hours
  end

  should 'do not schedule duplicated notification mail' do
    @member.notification_time = 12
    @member.notifier.schedule_next_notification_mail
    assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do
      @member.notifier.schedule_next_notification_mail
    end
  end

  should 'do not schedule next mail if notification time is zero' do
    @member.notification_time = 0
    assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do
      @member.notifier.schedule_next_notification_mail
    end
  end

  should 'schedule next notifications for all person with notification time greater than zero' do
    @member.notification_time = 1
    @admin.notification_time = 0
    @admin.save!
    @member.save!
    Delayed::Job.delete_all
    PersonNotifier.schedule_all_next_notification_mail
    process_delayed_job_queue
    assert_equal 1, Delayed::Job.count
  end

  should 'do not create duplicated job' do
    PersonNotifier.schedule_all_next_notification_mail
    assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do
      PersonNotifier.schedule_all_next_notification_mail
    end
  end

  should 'schedule after update and set a valid notification time' do
    assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do
      @member.notification_time = 0
      @member.save!
    end
    assert_difference 'job_count(PersonNotifier::NotifyJob)', 1 do
      @member.notification_time = 12
      @member.save!
    end
  end

  should 'reschedule with changed notification time' do
    time = Time.now.in_time_zone
    assert_difference 'job_count(PersonNotifier::NotifyJob)', 1 do
      @member.notification_time = 2
      @member.save!
    end
    assert_no_difference 'job_count(PersonNotifier::NotifyJob)' do
      @member.notification_time = 12
      @member.save!
    end
    @member.notifier.schedule_next_notification_mail
    job = Delayed::Job.handler_like(PersonNotifier::NotifyJob.name).last
    assert job.run_at >= time + @member.notification_time.hours
    assert job.run_at < time + (@member.notification_time+1).hours
  end

  should 'fail to render an invalid notificiation' do
    @community.add_member(@member)
    Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article)
    ActionTracker::Record.any_instance.stubs(:verb).returns("some_invalid_verb")
    process_delayed_job_queue

    Logger.any_instance.expects(:warn).with(regexp_matches(/#{:some_invalid_verb}/)).at_least(1)
    notify
  end

  Targets = {
    create_article: -> { create Forum, profile: @profile },
    new_friendship: -> { create Friendship, person: @member, friend: @member },
    join_community: -> { @member },
    add_member_in_community: -> { create_user.person },
    upload_image: -> { create Forum, profile: @profile },
    leave_scrap: -> { create Scrap, sender: @member, receiver: @profile },
    leave_scrap_to_self: -> { create Scrap, sender: @member, receiver: @profile },
    reply_scrap_on_self: -> { create Scrap, sender: @member, receiver: @profile },
    create_product: -> { create Product, profile: @profile, product_category: create(ProductCategory, environment: Environment.default) },
    update_product: -> { create Product, profile: @profile, product_category: create(ProductCategory, environment: Environment.default) },
    remove_product: -> { create Product, profile: @profile, product_category: create(ProductCategory, environment: Environment.default) },
    favorite_enterprise: -> { create FavoriteEnterprisePerson, enterprise: create(Enterprise), person: @member },
    new_follower: -> { @member }
  }

  ActionTrackerConfig.verb_names.each do |verb|
    should "render notification for verb #{verb}" do
      @member.tracked_notifications = []

      a = @member.tracked_notifications.build
      a.verb = verb
      a.user = @member
      a.created_at = @member.notifier.notify_from + 1.day
      @profile = create(Community)
      a.target = instance_exec &Targets[verb.to_sym]
      a.comments_count = 0
      a.params = {
        'name' => 'home', 'url' => '/', 'lead' => '',
        'receiver_url' => '/', 'content' => 'nothing',
        'friend_url' => '/', 'friend_profile_custom_icon' => [], 'friend_name' => ['joe'],
        'resource_name' => ['resource'], 'resource_profile_custom_icon' => [], 'resource_url' => ['/'],
        'enterprise_name' => 'coop', 'enterprise_url' => '/coop',
        'follower_url' => '/', 'follower_profile_custom_icon' => [], 'follower_name' => ['joe'],
        'view_url'=> ['/'], 'thumbnail_path' => ['1'],
      }
      a.get_url = ''
      a.save!
      n = @member.action_tracker_notifications.build
      n.action_tracker = a
      n.profile = @member
      n.save!

      assert_nothing_raised do
        notify
      end
    end
  end

  should 'exists? method in NotifyAllJob return false if there is no instance of this class created' do
    Delayed::Job.enqueue(PersonNotifier::NotifyJob.new)
    refute PersonNotifier::NotifyAllJob.exists?
  end

  should 'exists? method in NotifyAllJob return false if there is no jobs created' do
    refute PersonNotifier::NotifyAllJob.exists?
  end

  should 'exists? method in NotifyAllJob return true if there is at least one instance of this class' do
    Delayed::Job.enqueue(PersonNotifier::NotifyAllJob.new)
    assert PersonNotifier::NotifyAllJob.exists?
  end

  should 'perform create NotifyJob for all users with notification_time' do
    assert_difference 'job_count(PersonNotifier::NotifyJob)', 2 do
      Delayed::Job.enqueue(PersonNotifier::NotifyAllJob.new)
      process_delayed_job_queue
    end
  end

  should 'perform create NotifyJob for all users with notification_time defined greater than zero' do
    @member.notification_time = 1
    @admin.notification_time = 0
    @admin.save!
    @member.save!
    Delayed::Job.delete_all
    Delayed::Job.enqueue(PersonNotifier::NotifyAllJob.new)
    process_delayed_job_queue
    assert_equal 1, Delayed::Job.count
  end

  should 'NotifyJob failed jobs create a new NotifyJob on failure' do
    Delayed::Worker.max_attempts = 1
    Delayed::Job.enqueue(PersonNotifier::NotifyJob.new(@member.id))

    PersonNotifier.any_instance.stubs(:notify).raises('error')

    process_delayed_job_queue
    jobs = PersonNotifier::NotifyJob.find(@member.id)
    refute jobs.select {|j| !j.failed? && j.last_error.nil? }.empty?
  end

  should 'render image tags for both internal and external src' do
    @community.add_member(@member)
    process_delayed_job_queue
    notify
    sent = ActionMailer::Base.deliveries.last
    assert_match /src="\/\/www.gravatar.com\/avatar.*"/, sent.body.to_s
    assert_match /src="http:\/\/.*\/images\/icons-app\/community-icon.png.*"/, sent.body.to_s
  end

  should 'do not raise errors in NotifyJob failure to avoid loop' do
    Delayed::Worker.max_attempts = 1
    Delayed::Job.enqueue(PersonNotifier::NotifyJob.new(@member.id))

    PersonNotifier.any_instance.stubs(:notify).raises('error')
    PersonNotifier.any_instance.stubs(:dispatch_notification_mail).raises('error')

    process_delayed_job_queue
    jobs = PersonNotifier::NotifyJob.find(@member.id)
    assert jobs.select {|j| !j.failed? && j.last_error.nil? }.empty?
  end

  should 'list tasks in notification mail' do
    task = @member.tasks.create!
    process_delayed_job_queue
    notify
    sent = ActionMailer::Base.deliveries.last
    assert_match /href=".*\/myprofile\/member\/tasks"/, sent.body.to_s
  end

  should 'send the content summary even if one notification raise an exception during render' do
    @community.add_member(@member)
    process_delayed_job_queue

    ActionTracker::Record.any_instance.stubs(:get_resource_name).raises(StandardError)
    assert_difference 'ActionMailer::Base.deliveries.count' do
      notify
    end
  end

  private

  def notify
    @member.notifier.notify
  end

  def job_count(job)
    Delayed::Job.handler_like(job.name).count
  end

end