forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessageable_user_spec.rb
More file actions
251 lines (215 loc) · 10.4 KB
/
Copy pathmessageable_user_spec.rb
File metadata and controls
251 lines (215 loc) · 10.4 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
#
# Copyright (C) 2011 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')
describe "MessageableUser" do
describe ".build_select" do
it "should ignore common_course_column without common_role_column" do
expect(MessageableUser.build_select(:common_course_column => 'ignored_column')).
to match(/NULL AS common_courses/)
end
it "should require common_course_column with common_role_column" do
expect{ MessageableUser.build_select(:common_role_column => 'role_column') }.
to raise_error(ArgumentError)
end
it "should combine common_course_column and common_role_column in common_courses" do
course_with_student(:active_all => true)
messageable_user = MessageableUser.
select(MessageableUser.build_select(
:common_course_column => "'course_column'",
:common_role_column => "'role_column'")).
where(:id => @student).
group(MessageableUser.connection.group_by(*MessageableUser::COLUMNS)).
first
expect(messageable_user.send(:read_attribute, :common_courses)).
to eq "course_column:role_column"
end
it "should combine multiple (course,role) pairs in common_courses" do
course_with_ta(:active_all => true)
multiple_student_enrollment(@ta, @course.course_sections.create!)
messageable_user = MessageableUser.
select(MessageableUser.build_select(
:common_course_column => "'course'",
:common_role_column => 'enrollments.type')).
joins("INNER JOIN #{Enrollment.quoted_table_name} ON enrollments.user_id=users.id").
where(:id => @ta.id).
group(MessageableUser.connection.group_by(*MessageableUser::COLUMNS)).
first
expect(messageable_user.send(:read_attribute, :common_courses).split(/,/).sort).
to eq ["course:StudentEnrollment", "course:TaEnrollment"]
end
it "should combine multiple common_group_column values in common_groups" do
group1 = group_with_user(:active_all => true).group
group2 = group_with_user(:user => @user, :active_all => true).group
messageable_user = MessageableUser.
select(MessageableUser.build_select(:common_group_column => "group_memberships.group_id")).
joins("INNER JOIN #{GroupMembership.quoted_table_name} ON group_memberships.user_id=users.id").
where(:id => @user).
group(MessageableUser.connection.group_by(*MessageableUser::COLUMNS)).
first
expect(messageable_user.send(:read_attribute, :common_groups).split(/,/).map(&:to_i).sort).
to eq [group1.id, group2.id].sort
end
end
describe ".prepped" do
def group_scope(scope)
scope.group_values.join(", ")
end
it "should group by id" do
expect(group_scope(MessageableUser.prepped())).
to match(MessageableUser::COLUMNS.first)
end
it "should include column-based common_course_column in group by" do
expect(group_scope(MessageableUser.prepped(:common_course_column => 'course_column'))).
to match('course_column')
end
it "should include column-based common_group_column in group by" do
expect(group_scope(MessageableUser.prepped(:common_group_column => 'group_column'))).
to match('group_column')
end
it "should not include literal common_course_column value in group by" do
expect(group_scope(MessageableUser.prepped(:common_course_column => 5))).
not_to match('5')
end
it "should not include literal common_group_column value in group by" do
expect(group_scope(MessageableUser.prepped(:common_group_column => 5))).
not_to match('5')
end
it "should order by sortable_name before id" do
user1 = user_factory(active_all: true, :name => 'Yellow Bob')
user2 = user_factory(active_all: true, :name => 'Zebra Alice')
expect(MessageableUser.prepped().where(id: [user1, user2]).first.id).to eq user2.id
end
it "should ignore case when ordering by sortable_name" do
user1 = user_factory(active_all: true, :name => 'bob')
user2 = user_factory(active_all: true, :name => 'ALICE')
expect(MessageableUser.prepped().where(id: [user1, user2]).first.id).to eq user2.id
end
it "should order by id as tiebreaker" do
user1 = user_factory(active_all: true, :name => 'Alice')
user2 = user_factory(active_all: true, :name => 'Alice')
expect(MessageableUser.prepped().where(id: [user1, user2]).first.id).to eq user1.id
end
it "should exclude creation_pending students with strict_checks true" do
user_factory(:user_state => 'creation_pending')
expect(MessageableUser.prepped(:strict_checks => true).where(id: @user).length).to eq 0
end
it "should include creation_pending students with strict_checks false" do
user_factory(:user_state => 'creation_pending')
expect(MessageableUser.prepped(:strict_checks => false).where(id: @user).length).to eq 1
end
it "should exclude deleted students with include_deleted true but strict_checks true" do
user_factory(:user_state => 'deleted')
expect(MessageableUser.prepped(:strict_checks => true, :include_deleted => true).where(id: @user).length).to eq 0
end
it "should exclude deleted students with with strict_checks false but include_deleted false" do
user_factory(:user_state => 'deleted')
expect(MessageableUser.prepped(:strict_checks => false, :include_deleted => false).where(id: @user).length).to eq 0
end
it "should include deleted students with strict_checks false and include_deleted true" do
user_factory(:user_state => 'deleted')
expect(MessageableUser.prepped(:strict_checks => false, :include_deleted => true).where(id: @user).length).to eq 1
end
it "should default strict_checks to true" do
user_factory(:user_state => 'creation_pending')
expect(MessageableUser.prepped().where(id: @user).length).to eq 0
end
it "should default include_delete to false" do
user_factory(:user_state => 'deleted')
expect(MessageableUser.prepped(:strict_checks => false).where(id: @user).length).to eq 0
end
end
describe "#common_courses" do
before do
user_factory(active_all: true)
end
it "should be empty with no common_courses selected" do
expect(MessageableUser.prepped().where(id: @user).first.common_courses).
to eq({})
end
it "should populate from non-null common_courses" do
user = MessageableUser.prepped(:common_course_column => 1, :common_role_column => "'StudentEnrollment'").where(id: @user).first
expect(user.common_courses).to eq({1 => ['StudentEnrollment']})
end
describe "sharding" do
specs_require_sharding
it "should translate keys to the current shard" do
user = MessageableUser.prepped(:common_course_column => Shard.relative_id_for(1, @shard2, Shard.current), :common_role_column => "'StudentEnrollment'").where(id: @user).first
[Shard.default, @shard1, @shard2].each do |shard|
shard.activate do
expect(user.common_courses).to eq({Shard.relative_id_for(1, @shard2, Shard.current) => ['StudentEnrollment']})
end
end
end
it "should not translate a 0 key" do
user = MessageableUser.prepped(:common_course_column => 0, :common_role_column => "'StudentEnrollment'").where(id: @user).first
[Shard.default, @shard1, @shard2].each do |shard|
shard.activate do
expect(user.common_courses).to eq({0 => ['StudentEnrollment']})
end
end
end
end
end
describe "#common_groups" do
before do
user_factory(active_all: true)
end
it "should be empty with no common_groups selected" do
expect(MessageableUser.prepped().where(id: @user).first.common_groups).
to eq({})
end
it "should populate from non-null common_groups with 'Member' roles" do
user = MessageableUser.prepped(:common_group_column => 1).where(id: @user).first
expect(user.common_groups).to eq({1 => ['Member']})
end
describe "sharding" do
specs_require_sharding
it "should translate keys to the current shard" do
user = MessageableUser.prepped(:common_group_column => Shard.relative_id_for(1, @shard2, Shard.current)).where(id: @user).first
[Shard.default, @shard1, @shard2].each do |shard|
shard.activate do
expect(user.common_groups).to eq({Shard.relative_id_for(1, @shard2, Shard.current) => ['Member']})
end
end
end
end
end
describe "#include_common_contexts_from" do
before do
user_factory(active_all: true)
end
it "should merge disparate ids" do
# e.g. two copies of the user from different shards with course
# visibility on each
user1 = MessageableUser.prepped(:common_course_column => 1, :common_role_column => "'StudentEnrollment'").where(id: @user).first
user2 = MessageableUser.prepped(:common_course_column => 2, :common_role_column => "'StudentEnrollment'").where(id: @user).first
user1.include_common_contexts_from(user2)
expect(user1.common_courses[1]).to include('StudentEnrollment')
expect(user1.common_courses[2]).to include('StudentEnrollment')
end
it "should stack coinciding ids" do
# e.g. two copies of the user from different shards with admin visibility
# on each
user1 = MessageableUser.prepped(:common_course_column => 0, :common_role_column => "'StudentEnrollment'").where(id: @user).first
user2 = MessageableUser.prepped(:common_course_column => 0, :common_role_column => "'TeacherEnrollment'").where(id: @user).first
user1.include_common_contexts_from(user2)
expect(user1.common_courses[0]).to include('StudentEnrollment')
expect(user1.common_courses[0]).to include('TeacherEnrollment')
end
end
end