cirandas.net

ref: master

test/unit/notify_activity_to_profiles_job_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
require_relative "../test_helper"

class NotifyActivityToProfilesJobTest < ActiveSupport::TestCase

  should 'notify just the community in tracker with add_member_in_community verb' do
    person = fast_create(Person)
    community  = fast_create(Community)
    action_tracker = fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person.id, :target_type => 'Profile', :target_id => community.id, :verb => 'add_member_in_community')
    assert NotifyActivityToProfilesJob::NOTIFY_ONLY_COMMUNITY.include?(action_tracker.verb)
    p1, p2, m1, m2 = fast_create(Person), fast_create(Person), fast_create(Person), fast_create(Person)
    fast_create(Friendship, :person_id => person.id, :friend_id => p1.id)
    fast_create(Friendship, :person_id => person.id, :friend_id => p2.id)
    fast_create(RoleAssignment, :accessor_id => m1.id, :role_id => 3, :resource_id => community.id)
    fast_create(RoleAssignment, :accessor_id => m2.id, :role_id => 3, :resource_id => community.id)
    ActionTrackerNotification.delete_all
    job = NotifyActivityToProfilesJob.new(action_tracker.id)
    job.perform
    process_delayed_job_queue

    assert_equal 1, ActionTrackerNotification.count
    [community].each do |profile|
      notification = ActionTrackerNotification.find_by profile_id: profile.id
      assert_equal action_tracker, notification.action_tracker
    end
  end

  should 'notify just the users and his followers tracking user actions' do
    person = fast_create(Person)
    community  = fast_create(Community)
    action_tracker = fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person.id, :target_type => 'Profile', :verb => 'create_article')
    refute NotifyActivityToProfilesJob::NOTIFY_ONLY_COMMUNITY.include?(action_tracker.verb)
    p1, p2, m1, m2 = fast_create(Person), fast_create(Person), fast_create(Person), fast_create(Person)

    circle1 = Circle.create!(:person=> p1, :name => "Zombies", :profile_type => 'Person')
    circle2 = Circle.create!(:person=> p2, :name => "Zombies", :profile_type => 'Person')
    circle = Circle.create!(:person=> person, :name => "Zombies", :profile_type => 'Person')

    fast_create(ProfileFollower, :profile_id => person.id, :circle_id => circle1.id)
    fast_create(ProfileFollower, :profile_id => person.id, :circle_id => circle2.id)
    fast_create(ProfileFollower, :profile_id => m1.id, :circle_id => circle.id)

    fast_create(RoleAssignment, :accessor_id => m2.id, :role_id => 3, :resource_id => community.id)
    ActionTrackerNotification.delete_all
    job = NotifyActivityToProfilesJob.new(action_tracker.id)
    job.perform
    process_delayed_job_queue

    assert_equal 3, ActionTrackerNotification.count
    [person, p1, p2].each do |profile|
      notification = ActionTrackerNotification.find_by profile_id: profile.id
      assert_equal action_tracker, notification.action_tracker
    end
  end

  should 'notify only marked people on marked scraps' do
    profile = create_user('scrap-creator').person
    c1 = Circle.create!(:name => 'Family', :person => profile, :profile_type => Person)
    p1 = create_user('emily').person
    p2 = create_user('wollie').person
    not_marked = create_user('jack').person
    not_marked.add_friend(p1)
    not_marked.add_friend(p2)
    not_marked.add_friend(profile)
    ProfileFollower.create!(:profile => p1, :circle => c1)
    ProfileFollower.create!(:profile => p2, :circle => c1)
    ProfileFollower.create!(:profile => not_marked, :circle => c1)

    scrap = Scrap.create!(:content => 'Secret message.', :sender_id => profile.id, :receiver_id => profile.id, :marked_people => [p1,p2])
    process_delayed_job_queue

    assert p1.tracked_notifications.where(:target => scrap).present?
    assert p2.tracked_notifications.where(:target => scrap).present?
    assert not_marked.tracked_notifications.where(:target => scrap).blank?
  end

  should 'notify the community members on private articles' do
    person = fast_create(Person)
    community  = fast_create(Community)
    article = fast_create(TextArticle, :published => false, :profile_id => community.id)
    action_tracker = fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person.id, :target_type => 'Article', :target_id => article.id, :verb => 'create_article')
    refute NotifyActivityToProfilesJob::NOTIFY_ONLY_COMMUNITY.include?(action_tracker.verb)
    m1, m2 = fast_create(Person), fast_create(Person), fast_create(Person), fast_create(Person)

    circle1 = Circle.create!(:person=> m1, :name => "Zombies", :profile_type => 'Community')
    circle2 = Circle.create!(:person=> m2, :name => "Zombies", :profile_type => 'Community')
    fast_create(ProfileFollower, :profile_id => community.id, :circle_id => circle1.id)
    fast_create(ProfileFollower, :profile_id => community.id, :circle_id => circle2.id)

    ActionTrackerNotification.delete_all
    job = NotifyActivityToProfilesJob.new(action_tracker.id)
    job.perform
    process_delayed_job_queue

    assert_equal 4, ActionTrackerNotification.count
    [person, community, m1, m2].each do |profile|
      notification = ActionTrackerNotification.find_by profile_id: profile.id
      assert_equal action_tracker, notification.action_tracker
    end
  end

  should 'notify users its followers, the community and its members' do
    person = fast_create(Person)
    community  = fast_create(Community)
    action_tracker = fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person.id, :target_type => 'Profile', :target_id => community.id, :verb => 'create_article')
    refute NotifyActivityToProfilesJob::NOTIFY_ONLY_COMMUNITY.include?(action_tracker.verb)
    p1, p2, m1, m2 = fast_create(Person), fast_create(Person), fast_create(Person), fast_create(Person)

    circle1 = Circle.create!(:person=> p1, :name => "Zombies", :profile_type => 'Person')
    fast_create(ProfileFollower, :profile_id => person.id, :circle_id => circle1.id)

    circle1 = Circle.create!(:person=> m1, :name => "Zombies", :profile_type => 'Community')
    circle2 = Circle.create!(:person=> m2, :name => "Zombies", :profile_type => 'Community')
    fast_create(ProfileFollower, :profile_id => community.id, :circle_id => circle1.id)
    fast_create(ProfileFollower, :profile_id => community.id, :circle_id => circle2.id)

    ActionTrackerNotification.delete_all
    job = NotifyActivityToProfilesJob.new(action_tracker.id)
    job.perform
    process_delayed_job_queue
    assert_equal 5, ActionTrackerNotification.count
    [person, community, p1, m1, m2].each do |profile|
      notification = ActionTrackerNotification.find_by profile_id: profile.id
      assert_equal action_tracker, notification.action_tracker
    end
  end

  should 'notify only the community and its members if it is private' do
    person = fast_create(Person)
    private_community  = fast_create(Community, :public_profile => false)
    action_tracker = fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person.id, :target_type => 'Profile', :target_id => private_community.id, :verb => 'create_article')
    refute NotifyActivityToProfilesJob::NOTIFY_ONLY_COMMUNITY.include?(action_tracker.verb)
    p1, p2, m1, m2 = fast_create(Person), fast_create(Person), fast_create(Person), fast_create(Person)
    fast_create(Friendship, :person_id => person.id, :friend_id => p1.id)
    fast_create(Friendship, :person_id => person.id, :friend_id => p2.id)

    circle1 = Circle.create!(:person=> m1, :name => "Zombies", :profile_type => 'Community')
    circle2 = Circle.create!(:person=> m2, :name => "Zombies", :profile_type => 'Community')
    fast_create(ProfileFollower, :profile_id => private_community.id, :circle_id => circle1.id)
    fast_create(ProfileFollower, :profile_id => private_community.id, :circle_id => circle2.id)

    ActionTrackerNotification.delete_all
    job = NotifyActivityToProfilesJob.new(action_tracker.id)
    job.perform
    process_delayed_job_queue

    assert_equal 4, ActionTrackerNotification.count

    # Community notification
    notification = ActionTrackerNotification.find_by profile_id: private_community.id
    assert_equal action_tracker, notification.action_tracker

    # User notification
    notification = ActionTrackerNotification.find_by profile_id: person.id
    assert_equal action_tracker, notification.action_tracker

    # Community members notifications
    assert ActionTrackerNotification.find_by profile_id: m1.id
    assert ActionTrackerNotification.find_by profile_id: m2.id

    # No user friends notification
    assert_nil ActionTrackerNotification.find_by profile_id: p1.id
    assert_nil ActionTrackerNotification.find_by profile_id: p2.id
  end

  should 'not notify the community tracking join_community verb' do
    person = fast_create(Person)
    community  = fast_create(Community)
    action_tracker = fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person.id, :target_type => 'Profile', :target_id => community.id, :verb => 'join_community')
    refute NotifyActivityToProfilesJob::NOTIFY_ONLY_COMMUNITY.include?(action_tracker.verb)
    p1, p2, m1, m2 = fast_create(Person), fast_create(Person), fast_create(Person), fast_create(Person)

    circle1 = Circle.create!(:person=> p1, :name => "Zombies", :profile_type => 'Person')
    circle2 = Circle.create!(:person=> p2, :name => "Zombies", :profile_type => 'Person')
    circle3 = Circle.create!(:person=> m1, :name => "Zombies", :profile_type => 'Community')
    circle4 = Circle.create!(:person=> m2, :name => "Zombies", :profile_type => 'Community')

    fast_create(ProfileFollower, :profile_id => person.id, :circle_id => circle1.id)
    fast_create(ProfileFollower, :profile_id => person.id, :circle_id => circle2.id)
    fast_create(ProfileFollower, :profile_id => community.id, :circle_id => circle3.id)
    fast_create(ProfileFollower, :profile_id => community.id, :circle_id => circle4.id)

    ActionTrackerNotification.delete_all
    job = NotifyActivityToProfilesJob.new(action_tracker.id)
    job.perform
    process_delayed_job_queue

    assert_equal 5, ActionTrackerNotification.count
    [person, p1, p2, m1, m2].each do |profile|
      notification = ActionTrackerNotification.find_by profile_id: profile.id
      assert_equal action_tracker, notification.action_tracker
    end
  end

  should "the NOTIFY_ONLY_COMMUNITY constant has all the verbs tested" do
    notify_community_verbs = ['add_member_in_community']
    assert_equal [], notify_community_verbs - NotifyActivityToProfilesJob::NOTIFY_ONLY_COMMUNITY
    assert_equal [], NotifyActivityToProfilesJob::NOTIFY_ONLY_COMMUNITY - notify_community_verbs
  end

  should "the NOT_NOTIFY_COMMUNITY constant has all the verbs tested" do
    not_notify_community_verbs = ['join_community']
    assert_equal [], not_notify_community_verbs - NotifyActivityToProfilesJob::NOT_NOTIFY_COMMUNITY
    assert_equal [], NotifyActivityToProfilesJob::NOT_NOTIFY_COMMUNITY - not_notify_community_verbs
  end

  should 'cancel notify when target no more exists' do
    person = fast_create(Person)
    friend = fast_create(Person)
    fast_create(Friendship, :person_id => person.id, :friend_id => friend.id)
    action_tracker = fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person.id, :target_type => 'Profile', :verb => 'create_article')
    ActionTrackerNotification.delete_all
    job = NotifyActivityToProfilesJob.new(action_tracker.id)
    person.destroy
    job.perform
    process_delayed_job_queue
    assert_equal 0, ActionTrackerNotification.count
  end

end