-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtest_student_view.py
1676 lines (1513 loc) · 66 KB
/
test_student_view.py
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
# coding=utf-8
# pylint: disable=too-many-lines, invalid-name
"""
All tests for the api.py
"""
import itertools
import json
from datetime import datetime, timedelta
from unittest.mock import MagicMock, patch
import ddt
import pytz
from freezegun import freeze_time
from django.test.utils import override_settings
from django.urls import reverse
from edx_proctoring.api import (
add_allowance_for_user,
get_current_exam_attempt,
get_exam_attempt_by_id,
get_exam_by_id,
get_student_view,
update_attempt_status,
update_exam
)
from edx_proctoring.constants import DEFAULT_DESKTOP_APPLICATION_PING_INTERVAL_SECONDS
from edx_proctoring.models import ProctoredExam, ProctoredExamStudentAllowance, ProctoredExamStudentAttempt
from edx_proctoring.runtime import set_runtime_service
from edx_proctoring.statuses import ProctoredExamStudentAttemptStatus
from edx_proctoring.tests import mock_perm
from edx_proctoring.utils import humanized_time
from .test_services import (
MockCreditService,
MockCreditServiceNone,
MockCreditServiceWithCourseEndDate,
MockInstructorService
)
from .test_utils.utils import ProctoredExamTestCase
@patch('django.urls.reverse', MagicMock)
@ddt.ddt
class ProctoredExamStudentViewTests(ProctoredExamTestCase):
"""
All tests for the student view
"""
def setUp(self):
"""
Build out test harnessing
"""
super().setUp()
self.proctored_exam_id = self._create_proctored_exam()
self.timed_exam_id = self._create_timed_exam()
self.practice_exam_id = self._create_practice_exam()
self.onboarding_exam_id = self._create_onboarding_exam()
self.disabled_exam_id = self._create_disabled_exam()
# Messages for get_student_view
self.start_an_exam_msg = 'This exam is proctored'
self.exam_expired_msg = 'The due date for this exam has passed'
self.timed_exam_msg = '{exam_name} is a Timed Exam'
self.timed_exam_submitted = 'You have submitted your timed exam.'
self.timed_exam_expired = 'The time allotted for this exam has expired.'
self.timed_exam_submitted_expired = 'The time allotted for this exam has expired. Your exam has been submitted'
self.submitted_timed_exam_msg_with_due_date = 'After the due date has passed,'
self.exam_time_expired_msg = 'You did not complete the exam in the allotted time'
self.exam_time_error_msg = 'A system error has occurred with your proctored exam'
self.chose_proctored_exam_msg = 'Set up and start your proctored exam'
self.proctored_exam_optout_msg = 'Take this exam without proctoring'
self.proctored_exam_completed_msg = 'Are you sure you want to end your proctored exam'
self.proctored_exam_submitted_msg = 'You have submitted this proctored exam for review'
self.proctored_exam_ready_to_resume_msg = 'Your exam is ready to be resumed.'
self.take_exam_without_proctoring_msg = 'Take this exam without proctoring'
self.ready_to_start_msg = 'Important'
self.wrong_browser_msg = 'The content of this exam can only be viewed'
self.footer_msg = 'About Proctored Exams'
self.timed_footer_msg = 'Can I request additional time to complete my exam?'
self.wait_deadline_msg = "The result will be visible after"
self.inactive_account_msg = "You have not activated your account"
self.review_exam_msg = "To view your exam questions and responses"
def _render_exam(self, content_id, context_overrides=None):
"""
Renders a test exam.
"""
exam = get_exam_by_id(content_id)
context = {
'is_proctored': True,
'allow_proctoring_opt_out': True,
'display_name': self.exam_name,
'default_time_limit_mins': 90,
'is_practice_exam': False,
'credit_state': {
'enrollment_mode': 'verified',
'credit_requirement_status': [],
},
'is_integrity_signature_enabled': False,
}
if context_overrides:
context.update(context_overrides)
return get_student_view(
user_id=self.user_id,
course_id=exam['course_id'],
content_id=exam['content_id'],
context=context,
)
def render_proctored_exam(self, context_overrides=None):
"""
Renders a test proctored exam.
"""
exam_context_overrides = {
'is_proctored': True,
'allow_proctoring_opt_out': True,
'is_practice_exam': False,
'credit_state': {
'enrollment_mode': 'verified',
'credit_requirement_status': [],
},
}
if context_overrides:
exam_context_overrides.update(context_overrides)
return self._render_exam(
self.proctored_exam_id,
context_overrides=exam_context_overrides
)
def render_practice_exam(self, context_overrides=None):
"""
Renders a test practice exam.
"""
exam_context_overrides = {
'is_proctored': True,
'is_practice_exam': True,
}
if context_overrides:
exam_context_overrides.update(context_overrides)
return self._render_exam(
self.practice_exam_id,
context_overrides=exam_context_overrides
)
def render_onboarding_exam(self):
"""
Renders a test practice exam.
"""
return self._render_exam(self.onboarding_exam_id)
def test_get_student_view(self):
"""
Test for get_student_view prompting the user to take the exam
as a timed exam or a proctored exam.
"""
rendered_response = self.render_proctored_exam()
self.assertIn(
f'data-exam-id="{self.proctored_exam_id}"',
rendered_response
)
self.assertIn(self.start_an_exam_msg, rendered_response)
# try practice exam variant
rendered_response = self.render_practice_exam()
self.assertIn(
'sequence proctored-exam entrance',
rendered_response
)
def test_get_honor_view_with_practice_exam(self):
"""
Test for get_student_view prompting when the student is enrolled in non-verified
track for a practice exam, this should return not None, meaning
student will see proctored content
"""
rendered_response = self.render_practice_exam({
'credit_state': {
'enrollment_mode': 'honor',
},
})
self.assertIsNotNone(rendered_response)
def test_proctored_only_entrance(self):
"""
This test verifies that learners are not given the option to take
an exam without proctoring if allow_proctoring_opt_out is false.
"""
rendered_response = self.render_proctored_exam({
'allow_proctoring_opt_out': False,
})
self.assertNotIn(self.take_exam_without_proctoring_msg, rendered_response)
@ddt.data(
'pending',
'failed',
)
def test_proctored_only_with_prereqs(self, status):
"""
This test verifies that learners are not given the option to take
an exam without proctoring when they have prerequisites and when
the setting allow_proctoring_opt_out is false.
"""
rendered_response = self.render_proctored_exam({
'allow_proctoring_opt_out': False,
'credit_state': {
'enrollment_mode': 'verified',
'credit_requirement_status': [
{
'namespace': 'proctored_exam',
'name': 'foo',
'display_name': 'Mock Requirement',
'status': status,
'order': 0
}
]
},
})
self.assertNotIn(self.take_exam_without_proctoring_msg, rendered_response)
@ddt.data(
('reverification', None, 'The following prerequisites are in a <strong>pending</strong> state', True),
('reverification', 'pending', 'The following prerequisites are in a <strong>pending</strong> state', True),
('reverification', 'failed', 'You did not satisfy the following prerequisites', True),
('reverification', 'satisfied', 'To be eligible for credit', False),
('reverification', 'declined', None, False),
('proctored_exam', None, 'The following prerequisites are in a <strong>pending</strong> state', True),
('proctored_exam', 'pending', 'The following prerequisites are in a <strong>pending</strong> state', True),
('proctored_exam', 'failed', 'You did not satisfy the following prerequisites', True),
('proctored_exam', 'satisfied', 'To be eligible for credit', False),
('proctored_exam', 'declined', None, False),
('grade', 'failed', 'To be eligible for credit', False),
# this is nonsense, but let's double check it
('grade', 'declined', 'To be eligible for credit', False),
)
@ddt.unpack
def test_prereq_scenarios(self, namespace, req_status, expected_content, should_see_prereq):
"""
This test asserts that proctoring will not be displayed under the following
conditions:
- Verified student has not completed all 'reverification' requirements
"""
# user hasn't attempted reverifications
rendered_response = self.render_proctored_exam({
'credit_state': {
'enrollment_mode': 'verified',
'credit_requirement_status': [
{
'namespace': namespace,
'name': 'foo',
'display_name': 'Foo Requirement',
'status': req_status,
'order': 0
}
]
},
})
if expected_content:
self.assertIn(expected_content, rendered_response)
else:
self.assertIsNone(rendered_response)
if req_status == 'declined' and not expected_content:
# also we should have auto-declined if a pre-requisite was declined
attempt = get_current_exam_attempt(self.proctored_exam_id, self.user_id)
self.assertIsNotNone(attempt)
self.assertEqual(attempt['status'], ProctoredExamStudentAttemptStatus.declined)
if should_see_prereq:
self.assertIn('Foo Requirement', rendered_response)
def test_student_view_non_student(self):
"""
Make sure that if we ask for a student view if we are not in a student role,
then we don't see any proctoring views
"""
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id,
context={
'is_proctored': True,
'display_name': self.exam_name,
'default_time_limit_mins': 90
},
user_role='staff'
)
self.assertIsNone(rendered_response)
def test_wrong_exam_combo(self):
"""
Verify that we get a None back when rendering a view
for a practice, non-proctored exam. This is unsupported.
"""
rendered_response = get_student_view(
user_id=self.user_id,
course_id='foo',
content_id='bar',
context={
'is_proctored': False,
'is_practice_exam': True,
'display_name': self.exam_name,
'default_time_limit_mins': 90,
'hide_after_due': False,
},
user_role='student'
)
self.assertIsNone(rendered_response)
def test_proctored_exam_passed_end_date(self):
"""
Verify that we get a None back on a proctored exam
if the course end date is passed
"""
credit_state = MockCreditServiceWithCourseEndDate().get_credit_state(self.user_id, 'foo', True)
rendered_response = get_student_view(
user_id=self.user_id,
course_id='foo',
content_id='bar',
context={
'is_proctored': True,
'is_practice_exam': False,
'display_name': self.exam_name,
'default_time_limit_mins': 90,
'due_date': None,
'hide_after_due': False,
'credit_state': credit_state,
},
user_role='student'
)
self.assertIsNone(rendered_response)
def test_practice_exam_passed_end_date(self):
"""
Verify that we get a None back on a practice exam
if the course end date is passed
"""
credit_state = MockCreditServiceWithCourseEndDate().get_credit_state(self.user_id, 'foo', True)
rendered_response = get_student_view(
user_id=self.user_id,
course_id='foo',
content_id='bar',
context={
'is_proctored': True,
'is_practice_exam': True,
'display_name': self.exam_name,
'default_time_limit_mins': 90,
'due_date': None,
'hide_after_due': False,
'credit_state': credit_state,
},
user_role='student'
)
self.assertIsNone(rendered_response)
def test_get_disabled_student_view(self):
"""
Assert that a disabled proctored exam will not override the
student_view
"""
self.assertIsNone(
get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.disabled_content_id,
context={
'is_proctored': True,
'display_name': self.exam_name,
'default_time_limit_mins': 90
}
)
)
def test_student_response_without_credit_state(self):
"""
Test that response is not None for users who are not enrolled.
"""
set_runtime_service('credit', MockCreditServiceNone())
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id,
context={
'is_proctored': True,
'display_name': self.exam_name,
'default_time_limit_mins': 90
},
user_role='student'
)
self.assertIsNotNone(rendered_response)
def test_proctoring_instruction_without_software_download_link(self):
"""
Test for get_student_view proctored exam without software download link.
Other providers could have no onboarding step requires software download
Redundant `Start System Check` button is absent in that case.
"""
self._create_unstarted_exam_attempt()
rendered_response = self.render_proctored_exam()
self.assertNotIn('id="software_download_link"', rendered_response)
@ddt.data(False, True)
def test_get_studentview_unstarted_exam(self, allow_proctoring_opt_out):
"""
Test for get_student_view proctored exam which has not started yet.
"""
attempt = self._create_unstarted_exam_attempt()
# Verify that the option to skip proctoring is shown if allowed
rendered_response = self.render_proctored_exam({
'allow_proctoring_opt_out': allow_proctoring_opt_out,
})
self.assertIn(self.chose_proctored_exam_msg, rendered_response)
if allow_proctoring_opt_out:
self.assertIn(self.proctored_exam_optout_msg, rendered_response)
else:
self.assertNotIn(self.proctored_exam_optout_msg, rendered_response)
# Now make sure content remains the same if the status transitions
# to 'download_software_clicked'.
update_attempt_status(
attempt.id,
ProctoredExamStudentAttemptStatus.download_software_clicked
)
rendered_response = self.render_proctored_exam()
self.assertIn(self.chose_proctored_exam_msg, rendered_response)
self.assertIn(self.proctored_exam_optout_msg, rendered_response)
def test_get_studentview_unstarted_practice_exam(self):
"""
Test for get_student_view Practice exam which has not started yet.
"""
self._create_unstarted_exam_attempt(is_practice=True)
rendered_response = self.render_practice_exam()
self.assertIn(self.chose_proctored_exam_msg, rendered_response)
self.assertNotIn(self.proctored_exam_optout_msg, rendered_response)
def test_declined_attempt(self):
"""
Make sure that a declined attempt does not show proctoring
"""
attempt_obj = self._create_unstarted_exam_attempt()
attempt_obj.status = ProctoredExamStudentAttemptStatus.declined
attempt_obj.save()
rendered_response = self.render_proctored_exam()
self.assertIsNone(rendered_response)
def test_get_studentview_ready(self):
"""
Assert that we get the right content
when the exam is ready to be started
"""
exam_attempt = self._create_started_exam_attempt()
exam_attempt.status = ProctoredExamStudentAttemptStatus.ready_to_start
exam_attempt.save()
rendered_response = self.render_proctored_exam()
self.assertIn(self.ready_to_start_msg, rendered_response)
def test_get_studentview_started_exam(self):
"""
Test for get_student_view proctored exam which has started.
"""
self._create_started_exam_attempt()
rendered_response = self.render_proctored_exam()
self.assertIsNone(rendered_response)
@patch('edx_proctoring.api.get_backend_provider')
def test_get_studentview_started_from_wrong_browser(self, mocked_get_backend):
"""
Test for get_student_view proctored exam as viewed from an
insecure browser.
"""
self._create_started_exam_attempt()
mocked_get_backend.return_value.should_block_access_to_exam_material.return_value = True
rendered_response = self.render_proctored_exam()
self.assertIn(self.wrong_browser_msg, rendered_response)
def test_get_studentview_started_practice_exam(self):
"""
Test for get_student_view practice proctored exam which has started.
"""
self._create_started_practice_exam_attempt()
rendered_response = self.render_practice_exam()
self.assertIsNone(rendered_response)
@patch('edx_proctoring.api.get_backend_provider')
def test_get_studentview_practice_from_wrong_browser(self, mocked_get_backend):
"""
Test for get_student_view practice proctored exam as viewed
from an insecure browser.
"""
self._create_started_practice_exam_attempt()
mocked_get_backend.return_value.should_block_access_to_exam_material.return_value = True
# Need to make sure our mock doesn't behave like a different
# type of backend before we reach to code under test
mocked_get_backend.return_value.supports_onboarding = False
rendered_response = self.render_practice_exam()
self.assertIn(self.wrong_browser_msg, rendered_response)
def test_get_studentview_started_timed_exam(self):
"""
Test for get_student_view timed exam which has started.
"""
self._create_started_exam_attempt(is_proctored=False)
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id_timed,
context={
'is_proctored': True,
'display_name': self.exam_name,
'default_time_limit_mins': 90
}
)
self.assertIsNone(rendered_response)
@ddt.data(True, False)
def test_get_studentview_long_limit(self, under_exception):
"""
Test for hide_extra_time_footer on exams with > 20 hours time limit
"""
exam_id = self._create_exam_with_due_time(is_proctored=False, )
if under_exception:
update_exam(exam_id, time_limit_mins=((20 * 60))) # exactly 20 hours
else:
update_exam(exam_id, time_limit_mins=(20 * 60) + 1) # 1 minute greater than 20 hours
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id_for_exam_with_due_date,
context={
'is_proctored': False,
'display_name': self.exam_name,
}
)
if under_exception:
self.assertIn(self.timed_footer_msg, rendered_response)
else:
self.assertNotIn(self.timed_footer_msg, rendered_response)
@ddt.data(
(datetime.now(pytz.UTC) + timedelta(days=1), False),
(datetime.now(pytz.UTC) - timedelta(days=1), False),
(datetime.now(pytz.UTC) - timedelta(days=1), True),
)
@ddt.unpack
def test_get_studentview_submitted_timed_exam_with_past_due_date(self, due_date, hide_after_due):
"""
Test for get_student_view timed exam with the due date.
"""
# exam is created with due datetime which has already passed
exam_id = self._create_exam_with_due_time(is_proctored=False, due_date=due_date)
if hide_after_due:
update_exam(exam_id, hide_after_due=hide_after_due)
# now create the timed_exam attempt in the submitted state
self._create_exam_attempt(exam_id, status='submitted')
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id_for_exam_with_due_date,
context={
'is_proctored': False,
'display_name': self.exam_name,
'default_time_limit_mins': 10,
'due_date': due_date,
}
)
if datetime.now(pytz.UTC) < due_date:
self.assertIn(self.timed_exam_submitted, rendered_response)
self.assertIn(self.submitted_timed_exam_msg_with_due_date, rendered_response)
elif hide_after_due:
self.assertIn(self.timed_exam_submitted, rendered_response)
self.assertNotIn(self.submitted_timed_exam_msg_with_due_date, rendered_response)
else:
self.assertIsNone(rendered_response)
@ddt.data(
datetime(2019, 4, 4).replace(tzinfo=pytz.UTC),
datetime.now(pytz.UTC) + timedelta(days=2),
datetime(9999, 4, 4).replace(tzinfo=pytz.UTC),
'bad'
)
@patch('edx_when.api.get_dates_for_course')
def test_get_studentview_submitted_timed_exam_past_course_end(self, end_date, mock_course_dates):
"""
Test get_student_view timed exam returns None if we are passed the course end date
This is to fix inconsistent behavior for timed exam visibility in self paced courses,
where timed exams would be visible after a self paced due date, but not visible after
the course end date. Timed exams in instructor-paced courses are always visible after
the exam due date, and therefore after the course end date.
"""
dates = [
(('11111111', 'start'), datetime(2019, 3, 15).replace(tzinfo=pytz.UTC)),
(('22222222', 'due'), datetime(2019, 3, 28).replace(tzinfo=pytz.UTC)),
(('33333333', 'end'), end_date),
]
mock_course_dates.return_value = dict(dates)
self._create_exam_attempt(self.timed_exam_id, status='submitted')
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id_timed,
context={
'is_proctored': False,
'display_name': self.exam_name,
'default_time_limit_mins': 10,
}
)
if not isinstance(end_date, str) and end_date < datetime.now(pytz.UTC):
self.assertIsNone(rendered_response)
else:
self.assertIn(self.timed_exam_submitted, rendered_response)
@ddt.data(
True,
False
)
@patch('edx_when.api.get_dates_for_course')
def test_get_studentview_submitted_timed_exam_past_course_end_no_date(self, include_end, mock_course_dates):
"""
Test get_student_view timed exam returns blocks learners if no end date is specified
This is to fix inconsistent behavior for timed exam visibility in self paced courses,
where timed exams would be visible after a self paced due date, but not visible after
the course end date. Timed exams in instructor-paced courses are always visible after
the exam due date, and therefore after the course end date.
"""
dates = [
(('11111111', 'start'), datetime(2019, 3, 15).replace(tzinfo=pytz.UTC)),
(('22222222', 'due'), datetime(2019, 3, 28).replace(tzinfo=pytz.UTC)),
]
if include_end:
dates.append((('33333333', 'end'), None))
mock_course_dates.return_value = dict(dates)
self._create_exam_attempt(self.timed_exam_id, status='submitted')
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id_timed,
context={
'is_proctored': False,
'display_name': self.exam_name,
'default_time_limit_mins': 10,
}
)
self.assertIn(self.timed_exam_submitted, rendered_response)
@ddt.data(
(False, 'submitted', True, 1),
(True, 'verified', False, 1),
(False, 'submitted', True, 0),
(True, 'verified', False, 0),
)
@ddt.unpack
def test_get_studentview_submitted_timed_exam_with_grace_period(self, is_proctored, status, is_timed, graceperiod):
"""
Test the student view for a submitted exam, after the
due date, when grace period is in effect.
Scenario: Given an exam with past due
When a user submission exists for that exam
Then get the user view with an active grace period
Then user will not be able to see exam content
And a banner will be visible
If the grace period is past due
For timed exam, user will not see any banner
And user will be able to see exam contents
And For proctored exam, view exam button will be visible
"""
due_date = datetime.now(pytz.UTC) - timedelta(days=1)
context = {
'is_proctored': is_proctored,
'display_name': self.exam_name,
'default_time_limit_mins': 10,
'due_date': due_date,
'grace_period': timedelta(days=2)
}
exam_id = self._create_exam_with_due_time(
is_proctored=is_proctored, due_date=due_date
)
self._create_exam_attempt(exam_id, status=status)
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id_for_exam_with_due_date,
context=context
)
self.assertIn(self.wait_deadline_msg, rendered_response)
# This pop is required as the student view updates the
# context dict that was passed in the arguments
context.pop('wait_deadline')
context['grace_period'] = timedelta(days=graceperiod)
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id_for_exam_with_due_date,
context=context
)
if is_timed:
self.assertIsNone(rendered_response)
else:
self.assertNotIn(self.wait_deadline_msg, rendered_response)
@patch("edx_proctoring.api.constants.CONTENT_VIEWABLE_PAST_DUE_DATE", True)
def test_get_studentview_acknowledged_proctored_exam_with_grace_period(self):
"""
Verify the student view for an acknowledge proctored exam with an active
grace period.
Given a proctored exam with a past due date and an inactive grace period
And a verified user submission exists for that exam
When user navigates to the exam
Then the wait deadline part is not shown
If the attempt is acknowledged to view the exam result
Then visiting the page again will not show any banner
When an active grace period is applied
Then navigating to the exam will not exam content
And the wait deadline will be shown
"""
due_date = datetime.now(pytz.UTC) - timedelta(days=1)
context = {
'is_proctored': True,
'display_name': self.exam_name,
'default_time_limit_mins': 10,
'due_date': due_date,
'grace_period': timedelta(days=0)
}
exam_id = self._create_exam_with_due_time(
is_proctored=True, due_date=due_date
)
attempt = self._create_exam_attempt(exam_id, status='verified')
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id_for_exam_with_due_date,
context=context
)
self.assertNotIn(self.wait_deadline_msg, rendered_response)
attempt.is_status_acknowledged = True
attempt.save()
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id_for_exam_with_due_date,
context=context
)
self.assertIsNone(rendered_response)
context['grace_period'] = timedelta(days=2)
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id_for_exam_with_due_date,
context=context
)
self.assertIn(self.wait_deadline_msg, rendered_response)
@ddt.data(
False,
True,
)
def test_proctored_exam_attempt_with_past_due_datetime(self, is_onboarding_exam):
"""
Test for get_student_view for proctored exam with past due datetime
"""
due_date = datetime.now(pytz.UTC) + timedelta(days=1)
# exam is created with due datetime which has already passed
self._create_exam_with_due_time(due_date=due_date, is_practice_exam=is_onboarding_exam)
# due_date is exactly after 24 hours, if student arrives after 2 days
# then he can not attempt the proctored exam
reset_time = due_date + timedelta(days=2)
with freeze_time(reset_time):
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id_for_exam_with_due_date,
context={
'is_proctored': True,
'display_name': self.exam_name,
'default_time_limit_mins': self.default_time_limit,
'due_date': due_date,
}
)
self.assertIn(self.exam_expired_msg, rendered_response)
# call the view again, because the first call set the exam attempt to 'expired'
# this second call will render the view based on the state
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id_for_exam_with_due_date,
context={
'is_proctored': True,
'is_practice_exam': is_onboarding_exam,
'display_name': self.exam_name,
'default_time_limit_mins': self.default_time_limit,
'due_date': due_date,
}
)
self.assertIn(self.exam_expired_msg, rendered_response)
def test_timed_exam_attempt_with_past_due_datetime(self):
"""
Test for get_student_view for timed exam with past due datetime
"""
due_date = datetime.now(pytz.UTC) + timedelta(days=1)
# exam is created with due datetime which has already passed
self._create_exam_with_due_time(
due_date=due_date,
is_proctored=False
)
# due_date is exactly after 24 hours, if student arrives after 2 days
# then he can not attempt the proctored exam
reset_time = due_date + timedelta(days=2)
with freeze_time(reset_time):
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id_for_exam_with_due_date,
context={
'is_proctored': False,
'display_name': self.exam_name,
'default_time_limit_mins': self.default_time_limit,
'due_date': due_date,
}
)
self.assertIn(self.exam_expired_msg, rendered_response)
# call the view again, because the first call set the exam attempt to 'expired'
# this second call will render the view based on the state
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id_for_exam_with_due_date,
context={
'is_proctored': True,
'is_practice_exam': True,
'display_name': self.exam_name,
'default_time_limit_mins': self.default_time_limit,
'due_date': due_date,
}
)
self.assertIn(self.exam_expired_msg, rendered_response)
@patch('edx_when.api.get_date_for_block')
@patch('edx_when.api.get_dates_for_course')
def test_timed_exam_attempt_with_past_course_due_date_and_future_exam_due_date(
self, mock_course_dates, mock_exam_due_date
):
"""
When the course due date is in the past, but the exam-specific due date is in the future, students should still
be able to start this exam.
"""
current_date = datetime.now(pytz.UTC)
course_end_date = current_date - timedelta(days=14)
exam_due_date = current_date + timedelta(days=14)
mock_course_dates.return_value = {('dummy', 'end'): course_end_date}
mock_exam_due_date.return_value = exam_due_date
self._create_exam_with_due_time(due_date=exam_due_date, is_proctored=False)
with freeze_time(current_date):
rendered_response = get_student_view(
user_id=self.user_id,
course_id=self.course_id,
content_id=self.content_id_for_exam_with_due_date,
context={},
)
self.assertIn(self.timed_footer_msg, rendered_response)
@patch.dict('django.conf.settings.PROCTORING_SETTINGS', {'ALLOW_TIMED_OUT_STATE': True})
def test_get_studentview_timedout(self):
"""
Verifies that if we call get_studentview when the timer has expired
it will automatically state transition into timed_out
"""
self._create_started_exam_attempt()
reset_time = datetime.now(pytz.UTC) + timedelta(days=1)
with freeze_time(reset_time):
with self.assertRaises(NotImplementedError):
self.render_proctored_exam()
def test_get_studentview_submitted_status(self):
"""
Test for get_student_view proctored exam which has been submitted.
"""
exam_attempt = self._create_started_exam_attempt()
exam_attempt.status = ProctoredExamStudentAttemptStatus.submitted
exam_attempt.save()
rendered_response = self.render_proctored_exam()
self.assertIn(self.proctored_exam_submitted_msg, rendered_response)
# now make sure if this status transitions to 'second_review_required'
# the student will still see a 'submitted' message
update_attempt_status(
exam_attempt.id,
ProctoredExamStudentAttemptStatus.second_review_required
)
rendered_response = self.render_proctored_exam()
self.assertIn(self.proctored_exam_submitted_msg, rendered_response)
@override_settings(PROCTORED_EXAM_VIEWABLE_PAST_DUE=False)
@ddt.data(
(ProctoredExamStudentAttemptStatus.submitted, True),
(ProctoredExamStudentAttemptStatus.submitted, False),
(ProctoredExamStudentAttemptStatus.second_review_required, True),
(ProctoredExamStudentAttemptStatus.second_review_required, False),
(ProctoredExamStudentAttemptStatus.rejected, True),
(ProctoredExamStudentAttemptStatus.rejected, False),
(ProctoredExamStudentAttemptStatus.verified, True),
(ProctoredExamStudentAttemptStatus.verified, False)
)
@ddt.unpack
def test_get_studentview_without_viewable_content(self, status, status_acknowledged):
"""
Test for get_student_view proctored exam which has been submitted
but exam content is not viewable if the due date has passed
"""
due_date = datetime.now(pytz.UTC) + timedelta(minutes=40)
exam_id = self._create_exam_with_due_time(
is_proctored=True, due_date=due_date
)
exam_attempt = ProctoredExamStudentAttempt.objects.create(
proctored_exam_id=exam_id,
user=self.user,
allowed_time_limit_mins=30,
taking_as_proctored=True,
external_id='fdage332',
status=status,
)
exam_attempt.is_status_acknowledged = status_acknowledged
exam_attempt.save()
# due date is after 10 minutes
reset_time = datetime.now(pytz.UTC) + timedelta(minutes=60)
with freeze_time(reset_time):
rendered_response = get_student_view(
user_id=self.user.id,
course_id=self.course_id,
content_id=self.content_id_for_exam_with_due_date,
context={
'is_proctored': True,
'display_name': 'Test Exam',
'default_time_limit_mins': 30,
'due_date': due_date
}
)
self.assertIsNotNone(rendered_response)
self.assertNotIn(self.review_exam_msg, rendered_response)
@patch("edx_proctoring.api.constants.CONTENT_VIEWABLE_PAST_DUE_DATE", True)
@ddt.data(
60,
20,
)
def test_get_studentview_submitted_status_with_duedate_status_acknowledged(self, reset_time_delta):
"""
Test for get_student_view proctored exam which has been submitted
And status acknowledged
The test sets up a proctored exam with due date.
The test would check, before the due date passed, if is_status_acknowledged is true on the attempt,
the exam interstial still shows. This means the learner cannot see exam content
The test also checks, after the due date passed, if is_status_acknowledged is true on the attempt,