forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment_override_applicator_spec.rb
1120 lines (955 loc) · 46 KB
/
assignment_override_applicator_spec.rb
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# 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__) + '/../spec_helper.rb')
describe AssignmentOverrideApplicator do
def create_group_override
@category = group_category
@group = @category.groups.create!(context: @course)
@assignment.group_category = @category
@assignment.save!
@override = assignment_override_model(:assignment => @assignment)
@override.set = @group
@override.save!
@membership = @group.add_user(@student)
end
def create_assignment(*args)
# need to make sure it doesn't invalidate the cache right away
Timecop.freeze(5.seconds.ago) do
assignment_model(*args)
end
end
describe "assignment_overridden_for" do
before :each do
student_in_course
@assignment = create_assignment(:course => @course)
end
it "should note the user id for whom overrides were applied" do
@adhoc_override = assignment_override_model(:assignment => @assignment)
@override_student = @adhoc_override.assignment_override_students.build
@override_student.user = @student
@override_student.save!
@adhoc_override.override_due_at(7.days.from_now)
@adhoc_override.save!
@overridden_assignment = AssignmentOverrideApplicator.assignment_overridden_for(@assignment, @student)
expect(@overridden_assignment.overridden_for_user.id).to eq @student.id
end
it "should note the user id for whom overrides were not found" do
@overridden_assignment = AssignmentOverrideApplicator.assignment_overridden_for(@assignment, @student)
expect(@overridden_assignment.overridden_for_user.id).to eq @student.id
end
it "should apply new overrides if an overridden assignment is overridden for a new user" do
@student1 = @student
@overridden_assignment = AssignmentOverrideApplicator.assignment_overridden_for(@assignment, @student1)
expect(@overridden_assignment.overridden_for_user.id).to eq @student1.id
student_in_course
@student2 = @student
AssignmentOverrideApplicator.expects(:overrides_for_assignment_and_user).with(@overridden_assignment, @student2).returns([])
@reoverridden_assignment = AssignmentOverrideApplicator.assignment_overridden_for(@overridden_assignment, @student2)
end
it "should not attempt to apply overrides if an overridden assignment is overridden for the same user" do
@overridden_assignment = AssignmentOverrideApplicator.assignment_overridden_for(@assignment, @student)
expect(@overridden_assignment.overridden_for_user.id).to eq @student.id
AssignmentOverrideApplicator.expects(:overrides_for_assignment_and_user).never
@reoverridden_assignment = AssignmentOverrideApplicator.assignment_overridden_for(@overridden_assignment, @student)
end
context "give teachers the more lenient of override.due_at or assignment.due_at" do
before do
teacher_in_course
@section = @course.course_sections.create! :name => "Overridden Section"
student_in_section(@section)
@student = @user
end
def override_section(section, due)
override = assignment_override_model(:assignment => @assignment)
override.set = section
override.override_due_at(due)
override.save!
end
def setup_overridden_assignments(section_due_at, assignment_due_at)
override_section(@section, section_due_at)
@assignment.update_attribute(:due_at, assignment_due_at)
@students_assignment = AssignmentOverrideApplicator.
assignment_overridden_for(@assignment, @student)
@teachers_assignment = AssignmentOverrideApplicator.
assignment_overridden_for(@assignment, @teacher)
end
it "assignment.due_at is more lenient" do
section_due_at = 5.days.ago
assignment_due_at = nil
setup_overridden_assignments(section_due_at, assignment_due_at)
expect(@teachers_assignment.due_at.to_i).to eq assignment_due_at.to_i
expect(@students_assignment.due_at.to_i).to eq section_due_at.to_i
end
it "override.due_at is more lenient" do
section_due_at = 5.days.from_now
assignment_due_at = 5.days.ago
setup_overridden_assignments(section_due_at, assignment_due_at)
expect(@teachers_assignment.due_at.to_i).to eq section_due_at.to_i
expect(@students_assignment.due_at.to_i).to eq section_due_at.to_i
end
it "ignores assignment.due_at if all sections have overrides" do
section_due_at = 5.days.from_now
assignment_due_at = 1.year.from_now
override_section(@course.default_section, section_due_at)
setup_overridden_assignments(section_due_at, assignment_due_at)
expect(@teachers_assignment.due_at.to_i).to eq section_due_at.to_i
expect(@students_assignment.due_at.to_i).to eq section_due_at.to_i
end
end
end
describe "overrides_for_assignment_and_user" do
before :each do
student_in_course
@assignment = create_assignment(:course => @course, :due_at => 5.days.from_now)
end
context 'it works' do
it "should be serializable" do
override = AssignmentOverrideApplicator.assignment_overridden_for(@assignment, @student)
expect { Marshal.dump(override) }.not_to raise_error
end
it "should cache by assignment and user" do
enable_cache do
AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student)
Rails.cache.expects(:write_entry).never
AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student)
end
end
it "should distinguish cache by assignment" do
enable_cache do
overrides1 = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student)
assignment = create_assignment
Rails.cache.expects(:write_entry)
overrides2 = AssignmentOverrideApplicator.overrides_for_assignment_and_user(assignment, @student)
end
end
it "should distinguish cache by assignment version" do
Timecop.travel Time.now + 1.hour do
@assignment.due_at = 7.days.from_now
@assignment.save!
expect(@assignment.versions.count).to eq 2
enable_cache do
AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment.versions.first.model, @student)
Rails.cache.expects(:write_entry)
AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment.versions.current.model, @student)
end
end
end
it "should distinguish cache by user" do
enable_cache do
AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student)
user = user_model
Rails.cache.expects(:write_entry)
AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, user)
end
end
it "should order adhoc override before group override" do
@category = group_category
@group = @category.groups.create!(:context => @course)
@membership = @group.add_user(@student)
@assignment.group_category = @category
@assignment.save!
@group_override = assignment_override_model(:assignment => @assignment)
@group_override.set = @group
@group_override.save!
@adhoc_override = assignment_override_model(:assignment => @assignment)
@override_student = @adhoc_override.assignment_override_students.build
@override_student.user = @student
@override_student.save!
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student)
expect(overrides.size).to eq 2
expect(overrides.first).to eq @adhoc_override
expect(overrides.last).to eq @group_override
end
it "should order group override before section overrides" do
@category = group_category
@group = @category.groups.create!(:context => @course)
@membership = @group.add_user(@student)
@assignment.group_category = @category
@assignment.save!
@section_override = assignment_override_model(:assignment => @assignment)
@section_override.set = @course.default_section
@section_override.save!
@group_override = assignment_override_model(:assignment => @assignment)
@group_override.set = @group
@group_override.save!
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student)
expect(overrides.size).to eq 2
expect(overrides.first).to eq @group_override
expect(overrides.last).to eq @section_override
end
it "should order section overrides by position" # see TODO in implementation
end
context 'adhoc overrides' do
before :each do
@override = assignment_override_model(:assignment => @assignment)
@override_student = @override.assignment_override_students.build
@override_student.user = @student
@override_student.save!
end
describe 'for students' do
it "should include adhoc override for the user" do
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student)
expect(overrides).to eq [@override]
end
it "should not include adhoc overrides that don't include the user" do
new_student = student_in_course
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, new_student.user)
expect(overrides).to be_empty
end
it "finds the overrides for the correct student" do
result = AssignmentOverrideApplicator::adhoc_override(@assignment, @student)
expect(result.assignment_override_id).to eq @override.id
end
it "returns AssignmentOverrideStudent" do
result = AssignmentOverrideApplicator::adhoc_override(@assignment, @student)
expect(result).to be_an_instance_of(AssignmentOverrideStudent)
end
end
describe 'for teachers' do
it "works" do
teacher_in_course
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @teacher)
expect(overrides).to eq [@override]
end
end
describe 'for observers' do
it "works" do
course_with_observer({:course => @course, :active_all => true})
@course.enroll_user(@observer, "ObserverEnrollment", {:associated_user_id => @student.id})
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @observer)
expect(overrides).to eq [@override]
end
end
describe 'for admins' do
it "works" do
account_admin_user
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @admin)
expect(overrides).to eq [@override]
end
end
end
context 'group overrides' do
before :each do
create_group_override
end
describe 'for students' do
it 'returns group overrides' do
result = AssignmentOverrideApplicator.group_override(@assignment, @student)
expect(result).to eq @override
end
it "should not include group override for groups other than the user's" do
@override.set = @category.groups.create!(context: @course)
@override.save!
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student)
expect(overrides).to be_empty
end
it "should not include group override for deleted groups" do
@group.destroy
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student)
expect(overrides).to be_empty
end
it "should not include group override for deleted group memberships" do
@membership.destroy
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student)
expect(overrides).to be_empty
end
end
describe 'for teachers' do
it 'works' do
teacher_in_course
result = AssignmentOverrideApplicator.group_override(@assignment, @teacher)
expect(result).to eq @override
end
end
describe 'for observers' do
it 'works' do
course_with_observer({:course => @course, :active_all => true})
@course.enroll_user(@observer, "ObserverEnrollment", {:associated_user_id => @student.id})
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @observer)
expect(overrides).to eq [@override]
end
end
describe 'for admins' do
it 'works' do
account_admin_user
user_session(@admin)
result = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @admin)
expect(result).to eq [@override]
end
end
end
context 'section overrides' do
before :each do
@override = assignment_override_model(:assignment => @assignment)
@override.set = @course.default_section
@override.save!
@section2 = @course.course_sections.create!(:name => "Summer session")
@override2 = assignment_override_model(:assignment => @assignment)
@override2.set_type = 'CourseSection'
@override2.set_id = @section2.id
@override2.due_at = 7.days.from_now
@override2.save!
@student2 = student_in_section(@section2, {:active_all => true})
end
describe 'for students' do
it "returns section overrides" do
result = AssignmentOverrideApplicator::section_overrides(@assignment, @student2)
expect(result.length).to eq 1
end
it "should enforce lenient date" do
adhoc_due_at = 10.days.from_now
ao = AssignmentOverride.new()
ao.assignment = @assignment
ao.title = "ADHOC OVERRIDE"
ao.workflow_state = "active"
ao.set_type = "ADHOC"
ao.override_due_at(adhoc_due_at)
ao.save!
override_student = ao.assignment_override_students.build
override_student.user = @student2
override_student.save!
@assignment.reload
students_assignment = AssignmentOverrideApplicator.
assignment_overridden_for(@assignment, @student2)
expect(students_assignment.due_at.to_i).to eq adhoc_due_at.to_i
end
it "should include section overrides for sections with an active student enrollment" do
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student2)
expect(overrides).to eq [@override2]
end
it "should not include section overrides for sections with deleted enrollments" do
@student2.student_enrollments.first.destroy
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student2)
expect(overrides).to be_empty
end
it "should include all relevant section overrides" do
@course.enroll_student(@student, :section => @override2.set, :allow_multiple_enrollments => true)
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student)
expect(overrides.size).to eq 2
expect(overrides).to include(@override)
expect(overrides).to include(@override2)
end
it "should work even if :read_roster is disabled" do
RoleOverride.create!(:context => @course.root_account, :permission => 'read_roster',
:role => student_role, :enabled => false)
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student2)
expect(overrides).to eq [@override2]
end
it "should only use the latest due_date for student_view_student" do
due_at = 3.days.from_now
a = create_assignment(:course => @course)
cs1 = @course.course_sections.create!
override1 = assignment_override_model(:assignment => a)
override1.set = cs1
override1.override_due_at(due_at)
override1.save!
cs2 = @course.course_sections.create!
override2 = assignment_override_model(:assignment => a)
override2.set = cs2
override2.override_due_at(due_at - 1.day)
override2.save!
@fake_student = @course.student_view_student
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(a, @fake_student)
expect(overrides).to include(override1, override2)
expect(AssignmentOverrideApplicator.collapsed_overrides(a, overrides)[:due_at].to_i).to eq due_at.to_i
end
it "should not include section overrides for sections without an enrollment" do
assignment = create_assignment(:course => @course, :due_at => 5.days.from_now)
override = assignment_override_model(:assignment => assignment)
override.set = @course.course_sections.create!
override.save!
overrides = AssignmentOverrideApplicator.section_overrides(assignment, @student)
expect(overrides).to be_empty
end
end
describe 'for teachers' do
it 'works' do
teacher_in_course
result = AssignmentOverrideApplicator.section_overrides(@assignment, @teacher)
expect(result).to include(@override, @override2)
end
end
describe 'for observers' do
it 'works' do
course_with_observer({:course => @course, :active_all => true})
@course.enroll_user(@observer, "ObserverEnrollment", {:associated_user_id => @student2.id})
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @observer)
expect(overrides).to eq [@override2]
end
end
describe 'for admins' do
it 'works' do
account_admin_user
result = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @admin)
expect(result).to include(@override, @override2)
end
end
end
context '#observer_overrides' do
it "returns all dates visible to observer" do
@override = assignment_override_model(:assignment => @assignment)
@override_student = @override.assignment_override_students.build
@override_student.user = @student
@override_student.save!
course_with_observer({:course => @course, :active_all => true})
@course.enroll_user(@observer, "ObserverEnrollment", {:associated_user_id => @student.id})
@section2 = @course.course_sections.create!(:name => "Summer session")
@override2 = assignment_override_model(:assignment => @assignment)
@override2.set_type = 'ADHOC'
@override2.due_at = 7.days.from_now
@override2.save!
@override2_student = @override2.assignment_override_students.build
@student2 = student_in_section(@section2, {:active_all => true})
@override2_student.user = @student2
@override2_student.save!
@course.enroll_user(@observer, "ObserverEnrollment", {:allow_multiple_enrollments => true, :associated_user_id => @student2.id})
result = AssignmentOverrideApplicator::observer_overrides(@assignment, @observer)
expect(result.length).to eq 2
end
end
context '#has_invalid_args?' do
it "returns true with nil user" do
result = AssignmentOverrideApplicator::has_invalid_args?(@assignment, nil)
expect(result).to be_truthy
end
it "returns true for assignments with no overrides" do
result = AssignmentOverrideApplicator::has_invalid_args?(@assignment, @student)
expect(result).to be_truthy
end
it "returns false if user and overrides are valid" do
@override = assignment_override_model(:assignment => @assignment)
@override_student = @override.assignment_override_students.build
@override_student.user = @student
@override_student.save!
result = AssignmentOverrideApplicator::has_invalid_args?(@assignment, @student)
expect(result).to be_falsey
end
end
context "versioning" do
it "should use the appropriate version of an override" do
@override = assignment_override_model(:assignment => @assignment)
@override_student = @override.assignment_override_students.build
@override_student.user = @student
@override_student.save!
original_override_version_number = @override.version_number
@assignment.due_at = 3.days.from_now
@assignment.save!
@override.override_due_at(5.days.from_now)
@override.save!
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student)
expect(overrides.first.version_number).to eq @override.version_number
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment.versions.first.model, @student)
expect(overrides.first.version_number).to eq original_override_version_number
end
it "should use the most-recent override version for the given assignment version" do
@override = assignment_override_model(:assignment => @assignment)
@override_student = @override.assignment_override_students.build
@override_student.user = @student
@override_student.save!
first_version = @override.version_number
@override.override_due_at(7.days.from_now)
@override.save!
second_version = @override.version_number
expect(first_version).not_to eq second_version
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student)
expect(overrides.first.version_number).to eq second_version
end
it "should exclude overrides that weren't created until a later assignment version" do
@assignment.due_at = 3.days.from_now
@assignment.save!
@override = assignment_override_model(:assignment => @assignment)
@override_student = @override.assignment_override_students.build
@override_student.user = @student
@override_student.save!
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment.versions.first.model, @student)
expect(overrides).to be_empty
end
it "should exclude overrides that were deleted as of the assignment version" do
@override = assignment_override_model(:assignment => @assignment)
@override_student = @override.assignment_override_students.build
@override_student.user = @student
@override_student.save!
@override.destroy
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment, @student)
expect(overrides).to be_empty
end
it "should include now-deleted overrides that weren't deleted yet as of the assignment version" do
@override = assignment_override_model(:assignment => @assignment)
@override.set = @course.default_section
@override.save!
@assignment.due_at = 3.days.from_now
@assignment.save!
@override.destroy
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment.versions.first.model, @student)
expect(overrides).to eq [@override]
expect(overrides.first).not_to be_deleted
end
it "should include now-deleted overrides that weren't deleted yet as of the assignment version (with manage_courses permission)" do
account_admin_user
@override = assignment_override_model(:assignment => @assignment)
@override.set = @course.default_section
@override.save!
@assignment.due_at = 3.days.from_now
@assignment.save!
@override.destroy
overrides = AssignmentOverrideApplicator.overrides_for_assignment_and_user(@assignment.versions.first.model, @admin)
expect(overrides).to eq [@override]
expect(overrides.first).not_to be_deleted
end
context "overrides for an assignment for a quiz, where the overrides were created before the quiz was published" do
context "without draft states" do
it "skips versions of the override that have nil for an assignment version" do
student_in_course
expected_time = Time.zone.now
quiz = @course.quizzes.create! :title => "VDD Quiz", :quiz_type => 'assignment'
section = @course.course_sections.create! :name => "title"
@course.enroll_user(@student,
'StudentEnrollment',
:section => section,
:enrollment_state => 'active',
:allow_multiple_enrollments => true)
override = quiz.assignment_overrides.build
override.quiz_id = quiz.id
override.quiz = quiz
override.set_type = 'CourseSection'
override.set_id = section.id
override.title = "Quiz Assignment override"
override.due_at = expected_time
override.save!
quiz.publish!
override = quiz.reload.assignment.assignment_overrides.first
expect(override.versions.length).to eq 1
expect(override.versions[0].model.assignment_version).not_to be_nil
# Assert that it won't call the "<=" method on nil
expect do
overrides = AssignmentOverrideApplicator.
overrides_for_assignment_and_user(quiz.assignment, @student)
end.to_not raise_error
end
end
context "with draft states" do
it "quiz should always have an assignment for overrides" do
# with draft states quizzes always have an assignment.
student_in_course
expected_time = Time.zone.now
quiz = @course.quizzes.create! :title => "VDD Quiz", :quiz_type => 'assignment'
section = @course.course_sections.create! :name => "title"
@course.enroll_user(@student,
'StudentEnrollment',
:section => section,
:enrollment_state => 'active',
:allow_multiple_enrollments => true)
override = quiz.assignment_overrides.build
override.quiz_id = quiz.id
override.quiz = quiz
override.set_type = 'CourseSection'
override.set_id = section.id
override.title = "Quiz Assignment override"
override.due_at = expected_time
override.save!
quiz.publish!
override = quiz.reload.assignment.assignment_overrides.first
expect(override.versions.length).to eq 1
expect(override.versions[0].model.assignment_version).not_to be_nil
# Assert that it won't call the "<=" method on nil
expect do
overrides = AssignmentOverrideApplicator.
overrides_for_assignment_and_user(quiz.assignment, @student)
end.to_not raise_error
end
end
end
end
end
describe "assignment_with_overrides" do
before :each do
Time.zone == 'Alaska'
@assignment = create_assignment(
:due_at => 5.days.from_now,
:unlock_at => 4.days.from_now,
:lock_at => 6.days.from_now,
:title => 'Some Title')
@override = assignment_override_model(:assignment => @assignment)
@override.override_due_at(7.days.from_now)
@overridden = AssignmentOverrideApplicator.assignment_with_overrides(@assignment, [@override])
end
it "should return a new assignment object" do
expect(@overridden.class).to eq @assignment.class
expect(@overridden.object_id).not_to eq @assignment.object_id
end
it "should preserve assignment id" do
expect(@overridden.id).to eq @assignment.id
end
it "should be new_record? iff the original assignment is" do
expect(@overridden).not_to be_new_record
@assignment = Assignment.new
@overridden = AssignmentOverrideApplicator.assignment_with_overrides(@assignment, [])
expect(@overridden).to be_new_record
end
it "should apply overrides to the returned assignment object" do
expect(@overridden.due_at).to eq @override.due_at
end
it "should not change the original assignment object" do
expect(@assignment.due_at).not_to eq @overridden.due_at
end
it "should inherit other values from the original assignment object" do
expect(@overridden.title).to eq @assignment.title
end
it "should return a readonly assignment object" do
expect(@overridden).to be_readonly
expect{ @overridden.save! }.to raise_exception ActiveRecord::ReadOnlyRecord
end
it "should cast datetimes to the active time zone" do
expect(@overridden.due_at.time_zone).to eq Time.zone
expect(@overridden.unlock_at.time_zone).to eq Time.zone
expect(@overridden.lock_at.time_zone).to eq Time.zone
end
it "should not cast dates to zoned datetimes" do
expect(@overridden.all_day_date.class).to eq Date
end
it "should copy pre-loaded associations" do
expect(@overridden.association(:context).loaded?).to eq @assignment.association(:context).loaded?
expect(@overridden.association(:rubric).loaded?).to eq @assignment.association(:rubric).loaded?
@overridden.learning_outcome_alignments.loaded? == @assignment.learning_outcome_alignments.loaded?
end
end
describe "collapsed_overrides" do
it "should cache by assignment and overrides" do
@assignment = create_assignment
@override = assignment_override_model(:assignment => @assignment)
enable_cache do
overrides1 = AssignmentOverrideApplicator.collapsed_overrides(@assignment, [@override])
Rails.cache.expects(:write_entry).never
overrides2 = AssignmentOverrideApplicator.collapsed_overrides(@assignment, [@override])
end
end
it "should distinguish cache by assignment" do
@assignment1 = create_assignment
@assignment2 = create_assignment
@override = assignment_override_model(:assignment => @assignment1)
enable_cache do
AssignmentOverrideApplicator.collapsed_overrides(@assignment1, [@override])
Rails.cache.expects(:write_entry)
AssignmentOverrideApplicator.collapsed_overrides(@assignment2, [@override])
end
end
it "should distinguish cache by assignment updated_at" do
@assignment = create_assignment
Timecop.travel Time.now + 1.hour do
@assignment.due_at = 5.days.from_now
@assignment.save!
expect(@assignment.versions.count).to eq 2
@override = assignment_override_model(:assignment => @assignment)
enable_cache do
expect(@assignment.versions.first.updated_at).not_to eq @assignment.versions.current.model.updated_at
AssignmentOverrideApplicator.collapsed_overrides(@assignment.versions.first.model, [@override])
Rails.cache.expects(:write_entry)
AssignmentOverrideApplicator.collapsed_overrides(@assignment.versions.current.model, [@override])
end
end
end
it "should distinguish cache by overrides" do
@assignment = create_assignment
@override1 = assignment_override_model(:assignment => @assignment)
@override2 = assignment_override_model(:assignment => @assignment)
enable_cache do
AssignmentOverrideApplicator.collapsed_overrides(@assignment, [@override1])
Rails.cache.expects(:write_entry)
AssignmentOverrideApplicator.collapsed_overrides(@assignment, [@override2])
end
end
it "should have a collapsed value for each recognized field" do
@assignment = create_assignment
@override = assignment_override_model(:assignment => @assignment)
overrides = AssignmentOverrideApplicator.collapsed_overrides(@assignment, [@override])
expect(overrides.class).to eq Hash
expect(overrides.keys.to_set).to eq [:due_at, :all_day, :all_day_date, :unlock_at, :lock_at].to_set
end
it "should use raw UTC time for datetime fields" do
Time.zone = 'Alaska'
@assignment = create_assignment(
:due_at => 5.days.from_now,
:unlock_at => 6.days.from_now,
:lock_at => 7.days.from_now)
collapsed = AssignmentOverrideApplicator.collapsed_overrides(@assignment, [])
expect(collapsed[:due_at].class).to eq Time; expect(collapsed[:due_at]).to eq @assignment.due_at.utc
expect(collapsed[:unlock_at].class).to eq Time; expect(collapsed[:unlock_at]).to eq @assignment.unlock_at.utc
expect(collapsed[:lock_at].class).to eq Time; expect(collapsed[:lock_at]).to eq @assignment.lock_at.utc
end
it "should not use raw UTC time for date fields" do
Time.zone = 'Alaska'
@assignment = create_assignment(:due_at => 5.days.from_now)
collapsed = AssignmentOverrideApplicator.collapsed_overrides(@assignment, [])
expect(collapsed[:all_day_date].class).to eq Date
expect(collapsed[:all_day_date]).to eq @assignment.all_day_date
end
end
describe "overrides_hash" do
it "should be consistent for the same overrides" do
overrides = 5.times.map{ assignment_override_model }
hash1 = AssignmentOverrideApplicator.overrides_hash(overrides)
hash2 = AssignmentOverrideApplicator.overrides_hash(overrides)
expect(hash1).to eq hash2
end
it "should be unique for different overrides" do
overrides1 = 5.times.map{ assignment_override_model }
overrides2 = 5.times.map{ assignment_override_model }
hash1 = AssignmentOverrideApplicator.overrides_hash(overrides1)
hash2 = AssignmentOverrideApplicator.overrides_hash(overrides2)
expect(hash1).not_to eq hash2
end
it "should be unique for different versions of the same overrides" do
overrides = 5.times.map{ assignment_override_model }
hash1 = AssignmentOverrideApplicator.overrides_hash(overrides)
overrides.first.override_due_at(5.days.from_now)
overrides.first.save!
hash2 = AssignmentOverrideApplicator.overrides_hash(overrides)
expect(hash1).not_to eq hash2
end
it "should be unique for different orders of the same overrides" do
overrides = 5.times.map{ assignment_override_model }
hash1 = AssignmentOverrideApplicator.overrides_hash(overrides)
hash2 = AssignmentOverrideApplicator.overrides_hash(overrides.reverse)
expect(hash1).not_to eq hash2
end
end
def fancy_midnight(opts={})
zone = opts[:zone] || Time.zone
Time.use_zone(zone) do
time = opts[:time] || Time.zone.now
time.in_time_zone.midnight + 1.day - 1.minute
end
end
describe "overridden_due_at" do
before :each do
@assignment = create_assignment(:due_at => 5.days.from_now)
@override = assignment_override_model(:assignment => @assignment)
end
it "should use overrides that override due_at" do
@override.override_due_at(7.days.from_now)
due_at = AssignmentOverrideApplicator.overridden_due_at(@assignment, [@override])
expect(due_at).to eq @override.due_at
end
it "should skip overrides that don't override due_at" do
@override2 = assignment_override_model(:assignment => @assignment)
@override2.override_due_at(7.days.from_now)
due_at = AssignmentOverrideApplicator.overridden_due_at(@assignment, [@override, @override2])
expect(due_at).to eq @override2.due_at
end
it "should prefer most lenient override" do
@override.override_due_at(6.days.from_now)
@override2 = assignment_override_model(:assignment => @assignment)
@override2.override_due_at(7.days.from_now)
due_at = AssignmentOverrideApplicator.overridden_due_at(@assignment, [@override, @override2])
expect(due_at).to eq @override2.due_at
end
it "should consider no due date as most lenient" do
@override.override_due_at(nil)
@override2 = assignment_override_model(:assignment => @assignment)
@override2.override_due_at(7.days.from_now)
due_at = AssignmentOverrideApplicator.overridden_due_at(@assignment, [@override, @override2])
expect(due_at).to eq @override.due_at
end
it "should not consider empty original due date as more lenient than an override due date" do
@assignment.due_at = nil
@override.override_due_at(6.days.from_now)
due_at = AssignmentOverrideApplicator.overridden_due_at(@assignment, [@override])
expect(due_at).to eq @override.due_at
end
it "prefers overrides even when earlier when determining most lenient due date" do
earlier = 6.days.from_now
@assignment.due_at = 7.days.from_now
@override.override_due_at(earlier)
due_at = AssignmentOverrideApplicator.overridden_due_at(@assignment, [@override])
expect(due_at).to eq earlier
end
it "should fallback on the assignment's due_at" do
due_at = AssignmentOverrideApplicator.overridden_due_at(@assignment, [@override])
expect(due_at).to eq @assignment.due_at
end
it "should recognize overrides with overridden-but-nil due_at" do
@override.override_due_at(nil)
due_at = AssignmentOverrideApplicator.overridden_due_at(@assignment, [@override])
expect(due_at).to eq @override.due_at
end
end
# specs for overridden_due_at cover all_day and all_day_date, since they're
# pulled from the same assignment/override the due_at is
describe "overridden_unlock_at" do
before :each do
@assignment = create_assignment(:unlock_at => 10.days.from_now)
@override = assignment_override_model(:assignment => @assignment)
end
it "should use overrides that override unlock_at" do
@override.override_unlock_at(7.days.from_now)
unlock_at = AssignmentOverrideApplicator.overridden_unlock_at(@assignment, [@override])
expect(unlock_at).to eq @override.unlock_at
end
it "should skip overrides that don't override unlock_at" do
@override2 = assignment_override_model(:assignment => @assignment)
@override2.override_unlock_at(7.days.from_now)
unlock_at = AssignmentOverrideApplicator.overridden_unlock_at(@assignment, [@override, @override2])
expect(unlock_at).to eq @override2.unlock_at
end
it "should prefer most lenient override" do
@override.override_unlock_at(7.days.from_now)
@override2 = assignment_override_model(:assignment => @assignment)
@override2.override_unlock_at(6.days.from_now)
unlock_at = AssignmentOverrideApplicator.overridden_unlock_at(@assignment, [@override, @override2])
expect(unlock_at).to eq @override2.unlock_at
end
it "should consider no unlock date as most lenient" do
@override.override_unlock_at(nil)
@override2 = assignment_override_model(:assignment => @assignment)
@override2.override_unlock_at(7.days.from_now)
unlock_at = AssignmentOverrideApplicator.overridden_unlock_at(@assignment, [@override, @override2])
expect(unlock_at).to eq @override.unlock_at
end
it "should not consider empty original unlock date as more lenient than an override unlock date" do
@assignment.unlock_at = nil
@override.override_unlock_at(6.days.from_now)
unlock_at = AssignmentOverrideApplicator.overridden_unlock_at(@assignment, [@override])
expect(unlock_at).to eq @override.unlock_at
end
it "prefers overrides even when later when determining most lenient unlock date" do
later = 7.days.from_now
@assignment.unlock_at = 6.days.from_now
@override.override_unlock_at(later)
unlock_at = AssignmentOverrideApplicator.overridden_unlock_at(@assignment, [@override])
expect(unlock_at).to eq later
end
it "should fallback on the assignment's unlock_at" do
unlock_at = AssignmentOverrideApplicator.overridden_unlock_at(@assignment, [@override])
expect(unlock_at).to eq @assignment.unlock_at
end
it "should recognize overrides with overridden-but-nil unlock_at" do
@override.override_unlock_at(nil)
unlock_at = AssignmentOverrideApplicator.overridden_unlock_at(@assignment, [@override])
expect(unlock_at).to eq @override.unlock_at
end
end
describe "overridden_lock_at" do
before :each do
@assignment = create_assignment(:lock_at => 5.days.from_now)
@override = assignment_override_model(:assignment => @assignment)
end
it "should use overrides that override lock_at" do
@override.override_lock_at(7.days.from_now)
lock_at = AssignmentOverrideApplicator.overridden_lock_at(@assignment, [@override])