-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtest_reviews.py
598 lines (496 loc) · 24.7 KB
/
test_reviews.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
"""
Review callback tests
"""
import codecs
import json
from unittest.mock import call, patch
import ddt
from crum import set_current_request
from django.contrib.auth import get_user_model
from django.test import RequestFactory
from django.urls import reverse
from edx_proctoring import constants
from edx_proctoring.api import create_exam, create_exam_attempt, get_exam_attempt_by_id, remove_exam_attempt
from edx_proctoring.backends import get_backend_provider
from edx_proctoring.backends.software_secure import SoftwareSecureBackendProvider
from edx_proctoring.backends.tests.test_review_payload import create_test_review_payload
from edx_proctoring.exceptions import ProctoredExamBadReviewStatus, ProctoredExamReviewAlreadyExists
from edx_proctoring.models import (
ProctoredExamSoftwareSecureComment,
ProctoredExamSoftwareSecureReview,
ProctoredExamSoftwareSecureReviewHistory
)
from edx_proctoring.runtime import get_runtime_service, set_runtime_service
from edx_proctoring.statuses import ProctoredExamStudentAttemptStatus, ReviewStatus, SoftwareSecureReviewStatus
from edx_proctoring.tests.test_services import (
MockCertificateService,
MockCreditService,
MockGradesService,
MockInstructorService
)
from edx_proctoring.utils import decode_and_decrypt, locate_attempt_by_attempt_code
from edx_proctoring.views import ProctoredExamReviewCallback, is_user_course_or_global_staff
from .test_utils.utils import LoggedInTestCase
User = get_user_model()
@ddt.ddt
class ReviewTests(LoggedInTestCase):
"""
Tests for reviews
"""
def setUp(self):
super().setUp()
self.dummy_request = RequestFactory().get('/')
self.exam_creation_params = {
'course_id': 'foo/bar/baz',
'content_id': 'content',
'exam_name': 'Sample Exam',
'time_limit_mins': 10,
'is_proctored': True,
'backend': 'test'
}
self.exam_id = create_exam(**self.exam_creation_params)
self.attempt_id = create_exam_attempt(
self.exam_id,
self.user.id,
taking_as_proctored=True
)
self.attempt = get_exam_attempt_by_id(self.attempt_id)
set_runtime_service('credit', MockCreditService())
set_runtime_service('instructor', MockInstructorService())
set_runtime_service('grades', MockGradesService())
set_runtime_service('certificates', MockCertificateService())
set_current_request(self.dummy_request)
def tearDown(self):
super().tearDown()
set_runtime_service('credit', None)
set_runtime_service('grades', None)
set_runtime_service('certificates', None)
def get_review_payload(self, status=ReviewStatus.passed, **kwargs):
"""
Returns a standard review payload
"""
review = {
'status': status,
'comments': [
{
"comment": "Browsing other websites",
"duration": 88,
"stop": 88,
"start": 12,
"status": "suspicious"
},
{
"comment": "Browsing local computer",
"duration": 88,
"stop": 88,
"start": 15,
"status": "Rules Violation"
},
{
"comment": "Student never entered the exam.",
"duration": 88,
"stop": 88,
"start": 87,
"status": "Clean"
}
]
}
review.update(kwargs)
return review
@ddt.data(
('Bogus', None, None),
('Clean', 'Clean', 'satisfied'),
('Rules Violation', 'Rules Violation', 'satisfied'),
('Suspicious', 'Suspicious', 'failed'),
('Not Reviewed', 'Not Reviewed', 'failed'),
)
@ddt.unpack
@patch('edx_proctoring.constants.REQUIRE_FAILURE_SECOND_REVIEWS', False)
def test_psi_review_callback(self, psi_review_status, review_status, credit_requirement_status):
"""
Simulates callbacks from SoftwareSecure with various statuses
"""
test_payload = json.loads(create_test_review_payload(
attempt_code=self.attempt['attempt_code'],
external_id=self.attempt['external_id'],
review_status=psi_review_status
))
self.attempt['proctored_exam']['backend'] = 'software_secure'
if review_status is None:
with self.assertRaises(ProctoredExamBadReviewStatus):
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
else:
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
# make sure that what we have in the Database matches what we expect
review = ProctoredExamSoftwareSecureReview.get_review_by_attempt_code(self.attempt['attempt_code'])
self.assertIsNotNone(review)
self.assertEqual(review.review_status, review_status)
self.assertTrue(review.encrypted_video_url)
self.assertIsNotNone(review.raw_data)
self.assertIsNone(json.loads(review.raw_data).get('videoReviewLink'))
self.assertIsNone(review.reviewed_by)
# now check the comments that were stored
comments = ProctoredExamSoftwareSecureComment.objects.filter(review_id=review.id)
self.assertEqual(len(comments), 6)
# check that we got credit requirement set appropriately
credit_service = get_runtime_service('credit')
credit_status = credit_service.get_credit_state(self.user.id, 'foo/bar/baz')
self.assertEqual(
credit_status['credit_requirement_status'][0]['status'],
credit_requirement_status
)
instructor_service = get_runtime_service('instructor')
notifications = instructor_service.notifications
if psi_review_status == SoftwareSecureReviewStatus.suspicious:
# check to see whether the zendesk ticket was created
self.assertEqual(len(notifications), 1)
exam = self.attempt['proctored_exam']
review_url = 'http://testserver/edx_proctoring/v1/instructor/foo/bar/baz/1?attempt=testexternalid'
self.assertEqual(notifications,
[(exam['course_id'],
exam['exam_name'],
self.attempt['user']['username'],
review.review_status,
review_url)])
else:
self.assertEqual(len(notifications), 0)
def test_psi_video_url(self):
"""
Test that review callback from PSI produces encrypted video link
"""
test_payload = json.loads(create_test_review_payload(
attempt_code=self.attempt['attempt_code'],
external_id=self.attempt['external_id'],
review_status='Clean'
))
video_url = test_payload['videoReviewLink']
self.attempt['proctored_exam']['backend'] = 'software_secure'
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
review = ProctoredExamSoftwareSecureReview.get_review_by_attempt_code(self.attempt['attempt_code'])
aes_key_str = get_backend_provider(name='software_secure').get_video_review_aes_key()
aes_key = codecs.decode(aes_key_str, "hex")
decoded_video_url = decode_and_decrypt(review.encrypted_video_url, aes_key)
self.assertEqual(decoded_video_url.decode("utf-8"), video_url)
def test_psi_video_url_no_key(self):
"""
Test that review callback from PSI does not produce encrypted video url if now encryption key is provided
"""
with patch.object(SoftwareSecureBackendProvider, 'get_video_review_aes_key') as key_mock:
key_mock.return_value = None
test_payload = json.loads(create_test_review_payload(
attempt_code=self.attempt['attempt_code'],
external_id=self.attempt['external_id'],
review_status='Clean'
))
self.attempt['proctored_exam']['backend'] = 'software_secure'
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
review = ProctoredExamSoftwareSecureReview.get_review_by_attempt_code(self.attempt['attempt_code'])
self.assertFalse(review.encrypted_video_url)
def test_bad_review_status(self):
"""
Tests that an exception is raised if the review has an invalid status
"""
test_payload = self.get_review_payload('bogus')
with self.assertRaises(ProctoredExamBadReviewStatus):
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
@ddt.data(
('bad', 400),
(None, 200),
)
@ddt.unpack
def test_post_review(self, external_id, status):
self.user.is_staff = True
self.user.save()
review = self.get_review_payload()
if not external_id:
external_id = self.attempt['external_id']
response = self.client.post(
reverse('edx_proctoring:proctored_exam.attempt.callback',
kwargs={'external_id': external_id}),
json.dumps(review),
content_type='application/json'
)
self.assertEqual(response.status_code, status)
def test_post_review_auth(self):
review = json.dumps(self.get_review_payload())
external_id = self.attempt['external_id']
url = reverse('edx_proctoring:proctored_exam.attempt.callback',
kwargs={'external_id': external_id})
response = self.client.post(
url,
review,
content_type='application/json'
)
assert response.status_code == 403
# staff users can review
self.user.is_staff = True
self.user.save()
response = self.client.post(
url,
review,
content_type='application/json'
)
assert response.status_code == 200
# user in the review group
group_name = f"{self.attempt['proctored_exam']['backend']}_review"
self.user.groups.get_or_create(name=group_name)
self.user.is_staff = False
self.user.save()
response = self.client.post(
url,
review,
content_type='application/json'
)
assert response.status_code == 200
@patch('edx_proctoring.constants.REQUIRE_FAILURE_SECOND_REVIEWS', False)
def test_review_on_archived_attempt(self):
"""
Make sure we can process a review report for
an attempt which has been archived
"""
test_payload = self.get_review_payload(ReviewStatus.passed)
# now delete the attempt, which puts it into the archive table
remove_exam_attempt(self.attempt_id, requesting_user=self.user)
# now process the report
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
# make sure that what we have in the Database matches what we expect
review = ProctoredExamSoftwareSecureReview.get_review_by_attempt_code(self.attempt['attempt_code'])
self.assertIsNotNone(review)
self.assertEqual(review.review_status, SoftwareSecureReviewStatus.clean)
self.assertIsNotNone(review.raw_data)
# now check the comments that were stored
comments = ProctoredExamSoftwareSecureComment.objects.filter(review_id=review.id)
self.assertEqual(len(comments), 3)
@patch('edx_proctoring.constants.ALLOW_REVIEW_UPDATES', False)
@patch('edx_proctoring.constants.REQUIRE_FAILURE_SECOND_REVIEWS', False)
def test_disallow_review_resubmission(self):
"""
Tests that an exception is raised if a review report is resubmitted for the same
attempt
"""
test_payload = self.get_review_payload(ReviewStatus.passed)
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
# now call again
with self.assertRaises(ProctoredExamReviewAlreadyExists):
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
@patch('edx_proctoring.constants.ALLOW_REVIEW_UPDATES', True)
def test_allow_review_resubmission(self):
"""
Tests that an resubmission is allowed
"""
test_payload = self.get_review_payload(ReviewStatus.passed)
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
# make sure history table is empty
records = ProctoredExamSoftwareSecureReviewHistory.objects.filter(attempt_code=self.attempt['attempt_code'])
self.assertEqual(len(records), 0)
# now call again, this will not throw exception
test_payload['status'] = ReviewStatus.suspicious
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
# make sure that what we have in the Database matches what we expect
review = ProctoredExamSoftwareSecureReview.get_review_by_attempt_code(self.attempt['attempt_code'])
self.assertIsNotNone(review)
self.assertEqual(review.review_status, SoftwareSecureReviewStatus.suspicious)
self.assertIsNotNone(review.raw_data)
# make sure history table is no longer empty
records = ProctoredExamSoftwareSecureReviewHistory.objects.filter(attempt_code=self.attempt['attempt_code'])
self.assertEqual(len(records), 1)
self.assertEqual(records[0].review_status, 'Clean')
# now try to delete the record and make sure it was archived
review.delete()
records = ProctoredExamSoftwareSecureReviewHistory.objects.filter(attempt_code=self.attempt['attempt_code'])
self.assertEqual(len(records), 2)
self.assertEqual(records[0].review_status, SoftwareSecureReviewStatus.clean)
self.assertEqual(records[1].review_status, SoftwareSecureReviewStatus.suspicious)
@patch('edx_proctoring.constants.REQUIRE_FAILURE_SECOND_REVIEWS', True)
def test_failure_submission_rejected(self):
"""
Tests that a submission of a failed test and make sure that we
don't automatically update the status to failure
"""
test_payload = self.get_review_payload(ReviewStatus.suspicious)
allow_rejects = not constants.REQUIRE_FAILURE_SECOND_REVIEWS
# submit a Suspicious review payload
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
# now look at the attempt and make sure it did not
# transition to failure on the callback,
# as we'll need a manual confirmation via Django Admin pages
attempt = get_exam_attempt_by_id(self.attempt_id)
self.assertNotEqual(attempt['status'], ProctoredExamStudentAttemptStatus.rejected)
review = ProctoredExamSoftwareSecureReview.objects.get(attempt_code=self.attempt['attempt_code'])
attempt = get_exam_attempt_by_id(self.attempt_id)
# if we don't allow rejects to be stored in attempt status
# then we should expect a 'second_review_required' status
expected_status = (
ProctoredExamStudentAttemptStatus.rejected if allow_rejects else
ProctoredExamStudentAttemptStatus.second_review_required
)
self.assertEqual(attempt['status'], expected_status)
self.assertEqual(review.review_status, SoftwareSecureReviewStatus.suspicious)
def test_failure_not_reviewed(self):
"""
Tests that a review which comes back as "not reviewed"
transitions to an error state
"""
test_payload = self.get_review_payload(ReviewStatus.not_reviewed)
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
attempt = get_exam_attempt_by_id(self.attempt_id)
self.assertEqual(attempt['status'], ProctoredExamStudentAttemptStatus.error)
def test_update_archived_attempt(self):
"""
Test calling the interface point with an attempt_code that was archived
"""
test_payload = self.get_review_payload()
# now process the report
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
# now look at the attempt and make sure it did not
# transition to failure on the callback,
# as we'll need a manual confirmation via Django Admin pages
attempt = get_exam_attempt_by_id(self.attempt_id)
self.assertEqual(attempt['status'], 'verified')
attempt, is_archived = locate_attempt_by_attempt_code(self.attempt['attempt_code'])
self.assertFalse(is_archived)
self.assertEqual(attempt.status, 'verified')
# now delete the attempt, which puts it into the archive table
remove_exam_attempt(self.attempt_id, requesting_user=self.user)
attempt, is_archived = locate_attempt_by_attempt_code(self.attempt['attempt_code'])
self.assertTrue(is_archived)
self.assertEqual(attempt.status, 'verified')
review = ProctoredExamSoftwareSecureReview.objects.get(attempt_code=self.attempt['attempt_code'])
self.assertEqual(review.review_status, SoftwareSecureReviewStatus.clean)
# now we'll make another review for the archived attempt. It should NOT update the status
test_payload = self.get_review_payload(ReviewStatus.suspicious)
self.attempt['is_archived'] = True
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
attempt, is_archived = locate_attempt_by_attempt_code(self.attempt['attempt_code'])
self.assertTrue(is_archived)
self.assertEqual(attempt.status, 'verified')
def test_clean_status(self):
"""
Test that defining `passing_statuses` on the backend works
"""
test_backend = get_backend_provider(name='test')
with patch.object(test_backend, 'passing_statuses', [SoftwareSecureReviewStatus.clean], create=True):
test_payload = self.get_review_payload(status=ReviewStatus.violation)
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
attempt = get_exam_attempt_by_id(self.attempt_id)
self.assertEqual(attempt['status'], ProctoredExamStudentAttemptStatus.second_review_required)
def test_onboarding_attempts_no_second_review_necessary(self):
"""
Test that onboarding exams do not require a manual pass of review before they land in rejected
"""
exam_creation_params = self.exam_creation_params.copy()
exam_creation_params.update({
'is_practice_exam': True,
'content_id': 'onboarding_content',
})
onboarding_exam_id = create_exam(**exam_creation_params)
onboarding_attempt_id = create_exam_attempt(
onboarding_exam_id,
self.user.id,
taking_as_proctored=True,
)
onboarding_attempt = get_exam_attempt_by_id(onboarding_attempt_id)
test_payload = self.get_review_payload(ReviewStatus.suspicious)
ProctoredExamReviewCallback().make_review(onboarding_attempt, test_payload)
onboarding_attempt = get_exam_attempt_by_id(onboarding_attempt_id)
assert onboarding_attempt['status'] != ProctoredExamStudentAttemptStatus.second_review_required
def test_status_reviewed_by_field(self):
"""
Test that `reviewed_by` field of Review model is correctly assigned to None or to a User object.
"""
# no reviewed_by field
test_payload = self.get_review_payload(ReviewStatus.suspicious)
# submit a Suspicious review payload
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
review = ProctoredExamSoftwareSecureReview.objects.get(attempt_code=self.attempt['attempt_code'])
self.assertIsNone(review.reviewed_by)
# reviewed_by field with no corresponding User object
reviewed_by_email = '[email protected]'
test_payload['reviewed_by'] = reviewed_by_email
# submit a Suspicious review payload
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
review = ProctoredExamSoftwareSecureReview.objects.get(attempt_code=self.attempt['attempt_code'])
self.assertIsNone(review.reviewed_by)
# reviewed_by field with corresponding User object
user = User.objects.create(
email=reviewed_by_email,
username='TestyMcTesterson'
)
# submit a Suspicious review payload
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
review = ProctoredExamSoftwareSecureReview.objects.get(attempt_code=self.attempt['attempt_code'])
self.assertEqual(review.reviewed_by, user)
def test_is_user_course_or_global_staff(self):
"""
Test that is_user_course_or_global_staff function correctly returns whether
a user is either global staff or course staff.
"""
user = User.objects.create(
email='[email protected]',
username='TestyMcTesterson'
)
course_id = self.attempt['proctored_exam']['course_id']
# course_staff = true, is_staff = false
# by default, user.is_staff is false and instructor_service.is_course_staff returns true
self.assertTrue(is_user_course_or_global_staff(user, course_id))
# course_staff = true, is_staff = true
user.is_staff = True
self.assertTrue(is_user_course_or_global_staff(user, course_id))
# mock instructor service must be configured to treat users as not course staff.
set_runtime_service('instructor', MockInstructorService(is_user_course_staff=False))
# course_staff = false, is_staff = true
self.assertTrue(is_user_course_or_global_staff(user, course_id))
# course_staff = false, is_staff = false
user.is_staff = False
self.assertFalse(is_user_course_or_global_staff(user, course_id))
@patch('logging.Logger.warning')
def test_reviewed_by_is_course_or_global_staff(self, logger_mock):
"""
Test that a "reviewed_by" field of a review that corresponds to a user
that is not a course staff or global staff causes a warning to be logged.
Test that no warning is logged if a user is course staff or global staff.
"""
test_payload = self.get_review_payload()
reviewed_by_email = '[email protected]'
test_payload['reviewed_by'] = reviewed_by_email
# reviewed_by field with corresponding User object
user = User.objects.create(
email=reviewed_by_email,
username='TestyMcTesterson'
)
log_format_string = (
'user=%(user)s does not have the required permissions '
'to submit a review for attempt_code=%(attempt_code)s.'
)
log_format_dictionary = {
'user': user,
'attempt_code': self.attempt['attempt_code'],
}
with patch('edx_proctoring.views.is_user_course_or_global_staff', return_value=False):
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
# using assert_any_call instead of assert_called_with due to logging in analytics emit_event function
logger_mock.assert_any_call(log_format_string, log_format_dictionary)
logger_mock.reset_mock()
with patch('edx_proctoring.views.is_user_course_or_global_staff', return_value=True):
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
# the mock API doesn't have a "assert_not_called_with" method
# pylint: disable=wrong-assert-type
self.assertFalse(
call(log_format_string, log_format_dictionary) in logger_mock.call_args_list
)
def test_review_update_attempt_active_field(self):
"""
Make sure we update the is_active_attempt field when an attempt is archived
"""
test_payload = self.get_review_payload(ReviewStatus.passed)
ProctoredExamReviewCallback().make_review(self.attempt, test_payload)
review = ProctoredExamSoftwareSecureReview.get_review_by_attempt_code(self.attempt['attempt_code'])
self.assertTrue(review.is_attempt_active)
# now delete the attempt, which puts it into the archive table
with patch('edx_proctoring.api.update_attempt_status') as mock_update_status:
remove_exam_attempt(self.attempt_id, requesting_user=self.user)
# check that the field has been updated
review = ProctoredExamSoftwareSecureReview.get_review_by_attempt_code(self.attempt['attempt_code'])
self.assertFalse(review.is_attempt_active)
# check that update_attempt_status has not been called, as the attempt has been archived
mock_update_status.assert_not_called()