forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount_notification_spec.rb
More file actions
314 lines (271 loc) · 14.6 KB
/
Copy pathaccount_notification_spec.rb
File metadata and controls
314 lines (271 loc) · 14.6 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#
# Copyright (C) 2012 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require File.expand_path(File.dirname(__FILE__) + '/../sharding_spec_helper.rb')
describe AccountNotification do
before :once do
account_notification
user_factory
end
it "should find notifications" do
expect(AccountNotification.for_user_and_account(@user, @account)).to eq [@announcement]
end
it "should find site admin announcements" do
@announcement.destroy
account_notification(:account => Account.site_admin)
expect(AccountNotification.for_user_and_account(@user, Account.default)).to eq [@announcement]
end
it "should find announcements only if user has a role in the list of roles to which the announcement is restricted" do
@announcement.destroy
role_ids = ["TeacherEnrollment", "AccountAdmin"].map{|name| Role.get_built_in_role(name).id}
account_notification(:role_ids => role_ids, :message => "Announcement 1")
@a1 = @announcement
account_notification(:account => @account, :role_ids => [nil], :message => "Announcement 2") #students not currently taking a course
@a2 = @announcement
account_notification(:account => @account, :message => "Announcement 3") # no roles, should go to all
@a3 = @announcement
@unenrolled = @user
course_with_teacher(:account => @account)
@teacher = @user
account_admin_user(:account => @account)
@admin = @user
course_with_student(:course => @course)
@student = @user
expect(AccountNotification.for_user_and_account(@teacher, @account).map(&:id).sort).to eq [@a1.id, @a3.id]
expect(AccountNotification.for_user_and_account(@admin, @account).map(&:id).sort).to eq [@a1.id, @a2.id, @a3.id]
expect(AccountNotification.for_user_and_account(@student, @account).map(&:id).sort).to eq [@a3.id]
expect(AccountNotification.for_user_and_account(@unenrolled, @account).map(&:id).sort).to eq [@a2.id, @a3.id]
account_notification(:account => Account.site_admin, :role_ids => role_ids, :message => "Announcement 1")
@a4 = @announcement
account_notification(:account => Account.site_admin, :role_ids => [nil], :message => "Announcement 2") #students not currently taking a course
@a5 = @announcement
account_notification(:account => Account.site_admin, :message => "Announcement 3") # no roles, should go to all
@a6 = @announcement
expect(AccountNotification.for_user_and_account(@teacher, Account.site_admin).map(&:id).sort).to eq [@a4.id, @a6.id]
expect(AccountNotification.for_user_and_account(@admin, Account.site_admin).map(&:id).sort).to eq [@a4.id, @a5.id, @a6.id]
expect(AccountNotification.for_user_and_account(@student, Account.site_admin).map(&:id).sort).to eq [@a6.id]
expect(AccountNotification.for_user_and_account(@unenrolled, Account.site_admin).map(&:id).sort).to eq [@a5.id, @a6.id]
end
it "should allow closing an announcement" do
@user.close_announcement(@announcement)
expect(@user.preferences[:closed_notifications]).to eq [@announcement.id]
expect(AccountNotification.for_user_and_account(@user, Account.default)).to eq []
end
it "should remove non-applicable announcements from user preferences" do
@user.close_announcement(@announcement)
expect(@user.preferences[:closed_notifications]).to eq [@announcement.id]
@announcement.destroy
expect(AccountNotification.for_user_and_account(@user, Account.default)).to eq []
expect(@user.preferences[:closed_notifications]).to eq []
end
describe "sub accounts" do
before :once do
@sub_account = Account.default.sub_accounts.create!
end
it "should find announcements where user is enrolled" do
params = {
subject: 'sub account notification',
account: @sub_account,
role_ids: [Role.get_built_in_role("StudentEnrollment").id]
}
sub_account_announcement = sub_account_notification(params)
unenrolled = @user
course_with_student(account: @sub_account, active_all: true)
students_notifications = AccountNotification.for_user_and_account(@student, Account.default)
unenrolled_notifications = AccountNotification.for_user_and_account(unenrolled, Account.default)
expect(students_notifications).to include(@announcement)
expect(students_notifications).to include(sub_account_announcement)
expect(unenrolled_notifications).not_to include(sub_account_announcement)
end
it "should find announcements where user is an account admin" do
params = {
subject: 'sub account notification',
account: @sub_account,
role_ids: [Role.get_built_in_role("AccountAdmin").id]
}
sub_account_announcement = sub_account_notification(params)
non_admin_user = @user
account_admin_user(account: @sub_account)
admin_notifications = AccountNotification.for_user_and_account(@admin, Account.default)
non_admin_notifications = AccountNotification.for_user_and_account(non_admin_user, Account.default)
expect(admin_notifications).to include(@announcement)
expect(admin_notifications).to include(sub_account_announcement)
expect(non_admin_notifications).not_to include(sub_account_announcement)
end
it "should find announcements with no specified roles if users has any sub account role" do
params = {
subject: 'sub account notification',
account: @sub_account,
}
sub_account_announcement = sub_account_notification(params)
unenrolled_user = @user
account_admin_user(account: @sub_account)
course_with_student(account: @sub_account, active_all: true)
student_notifications = AccountNotification.for_user_and_account(@student, Account.default)
admin_notifications = AccountNotification.for_user_and_account(@admin, Account.default)
unenrolled_notifications = AccountNotification.for_user_and_account(unenrolled_user, Account.default)
expect(student_notifications).to include(sub_account_announcement)
expect(admin_notifications).to include(sub_account_announcement)
expect(unenrolled_notifications).not_to include(sub_account_announcement)
end
it "should cache based on sub_account_ids" do
params = {
subject: 'sub account notification',
account: @sub_account,
}
sub_account_notification(params)
enable_cache do
root_and_sub_account = AccountNotification.for_account(Account.default, [@sub_account.id])
expect(root_and_sub_account.count).to eq 2
root_account_only = AccountNotification.for_account(Account.default)
expect(root_account_only.count).to eq 1
end
end
it "scopes sub-accounts to the root account" do
sub_announcement = sub_account_notification(subject: 'blah', account: @sub_account)
course_with_student(user: @user, account: @sub_account, active_all: true)
other_root_account = Account.create!
other_announcement = account_notification(account: other_root_account)
course_with_student(user: @user, account: other_root_account, active_all: true)
notes = AccountNotification.for_user_and_account(@user, Account.default)
expect(notes).to include sub_announcement
expect(notes).not_to include other_announcement
other_notes = AccountNotification.for_user_and_account(@user, other_root_account)
expect(other_notes).not_to include sub_announcement
expect(other_notes).to include other_announcement
end
end
describe "survey notifications" do
it "should only display for flagged accounts" do
flag = AccountNotification::ACCOUNT_SERVICE_NOTIFICATION_FLAGS.first
account_notification(:required_account_service => flag, :account => Account.site_admin)
@a1 = account_model
@a2 = account_model
@a2.enable_service(flag)
@a2.save!
expect(AccountNotification.for_account(@a1)).to eq []
expect(AccountNotification.for_account(@a2)).to eq [@announcement]
end
describe "display_for_user?" do
it "should select each mod value once throughout the cycle" do
expect(AccountNotification.display_for_user?(5, 3, Time.zone.parse('2012-04-02'))).to eq false
expect(AccountNotification.display_for_user?(6, 3, Time.zone.parse('2012-04-02'))).to eq false
expect(AccountNotification.display_for_user?(7, 3, Time.zone.parse('2012-04-02'))).to eq true
expect(AccountNotification.display_for_user?(5, 3, Time.zone.parse('2012-05-05'))).to eq true
expect(AccountNotification.display_for_user?(6, 3, Time.zone.parse('2012-05-05'))).to eq false
expect(AccountNotification.display_for_user?(7, 3, Time.zone.parse('2012-05-05'))).to eq false
expect(AccountNotification.display_for_user?(5, 3, Time.zone.parse('2012-06-04'))).to eq false
expect(AccountNotification.display_for_user?(6, 3, Time.zone.parse('2012-06-04'))).to eq true
expect(AccountNotification.display_for_user?(7, 3, Time.zone.parse('2012-06-04'))).to eq false
end
it "should shift the mod values each new cycle" do
expect(AccountNotification.display_for_user?(7, 3, Time.zone.parse('2012-04-02'))).to eq true
expect(AccountNotification.display_for_user?(7, 3, Time.zone.parse('2012-07-02'))).to eq false
expect(AccountNotification.display_for_user?(7, 3, Time.zone.parse('2012-09-02'))).to eq true
end
end
describe "dexclude students for surveys?" do
before(:each) do
flag = AccountNotification::ACCOUNT_SERVICE_NOTIFICATION_FLAGS.first
@survey = account_notification(:required_account_service => flag, :account => Account.site_admin)
@a1 = account_model
@a1.enable_service(flag)
@a1.save!
@unenrolled = @user
course_with_teacher(account: @a1)
@student_teacher = @user
course_with_student(course: @course, user: @student_teacher)
course_with_teacher(course: @course, :account => @a1)
@teacher = @user
account_admin_user(:account => @a1)
@admin = @user
course_with_student(:course => @course)
@student = @user
end
it "should exclude students on surveys if the account restricts a student" do
@a1.settings[:include_students_in_global_survey] = false
@a1.save!
expect(AccountNotification.for_user_and_account(@teacher, @a1).map(&:id).sort).to eq [@survey.id]
expect(AccountNotification.for_user_and_account(@admin, @a1).map(&:id).sort).to eq [@survey.id]
expect(AccountNotification.for_user_and_account(@student, @a1).map(&:id).sort).to eq []
expect(AccountNotification.for_user_and_account(@student_teacher, @a1).map(&:id).sort).to eq [@survey.id]
expect(AccountNotification.for_user_and_account(@unenrolled, @a1).map(&:id).sort).to eq [@survey.id]
end
it "should exclude students on surveys by default" do
expect(AccountNotification.for_user_and_account(@teacher, @a1).map(&:id).sort).to eq [@survey.id]
expect(AccountNotification.for_user_and_account(@admin, @a1).map(&:id).sort).to eq [@survey.id]
expect(AccountNotification.for_user_and_account(@student, @a1).map(&:id).sort).to eq []
expect(AccountNotification.for_user_and_account(@student_teacher, @a1).map(&:id).sort).to eq [@survey.id]
expect(AccountNotification.for_user_and_account(@unenrolled, @a1).map(&:id).sort).to eq [@survey.id]
end
it "should include students on surveys when checked" do
@a1.settings[:include_students_in_global_survey] = true
@a1.save!
expect(AccountNotification.for_user_and_account(@teacher, @a1).map(&:id).sort).to eq [@survey.id]
expect(AccountNotification.for_user_and_account(@admin, @a1).map(&:id).sort).to eq [@survey.id]
expect(AccountNotification.for_user_and_account(@student, @a1).map(&:id).sort).to eq [@survey.id]
expect(AccountNotification.for_user_and_account(@student_teacher, @a1).map(&:id).sort).to eq [@survey.id]
expect(AccountNotification.for_user_and_account(@unenrolled, @a1).map(&:id).sort).to eq [@survey.id]
end
end
end
context "sharding" do
specs_require_sharding
it "should always find notifications for site admin" do
account_notification(:account => Account.site_admin)
@shard1.activate do
@account = Account.create!
user_factory
expect(AccountNotification.for_user_and_account(@user, @account)).to eq [@announcement]
end
@shard2.activate do
expect(AccountNotification.for_user_and_account(@user, @account)).to eq [@announcement]
end
end
it "should respect preferences regardless of current shard" do
@shard1.activate do
@user.close_announcement(@announcement)
end
expect(@user.preferences[:closed_notifications]).to eq [@announcement.id]
@shard1.activate do
expect(AccountNotification.for_user_and_account(@user, Account.default)).to eq []
end
end
it "should properly adjust for built in roles across shards" do
@announcement.destroy
@shard2.activate do
@my_frd_account = Account.create!
end
Account.site_admin.shard.activate do
@site_admin_announcement = account_notification(account: Account.site_admin, role_ids: [Role.get_built_in_role("TeacherEnrollment").id])
end
@my_frd_account.shard.activate do
@local_announcement = account_notification(account: @my_frd_account, role_ids: [Role.get_built_in_role("TeacherEnrollment").id])
course_with_teacher(account: @my_frd_account)
end
# announcements should show to teachers, regardless of combination of
# current shard, association shard, and notification shard
[Account.site_admin.shard, @my_frd_account.shard, @shard1].each do |shard|
shard.activate do
expect(AccountNotification.for_user_and_account(@teacher, Account.site_admin)).to include(@site_admin_announcement)
expect(AccountNotification.for_user_and_account(@teacher, @my_frd_account)).to include(@site_admin_announcement)
expect(AccountNotification.for_user_and_account(@teacher, @my_frd_account)).to include(@local_announcement)
end
end
end
end
end