forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse_spec.rb
More file actions
4900 lines (4219 loc) · 206 KB
/
Copy pathcourse_spec.rb
File metadata and controls
4900 lines (4219 loc) · 206 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 - 2015 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require File.expand_path(File.dirname(__FILE__) + '/../sharding_spec_helper.rb')
require 'csv'
require 'socket'
describe Course do
before :once do
Account.default
Account.default.default_enrollment_term
end
before :each do
@course = Account.default.courses.build
@course.workflow_state = 'claimed'
@course.root_account = Account.default
@course.enrollment_term = Account.default.default_enrollment_term
end
it "should properly determine if group weights are active" do
@course.update_attribute(:group_weighting_scheme, nil)
expect(@course.apply_group_weights?).to eq false
@course.update_attribute(:group_weighting_scheme, 'equal')
expect(@course.apply_group_weights?).to eq false
@course.update_attribute(:group_weighting_scheme, 'percent')
expect(@course.apply_group_weights?).to eq true
end
it "should return course visibility flag" do
@course.update_attribute(:is_public, nil)
@course.update_attribute(:is_public_to_auth_users, nil)
expect(@course.course_visibility).to eq('course')
@course.update_attribute(:is_public, nil)
@course.update_attribute(:is_public_to_auth_users, nil)
@course.update_attribute(:is_public_to_auth_users, true)
expect(@course.course_visibility).to eq('institution')
@course.update_attribute(:is_public, true)
expect(@course.course_visibility).to eq('public')
end
it "should return syllabus visibility flag" do
@course.update_attribute(:public_syllabus, nil)
@course.update_attribute(:public_syllabus_to_auth, nil)
expect(@course.syllabus_visibility_option).to eq('course')
@course.update_attribute(:public_syllabus, nil)
@course.update_attribute(:public_syllabus_to_auth, true)
expect(@course.syllabus_visibility_option).to eq('institution')
@course.update_attribute(:public_syllabus, true)
expect(@course.syllabus_visibility_option).to eq('public')
end
it 'should return offline web export flag' do
expect(@course.enable_offline_web_export?).to eq false
account = Account.default
account.settings[:enable_offline_web_export] = true
account.save
expect(@course.enable_offline_web_export?).to eq true
@course.update_attribute(:enable_offline_web_export, false)
expect(@course.enable_offline_web_export?).to eq false
end
describe "soft-concluded?" do
before :once do
@term = Account.default.enrollment_terms.create!
end
before :each do
@course.enrollment_term = @term
end
context "without term end date" do
it "should know if it has been soft-concluded" do
@course.update_attributes({:conclude_at => nil, :restrict_enrollments_to_course_dates => true })
expect(@course).not_to be_soft_concluded
@course.update_attribute(:conclude_at, 1.week.from_now)
expect(@course).not_to be_soft_concluded
@course.update_attribute(:conclude_at, 1.week.ago)
expect(@course).to be_soft_concluded
end
end
context "with term end date in the past" do
before do
@course.enrollment_term.update_attribute(:end_at, 1.week.ago)
end
it "should know if it has been soft-concluded" do
@course.update_attributes({:conclude_at => nil, :restrict_enrollments_to_course_dates => true })
expect(@course).to be_soft_concluded
@course.update_attribute(:conclude_at, 1.week.from_now)
expect(@course).not_to be_soft_concluded
@course.update_attribute(:conclude_at, 1.week.ago)
expect(@course).to be_soft_concluded
end
end
context "with term end date in the future" do
before do
@course.enrollment_term.update_attribute(:end_at, 1.week.from_now)
end
it "should know if it has been soft-concluded" do
@course.update_attributes({:conclude_at => nil, :restrict_enrollments_to_course_dates => true })
expect(@course).not_to be_soft_concluded
@course.update_attribute(:conclude_at, 1.week.from_now)
expect(@course).not_to be_soft_concluded
@course.update_attribute(:conclude_at, 1.week.ago)
expect(@course).to be_soft_concluded
end
end
context "with coures dates not overriding term dates" do
before do
@course.update_attribute(:conclude_at, 1.week.from_now)
end
it "should ignore course dates if not set to override term dates when calculating soft-concluded state" do
@course.enrollment_term.update_attribute(:end_at, nil)
expect(@course).not_to be_soft_concluded
@course.enrollment_term.update_attribute(:end_at, 1.week.from_now)
expect(@course).not_to be_soft_concluded
@course.enrollment_term.update_attribute(:end_at, 1.week.ago)
expect(@course).to be_soft_concluded
end
end
it "should test conclusion for a specific enrollment type" do
@term.set_overrides(@course.account, 'StudentEnrollment' => {end_at: 1.week.ago})
expect(@course).not_to be_soft_concluded
expect(@course).not_to be_concluded
expect(@course).to be_soft_concluded('StudentEnrollment')
expect(@course).to be_concluded('StudentEnrollment')
end
end
describe "allow_student_discussion_topics" do
it "should default true" do
expect(@course.allow_student_discussion_topics).to eq true
end
it "should set and get" do
@course.allow_student_discussion_topics = false
@course.save!
expect(@course.allow_student_discussion_topics).to eq false
end
end
describe "#grading_periods?" do
it "should return true if course has grading periods" do
@course.save!
Factories::GradingPeriodGroupHelper.new.legacy_create_for_course(@course)
expect(@course.grading_periods?).to be true
end
it "should return true if account has grading periods for course term" do
@course.save!
group = Factories::GradingPeriodGroupHelper.new.create_for_account(@course.root_account)
group.enrollment_terms << @course.enrollment_term
expect(@course.grading_periods?).to be true
end
it "should return false if account has grading periods without course term" do
@course.save!
Factories::GradingPeriodGroupHelper.new.create_for_account(@course.root_account)
expect(@course.grading_periods?).to be false
end
it "should return false if neither course nor account have grading periods" do
expect(@course.grading_periods?).to be false
end
end
describe "#weighted_grading_periods?" do
it "returns false if course has legacy grading periods" do
@course.save!
account_group = Factories::GradingPeriodGroupHelper.new.create_for_account(@course.root_account)
account_group.enrollment_terms << @course.enrollment_term
group = Factories::GradingPeriodGroupHelper.new.legacy_create_for_course(@course)
group.weighted = true
group.save!
expect(@course.weighted_grading_periods?).to be false
end
it "returns false if account has unweighted grading periods for course term" do
@course.save!
group = Factories::GradingPeriodGroupHelper.new.create_for_account(@course.root_account)
group.enrollment_terms << @course.enrollment_term
expect(@course.weighted_grading_periods?).to be false
end
it "returns false if account has weighted grading periods without course term" do
@course.save!
group = Factories::GradingPeriodGroupHelper.new.create_for_account(@course.root_account)
group.weighted = true
group.save!
expect(@course.weighted_grading_periods?).to be false
end
it "returns true if account has weighted grading periods for course term" do
@course.save!
legacy_group = Factories::GradingPeriodGroupHelper.new.legacy_create_for_course(@course)
legacy_group.destroy
group = Factories::GradingPeriodGroupHelper.new.create_for_account(@course.root_account)
group.enrollment_terms << @course.enrollment_term
group.weighted = true
group.save!
expect(@course.weighted_grading_periods?).to be true
end
end
describe '#display_totals_for_all_grading_periods?' do
before do
@course.save!
end
it 'returns false for a course without an associated grading period group' do
expect(@course).not_to be_display_totals_for_all_grading_periods
end
it 'returns false for a course with an associated grading period group that is soft-deleted' do
group = Factories::GradingPeriodGroupHelper.new.create_for_account(@course.root_account)
group.enrollment_terms << @course.enrollment_term
group.update!(display_totals_for_all_grading_periods: true)
group.destroy
expect(@course).not_to be_display_totals_for_all_grading_periods
end
it 'returns true if the associated grading period group has the setting enabled' do
group = Factories::GradingPeriodGroupHelper.new.create_for_account(@course.root_account)
group.enrollment_terms << @course.enrollment_term
group.update!(display_totals_for_all_grading_periods: true)
expect(@course).to be_display_totals_for_all_grading_periods
end
it 'returns false if the associated grading period group has the setting disabled' do
group = Factories::GradingPeriodGroupHelper.new.create_for_account(@course.root_account)
group.enrollment_terms << @course.enrollment_term
expect(@course).not_to be_display_totals_for_all_grading_periods
end
context 'legacy grading periods support' do
before do
@group = Factories::GradingPeriodGroupHelper.new.legacy_create_for_course(@course)
end
it 'returns true if the associated grading period group has the setting enabled' do
@group.update!(display_totals_for_all_grading_periods: true)
expect(@course).to be_display_totals_for_all_grading_periods
end
it 'returns false if the associated grading period group has the setting disabled' do
expect(@course).not_to be_display_totals_for_all_grading_periods
end
it 'returns false for a course with an associated grading period group that is soft-deleted' do
@group.update!(display_totals_for_all_grading_periods: true)
@group.destroy
expect(@course).not_to be_display_totals_for_all_grading_periods
end
end
end
describe "#time_zone" do
it "should use provided value when set, regardless of root account setting" do
@root_account = Account.default
@root_account.default_time_zone = 'America/Chicago'
@course.time_zone = 'America/New_York'
expect(@course.time_zone).to eq ActiveSupport::TimeZone['Eastern Time (US & Canada)']
end
it "should default to root account value if not set" do
@root_account = Account.default
@root_account.default_time_zone = 'America/Chicago'
expect(@course.time_zone).to eq ActiveSupport::TimeZone['Central Time (US & Canada)']
end
end
context "validation" do
it "should create a new instance given valid attributes" do
course_model
end
it "should require unique sis_source_id" do
other_course = course_factory
other_course.sis_source_id = "sisid"
other_course.save!
new_course = course_factory
new_course.sis_source_id = other_course.sis_source_id
expect(new_course).to_not be_valid
new_course.sis_source_id = nil
expect(new_course).to be_valid
end
it "should require unique integration_id" do
other_course = course_factory
other_course.integration_id = "intid"
other_course.save!
new_course = course_factory
new_course.integration_id = other_course.integration_id
expect(new_course).to_not be_valid
new_course.integration_id = nil
expect(new_course).to be_valid
end
end
it "should create a unique course." do
@course = Course.create_unique
expect(@course.name).to eql("My Course")
@uuid = @course.uuid
@course2 = Course.create_unique(@uuid)
expect(@course).to eql(@course2)
end
it "should only change the course code using the course name if the code is nil or empty" do
@course = Course.create_unique
code = @course.course_code
@course.name = 'test123'
@course.save
expect(code).to eql(@course.course_code)
@course.course_code = nil
@course.save
expect(code).to_not eql(@course.course_code)
end
it "should throw error for long sis id" do
#should throw rails validation error instead of db invalid statement error
@course = Course.create_unique
@course.sis_source_id = 'qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm'
expect(lambda {@course.save!}).to raise_error("Validation failed: Sis source is too long (maximum is 255 characters)")
end
it "should always have a uuid, if it was created" do
@course.save!
expect(@course.uuid).not_to be_nil
end
context "permissions" do
def clear_permissions_cache
@course.clear_permissions_cache(@teacher)
@course.clear_permissions_cache(@designer)
@course.clear_permissions_cache(@ta)
@course.clear_permissions_cache(@admin1)
@course.clear_permissions_cache(@admin2)
end
it "should follow account chain when looking for generic permissions from AccountUsers" do
account = Account.create!
sub_account = Account.create!(:parent_account => account)
sub_sub_account = Account.create!(:parent_account => sub_account)
user = account_admin_user(:account => sub_account)
course = Course.create!(:account => sub_sub_account)
expect(course.grants_right?(user, :manage)).to be_truthy
end
# we have to reload the users after each course change here to catch the
# enrollment changes that are applied directly to the db with update_all
it "should grant delete to the proper individuals" do
@role1 = custom_account_role('managecourses', :account => Account.default)
@role2 = custom_account_role('managesis', :account => Account.default)
account_admin_user_with_role_changes(:role => @role1, :role_changes => {:manage_courses => true, :change_course_state => true})
@admin1 = @admin
account_admin_user_with_role_changes(:role => @role2, :role_changes => {:manage_sis => true, :change_course_state => true})
@admin2 = @admin
course_with_teacher(:active_all => true)
@designer = user_factory(active_all: true)
@course.enroll_designer(@designer).accept!
@ta = user_factory(active_all: true)
@course.enroll_ta(@ta).accept!
# active, non-sis course
expect(@course.grants_right?(@teacher, :delete)).to be_truthy
expect(@course.grants_right?(@designer, :delete)).to be_truthy
expect(@course.grants_right?(@ta, :delete)).to be_falsey
expect(@course.grants_right?(@admin1, :delete)).to be_truthy
expect(@course.grants_right?(@admin2, :delete)).to be_falsey
# active, sis course
@course.sis_source_id = 'sis_id'
@course.save!
[@course, @teacher, @designer, @ta, @admin1, @admin2].each(&:reload)
clear_permissions_cache
expect(@course.grants_right?(@teacher, :delete)).to be_falsey
expect(@course.grants_right?(@designer, :delete)).to be_falsey
expect(@course.grants_right?(@ta, :delete)).to be_falsey
expect(@course.grants_right?(@admin1, :delete)).to be_truthy
expect(@course.grants_right?(@admin2, :delete)).to be_truthy
# completed, non-sis course
@course.sis_source_id = nil
@course.complete!
[@course, @teacher, @designer, @ta, @admin1, @admin2].each(&:reload)
clear_permissions_cache
expect(@course.grants_right?(@teacher, :delete)).to be_truthy
expect(@course.grants_right?(@designer, :delete)).to be_truthy
expect(@course.grants_right?(@ta, :delete)).to be_falsey
expect(@course.grants_right?(@admin1, :delete)).to be_truthy
expect(@course.grants_right?(@admin2, :delete)).to be_falsey
@course.clear_permissions_cache(@user)
# completed, sis course
@course.sis_source_id = 'sis_id'
@course.save!
[@course, @teacher, @designer, @ta, @admin1, @admin2].each(&:reload)
clear_permissions_cache
expect(@course.grants_right?(@teacher, :delete)).to be_falsey
expect(@course.grants_right?(@designer, :delete)).to be_falsey
expect(@course.grants_right?(@ta, :delete)).to be_falsey
expect(@course.grants_right?(@admin1, :delete)).to be_truthy
expect(@course.grants_right?(@admin2, :delete)).to be_truthy
end
it "should not grant delete to anyone without :change_course_state rights" do
@role1 = custom_account_role('managecourses', :account => Account.default)
@role2 = custom_account_role('managesis', :account => Account.default)
account_admin_user_with_role_changes(:role => @role1, :role_changes => {:manage_courses => true})
@admin1 = @admin
account_admin_user_with_role_changes(:role => @role2, :role_changes => {:manage_sis => true})
@admin2 = @admin
course_with_teacher(:active_all => true)
@designer = user_factory(active_all: true)
@course.enroll_designer(@designer).accept!
Account.default.role_overrides.create!(:role => teacher_role, :permission => :change_course_state, :enabled => false)
Account.default.role_overrides.create!(:role => designer_role, :permission => :change_course_state, :enabled => false)
# active, non-sis course
expect(@course.grants_right?(@teacher, :delete)).to be_falsey
expect(@course.grants_right?(@designer, :delete)).to be_falsey
expect(@course.grants_right?(@admin1, :delete)).to be_falsey
expect(@course.grants_right?(@admin2, :delete)).to be_falsey
# active, sis course
@course.sis_source_id = 'sis_id'
@course.save!
[@course, @teacher, @designer, @admin1, @admin2].each(&:reload)
clear_permissions_cache
expect(@course.grants_right?(@teacher, :delete)).to be_falsey
expect(@course.grants_right?(@designer, :delete)).to be_falsey
expect(@course.grants_right?(@admin1, :delete)).to be_falsey
expect(@course.grants_right?(@admin2, :delete)).to be_falsey
# completed, non-sis course
@course.sis_source_id = nil
@course.complete!
[@course, @teacher, @designer, @admin1, @admin2].each(&:reload)
clear_permissions_cache
expect(@course.grants_right?(@teacher, :delete)).to be_falsey
expect(@course.grants_right?(@designer, :delete)).to be_falsey
expect(@course.grants_right?(@admin1, :delete)).to be_falsey
expect(@course.grants_right?(@admin2, :delete)).to be_falsey
@course.clear_permissions_cache(@user)
# completed, sis course
@course.sis_source_id = 'sis_id'
@course.save!
[@course, @teacher, @designer, @admin1, @admin2].each(&:reload)
clear_permissions_cache
expect(@course.grants_right?(@teacher, :delete)).to be_falsey
expect(@course.grants_right?(@designer, :delete)).to be_falsey
expect(@course.grants_right?(@admin1, :delete)).to be_falsey
expect(@course.grants_right?(@admin2, :delete)).to be_falsey
end
it "should grant reset_content to the proper individuals" do
@role1 = custom_account_role('managecourses', :account => Account.default)
@role2 = custom_account_role('managesis', :account => Account.default)
account_admin_user_with_role_changes(:role => @role1, :role_changes => {:manage_courses => true})
@admin1 = @admin
account_admin_user_with_role_changes(:role => @role2, :role_changes => {:manage_sis => true})
@admin2 = @admin
course_with_teacher(:active_all => true)
@designer = user_factory(active_all: true)
@course.enroll_designer(@designer).accept!
@ta = user_factory(active_all: true)
@course.enroll_ta(@ta).accept!
# active, non-sis course
clear_permissions_cache
expect(@course.grants_right?(@teacher, :reset_content)).to be_truthy
expect(@course.grants_right?(@designer, :reset_content)).to be_truthy
expect(@course.grants_right?(@ta, :reset_content)).to be_falsey
expect(@course.grants_right?(@admin1, :reset_content)).to be_truthy
expect(@course.grants_right?(@admin2, :reset_content)).to be_falsey
# active, sis course
@course.sis_source_id = 'sis_id'
@course.save!
[@course, @teacher, @designer, @ta, @admin1, @admin2].each(&:reload)
clear_permissions_cache
expect(@course.grants_right?(@teacher, :reset_content)).to be_truthy
expect(@course.grants_right?(@designer, :reset_content)).to be_truthy
expect(@course.grants_right?(@ta, :reset_content)).to be_falsey
expect(@course.grants_right?(@admin1, :reset_content)).to be_truthy
expect(@course.grants_right?(@admin2, :reset_content)).to be_falsey
# completed, non-sis course
@course.sis_source_id = nil
@course.complete!
[@course, @teacher, @designer, @ta, @admin1, @admin2].each(&:reload)
clear_permissions_cache
expect(@course.grants_right?(@teacher, :reset_content)).to be_falsey
expect(@course.grants_right?(@designer, :reset_content)).to be_falsey
expect(@course.grants_right?(@ta, :reset_content)).to be_falsey
expect(@course.grants_right?(@admin1, :reset_content)).to be_truthy
expect(@course.grants_right?(@admin2, :reset_content)).to be_falsey
# completed, sis course
@course.sis_source_id = 'sis_id'
@course.save!
[@course, @teacher, @designer, @ta, @admin1, @admin2].each(&:reload)
clear_permissions_cache
expect(@course.grants_right?(@teacher, :reset_content)).to be_falsey
expect(@course.grants_right?(@designer, :reset_content)).to be_falsey
expect(@course.grants_right?(@ta, :reset_content)).to be_falsey
expect(@course.grants_right?(@admin1, :reset_content)).to be_truthy
expect(@course.grants_right?(@admin2, :reset_content)).to be_falsey
end
it "should grant create_tool_manually to the proper individuals" do
course_with_teacher(:active_all => true)
@teacher = user_factory(active_all: true)
@course.enroll_teacher(@teacher).accept!
@ta = user_factory(active_all: true)
@course.enroll_ta(@ta).accept!
@designer = user_factory(active_all: true)
@course.enroll_designer(@designer).accept!
@student = user_factory(active_all: true)
@course.enroll_student(@student).accept!
clear_permissions_cache
expect(@course.grants_right?(@teacher, :create_tool_manually)).to be_truthy
expect(@course.grants_right?(@ta, :create_tool_manually)).to be_truthy
expect(@course.grants_right?(@designer, :create_tool_manually)).to be_truthy
expect(@course.grants_right?(@student, :create_tool_manually)).to be_falsey
end
def make_date_completed
@enrollment.reload
@enrollment.start_at = 4.days.ago
@enrollment.end_at = 2.days.ago
@enrollment.save!
@enrollment.reload
expect(@enrollment.state_based_on_date).to eq :completed
end
context "as a teacher" do
let_once :c do
course_with_teacher(:active_all => 1)
@course
end
it "should grant read_as_admin and read_forum to date-completed teacher" do
make_date_completed
expect(c.prior_enrollments).to eq []
expect(c.grants_right?(@teacher, :read_as_admin)).to be_truthy
expect(c.grants_right?(@teacher, :read_forum)).to be_truthy
end
it "should grant read_as_admin and read to date-completed teacher of unpublished course" do
course_factory.update_attribute(:workflow_state, 'claimed')
make_date_completed
expect(c.prior_enrollments).to eq []
expect(c.grants_right?(@teacher, :read_as_admin)).to be_truthy
expect(c.grants_right?(@teacher, :read)).to be_truthy
end
it "should grant :read_outcomes to teachers in the course" do
expect(c.grants_right?(@teacher, :read_outcomes)).to be_truthy
end
end
context "as a designer" do
let_once :c do
course_factory(active_all: true)
@designer = user_factory(active_all: true)
@enrollment = @course.enroll_designer(@designer)
@enrollment.accept!
@course
end
it "should grant read_as_admin, read, manage, and update to date-active designer" do
expect(c.grants_right?(@designer, :read_as_admin)).to be_truthy
expect(c.grants_right?(@designer, :read)).to be_truthy
expect(c.grants_right?(@designer, :manage)).to be_truthy
expect(c.grants_right?(@designer, :update)).to be_truthy
end
it "should grant permissions for unpublished courses" do
c.claim!
expect(c.grants_right?(@designer, :read_as_admin)).to be_truthy
expect(c.grants_right?(@designer, :read)).to be_truthy
expect(c.grants_right?(@designer, :manage)).to be_truthy
expect(c.grants_right?(@designer, :update)).to be_truthy
expect(c.grants_right?(@designer, :read_roster)).to be_truthy
end
it "should grant read_as_admin, read_roster, and read_prior_roster to date-completed designer" do
@enrollment.start_at = 4.days.ago
@enrollment.end_at = 2.days.ago
@enrollment.save!
expect(@enrollment.reload.state_based_on_date).to eq :completed
expect(c.prior_enrollments).to eq []
expect(c.grants_right?(@designer, :read_as_admin)).to be_truthy
expect(c.grants_right?(@designer, :read_roster)).to be_truthy
expect(c.grants_right?(@designer, :read_prior_roster)).to be_truthy
end
it "should grant be able to disable read_roster to date-completed designer" do
Account.default.role_overrides.create!(:permission => :read_roster, :role => designer_role, :enabled => false)
@enrollment.start_at = 4.days.ago
@enrollment.end_at = 2.days.ago
@enrollment.save!
expect(@enrollment.reload.state_based_on_date).to eq :completed
expect(c.prior_enrollments).to eq []
expect(c.grants_right?(@designer, :read_as_admin)).to be_truthy
expect(c.grants_right?(@designer, :read_roster)).to be_falsey
expect(c.grants_right?(@designer, :read_prior_roster)).to be_falsey
end
it "should grant read_as_admin and read to date-completed designer of unpublished course" do
c.update_attribute(:workflow_state, 'claimed')
make_date_completed
expect(c.prior_enrollments).to eq []
expect(c.grants_right?(@designer, :read_as_admin)).to be_truthy
expect(c.grants_right?(@designer, :read)).to be_truthy
end
it "should not grant read_user_notes or view_all_grades to designer" do
expect(c.grants_right?(@designer, :read_user_notes)).to be_falsey
expect(c.grants_right?(@designer, :view_all_grades)).to be_falsey
end
end
context "as a 'student view' student" do
it "should grant read rights for unpublished courses" do
course_factory
test_student = @course.student_view_student
expect(@course.grants_right?(test_student, :read)).to be_truthy
expect(@course.grants_right?(test_student, :read_grades)).to be_truthy
expect(@course.grants_right?(test_student, :read_forum)).to be_truthy
end
end
context "as a student" do
let_once :c do
course_with_student(:active_user => 1)
@course
end
it "should grant read_grades read_forum to date-completed student" do
c.offer!
make_date_completed
expect(c.prior_enrollments).to eq []
expect(c.grants_right?(@student, :read_grades)).to be_truthy
expect(c.grants_right?(@student, :read_forum)).to be_truthy
end
it "should not grant read_forum to date-completed student if disabled by role override" do
c.root_account.role_overrides.create!(:role => student_role, :permission => :read_forum, :enabled => false)
c.offer!
make_date_completed
expect(c.prior_enrollments).to eq []
expect(c.grants_right?(@student, :read_forum)).to be_falsey
end
it "should not grant permissions to active students of an unpublished course" do
expect(c).to be_created
@enrollment.update_attribute(:workflow_state, 'active')
expect(c.grants_right?(@student, :read)).to be_falsey
expect(c.grants_right?(@student, :read_grades)).to be_falsey
expect(c.grants_right?(@student, :read_forum)).to be_falsey
end
it "should not grant read to completed students of an unpublished course" do
expect(c).to be_created
@enrollment.update_attribute(:workflow_state, 'completed')
expect(@enrollment).to be_completed
expect(c.grants_right?(@student, :read)).to be_falsey
end
it "should not grant read to soft-completed students of an unpublished course" do
c.restrict_enrollments_to_course_dates = true
c.start_at = 4.days.ago
c.conclude_at = 2.days.ago
c.save!
expect(c).to be_created
@enrollment.update_attribute(:workflow_state, 'active')
expect(@enrollment.state_based_on_date).to eq :completed
expect(c.grants_right?(@student, :read)).to be_falsey
end
it "should grant :read_outcomes to students in the course" do
c.offer!
expect(c.grants_right?(@student, :read_outcomes)).to be_truthy
end
end
context "as an admin" do
it "should grant :read_outcomes to account admins" do
course_factory(active_all: true)
account_admin_user(:account => @course.account)
expect(@course.grants_right?(@admin, :read_outcomes)).to be_truthy
end
end
end
describe "#reset_content" do
before do :once
course_with_student
end
it "should clear content" do
@course.root_account.allow_self_enrollment!
@course.discussion_topics.create!
@course.quizzes.create!
@course.assignments.create!
@course.wiki.set_front_page_url!('front-page')
@course.wiki.front_page.save!
@course.self_enrollment = true
@course.sis_source_id = 'sis_id'
@course.lti_context_id = 'lti_context_id'
@course.stuck_sis_fields = [].to_set
profile = @course.profile
profile.description = "description"
profile.save!
@course.save!
@course.reload
expect(@course.course_sections).not_to be_empty
expect(@course.students).to eq [@student]
expect(@course.stuck_sis_fields).to eq [].to_set
self_enrollment_code = @course.self_enrollment_code
expect(self_enrollment_code).not_to be_nil
@new_course = @course.reset_content
@course.reload
expect(@course.stuck_sis_fields).to eq [:workflow_state].to_set
expect(@course.course_sections).to be_empty
expect(@course.students).to be_empty
expect(@course.sis_source_id).to be_nil
expect(@course.self_enrollment_code).to be_nil
expect(@course.lti_context_id).not_to be_nil
@new_course.reload
expect(@new_course).to be_created
expect(@new_course.course_sections).not_to be_empty
expect(@new_course.students).to eq [@student]
expect(@new_course.discussion_topics).to be_empty
expect(@new_course.quizzes).to be_empty
expect(@new_course.assignments).to be_empty
expect(@new_course.sis_source_id).to eq 'sis_id'
expect(@new_course.syllabus_body).to be_blank
expect(@new_course.stuck_sis_fields).to eq [].to_set
expect(@new_course.self_enrollment_code).to eq self_enrollment_code
expect(@new_course.lti_context_id).to be_nil
expect(@course.uuid).not_to eq @new_course.uuid
expect(@course.wiki_id).not_to eq @new_course.wiki_id
expect(@course.replacement_course_id).to eq @new_course.id
end
it "should not have self enrollment enabled if account setting disables it" do
@course.self_enrollment = true
@course.save!
expect(@course.self_enrollment_enabled?).to eq false
account = @course.root_account
account.allow_self_enrollment!
@course.self_enrollment = true
@course.save!
expect(@course.reload.self_enrollment_enabled?).to eq true
account.settings.delete(:self_enrollment)
account.save!
expect(@course.reload.self_enrollment_enabled?).to eq false
end
it "should retain original course profile" do
data = {:something => 'special here'}
description = 'simple story'
expect(@course.profile).not_to be_nil
@course.profile.tap do |p|
p.description = description
p.data = data
p.save!
end
@course.reload
@new_course = @course.reset_content
expect(@new_course.profile.data[:something]).to eq data[:something]
expect(@new_course.profile.description).to eq description
end
it "should preserve sticky fields" do
@course.sis_source_id = 'sis_id'
@course.course_code = "cid"
@course.save!
@course.stuck_sis_fields = [].to_set
@course.name = "course_name"
expect(@course.stuck_sis_fields).to eq [:name].to_set
profile = @course.profile
profile.description = "description"
profile.save!
@course.save!
expect(@course.stuck_sis_fields).to eq [:name].to_set
@course.reload
@new_course = @course.reset_content
@course.reload
expect(@course.stuck_sis_fields).to eq [:workflow_state, :name].to_set
expect(@course.sis_source_id).to be_nil
@new_course.reload
expect(@new_course.sis_source_id).to eq 'sis_id'
expect(@new_course.stuck_sis_fields).to eq [:name].to_set
expect(@course.uuid).not_to eq @new_course.uuid
expect(@course.replacement_course_id).to eq @new_course.id
end
end
context "group_categories" do
let_once(:course) { course_model }
it "group_categories should not include deleted categories" do
expect(course.group_categories.count).to eq 0
category1 = course.group_categories.create(:name => 'category 1')
category2 = course.group_categories.create(:name => 'category 2')
expect(course.group_categories.count).to eq 2
category1.destroy
course.reload
expect(course.group_categories.count).to eq 1
expect(course.group_categories.to_a).to eq [category2]
end
it "all_group_categories should include deleted categories" do
expect(course.all_group_categories.count).to eq 0
category1 = course.group_categories.create(:name => 'category 1')
category2 = course.group_categories.create(:name => 'category 2')
expect(course.all_group_categories.count).to eq 2
category1.destroy
course.reload
expect(course.all_group_categories.count).to eq 2
end
end
context "turnitin" do
it "should return turnitin_originality" do
@course.account.turnitin_originality = "after_grading"
@course.account.save!
expect(@course.turnitin_originality).to eq("after_grading")
end
end
describe '#quiz_lti_tool' do
before do
@course.save!
@tool = ContextExternalTool.new(
:name => 'Quizzes.Next',
:consumer_key => 'test_key',
:shared_secret => 'test_secret',
:tool_id => 'Quizzes 2',
:url => 'http://example.com/launch'
)
end
it 'returns the quiz LTI tool for the course' do
@course.context_external_tools << @tool
expect(@course.quiz_lti_tool).to eq @tool
end
it 'returns the quiz LTI tool for the account if not set up on the course' do
@course.account.context_external_tools << @tool
expect(@course.quiz_lti_tool).to eq @tool
end
it 'returns nil if no quiz LTI tool is configured' do
expect(@course.quiz_lti_tool).to be nil
end
end
end
describe Course do
context "users_not_in_groups" do
before :once do
@course = course_factory(active_all: true)
@user1 = user_model
@user2 = user_model
@user3 = user_model
@enrollment1 = @course.enroll_user(@user1)
@enrollment2 = @course.enroll_user(@user2)
@enrollment3 = @course.enroll_user(@user3)
end
it "should not include users through deleted/rejected/completed enrollments" do
@enrollment1.destroy
expect(@course.users_not_in_groups([]).size).to eq 2
end
it "should not include users in one of the groups" do
group = @course.groups.create
group.add_user(@user1)
users = @course.users_not_in_groups([group])
expect(users.size).to eq 2
expect(users).not_to be_include(@user1)
end
it "should include users otherwise" do
group = @course.groups.create
group.add_user(@user1)
users = @course.users_not_in_groups([group])
expect(users).to be_include(@user2)
expect(users).to be_include(@user3)
end
it "should allow ordering by user's sortable name" do
@user1.sortable_name = 'jonny'; @user1.save
@user2.sortable_name = 'bob'; @user2.save
@user3.sortable_name = 'richard'; @user3.save
users = @course.users_not_in_groups([], order: User.sortable_name_order_by_clause('users'))
expect(users.map{ |u| u.id }).to eq [@user2.id, @user1.id, @user3.id]
end
end
context "events_for" do
before :once do
course_with_teacher(:active_all => true)
@event1 = @course.calendar_events.create
@event2 = @course.calendar_events.build :child_event_data => [{:start_at => "2012-01-01", :end_at => "2012-01-02", :context_code => @course.default_section.asset_string}]
@event2.updating_user = @teacher
@event2.save!
@event3 = @event2.child_events.first
@appointment_group = AppointmentGroup.create! :title => "ag", :contexts => [@course]
@appointment_group.publish!
@assignment = @course.assignments.create!
end
it "should return appropriate events" do
events = @course.events_for(@teacher)
expect(events).to include @event1
expect(events).not_to include @event2
expect(events).to include @event3
expect(events).to include @appointment_group
expect(events).to include @assignment
end
it "should return appropriate events when no user is supplied" do