forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscussion_topic_spec.rb
More file actions
2043 lines (1736 loc) · 82.3 KB
/
Copy pathdiscussion_topic_spec.rb
File metadata and controls
2043 lines (1736 loc) · 82.3 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 File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
describe DiscussionTopic do
before :once do
course_with_teacher(:active_all => true)
student_in_course(:active_all => true)
end
describe "default values for boolean attributes" do
before(:once) do
@topic = @course.discussion_topics.create!
end
let(:values) do
DiscussionTopic.where(id: @topic).pluck(
:could_be_locked,
:podcast_enabled,
:podcast_has_student_posts,
:require_initial_post,
:pinned,
:locked,
:allow_rating,
:only_graders_can_rate,
:sort_by_rating
).first
end
it "saves boolean attributes as false if they are set to nil" do
@topic.update!(
could_be_locked: nil,
podcast_enabled: nil,
podcast_has_student_posts: nil,
require_initial_post: nil,
pinned: nil,
locked: nil,
allow_rating: nil,
only_graders_can_rate: nil,
sort_by_rating: nil
)
expect(values).to eq([false] * values.length)
end
it "saves boolean attributes as false if they are set to false" do
@topic.update!(
could_be_locked: false,
podcast_enabled: false,
podcast_has_student_posts: false,
require_initial_post: false,
pinned: false,
locked: false,
allow_rating: false,
only_graders_can_rate: false,
sort_by_rating: false
)
expect(values).to eq([false] * values.length)
end
it "saves boolean attributes as true if they are set to true" do
@topic.update!(
could_be_locked: true,
podcast_enabled: true,
podcast_has_student_posts: true,
require_initial_post: true,
pinned: true,
locked: true,
allow_rating: true,
only_graders_can_rate: true,
sort_by_rating: true
)
expect(values).to eq([true] * values.length)
end
end
it "should santize message" do
@course.discussion_topics.create!(:message => "<a href='#' onclick='alert(12);'>only this should stay</a>")
expect(@course.discussion_topics.first.message).to eql("<a href=\"#\">only this should stay</a>")
end
it "should default to side_comment type" do
d = DiscussionTopic.new
expect(d.discussion_type).to eq 'side_comment'
d.threaded = '1'
expect(d.discussion_type).to eq 'threaded'
d.threaded = ''
expect(d.discussion_type).to eq 'side_comment'
end
it "should require a valid discussion_type" do
@topic = @course.discussion_topics.build(:message => 'test', :discussion_type => "gesundheit")
expect(@topic.save).to eq false
expect(@topic.errors.detect { |e| e.first.to_s == 'discussion_type' }).to be_present
end
it "should update the assignment it is associated with" do
a = @course.assignments.create!(:title => "some assignment", :points_possible => 5)
expect(a.points_possible).to eql(5.0)
expect(a.submission_types).not_to eql("online_quiz")
t = @course.discussion_topics.build(:assignment => a, :title => "some topic", :message => "a little bit of content")
t.save
expect(t.assignment_id).to eql(a.id)
expect(t.assignment).to eql(a)
a.reload
expect(a.discussion_topic).to eql(t)
expect(a.submission_types).to eql("discussion_topic")
end
it "should delete the assignment if the topic is no longer graded" do
a = @course.assignments.create!(:title => "some assignment", :points_possible => 5)
expect(a.points_possible).to eql(5.0)
expect(a.submission_types).not_to eql("online_quiz")
t = @course.discussion_topics.build(:assignment => a, :title => "some topic", :message => "a little bit of content")
t.save
expect(t.assignment_id).to eql(a.id)
expect(t.assignment).to eql(a)
a.reload
expect(a.discussion_topic).to eql(t)
t.assignment = nil
t.save
t.reload
expect(t.assignment_id).to eql(nil)
expect(t.assignment).to eql(nil)
a.reload
expect(a).to be_deleted
end
context "permissions" do
before :each do
@teacher1 = @teacher
@teacher2 = user_factory
teacher_in_course(:course => @course, :user => @teacher2, :active_all => true)
@topic = @course.discussion_topics.create!(:user => @teacher1)
@topic.unpublish!
@entry = @topic.discussion_entries.create!(:user => @teacher1)
@entry.discussion_topic = @topic
@relevant_permissions = [:read, :reply, :update, :delete]
end
it "should not grant moderate permissions without read permissions" do
@course.account.role_overrides.create!(:role => teacher_role, :permission => 'read_forum', :enabled => false)
expect((@topic.check_policy(@teacher2) & @relevant_permissions)).to be_empty
end
it "should grant permissions if it not locked" do
@topic.publish!
expect((@topic.check_policy(@teacher1) & @relevant_permissions).map(&:to_s).sort).to eq ['read', 'reply', 'update', 'delete'].sort
expect((@topic.check_policy(@teacher2) & @relevant_permissions).map(&:to_s).sort).to eq ['read', 'reply', 'update', 'delete'].sort
expect((@topic.check_policy(@student) & @relevant_permissions).map(&:to_s).sort).to eq ['read', 'reply'].sort
expect((@entry.check_policy(@teacher1) & @relevant_permissions).map(&:to_s).sort).to eq ['read', 'reply', 'update', 'delete'].sort
expect((@entry.check_policy(@teacher2) & @relevant_permissions).map(&:to_s).sort).to eq ['read', 'reply', 'update', 'delete'].sort
expect((@entry.check_policy(@student) & @relevant_permissions).map(&:to_s).sort).to eq ['read', 'reply'].sort
end
it "should not grant reply permissions to students if it is locked" do
@topic.publish!
@topic.lock!
expect((@topic.check_policy(@teacher1) & @relevant_permissions).map(&:to_s).sort).to eq ['read', 'reply', 'update', 'delete'].sort
expect((@topic.check_policy(@teacher2) & @relevant_permissions).map(&:to_s).sort).to eq ['read', 'reply', 'update', 'delete'].sort
expect((@topic.check_policy(@student) & @relevant_permissions).map(&:to_s)).to eq ['read']
expect((@entry.check_policy(@teacher1) & @relevant_permissions).map(&:to_s).sort).to eq ['read', 'reply', 'update', 'delete'].sort
expect((@entry.check_policy(@teacher2) & @relevant_permissions).map(&:to_s).sort).to eq ['read', 'reply', 'update', 'delete'].sort
expect((@entry.check_policy(@student) & @relevant_permissions).map(&:to_s)).to eq ['read']
end
it "should not grant any permissions to students if it is unpublished" do
expect((@topic.check_policy(@teacher1) & @relevant_permissions).map(&:to_s).sort).to eq ['read', 'reply', 'update', 'delete'].sort
expect((@topic.check_policy(@teacher2) & @relevant_permissions).map(&:to_s).sort).to eq ['read', 'reply', 'update', 'delete'].sort
expect((@topic.check_policy(@student) & @relevant_permissions).map(&:to_s).sort).to eq []
expect((@entry.check_policy(@teacher1) & @relevant_permissions).map(&:to_s).sort).to eq ['read', 'reply', 'update', 'delete'].sort
expect((@entry.check_policy(@teacher2) & @relevant_permissions).map(&:to_s).sort).to eq ['read', 'reply', 'update', 'delete'].sort
expect((@entry.check_policy(@student) & @relevant_permissions).map(&:to_s).sort).to eq []
end
end
describe "visibility" do
before(:once) do
#student_in_course(:active_all => 1)
@topic = @course.discussion_topics.create!(:user => @teacher)
end
it "should be visible to author when unpublished" do
@topic.unpublish!
expect(@topic.visible_for?(@teacher)).to be_truthy
end
it "should be visible when published even when for delayed posting" do
@topic.delayed_post_at = 5.days.from_now
@topic.workflow_state = 'post_delayed'
@topic.save!
expect(@topic.visible_for?(@student)).to be_truthy
end
it "should not be visible when unpublished even when it is active" do
@topic.unpublish!
expect(@topic.visible_for?(@student)).to be_falsey
end
it "should be visible to students when topic is not locked" do
expect(@topic.visible_for?(@student)).to be_truthy
end
it "should be visible to students when topic delayed_post_at is in the future" do
@topic.delayed_post_at = 5.days.from_now
@topic.save!
expect(@topic.visible_for?(@student)).to be_truthy
end
it "should be visible to students when topic is for delayed posting" do
@topic.workflow_state = 'post_delayed'
@topic.save!
expect(@topic.visible_for?(@student)).to be_truthy
end
it "should be visible to students when topic delayed_post_at is in the past" do
@topic.delayed_post_at = 5.days.ago
@topic.save!
expect(@topic.visible_for?(@student)).to be_truthy
end
it "should be visible to students when topic delayed_post_at is nil" do
@topic.delayed_post_at = nil
@topic.save!
expect(@topic.visible_for?(@student)).to be_truthy
end
it "should not be visible to unauthenticated users in a public course" do
@course.update_attribute(:is_public, true)
expect(@topic.visible_for?(nil)).to be_falsey
end
it "should be visible when no delayed_post but assignment unlock date in future" do
@topic.delayed_post_at = nil
group_category = @course.group_categories.create(:name => "category")
@topic.group_category = group_category
@topic.assignment = @course.assignments.build(:submission_types => 'discussion_topic',
:title => @topic.title,
:unlock_at => 10.days.from_now,
:lock_at => 30.days.from_now)
@topic.assignment.infer_times
@topic.assignment.saved_by = :discussion_topic
@topic.save
expect(@topic.visible_for?(@student)).to be_truthy
end
it "should be visible to all teachers in the course" do
@topic.update_attribute(:delayed_post_at, Time.now + 1.day)
new_teacher = user_factory
@course.enroll_teacher(new_teacher).accept!
expect(@topic.visible_for?(new_teacher)).to be_truthy
end
it "unpublished topics should not be visible to custom account admins by default" do
@topic.unpublish
account = @course.root_account
nobody_role = custom_account_role('NobodyAdmin', account: account)
admin = account_admin_user(account: account, role: nobody_role, active_user: true)
expect(@topic.visible_for?(admin)).to be_falsey
end
it "unpublished topics should be visible to account admins with :read_course_content permission" do
@topic.unpublish
account = @course.root_account
nobody_role = custom_account_role('NobodyAdmin', account: account)
account_with_role_changes(account: account, role: nobody_role, role_changes: { read_course_content: true, read_forum: true })
admin = account_admin_user(account: account, role: nobody_role, active_user: true)
expect(@topic.visible_for?(admin)).to be_truthy
end
context "participants with teachers and tas" do
before(:once) do
group_course = course_factory(active_course: true)
@group_student, @group_ta, @group_teacher = create_users(3, return_type: :record)
@not_group_student, @group_designer = create_users(2, return_type: :record)
group_course.enroll_teacher(@group_teacher).accept!
group_course.enroll_ta(@group_ta).accept!
group_course.enroll_designer(@group_designer).accept!
group_category = group_course.group_categories.create(:name => "new cat")
group = group_course.groups.create(:name => "group", :group_category => group_category)
group.add_user(@group_student)
@announcement = group.announcements.build(:title => "group topic", :message => "group message")
@announcement.save!
end
it "should be visible to instructors and tas" do
[@group_student, @group_ta, @group_teacher].each do |user|
expect(@announcement.active_participants_include_tas_and_teachers.include?(user)).to be_truthy
end
end
it "should not include people out of the group or non-instructors" do
[@not_group_student, @group_designer].each do |user|
expect(@announcement.active_participants_include_tas_and_teachers.include?(user)).to be_falsey
end
end
end
context "differentiated assignements" do
before do
@course = course_factory(active_course: true)
discussion_topic_model(:user => @teacher, :context => @course)
@course.enroll_teacher(@teacher).accept!
@course_section = @course.course_sections.create
@student1, @student2, @student3 = create_users(3, return_type: :record)
@assignment = @course.assignments.create!(:title => "some discussion assignment", only_visible_to_overrides: true)
@assignment.submission_types = 'discussion_topic'
@assignment.save!
@topic.assignment_id = @assignment.id
@topic.save!
@course.enroll_student(@student2, :enrollment_state => 'active')
@section = @course.course_sections.create!(name: "test section")
student_in_section(@section, user: @student1)
create_section_override_for_assignment(@assignment, {course_section: @section})
@course.reload
end
it "should be visible to a student with an override" do
expect(@topic.visible_for?(@student1)).to be_truthy
end
it "should not be visible to a student without an override" do
expect(@topic.visible_for?(@student2)).to be_falsey
end
it "should be visible to a teacher" do
expect(@topic.visible_for?(@teacher)).to be_truthy
end
it "should not grant reply permissions to a student without an override" do
expect(@topic.check_policy(@student1)).to include :reply
expect(@topic.check_policy(@student2)).not_to include :reply
end
context "active_participants_with_visibility" do
it "should filter participants by visibility" do
[@student1, @teacher].each do |user|
expect(@topic.active_participants_with_visibility.include?(user)).to be_truthy
end
expect(@topic.active_participants_with_visibility.include?(@student2)).to be_falsey
end
it "should work when ungraded and context is a course" do
group_category = @course.group_categories.create(:name => "new cat")
@topic = @course.discussion_topics.create(:title => "group topic")
@topic.save!
expect(@topic.context).to eq(@course)
expect(@topic.active_participants_with_visibility.include?(@student1)).to be_truthy
expect(@topic.active_participants_with_visibility.include?(@student2)).to be_truthy
end
it "should work when ungraded and context is a group" do
group_category = @course.group_categories.create(:name => "new cat")
@group = @course.groups.create(:name => "group", :group_category => group_category)
@group.add_user(@student1)
@topic = @group.discussion_topics.create(:title => "group topic")
@topic.save!
expect(@topic.context).to eq(@group)
expect(@topic.active_participants_with_visibility.include?(@student1)).to be_truthy
expect(@topic.active_participants_with_visibility.include?(@student2)).to be_falsey
end
it "should not grant reply permissions to group if course is concluded" do
@relevant_permissions = [:read, :reply, :update, :delete, :read_replies]
group_category = @course.group_categories.create(:name => "new cat")
@group = @course.groups.create(:name => "group", :group_category => group_category)
@group.add_user(@student1)
@course.complete!
@topic = @group.discussion_topics.create(:title => "group topic")
@topic.save!
expect(@topic.context).to eq(@group)
expect((@topic.check_policy(@student1) & @relevant_permissions).sort).to eq [:read, :read_replies].sort
end
it "should not grant reply permissions to group if course is soft-concluded" do
@relevant_permissions = [:read, :reply, :update, :delete, :read_replies]
group_category = @course.group_categories.create(:name => "new cat")
@group = @course.groups.create(:name => "group", :group_category => group_category)
@group.add_user(@student1)
@course.update_attributes(:start_at => 2.days.ago, :conclude_at => 1.day.ago, :restrict_enrollments_to_course_dates => true)
@topic = @group.discussion_topics.create(:title => "group topic")
@topic.save!
expect(@topic.context).to eq(@group)
expect((@topic.check_policy(@student1) & @relevant_permissions).sort).to eq [:read, :read_replies].sort
end
it "should grant reply permissions to group members if course is concluded but their section isn't" do
@relevant_permissions = [:read, :reply, :update, :delete, :read_replies]
group_category = @course.group_categories.create(:name => "new cat")
@group = @course.groups.create(:name => "group", :group_category => group_category)
@group.add_user(@student1)
@course.update_attributes(:start_at => 2.days.ago, :conclude_at => 1.day.ago, :restrict_enrollments_to_course_dates => true)
@section.update_attributes(:start_at => 2.days.ago, :end_at => 2.days.from_now,
:restrict_enrollments_to_section_dates => true)
@topic = @group.discussion_topics.create(:title => "group topic")
@topic.save!
expect(@topic.context).to eq(@group)
expect((@topic.check_policy(@student1) & @relevant_permissions).sort).to eq [:read, :read_replies, :reply].sort
end
it "should not grant reply permissions to group if group isn't active" do
@relevant_permissions = [:read, :reply, :update, :delete, :read_replies]
group_category = @course.group_categories.create(:name => "new cat")
@group = @course.groups.create(:name => "group", :group_category => group_category)
@group.add_user(@student1)
@topic = @group.discussion_topics.create(:title => "group topic")
@topic.save!
@group.destroy
expect(@topic.reload.context).to eq(@group.reload)
expect((@topic.check_policy(@student1) & @relevant_permissions).sort).to eq [:read, :read_replies].sort
end
it "should grant reply permissions to teachers if course is claimed" do
course = course_factory(active_course: false)
discussion_topic_model(:user => @teacher, :context => course)
course.enroll_teacher(@teacher).accept!
course.enroll_student(@student1)
@relevant_permissions = [:read, :reply, :update, :delete, :read_replies]
group_category = course.group_categories.create(:name => "new cat")
@group = course.groups.create(:name => "group", :group_category => group_category)
@group.add_user(@student1)
@topic = @group.discussion_topics.create(:title => "group topic")
@topic.save!
expect(@topic.context).to eq(@group)
expect((@topic.check_policy(@teacher) & @relevant_permissions).sort).to eq @relevant_permissions.sort
expect((@topic.check_policy(@student1) & @relevant_permissions)).to be_empty
end
it "should work for subtopics for graded assignments" do
group_discussion_assignment
ct = @topic.child_topics.first
ct.context.add_user(@student)
@section = @course.course_sections.create!(name: "test section")
student_in_section(@section, user: @student)
create_section_override_for_assignment(@assignment, {course_section: @section})
@topic = @topic.child_topics.first
@topic.subscribe(@student)
@topic.save!
expect(@topic.context.class).to eq(Group)
expect(@topic.active_participants_with_visibility.include?(@student)).to be_truthy
end
end
end
end
describe "allow_student_discussion_topics setting" do
before(:once) do
@topic = @course.discussion_topics.create!(:user => @teacher)
end
it "should allow students to create topics by default" do
expect(@topic.check_policy(@teacher)).to include :create
expect(@topic.check_policy(@student)).to include :create
expect(@topic.check_policy(@course.student_view_student)).to include :create
end
it "should disallow students from creating topics" do
@course.allow_student_discussion_topics = false
@course.save!
@topic.reload
expect(@topic.check_policy(@teacher)).to include :create
expect(@topic.check_policy(@student)).not_to include :create
expect(@topic.check_policy(@course.student_view_student)).not_to include :create
end
end
context "observers" do
before :once do
course_with_observer(:course => @course, :active_all => true)
end
it "should grant observers read permission by default" do
@relevant_permissions = [:read, :reply, :update, :delete]
@topic = @course.discussion_topics.create!(:user => @teacher)
expect((@topic.check_policy(@observer) & @relevant_permissions).map(&:to_s).sort).to eq ['read'].sort
@entry = @topic.discussion_entries.create!(:user => @teacher)
expect((@entry.check_policy(@observer) & @relevant_permissions).map(&:to_s).sort).to eq ['read'].sort
end
it "should not grant observers read permission when read_forum override is false" do
RoleOverride.create!(:context => @course.account, :permission => 'read_forum',
:role => observer_role, :enabled => false)
@relevant_permissions = [:read, :reply, :update, :delete]
@topic = @course.discussion_topics.create!(:user => @teacher)
expect((@topic.check_policy(@observer) & @relevant_permissions).map(&:to_s)).to be_empty
@entry = @topic.discussion_entries.create!(:user => @teacher)
expect((@entry.check_policy(@observer) & @relevant_permissions).map(&:to_s)).to be_empty
end
end
context "delayed posting" do
before :once do
@student.register
end
def discussion_topic(opts = {})
workflow_state = opts.delete(:workflow_state)
@topic = @course.discussion_topics.build(opts)
@topic.workflow_state = workflow_state if workflow_state
@topic.save!
@topic
end
def delayed_discussion_topic(opts = {})
discussion_topic({:workflow_state => 'post_delayed'}.merge(opts))
end
it "shouldn't send to streams on creation or update if it's delayed" do
topic = @course.discussion_topics.create!(
title: "this should not be delayed",
message: "content here"
)
expect(topic.stream_item).not_to be_nil
topic = delayed_discussion_topic(
title: "this should be delayed",
message: "content here",
delayed_post_at: 1.day.from_now
)
expect(topic.stream_item).to be_nil
topic.message = "content changed!"
topic.save
expect(topic.stream_item).to be_nil
end
it "should send to streams on update from unpublished to active" do
topic = discussion_topic(
title: "this should be delayed",
message: "content here",
workflow_state: "unpublished"
)
expect(topic.workflow_state).to eq 'unpublished'
expect(topic.stream_item).to be_nil
topic.workflow_state = 'active'
topic.save!
expect(topic.stream_item).not_to be_nil
end
it "doesn't rely on broadcast policy when sending to stream" do
topic = discussion_topic(
title: "this should be delayed",
message: "content here",
workflow_state: "unpublished"
)
expect(topic.workflow_state).to eq 'unpublished'
expect(topic.stream_item).to be_nil
topic.workflow_state = 'active'
topic.save_without_broadcasting!
expect(topic.stream_item).not_to be_nil
end
describe "#update_based_on_date" do
it "should be active when delayed_post_at is in the past" do
topic = delayed_discussion_topic(:title => "title",
:message => "content here",
:delayed_post_at => Time.now - 1.day,
:lock_at => nil)
topic.update_based_on_date
expect(topic.workflow_state).to eql 'active'
expect(topic.locked?).to be_falsey
end
it "should be post_delayed when delayed_post_at is in the future" do
topic = delayed_discussion_topic(:title => "title",
:message => "content here",
:delayed_post_at => Time.now + 1.day,
:lock_at => nil)
topic.update_based_on_date
expect(topic.workflow_state).to eql 'post_delayed'
expect(topic.locked?).to be_falsey
end
it "should be locked when lock_at is in the past" do
topic = delayed_discussion_topic(:title => "title",
:message => "content here",
:delayed_post_at => nil,
:lock_at => Time.now - 1.day)
topic.update_based_on_date
expect(topic.locked?).to be_truthy
end
it "should be active when lock_at is in the future" do
topic = delayed_discussion_topic(:title => "title",
:message => "content here",
:delayed_post_at => nil,
:lock_at => Time.now + 1.day)
topic.update_based_on_date
expect(topic.workflow_state).to eql 'active'
expect(topic.locked?).to be_falsey
end
it "should be active when now is between delayed_post_at and lock_at" do
topic = delayed_discussion_topic(:title => "title",
:message => "content here",
:delayed_post_at => Time.now - 1.day,
:lock_at => Time.now + 1.day)
topic.update_based_on_date
expect(topic.workflow_state).to eql 'active'
expect(topic.locked?).to be_falsey
end
it "should be post_delayed when delayed_post_at and lock_at are in the future" do
topic = delayed_discussion_topic(:title => "title",
:message => "content here",
:delayed_post_at => Time.now + 1.day,
:lock_at => Time.now + 3.days)
topic.update_based_on_date
expect(topic.workflow_state).to eql 'post_delayed'
expect(topic.locked?).to be_falsey
end
it "should be locked when delayed_post_at and lock_at are in the past" do
topic = delayed_discussion_topic(:title => "title",
:message => "content here",
:delayed_post_at => Time.now - 3.days,
:lock_at => Time.now - 1.day)
topic.update_based_on_date
expect(topic.workflow_state).to eql 'active'
expect(topic.locked?).to be_truthy
end
it "should not unlock a topic even if the lock date is in the future" do
topic = discussion_topic(:title => "title",
:message => "content here",
:workflow_state => 'locked',
:locked => true,
:delayed_post_at => nil,
:lock_at => Time.now + 1.day)
topic.update_based_on_date
expect(topic.locked?).to be_truthy
end
it "should not mark a topic with post_delayed even if delayed_post_at even is in the future" do
topic = discussion_topic(:title => "title",
:message => "content here",
:workflow_state => 'active',
:delayed_post_at => Time.now + 1.day,
:lock_at => nil)
topic.update_based_on_date
expect(topic.workflow_state).to eql 'active'
expect(topic.locked?).to be_falsey
end
end
end
context "sub-topics" do
it "should default subtopics_refreshed_at on save if a group discussion" do
group_category = @course.group_categories.create(:name => "category")
@group = @course.groups.create(:name => "group", :group_category => group_category)
@topic = @course.discussion_topics.create(:title => "topic")
expect(@topic.subtopics_refreshed_at).to be_nil
@topic.group_category = group_category
@topic.save
expect(@topic.subtopics_refreshed_at).not_to be_nil
end
it "should not allow students to edit sub-topics" do
@first_user = @student
@second_user = user_model
@course.enroll_student(@second_user).accept
@parent_topic = @course.discussion_topics.create!(:title => "parent topic", :message => "msg")
@group = @course.groups.create!(:name => "course group")
@group.add_user(@first_user)
@group.add_user(@second_user)
@group_topic = @group.discussion_topics.create!(:title => "group topic", :message => "ok to be edited", :user => @first_user)
@sub_topic = @group.discussion_topics.build(:title => "sub topic", :message => "not ok to be edited", :user => @first_user)
@sub_topic.root_topic_id = @parent_topic.id
@sub_topic.save!
expect(@group_topic.grants_right?(@second_user, :update)).to eql(false)
expect(@sub_topic.grants_right?(@second_user, :update)).to eql(false)
end
end
context "refresh_subtopics" do
it "should be a no-op unless it has a group_category" do
@topic = @course.discussion_topics.create(:title => "topic")
@topic.refresh_subtopics
expect(@topic.reload.child_topics).to be_empty
@topic.assignment = @course.assignments.build(:submission_types => 'discussion_topic', :title => @topic.title)
@topic.assignment.saved_by = :discussion_topic
@topic.save
@topic.refresh_subtopics
expect(@topic.reload.child_topics).to be_empty
end
it "should refresh when groups are added to a group_category" do
group_category = @course.group_categories.create!(:name => "category")
topic = @course.discussion_topics.build(:title => "topic")
topic.group_category = group_category
topic.save!
group = @course.groups.create!(:name => "group 1", :group_category => group_category)
expect(topic.reload.child_topics.size).to eq 1
expect(group.reload.discussion_topics.size).to eq 1
end
it "should not break when groups have silly long names" do
group_category = @course.group_categories.create!(:name => "category")
topic = @course.discussion_topics.build(:title => "here's a reasonable topic name")
topic.group_category = group_category
topic.save!
group = @course.groups.create!(:name => "a" * 250, :group_category => group_category)
expect(topic.reload.child_topics.size).to eq 1
expect(group.reload.discussion_topics.size).to eq 1
end
it "should delete child topics when group category is removed" do
group_category = @course.group_categories.create!(:name => "category")
group = @course.groups.create!(:name => "group 1", :group_category => group_category)
topic = @course.discussion_topics.build(:title => "topic")
topic.group_category = group_category
topic.save!
expect(topic.reload.child_topics.active.count).to eq 1
expect(group.reload.discussion_topics.active.count).to eq 1
topic.group_category = nil
topic.save!
expect(topic.reload.child_topics.active.count).to eq 0
expect(group.reload.discussion_topics.active.count).to eq 0
end
context "in a group discussion" do
before :once do
group_discussion_assignment
end
it "should create a topic per active group in the category otherwise" do
@topic.refresh_subtopics
subtopics = @topic.reload.child_topics
expect(subtopics).not_to be_nil
expect(subtopics.size).to eq 2
subtopics.each { |t| expect(t.root_topic).to eq @topic }
expect(@group1.reload.discussion_topics).not_to be_empty
expect(@group2.reload.discussion_topics).not_to be_empty
end
it "should copy appropriate attributes from the parent topic to subtopics on updates to the parent" do
@topic.refresh_subtopics
subtopics = @topic.reload.child_topics
subtopics.each do |st|
expect(st.discussion_type).to eq 'side_comment'
expect(st.attachment_id).to be_nil
end
attachment_model(context: @course)
@topic.discussion_type = 'threaded'
@topic.attachment = @attachment
@topic.save!
subtopics = @topic.reload.child_topics
subtopics.each do |st|
expect(st.discussion_type).to eq 'threaded'
expect(st.attachment_id).to eq @attachment.id
end
end
it "should not rename the assignment to match a subtopic" do
original_name = @assignment.title
@assignment.reload
expect(@assignment.title).to eq original_name
end
end
end
context "root_topic?" do
it "should be false if the topic has a root topic" do
# subtopic has the assignment and group_category, but has a root topic
group_category = @course.group_categories.create(:name => "category")
@parent_topic = @course.discussion_topics.create(:title => "parent topic")
@parent_topic.group_category = group_category
@subtopic = @parent_topic.child_topics.build(:title => "subtopic")
@assignment = @course.assignments.build(:submission_types => 'discussion_topic', :title => @subtopic.title)
@assignment.infer_times
@assignment.saved_by = :discussion_topic
@subtopic.assignment = @assignment
@subtopic.group_category = group_category
@subtopic.save
expect(@subtopic).not_to be_root_topic
end
it "should be false unless the topic has an assignment" do
# topic has no root topic, but also has no assignment
@topic = @course.discussion_topics.create(:title => "subtopic")
expect(@topic).not_to be_root_topic
end
it "should be false unless the topic has a group_category" do
# topic has no root topic and has an assignment, but the assignment has no group_category
@topic = @course.discussion_topics.create(:title => "topic")
@assignment = @course.assignments.build(:submission_types => 'discussion_topic', :title => @topic.title)
@assignment.infer_times
@assignment.saved_by = :discussion_topic
@topic.assignment = @assignment
@topic.save
expect(@topic).not_to be_root_topic
end
it "should be true otherwise" do
# topic meets all criteria
group_category = @course.group_categories.create(:name => "category")
@topic = @course.discussion_topics.create(:title => "topic")
@topic.group_category = group_category
@assignment = @course.assignments.build(:submission_types => 'discussion_topic', :title => @topic.title)
@assignment.infer_times
@assignment.saved_by = :discussion_topic
@topic.assignment = @assignment
@topic.save
expect(@topic).to be_root_topic
end
end
context "#discussion_subentry_count" do
it "returns the count of all active discussion_entries" do
@topic = @course.discussion_topics.create(:title => "topic")
@topic.reply_from(:user => @teacher, :text => "entry 1").destroy # no count
@topic.reply_from(:user => @teacher, :text => "entry 1") # 1
@entry = @topic.reply_from(:user => @teacher, :text => "entry 2") # 2
@entry.reply_from(:user => @student, :html => "reply 1") # 3
@entry.reply_from(:user => @student, :html => "reply 2") # 4
# expect
expect(@topic.discussion_subentry_count).to eq 4
end
end
context "for_assignment?" do
it "should not be for_assignment? unless it has an assignment" do
@topic = @course.discussion_topics.create(:title => "topic")
expect(@topic).not_to be_for_assignment
@topic.assignment = @course.assignments.build(:submission_types => 'discussion_topic', :title => @topic.title)
@topic.assignment.infer_times
@topic.assignment.saved_by = :discussion_topic
@topic.save
expect(@topic).to be_for_assignment
end
end
context "for_group_discussion?" do
it "should not be for_group_discussion? unless it has a group_category" do
course_with_student(:active_all => true)
@topic = @course.discussion_topics.build(:title => "topic")
@assignment = @course.assignments.build(:submission_types => 'discussion_topic', :title => @topic.title)
@assignment.infer_times
@assignment.saved_by = :discussion_topic
@topic.assignment = @assignment
@topic.save
expect(@topic).not_to be_for_group_discussion
@topic.group_category = @course.group_categories.create(:name => "category")
@topic.save
expect(@topic).to be_for_group_discussion
end
end
context "should_send_to_stream" do
context "in a published course" do
it "should be true for non-assignment discussions" do
@topic = @course.discussion_topics.create(:title => "topic")
expect(@topic.should_send_to_stream).to be_truthy
end
it "should be true for non-group discussion assignments" do
@topic = @course.discussion_topics.build(:title => "topic")
@assignment = @course.assignments.build(:submission_types => 'discussion_topic', :title => @topic.title, :due_at => 1.day.from_now)
@assignment.saved_by = :discussion_topic
@topic.assignment = @assignment
@topic.save
expect(@topic.should_send_to_stream).to be_truthy
end
it "should be true for the parent topic only in group discussions, not the subtopics" do
group_category = @course.group_categories.create(:name => "category")
@parent_topic = @course.discussion_topics.create(:title => "parent topic")
@parent_topic.group_category = group_category
@parent_topic.save
@subtopic = @parent_topic.child_topics.build(:title => "subtopic")
@subtopic.group_category = group_category
@assignment = @course.assignments.build(:submission_types => 'discussion_topic', :title => @subtopic.title, :due_at => 1.day.from_now)
@assignment.saved_by = :discussion_topic
@subtopic.assignment = @assignment
@subtopic.save
expect(@parent_topic.should_send_to_stream).to be_truthy
expect(@subtopic.should_send_to_stream).to be_falsey
end
end
it "should not send stream items to students if course isn't published'" do
@course.update_attribute(:workflow_state, "created")
topic = @course.discussion_topics.create!(:title => "secret topic", :user => @teacher)
expect(@student.stream_item_instances.count).to eq 0
expect(@teacher.stream_item_instances.count).to eq 1
topic.discussion_entries.create!
expect(@student.stream_item_instances.count).to eq 0
expect(@teacher.stream_item_instances.count).to eq 1
end
it "should send stream items to students for graded discussions" do
@topic = @course.discussion_topics.build(:title => "topic")
@assignment = @course.assignments.build(:submission_types => 'discussion_topic', :title => @topic.title)
@assignment.saved_by = :discussion_topic
@topic.assignment = @assignment
@topic.save
expect(@student.stream_item_instances.count).to eq 1
end
end
context "posting first to view" do
before(:once) do
@observer = user_factory(active_all: true)
@context = @course
discussion_topic_model
@topic.require_initial_post = true
@topic.save
end
it "should allow admins to see posts without posting" do
expect(@topic.user_can_see_posts?(@teacher)).to eq true
end
it "should allow course admins to see posts in concluded group topics without posting" do
group_category = @course.group_categories.create(:name => "category")
@group = @course.groups.create(:name => "group", :group_category => group_category)
@topic.update_attribute(:group_category, group_category)
subtopic = @topic.child_topics.first
@course.complete!
expect(subtopic.user_can_see_posts?(@teacher)).to eq true
end
it "should only allow active admins to see posts without posting" do
@ta_enrollment = course_with_ta(:course => @course, :active_enrollment => true)
# TA should be able to see
expect(@topic.user_can_see_posts?(@ta)).to eq true
# Remove user as TA and enroll as student, should not be able to see
@ta_enrollment.destroy
# enroll as a student.
course_with_student(:course => @course, :user => @ta, :active_enrollment => true)
@topic.reload
@topic.clear_permissions_cache(@ta)
expect(@topic.user_can_see_posts?(@ta)).to eq false
end
it "shouldn't allow student who hasn't posted to see" do