forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenrollment_spec.rb
More file actions
2532 lines (2157 loc) · 95.9 KB
/
Copy pathenrollment_spec.rb
File metadata and controls
2532 lines (2157 loc) · 95.9 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
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 - 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_relative '../sharding_spec_helper'
describe Enrollment do
before(:once) do
@user = User.create!
@course = Course.create!
@enrollment = StudentEnrollment.new(valid_enrollment_attributes)
end
it "should be valid" do
expect(@enrollment).to be_valid
end
it "should have an interesting state machine" do
enrollment_model
@user.stubs(:dashboard_messages).returns(Message.none)
expect(@enrollment.state).to eql(:invited)
@enrollment.accept
expect(@enrollment.state).to eql(:active)
@enrollment.reject
expect(@enrollment.state).to eql(:rejected)
Score.where(enrollment_id: @enrollment).delete_all
@enrollment.destroy_permanently!
enrollment_model
@enrollment.complete
expect(@enrollment.state).to eql(:completed)
@enrollment.destroy_permanently!
enrollment_model
@enrollment.reject
expect(@enrollment.state).to eql(:rejected)
@enrollment.destroy_permanently!
enrollment_model
@enrollment.accept
expect(@enrollment.state).to eql(:active)
end
it "should be pending if it is invited or creation_pending" do
enrollment_model(:workflow_state => 'invited')
expect(@enrollment).to be_pending
@enrollment.destroy_permanently!
enrollment_model(:workflow_state => 'creation_pending')
expect(@enrollment).to be_pending
end
it "should have a context_id as the course_id" do
expect(@enrollment.course.id).not_to be_nil
expect(@enrollment.context_id).to eql(@enrollment.course.id)
end
it "should have a readable_type of Teacher for a TeacherEnrollment" do
e = TeacherEnrollment.new
e.type = 'TeacherEnrollment'
expect(e.readable_type).to eql('Teacher')
end
it "should have a readable_type of Student for a StudentEnrollment" do
e = StudentEnrollment.new
e.type = 'StudentEnrollment'
expect(e.readable_type).to eql('Student')
end
it "should have a readable_type of TaEnrollment for a TA" do
e = TaEnrollment.new(valid_enrollment_attributes)
e.type = 'TaEnrollment'
expect(e.readable_type).to eql('TA')
end
it "should have a defalt readable_type of Student" do
e = Enrollment.new
e.type = 'Other'
expect(e.readable_type).to eql('Student')
end
describe "sis_role" do
it "should return role_name if present" do
role = custom_account_role('Assistant Grader', :account => Account.default)
e = TaEnrollment.new
e.role_id = role.id
expect(e.sis_role).to eq 'Assistant Grader'
end
it "should return the sis enrollment type otherwise" do
e = TaEnrollment.new
expect(e.sis_role).to eq 'ta'
end
end
describe '#destroy' do
before(:once) do
@enrollment = StudentEnrollment.create!(valid_enrollment_attributes)
assignment = @course.assignments.create!
@override = assignment.assignment_overrides.create!
@override.assignment_override_students.create!(user: @enrollment.user)
end
let(:override_student) { @override.assignment_override_students.find_by(user_id: @enrollment.user) }
it 'does not destroy assignment override students on the user if other enrollments' \
'for the user exist in the course' do
@course.enroll_user(
@enrollment.user,
'StudentEnrollment',
section: @course.course_sections.create!,
allow_multiple_enrollments: true
)
@enrollment.destroy
expect(override_student).to be_present
end
it 'destroys assignment override students on the user if no other enrollments for the user exist in the course' do
@enrollment.destroy
expect(override_student).not_to be_present
end
end
describe '#restore' do
before(:once) do
@enrollment.save!
@enrollment.scores.create!
@enrollment.destroy
end
it 'restores associated scores that are deleted' do
expect { @enrollment.restore }.to change {
Score.find_by(enrollment_id: @enrollment, grading_period_id: nil).workflow_state
}.from('deleted').to('active')
end
it 'does not restore scores associated with other enrollments' do
new_enrollment = StudentEnrollment.create!(user: User.create!, course: @course)
new_score = new_enrollment.scores.new
new_score.workflow_state = :deleted
new_score.save!
expect { @enrollment.restore }.not_to change { new_score.reload.workflow_state }
end
end
describe 'scores and grades' do
let(:new_student_enrollment) do
@course.enroll_user(
@enrollment.user,
'StudentEnrollment',
section: @course.course_sections.create!,
allow_multiple_enrollments: true
)
end
let(:new_fake_student_enrollment) do
@course.enroll_user(
@enrollment.user,
'StudentViewEnrollment',
section: @course.course_sections.create!,
allow_multiple_enrollments: true
)
end
describe 'current scores and grades' do
before(:once) do
@enrollment = StudentEnrollment.create!(valid_enrollment_attributes)
end
let(:period) do
group = @course.root_account.grading_period_groups.create!
group.grading_periods.create!(
title: 'period',
start_date: 'Jan 1, 2015',
end_date: 'Jan 5, 2015'
)
end
describe '#computed_current_score' do
it 'uses the value from the associated score object, if one exists' do
@enrollment.scores.create!(current_score: 80.3)
expect(@enrollment.computed_current_score).to eq 80.3
end
it 'uses the value from the associated score object, even if it is nil' do
@enrollment.scores.create!(current_score: nil)
expect(@enrollment.computed_current_score).to be_nil
end
it 'ignores grading period scores when passed no arguments' do
@enrollment.scores.create!(current_score: 80.3, grading_period: period)
expect(@enrollment.computed_current_score).to be_nil
end
it 'ignores soft-deleted scores' do
score = @enrollment.scores.create!(current_score: 80.3)
score.destroy
expect(@enrollment.computed_current_score).to be_nil
end
it 'computes current score for a given grading period id' do
@enrollment.scores.create!(current_score: 80.3)
@enrollment.scores.create!(current_score: 70.6, grading_period: period)
current_score = @enrollment.computed_current_score(grading_period_id: period.id)
expect(current_score).to eq 70.6
end
it 'returns nil if a grading period score is requested and does not exist' do
current_score = @enrollment.computed_current_score(grading_period_id: period.id)
expect(current_score).to be_nil
end
end
describe '#computed_current_grade' do
before(:each) do
@course.grading_standard_enabled = true
@course.save!
end
it 'uses the value from the associated score object, if one exists' do
@enrollment.scores.create!(current_score: 80.3)
expect(@enrollment.computed_current_grade).to eq 'B-'
end
it 'ignores grading period grades when passed no arguments' do
@enrollment.scores.create!(current_score: 80.3, grading_period: period)
expect(@enrollment.computed_current_grade).to be_nil
end
it 'ignores grades from soft-deleted scores' do
score = @enrollment.scores.create!(current_score: 80.3)
score.destroy
expect(@enrollment.computed_current_grade).to be_nil
end
it 'computes current grade for a given grading period id' do
@enrollment.scores.create!(current_score: 70.6, grading_period: period)
current_grade = @enrollment.computed_current_grade(grading_period_id: period.id)
expect(current_grade).to eq 'C-'
end
it 'returns nil if a grading period grade is requested and does not exist' do
current_grade = @enrollment.computed_current_grade(grading_period_id: period.id)
expect(current_grade).to be_nil
end
end
describe '#graded_at' do
it 'uses the updated_at from the associated score object, if one exists' do
score = @enrollment.scores.create!(current_score: 80.3)
score.update_attribute(:updated_at, 5.days.from_now)
expect(@enrollment.graded_at).to eq score.updated_at
end
it 'uses the graded_at attribute if no associated score object exists' do
expect(@enrollment.graded_at).to eq @enrollment.read_attribute(:graded_at)
end
it 'ignores grading period scores' do
@enrollment.scores.create!(current_score: 80.3, grading_period: period)
expect(@enrollment.graded_at).to eq @enrollment.read_attribute(:graded_at)
end
it 'ignores soft-deleted scores' do
score = @enrollment.scores.create!(current_score: 80.3)
score.destroy
expect(@enrollment.graded_at).to eq @enrollment.read_attribute(:graded_at)
end
end
describe 'copying current scores' do
before(:once) do
teacher = User.create!
@course.enroll_teacher(teacher, active_all: true)
assignment = @course.assignments.create!(points_possible: 100)
assignment.grade_student(@enrollment.user, grade: 95, grader: teacher)
end
context 'on creation' do
it 'copies scores over from existing student enrollments to new student enrollments' do
expect(new_student_enrollment.computed_current_score).to eq(@enrollment.computed_current_score)
end
it 'copies scores over from existing fake student enrollments to new fake student enrollments' do
@enrollment.update!(type: 'StudentViewEnrollment')
expect(new_fake_student_enrollment.computed_current_score).to eq(@enrollment.computed_current_score)
end
it 'does not copy scores if the new enrollment type does not match existing enrollment types' do
expect(new_fake_student_enrollment.computed_current_score).to be_nil
end
it 'does not copy scores if the existing enrollment is soft-deleted' do
@enrollment.destroy
expect(new_student_enrollment.computed_current_score).to be_nil
end
end
context 'on restoration' do
it 'copies scores over from existing student enrollments to restored student enrollments' do
new_student_enrollment.destroy
new_student_enrollment.update!(workflow_state: :active)
expect(new_student_enrollment.computed_current_score).to eq(@enrollment.computed_current_score)
end
it 'copies scores over from existing fake student enrollments to restored fake student enrollments' do
@enrollment.update!(type: 'StudentViewEnrollment')
new_fake_student_enrollment.destroy
new_fake_student_enrollment.update!(workflow_state: :active)
expect(new_fake_student_enrollment.computed_current_score).to eq(@enrollment.computed_current_score)
end
it 'does not copy scores if the restored enrollment type does not match existing enrollment types' do
new_fake_student_enrollment.destroy
new_fake_student_enrollment.update!(workflow_state: :active)
expect(new_fake_student_enrollment.computed_current_score).to be_nil
end
it 'does not copy scores if the existing enrollment is soft-deleted' do
@enrollment.destroy
new_student_enrollment.destroy
new_student_enrollment.update!(workflow_state: :active)
expect(new_student_enrollment.computed_current_score).to be_nil
end
end
end
end
describe 'final scores and grades' do
before(:once) do
@enrollment.save!
end
let(:period) do
group = @course.root_account.grading_period_groups.create!
group.grading_periods.create!(
title: 'period',
start_date: 'Jan 1, 2015',
end_date: 'Jan 5, 2015'
)
end
describe '#find_score' do
it 'returns the course score when no arg is passed' do
score = @enrollment.scores.create!(final_score: 80.3)
@enrollment.scores.create!(final_score: 80.3, grading_period_id: period.id)
expect(@enrollment.find_score).to eq score
end
it 'returns the grading period score when grading_period_id is passed' do
@enrollment.scores.create!(final_score: 80.3)
score = @enrollment.scores.create!(final_score: 80.3, grading_period_id: period.id)
expect(@enrollment.find_score(grading_period_id: period.id)).to eq score
end
end
describe '#computed_final_score' do
it 'uses the value from the associated score object, if one exists' do
@enrollment.scores.create!(final_score: 80.3)
expect(@enrollment.computed_final_score).to eq 80.3
end
it 'uses the value from the associated score object, even if it is nil' do
@enrollment.scores.create!(final_score: nil)
expect(@enrollment.computed_final_score).to eq nil
end
it 'ignores grading period scores when passed no arguments' do
@enrollment.scores.create!(final_score: 80.3, grading_period: period)
expect(@enrollment.computed_final_score).to be_nil
end
it 'ignores soft-deleted scores' do
score = @enrollment.scores.create!(final_score: 80.3)
score.destroy
expect(@enrollment.computed_final_score).to be_nil
end
it 'computes final score for a given grading period id' do
@enrollment.scores.create!(final_score: 70.6, grading_period: period)
final_score = @enrollment.computed_final_score(grading_period_id: period.id)
expect(final_score).to eq 70.6
end
it 'returns nil if a grading period score is requested and does not exist' do
final_score = @enrollment.computed_final_score(grading_period_id: period.id)
expect(final_score).to eq nil
end
end
describe '#computed_final_grade' do
before(:each) do
@course.grading_standard_enabled = true
@course.save!
end
it 'uses the value from the associated score object, if one exists' do
@enrollment.scores.create!(final_score: 80.3)
expect(@enrollment.computed_final_grade).to eq 'B-'
end
it 'ignores grading period grades when passed no arguments' do
@enrollment.scores.create!(final_score: 80.3, grading_period: period)
expect(@enrollment.computed_final_grade).to be_nil
end
it 'ignores grades from soft-deleted scores' do
score = @enrollment.scores.create!(final_score: 80.3)
score.destroy
expect(@enrollment.computed_final_grade).to be_nil
end
it 'computes final grade for a given grading period id' do
8DE3
@enrollment.scores.create!(final_score: 80.3)
@enrollment.scores.create!(final_score: 70.6, grading_period: period)
final_grade = @enrollment.computed_final_grade(grading_period_id: period.id)
expect(final_grade).to eq 'C-'
end
it 'returns nil if a grading period grade is requested and does not exist' do
final_grade = @enrollment.computed_final_grade(grading_period_id: period.id)
expect(final_grade).to eq nil
end
end
context 'copying final scores on creation' do
before(:once) do
teacher = User.create!
@course.enroll_teacher(teacher, active_all: true)
assignment = @course.assignments.create!(points_possible: 100)
assignment.grade_student(@enrollment.user, grade: 95, grader: teacher)
# create this so the enrollment's current_score differs from its final_score
@course.assignments.create!(points_possible: 100)
end
it 'copies scores over from the user\'s existing student enrollments to new student enrollments' do
expect(new_student_enrollment.computed_final_score).to eq(@enrollment.computed_final_score)
end
it 'copies scores over from the user\'s existing fake student enrollments to new fake student enrollments' do
@enrollment.update!(type: 'StudentViewEnrollment')
expect(new_fake_student_enrollment.computed_final_score).to eq(@enrollment.computed_final_score)
end
it 'does not copy scores if the new enrollment type does not match existing enrollment types' do
expect(new_fake_student_enrollment.computed_final_score).to be_nil
end
it 'does not copy scores if the existing enrollment is soft-deleted' do
@enrollment.destroy
expect(new_student_enrollment.computed_final_score).to be_nil
end
end
end
end
it "should not allow an associated_user_id on a non-observer enrollment" do
observed = User.create!
@enrollment.type = 'ObserverEnrollment'
@enrollment.associated_user_id = observed.id
expect(@enrollment).to be_valid
@enrollment.type = 'StudentEnrollment'
expect(@enrollment).not_to be_valid
@enrollment.associated_user_id = nil
expect(@enrollment).to be_valid
end
it "should not allow an associated_user_id = user_id" do
observed = User.create!
@enrollment.type = 'ObserverEnrollment'
@enrollment.user_id = observed.id
@enrollment.associated_user_id = observed.id
expect(@enrollment).to_not be_valid
end
context "permissions" do
before(:once) do
course_with_student(:active_all => true)
end
it "should allow post_to_forum permission on a course if date is current" do
@enrollment.start_at = 2.days.ago
@enrollment.end_at = 4.days.from_now
@enrollment.workflow_state = 'active'
@enrollment.save!
expect(@enrollment.reload.state_based_on_date).to eq :active
expect(@course.grants_right?(@enrollment.user, :post_to_forum)).to eql(true)
end
it "should not allow post_to_forum permission on a course if date in future" do
@enrollment.start_at = 2.days.from_now
@enrollment.end_at = 4.days.from_now
@enrollment.workflow_state = 'active'
@enrollment.save!
expect(@enrollment.reload.state_based_on_date).to eq :accepted
expect(@course.grants_right?(@enrollment.user, :post_to_forum)).to eql(false)
end
it "should not allow read permission on a course if date inactive" do
@enrollment.start_at = 2.days.from_now
@enrollment.end_at = 4.days.from_now
@enrollment.workflow_state = 'active'
@enrollment.save!
@course.restrict_student_future_view = true
@course.save!
expect(@course.grants_right?(@enrollment.user, :read)).to eql(false)
# post to forum comes from role_override; inactive enrollments should not
# get any permissions form role_override
expect(@course.grants_right?(@enrollment.user, :post_to_forum)).to eql(false)
end
it "should not allow read permission on a course if explicitly inactive" do
@enrollment.workflow_state = 'inactive'
@enrollment.save!
expect(@course.grants_right?(@enrollment.user, :read)).to eql(false)
expect(@course.grants_right?(@enrollment.user, :post_to_forum)).to eql(false)
end
it "should allow read, but not post_to_forum on a course if date completed" do
@enrollment.start_at = 4.days.ago
@enrollment.end_at = 2.days.ago
@enrollment.workflow_state = 'active'
@enrollment.save!
expect(@course.grants_right?(@enrollment.user, :read)).to eql(true)
# post to forum comes from role_override; completed enrollments should not
# get any permissions form role_override
expect(@course.grants_right?(@enrollment.user, :post_to_forum)).to eql(false)
end
it "should allow read, but not post_to_forum on a course if explicitly completed" do
@enrollment.workflow_state = 'completed'
@enrollment.save!
expect(@course.grants_right?(@enrollment.user, :read)).to eql(true)
expect(@course.grants_right?(@enrollment.user, :post_to_forum)).to eql(false)
end
end
context "typed_enrollment" do
it "should allow StudentEnrollment" do
expect(Enrollment.typed_enrollment('StudentEnrollment')).to eql(StudentEnrollment)
end
it "should allow TeacherEnrollment" do
expect(Enrollment.typed_enrollment('TeacherEnrollment')).to eql(TeacherEnrollment)
end
it "should allow TaEnrollment" do
expect(Enrollment.typed_enrollment('TaEnrollment')).to eql(TaEnrollment)
end
it "should allow ObserverEnrollment" do
expect(Enrollment.typed_enrollment('ObserverEnrollment')).to eql(ObserverEnrollment)
end
it "should allow DesignerEnrollment" do
expect(Enrollment.typed_enrollment('DesignerEnrollment')).to eql(DesignerEnrollment)
end
it "should allow not NothingEnrollment" do
expect(Enrollment.typed_enrollment('NothingEnrollment')).to eql(nil)
end
end
context "drop scores" do
before(:once) do
course_with_teacher
course_with_student(course: @course)
@group = @course.assignment_groups.create!(:name => "some group", :group_weight => 50, :rules => "drop_lowest:1")
@assignment = @group.assignments.build(:title => "some assignments", :points_possible => 10)
@assignment.context = @course
@assignment.save!
@assignment2 = @group.assignments.build(:title => "some assignment 2", :points_possible => 40)
@assignment2.context = @course
@assignment2.save!
end
it "should drop high scores for groups when specified" do
@enrollment = @user.enrollments.first
@group.update_attribute(:rules, "drop_highest:1")
expect(@enrollment.reload.computed_current_score).to eql(nil)
@submission = @assignment.grade_student(@user, grade: "9", grader: @teacher)
expect(@submission[0].score).to eql(9.0)
expect(@enrollment.reload.computed_current_score).to eql(90.0)
@submission2 = @assignment2.grade_student(@user, grade: "20", grader: @teacher)
expect(@submission2[0].score).to eql(20.0)
expect(@enrollment.reload.computed_current_score).to eql(50.0)
@group.update_attribute(:rules, nil)
expect(@enrollment.reload.computed_current_score).to eql(58.0)
end
it "should drop low scores for groups when specified" do
@enrollment = @user.enrollments.first
expect(@enrollment.reload.computed_current_score).to eql(nil)
@submission = @assignment.grade_student(@user, grade: "9", grader: @teacher)
@submission2 = @assignment2.grade_student(@user, grade: "20", grader: @teacher)
expect(@submission2[0].score).to eql(20.0)
expect(@enrollment.reload.computed_current_score).to eql(90.0)
@group.update_attribute(:rules, "")
expect(@enrollment.reload.computed_current_score).to eql(58.0)
end
it "should not drop the last score for a group, even if the settings say it should be dropped" do
@enrollment = @user.enrollments.first
@group.update_attribute(:rules, "drop_lowest:2")
expect(@enrollment.reload.computed_current_score).to eql(nil)
@submission = @assignment.grade_student(@user, grade: "9", grader: @teacher)
expect(@submission[0].score).to eql(9.0)
expect(@enrollment.reload.computed_current_score).to eql(90.0)
@submission2 = @assignment2.grade_student(@user, grade: "20", grader: @teacher)
expect(@submission2[0].score).to eql(20.0)
expect(@enrollment.reload.computed_current_score).to eql(90.0)
end
end
context "notifications" do
it "should send out invitations if the course is already published" do
Notification.create!(:name => "Enrollment Registration")
course_with_teacher(:active_all => true)
user_with_pseudonym
e = @course.enroll_student(@user)
expect(e.messages_sent).to be_include("Enrollment Registration")
end
it "should not send out invitations immediately if the course restricts future viewing" do
Notification.create!(:name => "Enrollment Registration")
course_with_teacher(:active_all => true)
@course.restrict_student_future_view = true
@course.restrict_enrollments_to_course_dates = true
@course.start_at = 1.day.from_now
@course.conclude_at = 3.days.from_now
@course.save!
user_with_pseudonym
e = @course.enroll_student(@user)
expect(e).to be_inactive
expect(e.messages_sent).to_not include("Enrollment Registration")
Timecop.freeze(2.days.from_now) do
expect(e).to be_invited
e.any_instantiation.expects(:re_send_confirmation!).once
run_jobs
end
end
it "should not send out invitations to an observer if the student doesn't receive an invitation (e.g. sis import)" do
Notification.create!(:name => "Enrollment Registration", :category => "Registration")
course_with_teacher(:active_all => true)
student = user_with_pseudonym
observer = user_with_pseudonym
observer.observed_users << student
@course.enroll_student(student, :no_notify => true)
expect(student.messages).to be_empty
expect(observer.messages).to be_empty
course_with_teacher(:active_all => true)
@course.enroll_student(student)
student.reload
observer.reload
expect(student.messages).to_not be_empty
expect(observer.messages).to be_empty
end
it "should not send out invitations to an observer if the course is not published" do
Notification.create!(:name => "Enrollment Registration", :category => "Registration")
course_with_teacher
student = user_with_pseudonym
observer = user_with_pseudonym
observer.observed_users << student
@course.enroll_student(student)
expect(observer.messages).to be_empty
end
it "should not send out invitations if the course is not yet published" do
Notification.create!(:name => "Enrollment Registration")
course_with_teacher
user_with_pseudonym
e = @course.enroll_student(@user)
expect(e.messages_sent).not_to be_include("Enrollment Registration")
end
it "should send out invitations for previously-created enrollments when the course is published" do
n = Notification.create(:name => "Enrollment Registration", :category => "Registration")
course_with_teacher
user_with_pseudonym
e = @course.enroll_student(@user)
expect(e.messages_sent).not_to be_include("Enrollment Registration")
expect(@user.pseudonym).not_to be_nil
@course.offer
e.reload
expect(e).to be_invited
expect(e.user).not_to be_nil
expect(e.user.pseudonym).not_to be_nil
expect(Message.last).not_to be_nil
expect(Message.last.notification).to eql(n)
expect(Message.last.to).to eql(@user.email)
end
it "should send out notifications for enrollment acceptance correctly" do
teacher = user_with_pseudonym(:active_all => true)
n = Notification.create!(:name => "Enrollment Accepted")
NotificationPolicy.create!(:notification => n, :communication_channel => @user.communication_channel, :frequency => "immediately")
course_with_teacher(:active_all => true, :user => teacher)
student = user_factory
e = @course.enroll_student(student)
e.accept!
expect(teacher.messages).to be_exists
end
end
context "atom" do
it "should use the course and user name to derive a title" do
expect(@enrollment.to_atom.title).to eql("#{@enrollment.user.name} in #{@enrollment.course.name}")
end
it "should link to the enrollment" do
link_path = @enrollment.to_atom.links.first.to_s
expect(link_path).to eql("/courses/#{@enrollment.course.id}/enrollments/#{@enrollment.id}")
end
end
context "permissions" do
it "should grant read rights to account members with the ability to read_roster" do
role = Role.get_built_in_role("AccountMembership")
user = account_admin_user(:role => role)
RoleOverride.create!(:context => Account.default, :permission => :read_roster,
:role => role, :enabled => true)
@enrollment.save
expect(@enrollment.user.grants_right?(user, :read)).to eq false
expect(@enrollment.grants_right?(user, :read)).to eq true
end
it "should be able to read grades if the course grants management rights to the enrollment" do
@new_user = user_model
@enrollment.save
expect(@enrollment.grants_right?(@new_user, :read_grades)).to be_falsey
@course.enroll_teacher(@new_user)
@enrollment.reload
AdheresToPolicy::Cache.clear
expect(@enrollment.grants_right?(@user, :read_grades)).to be_truthy
end
it "should allow the user itself to read its own grades" do
expect(@enrollment.grants_right?(@user, :read_grades)).to be_truthy
end
end
context "recompute_final_score_if_stale" do
before(:once) { course_with_student }
it "should only call recompute_final_score once within the cache window" do
Enrollment.expects(:recompute_final_score).once
enable_cache do
Enrollment.recompute_final_score_if_stale @course
Enrollment.recompute_final_score_if_stale @course
end
end
it "should yield iff it calls recompute_final_score" do
Enrollment.expects(:recompute_final_score).once
count = 1
enable_cache do
Enrollment.recompute_final_score_if_stale(@course, @user){ count += 1 }
Enrollment.recompute_final_score_if_stale(@course, @user){ count += 1 }
end
expect(count).to eql 2
end
end
context "recompute_final_score_in_singleton" do
before(:once) { course_with_student }
it "should raise an exception if called with more than one user" do
expect { Enrollment.recompute_final_score_in_singleton([@user.id, 5], @course.id) }.
to raise_error(ArgumentError)
end
it "sends later for a single student" do
expect(Enrollment).to receive(:send_later_if_production_enqueue_args).with(
:recompute_final_score,
hash_including(singleton: "Enrollment.recompute_final_score:#{@user.id}:#{@course.id}:"),
@user.id, @course.id, {}
)
Enrollment.recompute_final_score_in_singleton(@user.id, @course.id)
end
end
context "recompute_final_scores" do
it "should only recompute once per student, per course" do
course_with_student(:active_all => true)
@c1 = @course
@s2 = @course.course_sections.create!(:name => 's2')
@course.enroll_student(@user, :section => @s2, :allow_multiple_enrollments => true)
expect(@user.student_enrollments.reload.count).to eq 2
course_with_student(:user => @user)
@c2 = @course
Enrollment.expects(:recompute_final_score).with(@user.id, @c1.id, {})
Enrollment.expects(:recompute_final_score).with(@user.id, @c2.id, {})
Enrollment.recompute_final_scores(@user.id)
end
end
context "date restrictions" do
context "accept" do
def enrollment_availability_test
@enrollment.start_at = 2.days.ago
@enrollment.end_at = 2.days.from_now
@enrollment.workflow_state = 'invited'
@enrollment.save!
expect(@enrollment.state).to eql(:invited)
expect(@enrollment.state_based_on_date).to eql(:invited)
@enrollment.accept
expect(@enrollment.reload.state).to eql(:active)
expect(@enrollment.state_based_on_date).to eql(:active)
@enrollment.start_at = 4.days.ago
@enrollment.end_at = 2.days.ago
@enrollment.workflow_state = 'invited'
@enrollment.save!
expect(@enrollment.reload.state).to eql(:invited)
expect(@enrollment.state_based_on_date).to eql(:completed)
expect(@enrollment.accept).to be_falsey
@enrollment.start_at = 2.days.from_now
@enrollment.end_at = 4.days.from_now
@enrollment.save!
expect(@enrollment.reload.state).to eql(:invited)
if @enrollment.admin?
expect(@enrollment.state_based_on_date).to eq(:inactive)
else
expect(@enrollment.state_based_on_date).to eql(:invited)
expect(@enrollment.accept).to be_truthy
end
end
def course_section_availability_test(should_be_invited=false)
@section = @course.course_sections.first
expect(@section).not_to be_nil
@enrollment.course_section = @section
@enrollment.workflow_state = 'invited'
@enrollment.save!
@section.start_at = 2.days.ago
@section.end_at = 2.days.from_now
@section.restrict_enrollments_to_section_dates = true
@section.save!
expect(@enrollment.state).to eql(:invited)
@enrollment.accept
expect(@enrollment.reload.state).to eql(:active)
expect(@enrollment.state_based_on_date).to eql(:active)
@section.start_at = 4.days.ago
@section.end_at = 2.days.ago
@section.save!
@enrollment.workflow_state = 'invited'
@enrollment.save!
expect(@enrollment.reload.state).to eql(:invited)
if should_be_invited
expect(@enrollment.state_based_on_date).to eql(:invited)
expect(@enrollment.accept).to be_truthy
else
expect(@enrollment.state_based_on_date).to eql(:completed)
expect(@enrollment.accept).to be_falsey
end
@section.start_at = 2.days.from_now
@section.end_at = 4.days.from_now
@section.save!
@enrollment.save!
@enrollment.reload
if should_be_invited
expect(@enrollment.state).to eql(:active)
expect(@enrollment.state_based_on_date).to eql(:active)
else
expect(@enrollment.state).to eql(:invited)
expect(@enrollment.state_based_on_date).to eql(:invited)
expect(@enrollment.accept).to be_truthy
end
end
def course_availability_test(state_based_state)
@course.start_at = 2.days.ago
@course.conclude_at = 2.days.from_now
@course.restrict_enrollments_to_course_dates = true
@course.save!
@enrollment.workflow_state = 'invited'
@enrollment.save!
expect(@enrollment.state).to eql(:invited)
@enrollment.accept
expect(@enrollment.reload.state).to eql(:active)
expect(@enrollment.state_based_on_date).to eql(:active)
@course.start_at = 4.days.ago
@course.conclude_at = 2.days.ago
@course.save!
@enrollment.workflow_state = 'invited'
@enrollment.save!
expect(@enrollment.state).to eql(:invited)
@enrollment.accept if @enrollment.invited?
expect(@enrollment.state_based_on_date).to eql(state_based_state)
@course.start_at = 2.days.from_now
@course.conclude_at = 4.days.from_now
@course.save!
@enrollment.workflow_state = 'invited'
@enrollment.save!
@enrollment.reload
expect(@enrollment.state).to eql(:invited)
expect(@enrollment.state_based_on_date).to eql(:invited)
expect(@enrollment.accept).to be_truthy
@course.complete!
expect(@enrollment.reload.state).to eql(:completed)
expect(@enrollment.state_based_on_date).to eql(:completed)
@enrollment.workflow_state = 'active'
@enrollment.save!
expect(@enrollment.state_based_on_date).to eql(:completed)
end
def enrollment_term_availability_test
@term = @course.enrollment_term
expect(@term).not_to be_nil
@term.start_at = 2.days.ago
@term.end_at = 2.days.from_now
@term.save!
@enrollment.workflow_state = 'invited'
@enrollment.save!
expect(@enrollment.state).to eql(:invited)
expect(@enrollment.state_based_on_date).to eql(:invited)
@enrollment.accept
expect(@enrollment.reload.state).to eql(:active)
expect(@enrollment.state_based_on_date).to eql(:active)
@term.start_at = 4.days.ago
@term.end_at = 2.days.ago
@term.save!
@enrollment.workflow_state = 'invited'
@enrollment.save!
expect(@enrollment.state).to eql(:invited)
expect(@enrollment.state_based_on_date).to eql(:completed)
expect(@enrollment.accept).to be_falsey
@term.start_at = 2.days.from_now
@term.end_at = 4.days.from_now
@term.save!
@enrollment.reload
expect(@enrollment.state).to eql(:invited)
expect(@enrollment.state_based_on_date).to eql(:invited)
expect(@enrollment.accept).to be_truthy
expect(@enrollment.reload.state_based_on_date).to eql(@enrollment.admin? ? :active : :accepted)
end
def enrollment_dates_override_test
@term = @course.enrollment_term
expect(@term).not_to be_nil
@term.save!
@override = @term.enrollment_dates_overrides.create!(:enrollment_type => @enrollment.type, :enrollment_term => @term)
@override.start_at = 2.days.ago
@override.end_at = 2.days.from_now
@override.save!
@enrollment.workflow_state = 'invited'
@enrollment.save!
expect(@enrollment.state).to eql(:invited)
@enrollment.accept
expect(@enrollment.reload.state).to eql(:active)
expect(@enrollment.state_based_on_date).to eql(:active)
@override.start_at = 4.days.ago
@override.end_at = 2.days.ago
@override.save!
@enrollment.workflow_state = 'invited'
@enrollment.save!
expect(@enrollment.state).to eql(:invited)
expect(@enrollment.state_based_on_date).to eql(:completed)
@override.start_at = 2.days.from_now
@override.end_at = 4.days.from_now
@override.save!
@enrollment.workflow_state = 'invited'
@enrollment.save!
@enrollment.reload
expect(@enrollment.state).to eql(:invited)
if @enrollment.admin?