person_notifier_test.rb
7.33 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
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
require File.dirname(__FILE__) + '/../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.destroy_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(TextileArticle, :name => 'Article test', :profile_id => @community.id, :notify_comments => false)
Delayed::Job.delete_all
notify
ActionMailer::Base.deliveries = []
end
should 'deliver mail to community members' do
@community.add_member(@member)
notify
sent = ActionMailer::Base.deliveries.first
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
notify
assert ActionMailer::Base.deliveries.empty?
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)
notify
assert ActionMailer::Base.deliveries.blank?
end
should 'display author name in delivered mail' do
@community.add_member(@member)
Comment.create!(:author => @admin, :title => 'test comment', :body => 'body!', :source => @article)
notify
sent = ActionMailer::Base.deliveries.first
assert_match /#{@admin.name}/, sent.body
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 + 1.day
notify
assert ActionMailer::Base.deliveries.empty?
end
should 'update last notification date' do
Comment.create!(:author => @admin, :title => 'test comment 2', :body => 'body 2!', :source => @article)
@community.add_member(@member)
initial_notification = @member.last_notification
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
@member.notifier.schedule_next_notification_mail
assert_equal @member.notification_time, DateTime.now.hour - Delayed::Job.first.run_at.hour
end
should 'do not schedule duplicated notification mail' do
@member.notification_time = 12
@member.notifier.schedule_next_notification_mail
@member.notifier.schedule_next_notification_mail
assert_equal 1, Delayed::Job.count
end
should 'do not schedule next mail if notification time is zero' do
@member.notification_time = 0
@member.notifier.schedule_next_notification_mail
assert_equal 0, Delayed::Job.count
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
PersonNotifier.schedule_all_next_notification_mail
assert_equal 1, Delayed::Job.count
end
should 'schedule after update and set a valid notification time' do
@member.notification_time = 0
@member.save!
assert_equal 0, Delayed::Job.count
@member.notification_time = 12
@member.save!
assert_equal 1, Delayed::Job.count
end
should 'reschedule with changed notification time' do
@member.notification_time = 2
@member.save!
assert_equal 1, Delayed::Job.count
@member.notification_time = 12
@member.save!
assert_equal 1, Delayed::Job.count
assert_equal @member.notification_time, DateTime.now.hour - Delayed::Job.first.run_at.hour
end
should 'display error message if fail to render a 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")
notify
sent = ActionMailer::Base.deliveries.first
assert_match /cannot render notification for some_invalid_verb/, sent.body
end
ActionTrackerConfig.verb_names.each do |verb|
should "render notification for verb #{verb}" do
action = mock()
action.stubs(:verb).returns(verb)
action.stubs(:user).returns(@member)
action.stubs(:created_at).returns(DateTime.now)
action.stubs(:target).returns(fast_create(Forum))
action.stubs(:comments_count).returns(0)
action.stubs(:comments).returns([])
action.stubs(:params).returns({'name' => 'home', 'url' => '/', 'lead' => ''})
action.stubs(:get_url).returns('')
notifications = []
notifications.stubs(:find).returns([action])
Person.any_instance.stubs(:tracked_notifications).returns(notifications)
notify
sent = ActionMailer::Base.deliveries.first
assert_no_match /cannot render notification for #{verb}/, sent.body
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)
assert !PersonNotifier::NotifyAllJob.exists?
end
should 'exists? method in NotifyAllJob return false if there is no jobs created' do
assert !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
Delayed::Job.enqueue(PersonNotifier::NotifyAllJob.new)
process_delayed_job_queue
assert_equal 2, Delayed::Job.count
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 permanent failure' do
Delayed::Job.enqueue(PersonNotifier::NotifyJob.new(@member.id))
PersonNotifier.any_instance.stubs(:notify).raises('error')
process_delayed_job_queue
jobs = Delayed::Job.all
assert 1, jobs.select{|j| j.failed?}.size
assert 1, jobs.select{|j| !j.failed?}.size
end
def notify
process_delayed_job_queue
@member.notifier.notify
end
end