notify_activity_to_profiles_job_test.rb
7.36 KB
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
require File.dirname(__FILE__) + '/../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 friends 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')
    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(Friendship, :person_id => p1.id, :friend_id => m1.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 'not notify the communities 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')
    assert !NotifyActivityToProfilesJob::NOTIFY_ONLY_COMMUNITY.include?(action_tracker.verb)
    m1, m2 = fast_create(Person), fast_create(Person), fast_create(Person), fast_create(Person)
    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 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 friends, 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')
    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 6, ActionTrackerNotification.count
    [person, community, 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 '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')
    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 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