forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrubrics_controller_spec.rb
More file actions
537 lines (496 loc) · 22.4 KB
/
Copy pathrubrics_controller_spec.rb
File metadata and controls
537 lines (496 loc) · 22.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#
# Copyright (C) 2011 - 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__) + '/../spec_helper')
describe RubricsController do
describe "GET 'index'" do
it "should require authorization" do
course_with_teacher(:active_all => true)
get 'index', :course_id => @course.id
assert_unauthorized
end
describe 'variables' do
before { course_with_teacher_logged_in(:active_all => true) }
it "should be assigned with a course" do
get 'index', :course_id => @course.id
expect(response).to be_success
end
it "should be assigned with a user" do
get 'index', :user_id => @user.id
expect(response).to be_success
end
end
end
describe "POST 'create' for course" do
it "should require authorization" do
course_with_teacher(:active_all => true)
post 'create', :course_id => @course.id
assert_unauthorized
end
it "should assign variables" do
course_with_teacher_logged_in(:active_all => true)
request.content_type = 'application/json' unless CANVAS_RAILS4_2
post 'create', :course_id => @course.id, :rubric => {}
expect(assigns[:rubric]).not_to be_nil
expect(assigns[:rubric]).not_to be_new_record
expect(response).to be_success
end
it "should create an association if specified" do
course_with_teacher_logged_in(:active_all => true)
association = @course.assignments.create!(assignment_valid_attributes)
request.content_type = 'application/json' unless CANVAS_RAILS4_2
post 'create', :course_id => @course.id, :rubric => {}, :rubric_association => {:association_type => association.class.to_s, :association_id => association.id}
expect(assigns[:rubric]).not_to be_nil
expect(assigns[:rubric]).not_to be_new_record
expect(assigns[:rubric].rubric_associations.length).to eql(1)
expect(response).to be_success
end
it "should associate outcomes correctly" do
course_with_teacher_logged_in(:active_all => true)
assignment = @course.assignments.create!(assignment_valid_attributes)
outcome_group = @course.root_outcome_group
outcome = @course.created_learning_outcomes.create!(
:description => 'hi',
:short_description => 'hi'
)
outcome_group.add_outcome(outcome)
outcome_group.save!
create_params = {
"course_id" => @course.id,
"points_possible" => "5",
"rubric" => {
"criteria" => {
"0" => {
"description" => "hi",
"id" => "",
"learning_outcome_id" => outcome.id,
"long_description" => "",
"mastery_points" => "3",
"points" => "5",
"ratings" => {
"0" => {
"description" => "Exceeds Expectations",
"id" => "blank",
"points" => "5"
},
"1" => {
"description" => "Meets Expectations",
"id" => "blank",
"points" => "3"
},
"2" => {
"description" => "Does Not Meet Expectations",
"id" => "blank_2",
"points" => "0"
}
}
}
},
"free_form_criterion_comments" => "0",
"points_possible" => "5",
"title" => "Some Rubric"
},
"rubric_association" => {
"association_id" => assignment.id,
"association_type" => "Assignment",
"hide_score_total" => "0",
"id" => "",
"purpose" => "grading",
"use_for_grading" => "1"
},
"rubric_association_id" => "",
"rubric_id" => "new",
"skip_updating_points_possible" => "false",
"title" => "Some Rubric"
}
post 'create', create_params
expect(assignment.reload.learning_outcome_alignments.count).to eq 1
expect(Rubric.last.learning_outcome_alignments.count).to eq 1
end
end
describe "PUT 'update'" do
it "should require authorization" do
course_with_teacher(:active_all => true)
rubric_association_model(:user => @user, :context => @course)
put 'update', :course_id => @course.id, :id => @rubric.id
assert_unauthorized
end
it "should assign variables" do
course_with_teacher_logged_in(:active_all => true)
rubric_association_model(:user => @user, :context => @course)
expect(@course.rubrics).to be_include(@rubric)
put 'update', :course_id => @course.id, :id => @rubric.id, :rubric => {}
expect(assigns[:rubric]).to eql(@rubric)
expect(response).to be_success
end
it "should update the rubric if updateable" do
course_with_teacher_logged_in(:active_all => true)
rubric_association_model(:user => @user, :context => @course)
put 'update', :course_id => @course.id, :id => @rubric.id, :rubric => {:title => "new title"}
expect(assigns[:rubric]).to eql(@rubric)
expect(assigns[:rubric].title).to eql("new title")
expect(assigns[:association]).to be_nil
expect(response).to be_success
end
it "should update the rubric even if it doesn't belong to the context, just an association" do
course_model
@course2 = @course
course_with_teacher_logged_in(:active_all => true)
@e = @course2.enroll_teacher(@user)
@e.accept
rubric_association_model(:user => @user, :context => @course)
@rubric.context = @course2
@rubric.save
put 'update', :course_id => @course.id, :id => @rubric.id, :rubric => {:title => "new title"}, :rubric_association_id => @rubric_association.id
expect(assigns[:rubric]).to eql(@rubric)
expect(assigns[:rubric].title).to eql("new title")
expect(assigns[:association]).not_to be_nil
expect(assigns[:association]).to eql(@rubric_association)
expect(response).to be_success
end
# this happens after a importing content into a new course, before a new
# association is set up
it "should create a new rubrice (and not update the existing rubric) if it doesn't belong to the context or to an association" do
course_model
@course2 = @course
course_with_teacher_logged_in(:active_all => true)
rubric_association_model(:user => @user, :context => @course)
@rubric.context = @course2
@rubric.save
@rubric_association.context = @course2
@rubric_association.save
put 'update', :course_id => @course.id, :id => @rubric.id, :rubric => {:title => "new title"}, :rubric_association_id => @rubric_association.id
expect(assigns[:rubric]).not_to be_nil
expect(assigns[:rubric]).not_to eql(@rubric)
expect(assigns[:rubric].title).to eql("new title")
end
it "should not update the rubric if not updateable (should make a new one instead)" do
course_with_teacher_logged_in(:active_all => true)
rubric_association_model(:user => @user, :context => @course, :purpose => 'grading')
@rubric.rubric_associations.create!(:purpose => 'grading', :context => @course, :association_object => @course)
put 'update', :course_id => @course.id, :id => @rubric.id, :rubric => {:title => "new title"}, :rubric_association_id => @rubric_association.id
expect(assigns[:rubric]).not_to eql(@rubric)
expect(assigns[:rubric]).not_to be_new_record
expect(assigns[:association]).not_to be_nil
expect(assigns[:association]).to eql(@rubric_association)
expect(assigns[:association].rubric).to eql(assigns[:rubric])
expect(assigns[:rubric].title).to eql("new title")
expect(response).to be_success
end
it "should not update the rubric and not create a new one if the parameters don't change the rubric" do
course_with_teacher_logged_in(:active_all => true)
rubric_association_model(:user => @user, :context => @course, :purpose => 'grading')
params = {
:title => 'new title',
:criteria => {
'0' => {
:description => 'desc',
:long_description => 'long_desc',
:points => '5',
:id => 'id_5',
:ratings => {
'0' => {
:description => 'a',
:points => '5',
:id => 'id_6'
},
'1' => {
:description => 'b',
:points => '0',
:id => 'id_7'
}
}
}
}
}
@rubric.update_criteria(params)
@rubric.save!
@rubric.rubric_associations.create!(:purpose => 'grading', :context => @course, :association_object => @course)
criteria = @rubric.criteria
put 'update', :course_id => @course.id, :id => @rubric.id, :rubric => params, :rubric_association_id => @rubric_association.id
expect(assigns[:rubric]).to eql(@rubric)
expect(assigns[:rubric].criteria).to eql(criteria)
expect(assigns[:rubric]).not_to be_new_record
expect(assigns[:association]).not_to be_nil
expect(assigns[:association]).to eql(@rubric_association)
expect(assigns[:association].rubric).to eql(assigns[:rubric])
expect(assigns[:rubric].title).to eql("new title")
expect(response).to be_success
end
it "should update the newly-created rubric if updateable, even if the old id is specified" do
course_with_teacher_logged_in(:active_all => true)
rubric_association_model(:user => @user, :context => @course)
put 'update', :course_id => @course.id, :id => @rubric.id, :rubric => {:title => "new title"}, :rubric_association_id => @rubric_association.id
expect(assigns[:rubric]).to eql(@rubric)
expect(assigns[:rubric].title).to eql("new title")
@rubric2 = assigns[:rubric]
expect(assigns[:association]).not_to be_nil
expect(assigns[:association]).to eql(@rubric_association)
expect(response).to be_success
put 'update', :course_id => @course.id, :id => @rubric.id, :rubric => {:title => "newer title"}, :rubric_association_id => @rubric_association.id
expect(assigns[:rubric]).to eql(@rubric2)
expect(assigns[:rubric].title).to eql("newer title")
expect(response).to be_success
end
it "should update the association if specified" do
course_with_teacher_logged_in(:active_all => true)
rubric_association_model(:user => @user, :context => @course)
put 'update', :course_id => @course.id, :id => @rubric.id, :rubric => {:title => "new title"}, :rubric_association => {:association_type => @rubric_association.association_object.class.to_s, :association_id => @rubric_association.association_object.id, :title => "some title", :id => @rubric_association.id}
expect(assigns[:rubric]).to eql(@rubric)
expect(assigns[:rubric].title).to eql("new title")
expect(assigns[:association]).to eql(@rubric_association)
expect(assigns[:rubric].rubric_associations.where(id: @rubric_association).first.title).to eql("some title")
expect(response).to be_success
end
it "should add an outcome association if one is linked" do
course_with_teacher_logged_in(:active_all => true)
assignment = @course.assignments.create!(assignment_valid_attributes)
rubric_association_model(:user => @user, :context => @course)
outcome_group = @course.root_outcome_group
outcome = @course.created_learning_outcomes.create!(
:description => 'hi',
:short_description => 'hi'
)
outcome_group.add_outcome(outcome)
outcome_group.save!
update_params = {
"course_id" => @course.id,
"id" => @rubric.id,
"points_possible" => "5",
"rubric" => {
"criteria" => {
"0" => {
"description" => "hi",
"id" => "",
"learning_outcome_id" => outcome.id,
"long_description" => "",
"points" => "5",
"ratings" => {
"0" => {
"description" => "Exceeds Expectations",
"id" => "blank",
"points" => "5"
},
"1" => {
"description" => "Meets Expectations",
"id" => "blank",
"points" => "3"
},
"2" => {
"description" => "Does Not Meet Expectations",
"id" => "blank_2",
"points" => "0"
}
}
}
},
"free_form_criterion_comments" => "0",
"points_possible" => "5",
"title" => "Some Rubric"
},
"rubric_association" => {
"association_id" => assignment.id,
"association_type" => "Assignment",
"hide_score_total" => "0",
"id" => @rubric_association.id,
"purpose" => "grading",
"use_for_grading" => "1"
},
"rubric_association_id" => @rubric_association.id,
"rubric_id" => @rubric.id,
"skip_updating_points_possible" => "false",
"title" => "Some Rubric"
}
expect(assignment.reload.learning_outcome_alignments.count).to eq 0
expect(@rubric.reload.learning_outcome_alignments.count).to eq 0
put 'update', update_params
expect(assignment.reload.learning_outcome_alignments.count).to eq 1
expect(@rubric.reload.learning_outcome_alignments.count).to eq 1
end
it "should remove an outcome association if one is removed" do
course_with_teacher_logged_in(:active_all => true)
outcome_with_rubric
assignment = @course.assignments.create!(assignment_valid_attributes)
association = @rubric.associate_with(assignment, @course, :purpose => 'grading')
update_params = {
"course_id" => @course.id,
"id" => @rubric.id,
"points_possible" => "5",
"rubric" => {
"criteria" => {
"0" => {
"description" => "Description of criterion",
"id" => "",
"learning_outcome_id" => "",
"long_description" => "",
"points" => "5",
"ratings" => {
"0" => {
"description" => "Full Marks",
"id" => "blank",
"points" => "5"
},
"1" => {
"description" => "No Marks",
"id" => "blank_2",
"points" => "0"
}
}
}
},
"free_form_criterion_comments" => "0",
"points_possible" => "5",
"title" => "Some Rubric"
},
"rubric_association" => {
"association_id" => assignment.id,
"association_type" => "Assignment",
"hide_score_total" => "0",
"id" => association.id,
"purpose" => "grading",
"use_for_grading" => "1"
},
"rubric_association_id" => association.id,
"rubric_id" => @rubric.id,
"skip_updating_points_possible" => "false",
"title" => "Some Rubric"
}
expect(assignment.reload.learning_outcome_alignments.count).to eq 1
expect(@rubric.reload.learning_outcome_alignments.count).to eq 1
put 'update', update_params
expect(assignment.reload.learning_outcome_alignments.count).to eq 0
expect(@rubric.reload.learning_outcome_alignments.count).to eq 0
end
it "should remove an outcome association for all associations" do
course_with_teacher_logged_in(:active_all => true)
outcome_with_rubric
assignment = @course.assignments.create!(assignment_valid_attributes)
association = @rubric.associate_with(assignment, @course, :purpose => 'grading')
update_params = {
"course_id" => @course.id,
"id" => @rubric.id,
"points_possible" => "5",
"rubric" => {
"criteria" => {
"0" => {
"description" => "Description of criterion",
"id" => "",
"learning_outcome_id" => "",
"long_description" => "",
"points" => "5",
"ratings" => {
"0" => {
"description" => "Full Marks",
"id" => "blank",
"points" => "5"
},
"1" => {
"description" => "No Marks",
"id" => "blank_2",
"points" => "0"
}
}
}
},
"free_form_criterion_comments" => "0",
"points_possible" => "5",
"title" => "Some Rubric"
},
"rubric_association" => {
"association_id" => @course.id,
"association_type" => "Course",
"hide_score_total" => "0",
"id" => "",
"purpose" => "bookmark",
"use_for_grading" => "0"
},
"rubric_association_id" => "",
"rubric_id" => @rubric.id,
"skip_updating_points_possible" => "false",
"title" => "Some Rubric"
}
expect(assignment.reload.learning_outcome_alignments.count).to eq 1
expect(@rubric.reload.learning_outcome_alignments.count).to eq 1
put 'update', update_params
expect(assignment.reload.learning_outcome_alignments.count).to eq 0
expect(@rubric.reload.learning_outcome_alignments.count).to eq 0
end
end
describe "DELETE 'destroy'" do
it "should require authorization" do
course_with_teacher(:active_all => true)
rubric_association_model(:user => @user, :context => @course)
delete 'destroy', :course_id => @course.id, :id => @rubric.id
assert_unauthorized
end
it "should delete the rubric" do
course_with_teacher_logged_in(:active_all => true)
rubric_association_model(:user => @user, :context => @course)
delete 'destroy', :course_id => @course.id, :id => @rubric.id
expect(response).to be_success
expect(assigns[:rubric]).to be_deleted
end
it "should delete the rubric if the rubric is only associated with a course" do
course_with_teacher_logged_in :active_all => true
Account.site_admin.account_users.create!(user: @user)
Account.default.account_users.create!(user: @user)
@rubric = Rubric.create!(:user => @user, :context => @course)
RubricAssociation.create!(:rubric => @rubric, :context => @course, :purpose => :bookmark, :association_object => @course)
expect(@course.rubric_associations.bookmarked.include_rubric.to_a.select(&:rubric_id).uniq(&:rubric_id).sort_by{|a| a.rubric.title }.map(&:rubric)).to eq [@rubric]
delete 'destroy', :course_id => @course.id, :id => @rubric.id
expect(response).to be_success
expect(@course.rubric_associations.bookmarked.include_rubric.to_a.select(&:rubric_id).uniq(&:rubric_id).sort_by{|a| a.rubric.title }.map(&:rubric)).to eq []
@rubric.reload
expect(@rubric.deleted?).to be_truthy
end
it "should delete the rubric association even if the rubric doesn't belong to a course" do
course_with_teacher_logged_in :active_all => true
Account.site_admin.account_users.create!(user: @user)
Account.default.account_users.create!(user: @user)
@user.reload
@rubric = Rubric.create!(:user => @user, :context => Account.default)
RubricAssociation.create!(:rubric => @rubric, :context => @course, :purpose => :bookmark, :association_object => @course)
RubricAssociation.create!(:rubric => @rubric, :context => Account.default, :purpose => :bookmark, :association_object => @course)
expect(@course.rubric_associations.bookmarked.include_rubric.to_a.select(&:rubric_id).uniq(&:rubric_id).sort_by{|a| a.rubric.title }.map(&:rubric)).to eq [@rubric]
delete 'destroy', :course_id => @course.id, :id => @rubric.id
expect(response).to be_success
expect(@course.rubric_associations.bookmarked.include_rubric.to_a.select(&:rubric_id).uniq(&:rubric_id).sort_by{|a| a.rubric.title }.map(&:rubric)).to eq []
@rubric.reload
expect(@rubric.deleted?).to be_falsey
end
end
describe "GET 'show'" do
before { course_with_teacher_logged_in(active_all: true) }
it "doesn't load nonsense" do
assert_page_not_found do
get 'show', id: "cats", course_id: @course.id
end
end
it "returns 404 if record doesn't exist" do
assert_page_not_found do
get 'show', id: "1", course_id: @course.id
end
end
it "works" do
r = Rubric.create! user: @teacher, context: Account.default
ra = RubricAssociation.create! rubric: r, context: @course,
purpose: :bookmark, association_object: @course
get 'show', id: r.id, course_id: @course.id
expect(response).to be_success
end
end
end