Skip to content

Commit 4ca649c

Browse files
committed
don't send account alerts to rejected enrollments
we'll still send them to invited or pending enrollments which is a little weird, but that is how our default scopes are set up, so I erred on the side of just re-using them. fixes CNVS-20219 test plan: - in a course with a user who has a rejected teacher enrollment - set up an account alert and trigger the condition - it should not send to that rejected enrollment Change-Id: I62ea17aa4bc84b7a53f3d7a65d5522bd925eddaa Reviewed-on: https://gerrit.instructure.com/53543 Tested-by: Jenkins Reviewed-by: Mike Nomitch <mnomitch@instructure.com> QA-Review: Deepeeca Soundarrajan <dsoundarrajan@instructure.com> Product-Review: Simon Williams <simon@instructure.com>
1 parent b7706b3 commit 4ca649c

2 files changed

Lines changed: 58 additions & 28 deletions

File tree

app/models/alerts/delayed_alert_sender.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ def self.evaluate_for_course(course, account_alerts)
2323
alerts.concat course.alerts.all
2424
return if alerts.empty?
2525

26-
student_enrollments = course.student_enrollments.active
26+
student_enrollments = course.student_enrollments
2727
student_ids = student_enrollments.map(&:user_id)
2828
return if student_ids.empty?
2929

30-
teacher_enrollments = course.instructor_enrollments.active
30+
teacher_enrollments = course.instructor_enrollments.active_or_pending
3131
teacher_ids = teacher_enrollments.map(&:user_id)
3232
return if teacher_ids.empty?
3333

spec/models/alerts/delayed_alert_sender_spec.rb

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,54 @@ module Alerts
5555
end
5656

5757
it "should not trigger any alerts when there are no teachers in the class" do
58-
course_with_student(:active_course => 1)
58+
course_with_student(:active_course => true)
5959
@course.alerts.create!(:recipients => [:student], :criteria => [{:criterion_type => 'Interaction', :threshold => 7}])
6060
Notification.any_instance.expects(:create_message).never
6161

6262
DelayedAlertSender.evaluate_for_course(@course, nil)
6363
end
6464

6565
it "should not trigger any alerts in subsequent courses" do
66-
course_with_teacher(:active_all => 1)
67-
student_in_course(:active_all => 1)
66+
course_with_teacher(:active_all => true)
67+
student_in_course(:active_all => true)
6868
@course.alerts.create!(:recipients => [:student], :criteria => [{:criterion_type => 'Interaction', :threshold => 7}])
69-
@course.start_at = Time.now - 30.days
69+
@course.start_at = Time.zone.now - 30.days
7070
account_alerts = []
7171

7272
DelayedAlertSender.evaluate_for_course(@course, account_alerts)
7373

7474
expect(account_alerts).to eq []
7575
end
76+
77+
it "should not trigger to rejected teacher enrollments" do
78+
course_with_teacher(:active_course => true)
79+
student_in_course(:active_all => true)
80+
@teacher.enrollments.first.reject!
81+
@course.alerts.create!(
82+
:recipients => [:teachers],
83+
:criteria => [{:criterion_type => 'Interaction', :threshold => 7}]
84+
)
85+
@course.reload
86+
@course.start_at = Time.zone.now - 30.days
87+
88+
Notification.any_instance.expects(:create_message).never
89+
DelayedAlertSender.evaluate_for_course(@course, [])
90+
end
91+
92+
it "should not trigger to rejected student enrollments" do
93+
course_with_teacher(:active_course => true)
94+
student_in_course(:active_all => true)
95+
@student.enrollments.first.reject!
96+
@course.alerts.create!(
97+
:recipients => [:teachers],
98+
:criteria => [{:criterion_type => 'Interaction', :threshold => 7}]
99+
)
100+
@course.reload
101+
@course.start_at = Time.zone.now - 30.days
102+
103+
Notification.any_instance.expects(:create_message).never
104+
DelayedAlertSender.evaluate_for_course(@course, [])
105+
end
76106
end
77107

78108
context 'repetition' do
@@ -81,7 +111,7 @@ module Alerts
81111
course_with_teacher(:active_all => 1)
82112
student_in_course(:active_all => 1)
83113
@course.alerts.create!(:recipients => [:student], :criteria => [{:criterion_type => 'Interaction', :threshold => 7}])
84-
@course.start_at = Time.now - 30.days
114+
@course.start_at = Time.zone.now - 30.days
85115
@mock_notification.expects(:create_message).with(anything, [@user.id], anything).once
86116

87117
DelayedAlertSender.evaluate_for_course(@course, nil)
@@ -94,7 +124,7 @@ module Alerts
94124
course_with_teacher(:active_all => 1)
95125
student_in_course(:active_all => 1)
96126
@course.alerts.create!(:recipients => [:student], :repetition => 1, :criteria => [{:criterion_type => 'Interaction', :threshold => 7}])
97-
@course.start_at = Time.now - 30.days
127+
@course.start_at = Time.zone.now - 30.days
98128
@mock_notification.expects(:create_message).with(anything, [@user.id], anything).once
99129

100130
DelayedAlertSender.evaluate_for_course(@course, nil)
@@ -107,13 +137,13 @@ module Alerts
107137
course_with_teacher(:active_all => 1)
108138
student_in_course(:active_all => 1)
109139
alert = @course.alerts.create!(:recipients => [:student], :repetition => 1, :criteria => [{:criterion_type => 'Interaction', :threshold => 7}])
110-
@course.start_at = Time.now - 30.days
140+
@course.start_at = Time.zone.now - 30.days
111141

112142
@mock_notification.expects(:create_message).with(anything, [@user.id], anything).twice
113143

114144
DelayedAlertSender.evaluate_for_course(@course, nil)
115145
# update sent_at
116-
Rails.cache.write([alert, @user.id].cache_key, (Time.now - 1.day).beginning_of_day)
146+
Rails.cache.write([alert, @user.id].cache_key, (Time.zone.now - 1.day).beginning_of_day)
117147
DelayedAlertSender.evaluate_for_course(@course, nil)
118148
end
119149
end
@@ -126,7 +156,7 @@ module Alerts
126156
alert = @course.alerts.build(:recipients => [:student])
127157
alert.criteria.build(:criterion_type => 'Interaction', :threshold => 7)
128158
alert.save!
129-
@course.start_at = Time.now - 30.days
159+
@course.start_at = Time.zone.now - 30.days
130160
@mock_notification.expects(:create_message).with(anything, [@user.id], anything)
131161

132162
DelayedAlertSender.evaluate_for_course(@course, nil)
@@ -143,13 +173,13 @@ module Alerts
143173
@assignment.save
144174
@submission = @assignment.submit_homework(@user)
145175
SubmissionComment.create!(:submission => @submission, :comment => 'some comment', :author => @teacher, :recipient => @user) do |sc|
146-
sc.created_at = Time.now - 30.days
176+
sc.created_at = Time.zone.now - 30.days
147177
end
148178

149179
alert = @course.alerts.build(:recipients => [:student])
150180
alert.criteria.build(:criterion_type => 'Interaction', :threshold => 7)
151181
alert.save!
152-
@course.start_at = Time.now - 30.days
182+
@course.start_at = Time.zone.now - 30.days
153183

154184
mock_interaction = stub(should_not_receive_message?: true)
155185
Alerts::Interaction.expects(:new).once.returns(mock_interaction)
@@ -188,7 +218,7 @@ module Alerts
188218
@assignment.workflow_state = "published"
189219
@assignment.save
190220
@submission = @assignment.submit_homework(@user, :body => 'body')
191-
@submission.update_attribute(:submitted_at, Time.now - 30.days);
221+
@submission.update_attribute(:submitted_at, Time.zone.now - 30.days)
192222

193223
alert = @course.alerts.build(:recipients => [:student])
194224
alert.criteria.build(:criterion_type => 'UngradedTimespan', :threshold => 7)
@@ -210,7 +240,7 @@ module Alerts
210240
alert = @course.alerts.build(:recipients => [:student])
211241
alert.criteria.build(:criterion_type => 'UserNote', :threshold => 7)
212242
alert.save!
213-
@course.start_at = Time.now - 30.days
243+
@course.start_at = Time.zone.now - 30.days
214244
@mock_notification.expects(:create_message).with(anything, [@user.id], anything)
215245

216246
DelayedAlertSender.evaluate_for_course(@course, nil)
@@ -239,12 +269,12 @@ module Alerts
239269
end
240270

241271
it "should tell you what the alert is about timespan" do
242-
@submission.update_attribute(:submitted_at, Time.now - 30.days);
272+
@submission.update_attribute(:submitted_at, Time.zone.now - 30.days)
243273
alert = @course.alerts.build(:recipients => [:student])
244274
alert.criteria.build(:criterion_type => 'UngradedTimespan', :threshold => 7)
245275
alert.save!
246-
@mock_notification.expects(:create_message).with do |alert, _, _|
247-
expect(alert.criteria.first.criterion_type).to eq 'UngradedTimespan'
276+
@mock_notification.expects(:create_message).with do |alert_in, _, _|
277+
expect(alert_in.criteria.first.criterion_type).to eq 'UngradedTimespan'
248278
end
249279

250280
DelayedAlertSender.evaluate_for_course(@course, nil)
@@ -254,8 +284,8 @@ module Alerts
254284
alert = @course.alerts.build(:recipients => [:student])
255285
alert.criteria.build(:criterion_type => 'UngradedCount', :threshold => 1)
256286
alert.save!
257-
@mock_notification.expects(:create_message).with do |alert, _, _|
258-
expect(alert.criteria.first.criterion_type).to eq 'UngradedCount'
287+
@mock_notification.expects(:create_message).with do |alert_in, _, _|
288+
expect(alert_in.criteria.first.criterion_type).to eq 'UngradedCount'
259289
end
260290

261291
DelayedAlertSender.evaluate_for_course(@course, nil)
@@ -266,13 +296,13 @@ module Alerts
266296
root_account.enable_user_notes = true
267297
root_account.save!
268298

269-
::UserNote.create!(:creator => @teacher, :user => @user) { |un| un.created_at = Time.now - 30.days }
299+
::UserNote.create!(:creator => @teacher, :user => @user) { |un| un.created_at = Time.zone.now - 30.days }
270300
alert = @course.alerts.build(:recipients => [:student])
271301
alert.criteria.build(:criterion_type => 'UserNote', :threshold => 7)
272302
alert.save!
273-
@course.start_at = Time.now - 30.days
274-
@mock_notification.expects(:create_message).with do |alert, _, _|
275-
expect(alert.criteria.first.criterion_type).to eq 'UserNote'
303+
@course.start_at = Time.zone.now - 30.days
304+
@mock_notification.expects(:create_message).with do |alert_in, _, _|
305+
expect(alert_in.criteria.first.criterion_type).to eq 'UserNote'
276306
end
277307

278308
DelayedAlertSender.evaluate_for_course(@course, nil)
@@ -282,9 +312,9 @@ module Alerts
282312
alert = @course.alerts.build(:recipients => [:student])
283313
alert.criteria.build(:criterion_type => 'Interaction', :threshold => 7)
284314
alert.save!
285-
@course.start_at = Time.now - 30.days
286-
@mock_notification.expects(:create_message).with do |alert, _, _|
287-
expect(alert.criteria.first.criterion_type).to eq 'Interaction'
315+
@course.start_at = Time.zone.now - 30.days
316+
@mock_notification.expects(:create_message).with do |alert_in, _, _|
317+
expect(alert_in.criteria.first.criterion_type).to eq 'Interaction'
288318
end
289319

290320
DelayedAlertSender.evaluate_for_course(@course, nil)
@@ -302,7 +332,7 @@ module Alerts
302332
alert = @course.alerts.build(:recipients => [:student])
303333
alert.criteria.build(:criterion_type => 'Interaction', :threshold => 7)
304334
alert.save!
305-
@course.start_at = Time.now - 30.days
335+
@course.start_at = Time.zone.now - 30.days
306336

307337
expect {
308338
DelayedAlertSender.evaluate_for_course(@course, nil)

0 commit comments

Comments
 (0)