notify_mentioned_users_job_test.rb 4.53 KB
require_relative "../test_helper"

class NotifyMentionedUsersJobTest < ActiveSupport::TestCase

  def setup
    @person1 = fast_create(Person, :identifier => "tester")
    @person2 = fast_create(Person, :identifier => "friend")
    @person3 = fast_create(Person, :identifier => "third")
    @person1.add_friend @person2
    @person1.add_friend @person3

    process_delayed_job_queue
    ProfileFollower.delete_all
    ActionTrackerNotification.delete_all
  end

  should 'create notify mentioned uses job if scrap content has mentioned users' do
    s = Scrap.new
    s.sender = @person1
    s.receiver = @person2
    s.content = "Run @third, run!!!"
    s.save!

    action_tracker = ActionTracker::Record.create(:user => @person1, :target => s,
                                                  :verb => 'notify_mentioned_users',
                                                  :params => {"content" => s.content, "url"=>s.receiver.url})

    job = NotifyMentionedUsersJob.new(action_tracker.id)
    job.perform
    process_delayed_job_queue

    assert_equal 2, ActionTrackerNotification.count

    notification = ActionTrackerNotification.find_by profile_id: @person3.id
    assert_equal action_tracker, notification.action_tracker
  end

  should 'create notify mentioned uses job if comment body has mentioned users' do
    a = fast_create(Article, :profile_id => @person1.id, :name => "Zombiesss")

    comment = Comment.create!(:author => @person1, :body => 'Run @third, run!!!', :source => a)
    action_tracker = ActionTracker::Record.create(:user => @person1, :target => comment,
                                                  :verb => 'notify_mentioned_users',
                                                  :params => {"body" => comment.body, "url" => comment.url})

    job = NotifyMentionedUsersJob.new(action_tracker.id)
    job.perform
    process_delayed_job_queue

    assert_equal 1, ActionTrackerNotification.count

    notification = ActionTrackerNotification.find_by profile_id: @person3.id
    assert_equal action_tracker, notification.action_tracker
  end

  should 'not create notify mentioned users job if scrap content has no mentioned users' do
    s = Scrap.new
    s.sender = @person1
    s.receiver = @person2
    s.content = "Run third, run!!!"
    s.save!

    action_tracker = ActionTracker::Record.create(:user => @person1, :target => s,
                                                  :verb => 'notify_mentioned_users',
                                                  :params => {"content" => s.content, "url"=>s.receiver.url})

    job = NotifyMentionedUsersJob.new(action_tracker.id)
    job.perform
    process_delayed_job_queue

    assert_equal 1, ActionTrackerNotification.count
    assert_not_includes ActionTrackerNotification.all.map{|a|a.action_tracker.verb}, "notify_mentioned_users"
  end

  should 'not create notify mentioned users job if comment body has no mentioned users' do
    a = fast_create(Article, :profile_id => @person1.id, :name => "Zombiesss")

    comment = Comment.create!(:author => @person1, :body => 'Run third, run!!!', :source => a)
    action_tracker = ActionTracker::Record.create(:user => @person1, :target => comment,
                                                  :verb => 'notify_mentioned_users',
                                                  :params => {"body" => comment.body, "url" => comment.url})

    job = NotifyMentionedUsersJob.new(action_tracker.id)
    job.perform
    process_delayed_job_queue

    assert_empty ActionTrackerNotification.all
  end

  should 'not create notification from mention if the mentioned users are also followers' do
    circle = Circle.create!(:person=> @person3, :name => "Zombies", :profile_type => 'Person')
    @person3.follow(@person1, circle)
    process_delayed_job_queue
    ActionTrackerNotification.delete_all

    s = Scrap.new
    s.sender = @person1
    s.receiver = @person2
    s.content = "Run @third, run!!!"
    s.save!

    action_tracker = ActionTracker::Record.create(:user => @person1, :target => s,
                                                  :verb => 'notify_mentioned_users',
                                                  :params => {"content" => s.content, "url"=>s.receiver.url})

    job = NotifyMentionedUsersJob.new(action_tracker.id)
    job.perform
    process_delayed_job_queue

    assert_equal 2, ActionTrackerNotification.count
		assert_not_includes ActionTrackerNotification.all.map{|a|a.action_tracker.verb}, "notify_mentioned_users"
    assert_equal ActionTrackerNotification.all.map{|a|a.action_tracker.verb}, ["leave_scrap","leave_scrap"]
  end
end