forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquizzes_controller_spec.rb
1372 lines (1183 loc) · 49 KB
/
quizzes_controller_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
describe Quizzes::QuizzesController do
def course_quiz(active=false)
@quiz = @course.quizzes.create
@quiz.workflow_state = "available" if active
@quiz.save!
@quiz
end
def quiz_question
@quiz.quiz_questions.create
end
def quiz_group
@quiz.quiz_groups.create
end
def temporary_user_code(generate=true)
if generate
session[:temporary_user_code] ||= "tmp_#{Digest::MD5.hexdigest("#{Time.now.to_i.to_s}_#{rand.to_s}")}"
else
session[:temporary_user_code]
end
end
def logged_out_survey_with_submission(user, questions, &block)
course_with_teacher_logged_in(:active_all => true)
@assignment = @course.assignments.create(:title => "Test Assignment")
@assignment.workflow_state = "available"
@assignment.submission_types = "online_quiz"
@assignment.save
@quiz = Quizzes::Quiz.find_by_assignment_id(@assignment.id)
@quiz.anonymous_submissions = false
@quiz.quiz_type = "survey"
@questions = questions.map { |q| @quiz.quiz_questions.create!(q) }
@quiz.generate_quiz_data
@quiz.save!
@quiz_submission = @quiz.generate_submission(user)
@quiz_submission.mark_completed
@quiz_submission.submission_data = yield if block_given?
Quizzes::SubmissionGrader.new(@quiz_submission).grade_submission
@quiz_submission.save!
end
def ember_urls
CanvasEmberUrl::UrlMappings.new(
:course_quizzes => fabulous_quizzes_course_quizzes_url
)
end
describe "GET 'index'" do
it "should require authorization" do
course_with_teacher(:active_all => true)
get 'index', :course_id => @course.id
assert_unauthorized
end
it "should redirect 'disabled', if disabled by the teacher" do
course_with_student_logged_in(:active_all => true)
@course.update_attribute(:tab_configuration, [{'id'=>4,'hidden'=>true}])
get 'index', :course_id => @course.id
response.should be_redirect
flash[:notice].should match(/That page has been disabled/)
end
it "should assign variables" do
course_with_teacher_logged_in(:active_all => true)
get 'index', :course_id => @course.id
assigns[:quizzes].should_not be_nil
assigns[:unpublished_quizzes].should_not be_nil
assigns[:submissions_hash].should_not be_nil
end
it "should retrieve quizzes" do
course_with_teacher_logged_in(:active_all => true)
course_quiz(!!:active)
get 'index', :course_id => @course.id
assigns[:quizzes].should_not be_nil
assigns[:quizzes].should_not be_empty
assigns[:quizzes][0].should eql(@quiz)
end
end
describe "GET 'index' with draft state enabled" do
before do
Account.default.enable_feature! :draft_state
end
it "should assign variables" do
course_with_teacher_logged_in(:active_all => true)
get 'index', :course_id => @course.id
assigns[:assignment_json].should_not be_nil
assigns[:open_json].should_not be_nil
assigns[:surveys_json].should_not be_nil
assigns[:quiz_options].should_not be_nil
end
it "should filter out unpublished quizzes for student" do
course_with_student_logged_in(:active_all => true)
course_quiz
course_quiz(active = true)
get 'index', :course_id => @course.id
assigns[:quizzes].length.should eql 1
assigns[:quizzes].map do |quiz|
quiz.published?.should be_true
end
end
end
describe "GET 'index' without fabulous quizzes enabled" do
before :each do
a = Account.default
a.disable_fabulous_quizzes!
a.save!
end
after :each do
a = Account.default
a.enable_fabulous_quizzes!
a.save!
end
it "should not redirect" do
course_with_teacher_logged_in(:active_all => true)
course_quiz(active = true)
a = Account.default
a.enable_fabulous_quizzes?.should eql false
get 'index', :course_id => @course.id
assert_response(:success)
end
end
describe "GET 'index' with fabulous quizzes enabled" do
before :each do
a = Account.default
a.enable_fabulous_quizzes!
a.save!
end
after :each do
a = Account.default
a.disable_fabulous_quizzes!
a.save!
end
it "should redirect to fabulous quizzes app" do
course_with_teacher_logged_in(:active_all => true)
course_quiz(active = true)
a = Account.default
a.enable_fabulous_quizzes?.should eql true
get 'index', :course_id => @course.id
assert_redirected_to ember_urls.course_quizzes_url
end
end
describe "GET 'fabulous_quizzes' without fabulous quizzes enabled" do
it "should redirect to index" do
a = Account.default
a.disable_fabulous_quizzes!
a.save!
course_with_teacher_logged_in(:active_all => true)
course_quiz(active = true)
a = Account.default
a.enable_fabulous_quizzes?.should eql false
get 'fabulous_quizzes', :course_id => @course.id
assert_redirected_to(:controller => "quizzes", :action => "index")
end
end
describe "GET 'new'" do
it "should require authorization" do
course_with_teacher(:active_all => true)
get 'new', :course_id => @course.id
assert_unauthorized
end
it "should assign variables" do
course_with_teacher_logged_in(:active_all => true)
get 'new', :course_id => @course.id
assigns[:quiz].should_not be_nil
q = assigns[:quiz]
end
it "subsequent requests should return the same quiz unless ?fresh=1" do
course_with_teacher_logged_in(:active_all => true)
get 'new', :course_id => @course.id
assigns[:quiz].should_not be_nil
q = assigns[:quiz]
get 'new', :course_id => @course.id
assigns[:quiz].should_not be_nil
assigns[:quiz].should_not eql(q)
get 'new', :course_id => @course.id, :fresh => 1
# Quizzes::Quiz.find_by_id(q.id).should be_deleted
assigns[:quiz].should_not be_nil
assigns[:quiz].should_not eql(q)
end
end
describe "GET 'edit'" do
it "should require authorization" do
course_with_teacher(:active_all => true)
course_quiz
get 'edit', :course_id => @course.id, :id => @quiz.id
assert_unauthorized
assigns[:quiz].should_not be_nil
end
it "should assign variables" do
course_with_teacher_logged_in(:active_all => true)
course_quiz
regrade = @quiz.quiz_regrades.create!(:user_id => @teacher.id, quiz_version: @quiz.version_number)
q = @quiz.quiz_questions.create!
regrade.quiz_question_regrades.create!(:quiz_question_id => q.id,:regrade_option => 'no_regrade')
get 'edit', :course_id => @course.id, :id => @quiz.id
assigns[:quiz].should_not be_nil
assigns[:quiz].should eql(@quiz)
assigns[:js_env][:REGRADE_OPTIONS].should == {q.id => 'no_regrade' }
response.should render_template("new")
end
end
describe "GET 'show'" do
it "should require authorization" do
course_with_teacher(:active_all => true)
course_quiz
get 'show', :course_id => @course.id, :id => @quiz.id
assert_unauthorized
assigns[:quiz].should_not be_nil
assigns[:quiz].should eql(@quiz)
end
it "should assign variables" do
course_with_teacher_logged_in(:active_all => true)
course_quiz
get 'show', :course_id => @course.id, :id => @quiz.id
assigns[:quiz].should_not be_nil
assigns[:quiz].should eql(@quiz)
assigns[:question_count].should eql(@quiz.question_count)
assigns[:just_graded].should eql(false)
assigns[:stored_params].should_not be_nil
end
it "should set the submission count variables" do
course(:active_all => 1)
@section = @course.course_sections.create!(:name => 'section 2')
@user2 = user_with_pseudonym(:active_all => true, :name => 'Student2', :username => '[email protected]')
@section.enroll_user(@user2, 'StudentEnrollment', 'active')
@user1 = user_with_pseudonym(:active_all => true, :name => 'Student1', :username => '[email protected]')
@course.enroll_student(@user1)
@ta1 = user_with_pseudonym(:active_all => true, :name => 'TA1', :username => '[email protected]')
@course.enroll_ta(@ta1).update_attribute(:limit_privileges_to_course_section, true)
course_quiz
@sub1 = @quiz.generate_submission(@user1)
@sub2 = @quiz.generate_submission(@user2)
@sub2.start_grading
@sub2.update_attribute(:workflow_state, 'pending_review')
user_session @teacher
get 'show', :course_id => @course.id, :id => @quiz.id
assigns[:submitted_student_count].should == 2
assigns[:any_submissions_pending_review].should == true
controller.js_env.clear
user_session @ta1
get 'show', :course_id => @course.id, :id => @quiz.id
assigns[:submitted_student_count].should == 1
assigns[:any_submissions_pending_review].should == false
end
it "should allow forcing authentication on public quiz pages" do
course_with_student :active_all => 1
@course.update_attribute :is_public, true
course_quiz !!:active
get 'show', :course_id => @course.id, :id => @quiz.id, :force_user => 1
response.should be_redirect
response.location.should match /login/
end
it "should set session[headless_quiz] if persist_headless param is sent" do
course_with_student_logged_in :active_all => 1
course_quiz !!:active
get 'show', :course_id => @course.id, :id => @quiz.id, :persist_headless => 1
controller.session[:headless_quiz].should be_true
assigns[:headers].should be_false
end
it "should not render headers if session[:headless_quiz] is set" do
course_with_student_logged_in :active_all => 1
course_quiz !!:active
controller.session[:headless_quiz] = true
get 'show', :course_id => @course.id, :id => @quiz.id
assigns[:headers].should be_false
end
it "assigns js_env for attachments if submission is present" do
require 'action_controller_test_process'
course_with_student_logged_in :active_all => true
course_quiz !!:active
submission = @quiz.generate_submission @user
create_attachment_for_file_upload_submission!(submission)
get 'show', :course_id => @course.id, :id => @quiz.id
attachment = submission.attachments.first
attach = assigns[:js_env][:ATTACHMENTS][attachment.id]
attach[:id].should == attachment.id
attach[:display_name].should == attachment.display_name
end
it "assigns js_env for versions if submission is present" do
require 'action_controller_test_process'
course_with_student_logged_in :active_all => true
course_quiz !!:active
submission = @quiz.generate_submission @user
create_attachment_for_file_upload_submission!(submission)
get 'show', :course_id => @course.id, :id => @quiz.id
path = "courses/#{@course.id}/quizzes/#{@quiz.id}/submission_versions"
assigns[:js_env][:SUBMISSION_VERSIONS_URL].should include(path)
end
it "doesn't show unpublished quizzes to students with draft state" do
course_with_student_logged_in(active_all: true)
course_quiz(active=true)
Account.default.enable_feature!(:draft_state)
@quiz.unpublish!
get 'show', course_id: @course.id, id: @quiz.id
response.should_not be_success
end
it 'logs a single asset access entry with an action level of "view"' do
Setting.set('enable_page_views', 'db')
course_with_teacher_logged_in(:active_all => true)
course_quiz
get 'show', :course_id => @course.id, :id => @quiz.id
assigns[:access].should_not be_nil
assigns[:accessed_asset].should_not be_nil
assigns[:accessed_asset][:level].should == 'view'
assigns[:access].view_score.should == 1
end
end
describe "GET 'show' with fabulous quizzes enabled" do
before :each do
a = Account.default
a.enable_feature! :draft_state
a.enable_fabulous_quizzes!
a.save!
course_with_teacher_logged_in(:active_all => true)
course_quiz
end
it "should redirect to fabulous quizzes app" do
a = Account.default
a.enable_fabulous_quizzes?.should eql true
get 'show', :course_id => @course.id, :id => @quiz.id
assert_redirected_to ember_urls.course_quiz_url(@quiz.id)
end
end
describe "GET 'managed_quiz_data'" do
it "should respect section privilege limitations" do
course(:active_all => 1)
@section = @course.course_sections.create!(:name => 'section 2')
@user2 = user_with_pseudonym(:active_all => true, :name => 'Student2', :username => '[email protected]')
@section.enroll_user(@user2, 'StudentEnrollment', 'active')
@user1 = user_with_pseudonym(:active_all => true, :name => 'Student1', :username => '[email protected]')
@course.enroll_student(@user1)
@ta1 = user_with_pseudonym(:active_all => true, :name => 'TA1', :username => '[email protected]')
@course.enroll_ta(@ta1).update_attribute(:limit_privileges_to_course_section, true)
course_quiz
@sub1 = @quiz.generate_submission(@user1)
@sub2 = @quiz.generate_submission(@user2)
user_session @teacher
get 'managed_quiz_data', :course_id => @course.id, :quiz_id => @quiz.id
assigns[:submissions_from_users][@sub1.user_id].should == @sub1
assigns[:submissions_from_users][@sub2.user_id].should == @sub2
assigns[:submitted_students].sort_by(&:id).should == [@user1, @user2].sort_by(&:id)
user_session @ta1
get 'managed_quiz_data', :course_id => @course.id, :quiz_id => @quiz.id
assigns[:submissions_from_users][@sub1.user_id].should == @sub1
assigns[:submitted_students].should == [@user1]
end
it "should include survey results from logged out users in a public course" do
#logged out user
user = temporary_user_code
#make questions
questions = [{:question_data => { :name => "test 1" }},
{:question_data => { :name => "test 2" }},
{:question_data => { :name => "test 3" }},
{:question_data => { :name => "test 4" }}]
logged_out_survey_with_submission user, questions
get 'managed_quiz_data', :course_id => @course.id, :quiz_id => @quiz.id
assigns[:submissions_from_logged_out].should == [@quiz_submission]
assigns[:submissions_from_users].should == {}
end
it "should include survey results from a logged-in user in a public course" do
course_with_teacher_logged_in(:active_all => true)
@user1 = user_with_pseudonym(:active_all => true, :name => 'Student1', :username => '[email protected]')
@course.enroll_student(@user1)
questions = [{:question_data => { :name => "test 1" }},
{:question_data => { :name => "test 2" }},
{:question_data => { :name => "test 3" }},
{:question_data => { :name => "test 4" }}]
@assignment = @course.assignments.create(:title => "Test Assignment")
@assignment.workflow_state = "available"
@assignment.submission_types = "online_quiz"
@assignment.save
@quiz = Quizzes::Quiz.find_by_assignment_id(@assignment.id)
@quiz.anonymous_submissions = true
@quiz.quiz_type = "survey"
@questions = questions.map { |q| @quiz.quiz_questions.create!(q) }
@quiz.generate_quiz_data
@quiz.save!
@quiz_submission = @quiz.generate_submission(@user1)
@quiz_submission.mark_completed
get 'managed_quiz_data', :course_id => @course.id, :quiz_id => @quiz.id
assigns[:submissions_from_users][@quiz_submission.user_id].should == @quiz_submission
assigns[:submitted_students].should == [@user1]
end
end
describe "GET 'moderate'" do
it "should require authorization" do
course_with_teacher(:active_all => true)
course_quiz
get 'moderate', :course_id => @course.id, :quiz_id => @quiz.id
assert_unauthorized
end
it "should assign variables" do
@student = course_with_student(:active_all => true).user
course_with_teacher_logged_in(:course => @course, :active_all => true)
course_quiz
@sub = @quiz.generate_submission(@student)
get 'moderate', :course_id => @course.id, :quiz_id => @quiz.id
assigns[:quiz].should == @quiz
assigns[:students].should == [@student]
assigns[:submissions].should == [@sub]
end
it "should respect section privilege limitations" do
course_with_teacher(:active_all => 1)
@section = @course.course_sections.create!(:name => 'section 2')
@user2 = user_with_pseudonym(:active_all => true, :name => 'Student2', :username => '[email protected]')
@section.enroll_user(@user2, 'StudentEnrollment', 'active')
@user1 = user_with_pseudonym(:active_all => true, :name => 'Student1', :username => '[email protected]')
@course.enroll_student(@user1)
@ta1 = user_with_pseudonym(:active_all => true, :name => 'TA1', :username => '[email protected]')
@course.enroll_ta(@ta1).update_attribute(:limit_privileges_to_course_section, true)
course_quiz
@sub1 = @quiz.generate_submission(@user1)
@sub2 = @quiz.generate_submission(@user2)
user_session @teacher
get 'moderate', :course_id => @course.id, :quiz_id => @quiz.id
assigns[:students].sort_by(&:id).should == [@user1, @user2].sort_by(&:id)
assigns[:submissions].sort_by(&:id).should == [@sub1, @sub2].sort_by(&:id)
user_session @ta1
get 'moderate', :course_id => @course.id, :quiz_id => @quiz.id
assigns[:students].should == [@user1]
assigns[:submissions].should == [@sub1]
end
end
describe "GET 'moderate' with fabulous quizzes enabled" do
before :each do
a = Account.default
a.enable_feature! :draft_state
a.enable_fabulous_quizzes!
a.save!
course_with_teacher_logged_in(:active_all => true)
course_quiz
end
it "should redirect to fabulous quizzes app" do
a = Account.default
a.enable_fabulous_quizzes?.should eql true
get 'moderate', :course_id => @course.id, :quiz_id => @quiz.id
assert_redirected_to ember_urls.course_quiz_moderate_url(@quiz.id)
end
end
describe "POST 'take'" do
it "should require authorization" do
course_with_student(:active_all => true)
course_quiz(true)
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
assert_unauthorized
end
it "should allow taking the quiz" do
course_with_student_logged_in(:active_all => true)
course_quiz(true)
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
response.should redirect_to("/courses/#{@course.id}/quizzes/#{@quiz.id}/take")
end
context 'asset access logging' do
before :each do
Setting.set('enable_page_views', 'db')
course_with_teacher_logged_in(:active_all => true)
course_quiz
end
it 'should log a single entry with an action level of "participate"' do
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
assigns[:access].should_not be_nil
assigns[:accessed_asset].should_not be_nil
assigns[:accessed_asset][:level].should == 'participate'
assigns[:access].participate_score.should == 1
end
it 'should not log entries when resuming the quiz' do
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
assigns[:access].should_not be_nil
assigns[:accessed_asset].should_not be_nil
assigns[:accessed_asset][:level].should == 'participate'
assigns[:access].participate_score.should == 1
# Since the second request we will make is handled by the same controller
# instance, @accessed_asset must be reset otherwise
# ApplicationController#log_page_view will use it to log another entry.
controller.instance_variable_set('@accessed_asset', nil)
controller.js_env.clear
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
assigns[:access].participate_score.should == 1
end
end
context 'verification' do
before do
course_with_student_logged_in(:active_all => true)
course_quiz(true)
@quiz.access_code = 'bacon'
@quiz.save!
end
it "should render verification page if password required" do
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
response.should render_template('access_code')
end
it "shouldn't let you in on a bad access code" do
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1', :access_code => 'wrongpass'
response.should_not be_redirect
response.should render_template('access_code')
end
it "should send you to take with the right access code" do
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1', :access_code => 'bacon'
response.should redirect_to("/courses/#{@course.id}/quizzes/#{@quiz.id}/take")
end
it "should not ask for the access code again if you reload the quiz" do
get 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1', :access_code => 'bacon'
response.should_not be_redirect
response.should_not render_template('access_code')
controller.js_env.clear
get 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
response.should_not render_template('access_code')
end
end
it "should not let them take the quiz if it's locked" do
course_with_student_logged_in(:active_all => true)
course_quiz(true)
@quiz.locked = true
@quiz.save!
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
response.should render_template('show')
assigns[:locked].should_not be_nil
end
it "should let them take the quiz if it's locked but unlocked by an override" do
course_with_student_logged_in(:active_all => true)
course_quiz(true)
@quiz.lock_at = Time.now
@quiz.save!
override = AssignmentOverride.new
override.title = "ADHOC quiz override"
override.quiz = @quiz
override.lock_at = Time.now + 1.day
override.lock_at_overridden = true
override.save!
override_student = override.assignment_override_students.build
override_student.user = @user
override_student.save!
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
response.should redirect_to("/courses/#{@course.id}/quizzes/#{@quiz.id}/take")
end
it "should let them take the quiz if it's locked but they've been explicitly unlocked" do
course_with_student_logged_in(:active_all => true)
course_quiz(true)
@quiz.locked = true
@quiz.save!
@sub = Quizzes::SubmissionManager.new(@quiz).find_or_create_submission(@user, nil, 'settings_only')
@sub.manually_unlocked = true
@sub.save!
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
response.should redirect_to("/courses/#{@course.id}/quizzes/#{@quiz.id}/take")
end
it "should use default duration if no extensions specified" do
course_with_student_logged_in(:active_all => true)
course_quiz(true)
@quiz.time_limit = 60
@quiz.save!
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
response.should redirect_to("/courses/#{@course.id}/quizzes/#{@quiz.id}/take")
assigns[:submission].should_not be_nil
assigns[:submission].user.should eql(@user)
(assigns[:submission].end_at - assigns[:submission].started_at).to_i.should eql(60.minutes.to_i)
end
it "should give user more time if specified" do
course_with_student_logged_in(:active_all => true)
course_quiz(true)
@quiz.time_limit = 60
@quiz.save!
@sub = Quizzes::SubmissionManager.new(@quiz).find_or_create_submission(@user, nil, 'settings_only')
@sub.extra_time = 30
@sub.save!
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
response.should redirect_to("/courses/#{@course.id}/quizzes/#{@quiz.id}/take")
assigns[:submission].should_not be_nil
assigns[:submission].user.should eql(@user)
(assigns[:submission].end_at - assigns[:submission].started_at).to_i.should eql(90.minutes.to_i)
end
it "should render ip_filter page if ip_filter doesn't match" do
course_with_student_logged_in(:active_all => true)
course_quiz(true)
@quiz.ip_filter = '123.123.123.123'
@quiz.save!
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
response.should render_template('invalid_ip')
end
it "should let the user take the quiz if the ip_filter matches" do
course_with_student_logged_in(:active_all => true)
course_quiz(true)
@quiz.ip_filter = '123.123.123.123'
@quiz.save!
request.env['REMOTE_ADDR'] = '123.123.123.123'
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
response.should redirect_to("/courses/#{@course.id}/quizzes/#{@quiz.id}/take")
end
it "should work without a user for non-graded quizzes in public courses" do
course_with_student :active_all => true
@course.update_attribute :is_public, true
course_quiz :active
@quiz.update_attribute :quiz_type, 'practice_quiz'
post 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
response.should redirect_to("/courses/#{@course.id}/quizzes/#{@quiz.id}/take")
end
end
describe "GET 'take'" do
it "should require authorization" do
course_with_student(:active_all => true)
course_quiz(true)
get 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
assert_unauthorized
end
it "should render the quiz page if the user hasn't started the quiz" do
course_with_student_logged_in(:active_all => true)
course_quiz(true)
get 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
response.should render_template('show')
end
it "should render ip_filter page if the ip_filter stops matching" do
course_with_student_logged_in(:active_all => true)
course_quiz(true)
@quiz.ip_filter = '123.123.123.123'
@quiz.save!
@quiz.generate_submission(@user)
get 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
response.should render_template('invalid_ip')
end
it "should allow taking the quiz" do
course_with_student_logged_in(:active_all => true)
course_quiz(true)
@quiz.generate_submission(@user)
get 'show', :course_id => @course, :quiz_id => @quiz.id, :take => '1'
response.should render_template('take_quiz')
assigns[:submission].should_not be_nil
assigns[:submission].user.should eql(@user)
end
context "when the ID of a question is passed in" do
before do
course_with_student_logged_in(:active_all => true)
course_quiz(true)
@quiz.generate_submission(@user)
end
context "a valid question" do
it "renders take_quiz" do
Quizzes::QuizzesController.any_instance.stubs(:valid_question?).returns(true)
get 'show', :course_id => @course, :quiz_id => @quiz.id, :question_id => '1', :take => '1'
response.should render_template('take_quiz')
end
end
context "a question not in this quiz" do
it "redirects to the main quiz page" do
Quizzes::QuizzesController.any_instance.stubs(:valid_question?).returns(false)
get 'show', :course_id => @course, :quiz_id => @quiz.id, :question_id => '1', :take => '1'
response.should redirect_to course_quiz_url(@course, @quiz)
end
end
end
describe "valid_question?" do
let(:submission) { mock }
context "when the passed in question ID is in the submission" do
it "returns true" do
submission.stubs(:has_question?).with(1).returns(true)
controller.send(:valid_question?, submission, 1).should be_true
end
end
context "when the question ID isn't part of the submission" do
it "returns false" do
submission.stubs(:has_question?).with(1).returns(false)
controller.send(:valid_question?, submission, 1).should be_false
end
end
end
end
describe "GET 'history'" do
it "should require authorization" do
course_with_teacher(:active_all => true)
course_quiz
get 'history', :course_id => @course.id, :quiz_id => @quiz.id
assert_unauthorized
end
it "should redirect if there are no submissions for the user" do
course_with_student_logged_in(:active_all => true)
course_quiz
get 'history', :course_id => @course.id, :quiz_id => @quiz.id
response.should be_redirect
response.should redirect_to("/courses/#{@course.id}/quizzes/#{@quiz.id}")
end
it "should assign variables" do
course_with_student_logged_in(:active_all => true)
course_quiz
@submission = @quiz.generate_submission(@user)
get 'history', :course_id => @course.id, :quiz_id => @quiz.id
response.should be_success
assigns[:user].should_not be_nil
assigns[:user].should eql(@user)
assigns[:quiz].should_not be_nil
assigns[:quiz].should eql(@quiz)
assigns[:submission].should_not be_nil
assigns[:submission].should eql(@submission)
end
it "should find the observed submissions" do
course_with_student(:active_all => true)
course_quiz
@submission = @quiz.generate_submission(@student)
@observer = user
@enrollment = @course.enroll_user(@observer, 'ObserverEnrollment', :enrollment_state => 'active')
@enrollment.update_attribute(:associated_user, @student)
user_session(@observer)
get 'history', :course_id => @course.id, :quiz_id => @quiz.id, :user_id => @student.id
response.should be_success
assigns[:user].should_not be_nil
assigns[:user].should eql(@student)
assigns[:quiz].should_not be_nil
assigns[:quiz].should eql(@quiz)
assigns[:submission].should_not be_nil
assigns[:submission].should eql(@submission)
end
it "should not allow viewing other submissions if not a teacher" do
u = user(:active_all => true)
course_with_student_logged_in(:active_all => true)
course_quiz
s = @quiz.generate_submission(u)
@submission = @quiz.generate_submission(@user)
get 'history', :course_id => @course.id, :quiz_id => @quiz.id, :user_id => u.id
response.should_not be_success
end
it "should allow viewing other submissions if a teacher" do
u = user(:active_all => true)
course_with_teacher_logged_in(:active_all => true)
@course.enroll_student(u)
course_quiz
s = @quiz.generate_submission(u)
@submission = @quiz.generate_submission(@user)
get 'history', :course_id => @course.id, :quiz_id => @quiz.id, :user_id => u.id
response.should be_success
assigns[:user].should_not be_nil
assigns[:user].should eql(u)
assigns[:quiz].should_not be_nil
assigns[:quiz].should eql(@quiz)
assigns[:submission].should_not be_nil
assigns[:submission].should eql(s)
end
it "should not allow student viewing if the assignment is muted" do
u = user(:active_all => true)
course_with_student_logged_in(:active_all => true)
course_quiz
@quiz.generate_quiz_data
@quiz.workflow_state = 'available'
@quiz.published_at = Time.now
@quiz.save
@quiz.assignment.should_not be_nil
@quiz.assignment.mute!
s = @quiz.generate_submission(u)
@submission = @quiz.generate_submission(@user)
get 'history', :course_id => @course.id, :quiz_id => @quiz.id, :user_id => u.id
response.should be_redirect
response.should redirect_to("/courses/#{@course.id}/quizzes/#{@quiz.id}")
flash[:notice].should match(/You cannot view the quiz history while the quiz is muted/)
end
it "should allow teacher viewing if the assignment is muted" do
u = user(:active_all => true)
course_with_teacher_logged_in(:active_all => true)
@course.enroll_student(u)
course_quiz
@quiz.generate_quiz_data
@quiz.workflow_state = 'available'
@quiz.published_at = Time.now
@quiz.save
@quiz.assignment.should_not be_nil
@quiz.assignment.mute!
s = @quiz.generate_submission(u)
@submission = @quiz.generate_submission(@user)
get 'history', :course_id => @course.id, :quiz_id => @quiz.id, :user_id => u.id
response.should be_success
end
end
describe "POST 'create'" do
it "should require authorization" do
course_with_teacher(:active_all => true)
post 'create', :course_id => @course.id
assert_unauthorized
end
it "should not allow students to create quizzes" do
course_with_student_logged_in(:active_all => true)
post 'create', :course_id => @course.id, :quiz => {:title => "some quiz"}
assert_unauthorized
end
it "should create quiz" do
course_with_teacher_logged_in(:active_all => true)
post 'create', :course_id => @course.id, :quiz => {:title => "some quiz"}
assigns[:quiz].should_not be_nil
assigns[:quiz].title.should eql("some quiz")
response.should be_success
end
it "creates quizzes with overrides" do
course_with_teacher_logged_in(:active_all => true)
section = @course.course_sections.create!
course_due_date = 3.days.from_now.iso8601
section_due_date = 5.days.from_now.iso8601
post 'create', :course_id => @course.id,
:quiz => {
:title => "overridden quiz",
:due_at => course_due_date,
:assignment_overrides => [{
:course_section_id => section.id,
:due_at => section_due_date,
}]
}
response.should be_success
quiz = assigns[:quiz].overridden_for(@user)
overrides = quiz.overrides_visible_to(@user)
overrides.length.should == 1
overrides.first[:due_at].iso8601.should == section_due_date
end
end
describe "PUT 'update'" do
it "should require authorization" do
course_with_teacher(:active_all => true)
course_quiz
put 'update', :course_id => @course.id, :id => @quiz.id, :quiz => {:title => "test"}
assert_unauthorized
end
it "should not allow students to update quizzes" do
course_with_student_logged_in(:active_all => true)
course_quiz
post 'update', :course_id => @course.id, :id => @quiz.id, :quiz => {:title => "some quiz"}
assert_unauthorized
end
it "should update quizzes" do
course_with_teacher_logged_in(:active_all => true)
course_quiz
post 'update', :course_id => @course.id, :id => @quiz.id, :quiz => {:title => "some quiz"}
assigns[:quiz].should_not be_nil
assigns[:quiz].should eql(@quiz)
assigns[:quiz].title.should eql("some quiz")
end
it "should be able to change ungraded survey to quiz without error" do
# aka should handle the case where the quiz's assignment is nil/not present.
course_with_teacher_logged_in(active_all: true)
course_quiz
@quiz.update_attributes(quiz_type: 'ungraded_survey')
# make sure the assignment doesn't exist
@quiz.assignment = nil if @quiz.context.feature_enabled?(:draft_state)
@quiz.assignment.should_not be_present
post 'update', course_id: @course.id, id: @quiz.id, activate: true,
quiz: {quiz_type: 'assignment'}
response.should be_redirect
@quiz.reload.quiz_type.should == 'assignment'
@quiz.should be_available
@quiz.assignment.should be_present
end
it "should lock and unlock without removing assignment" do
course_with_teacher_logged_in(:active_all => true)
a = @course.assignments.create!(:title => "some assignment", :points_possible => 5)
a.points_possible.should eql(5.0)
a.submission_types.should_not eql("online_quiz")
@quiz = @course.quizzes.build(:assignment_id => a.id, :title => "some quiz", :points_possible => 10)
@quiz.workflow_state = 'available'
@quiz.save