-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathutils.py
402 lines (355 loc) · 12.8 KB
/
utils.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
# coding=utf-8
# pylint: disable=invalid-name
"""
Subclasses Django test client to allow for easy login
"""
from datetime import datetime
from importlib import import_module
import pytz
from eventtracking import tracker
from eventtracking.tracker import TRACKERS, Tracker
from django.conf import settings
from django.contrib.auth import get_user_model, login
from django.http import HttpRequest
from django.test import TestCase
from django.test.client import Client
from edx_proctoring.api import create_exam, create_exam_review_policy, is_attempt_in_resume_process
from edx_proctoring.models import ProctoredExamStudentAttempt
from edx_proctoring.runtime import set_runtime_service
from edx_proctoring.statuses import ProctoredExamStudentAttemptStatus
from edx_proctoring.tests.test_services import MockCreditService, MockInstructorService
User = get_user_model()
class TestClient(Client):
"""
Allows for 'fake logins' of a user so we don't need to expose a 'login' HTTP endpoint
"""
def login_user(self, user):
"""
Login as specified user, does not depend on auth backend (hopefully)
This is based on Client.login() with a small hack that does not
require the call to authenticate()
"""
user.backend = "django.contrib.auth.backends.ModelBackend"
engine = import_module(settings.SESSION_ENGINE)
# Create a fake request to store login details.
request = HttpRequest()
request.session = engine.SessionStore()
login(request, user)
# Set the cookie to represent the session.
session_cookie = settings.SESSION_COOKIE_NAME
self.cookies[session_cookie] = request.session.session_key
cookie_data = {
'max-age': None,
'path': '/',
'domain': settings.SESSION_COOKIE_DOMAIN,
'secure': settings.SESSION_COOKIE_SECURE or None,
'expires': None,
}
self.cookies[session_cookie].update(cookie_data)
# Save the session values.
request.session.save()
class LoggedInTestCase(TestCase):
"""
All tests for the views.py
"""
def setUp(self):
"""
Setup for tests
"""
super().setUp()
self.client = TestClient()
self.user = User(username='tester', email='[email protected]')
self.user.save()
self.client.login_user(self.user)
def create_batch_users(self, batch_size):
"""
Help create a bunch of users for testing
"""
users_list = []
for i in range(batch_size):
created_user = User(
username='student' + str(i),
email=f'student{i}@test.com',
)
created_user.save()
users_list.append(created_user)
return users_list
class MockTracker(Tracker):
"""
A mocked out tracker which implements the emit method
"""
def emit(self, name=None, data=None):
"""
Overload this method to do nothing
"""
class ProctoredExamTestCase(LoggedInTestCase):
"""
All tests for the models.py
"""
def setUp(self):
"""
Build out test harnessing
"""
super().setUp()
self.default_time_limit = 21
self.course_id = 'a/b/c'
self.content_id_for_exam_with_due_date = 'test_content_due_date_id'
self.content_id = 'block-v1:test+course+1+type@sequential+block@exam'
self.content_id_timed = 'block-v1:test+course+1+type@sequential+block@timed'
self.content_id_practice = 'block-v1:test+course+1+type@sequential+block@practice'
self.content_id_onboarding = 'block-v1:test+course+1+type@sequential+block@onboard'
self.disabled_content_id = 'block-v1:test+course+1+type@sequential+block@disabled'
self.exam_name = 'Test Exam'
self.user_id = self.user.id
self.key = 'additional_time_granted'
self.value = '10'
self.external_id = 'test_external_id'
set_runtime_service('credit', MockCreditService())
set_runtime_service('instructor', MockInstructorService(is_user_course_staff=True))
tracker.register_tracker(MockTracker())
self.prerequisites = [
{
'namespace': 'proctoring',
'name': 'proc1',
'order': 2,
'status': 'satisfied',
},
{
'namespace': 'reverification',
'name': 'rever1',
'order': 1,
'status': 'satisfied',
},
{
'namespace': 'grade',
'name': 'grade1',
'order': 0,
'status': 'pending',
},
{
'namespace': 'reverification',
'name': 'rever2',
'order': 3,
'status': 'failed',
},
{
'namespace': 'proctoring',
'name': 'proc2',
'order': 4,
'status': 'pending',
},
]
self.declined_prerequisites = [
{
'namespace': 'proctoring',
'name': 'proc1',
'order': 2,
'status': 'satisfied',
},
{
'namespace': 'reverification',
'name': 'rever1',
'order': 1,
'status': 'satisfied',
},
{
'namespace': 'grade',
'name': 'grade1',
'order': 0,
'status': 'pending',
},
{
'namespace': 'reverification',
'name': 'rever2',
'order': 3,
'status': 'declined',
},
{
'namespace': 'proctoring',
'name': 'proc2',
'order': 4,
'status': 'pending',
},
]
def tearDown(self):
"""
Cleanup
"""
super().tearDown()
del TRACKERS['default']
def _create_proctored_exam(self):
"""
Calls the api's create_exam to create an exam object.
"""
return create_exam(
course_id=self.course_id,
content_id=self.content_id,
exam_name=self.exam_name,
time_limit_mins=self.default_time_limit,
external_id=self.external_id,
)
def _create_exam_with_due_time(self, is_proctored=True, is_practice_exam=False, due_date=None):
"""
Calls the api's create_exam to create an exam object.
"""
return create_exam(
course_id=self.course_id,
content_id=self.content_id_for_exam_with_due_date,
exam_name=self.exam_name,
time_limit_mins=self.default_time_limit,
is_proctored=is_proctored,
is_practice_exam=is_practice_exam,
due_date=due_date
)
def _create_timed_exam(self):
"""
Calls the api's create_exam to create an exam object.
"""
return create_exam(
course_id=self.course_id,
content_id=self.content_id_timed,
exam_name=self.exam_name,
time_limit_mins=self.default_time_limit,
is_proctored=False
)
def _create_practice_exam(self):
"""
Calls the api's create_exam to create a practice exam object.
"""
return create_exam(
course_id=self.course_id,
content_id=self.content_id_practice,
exam_name=self.exam_name,
time_limit_mins=self.default_time_limit,
is_practice_exam=True,
is_proctored=True,
backend='null',
)
def _create_onboarding_exam(self):
"""
Create an onboarding exam
"""
return create_exam(
course_id=self.course_id,
content_id=self.content_id_onboarding,
exam_name=self.exam_name,
time_limit_mins=self.default_time_limit,
is_practice_exam=True,
is_proctored=True,
backend='test',
)
def _create_disabled_exam(self):
"""
Calls the api's create_exam to create an exam object.
"""
return create_exam(
course_id=self.course_id,
is_proctored=False,
content_id=self.disabled_content_id,
exam_name=self.exam_name,
time_limit_mins=self.default_time_limit,
is_active=False
)
# pylint: disable=too-many-positional-arguments
def _create_exam_attempt(
self, exam_id, status=ProctoredExamStudentAttemptStatus.created, is_practice_exam=False,
time_remaining_seconds=None, ready_to_resume=False, resumed=False
):
"""
Creates the ProctoredExamStudentAttempt object.
"""
attempt = ProctoredExamStudentAttempt(
proctored_exam_id=exam_id,
user_id=self.user_id,
external_id=self.external_id,
status=status,
allowed_time_limit_mins=10,
taking_as_proctored=True,
is_sample_attempt=is_practice_exam,
time_remaining_seconds=time_remaining_seconds,
ready_to_resume=ready_to_resume,
resumed=resumed
)
if status in (ProctoredExamStudentAttemptStatus.started,
ProctoredExamStudentAttemptStatus.ready_to_submit, ProctoredExamStudentAttemptStatus.submitted):
attempt.started_at = datetime.now(pytz.UTC)
if ProctoredExamStudentAttemptStatus.is_completed_status(status):
attempt.completed_at = datetime.now(pytz.UTC)
if status == ProctoredExamStudentAttemptStatus.error and not is_attempt_in_resume_process(attempt):
attempt.is_resumable = True
attempt.save()
return attempt
def _create_onboarding_attempt(self):
"""
Create onboarding attempt
"""
return self._create_exam_attempt(self.onboarding_exam_id, is_practice_exam=True)
def _create_unstarted_exam_attempt(self, is_proctored=True, is_practice=False):
"""
Creates the ProctoredExamStudentAttempt object.
"""
if is_proctored:
if is_practice:
exam_id = self.practice_exam_id
else:
exam_id = self.proctored_exam_id
else:
exam_id = self.timed_exam_id
return self._create_exam_attempt(exam_id, status=ProctoredExamStudentAttemptStatus.created)
def _create_started_exam_attempt(self, started_at=None, is_proctored=True, is_sample_attempt=False):
"""
Creates the ProctoredExamStudentAttempt object.
"""
return ProctoredExamStudentAttempt.objects.create(
proctored_exam_id=self.proctored_exam_id if is_proctored else self.timed_exam_id,
user_id=self.user_id,
external_id=self.external_id,
started_at=started_at if started_at else datetime.now(pytz.UTC),
status=ProctoredExamStudentAttemptStatus.started,
allowed_time_limit_mins=10,
taking_as_proctored=is_proctored,
is_sample_attempt=is_sample_attempt
)
def _create_started_practice_exam_attempt(self, started_at=None):
"""
Creates the ProctoredExamStudentAttempt object.
"""
return ProctoredExamStudentAttempt.objects.create(
proctored_exam_id=self.practice_exam_id,
taking_as_proctored=True,
user_id=self.user_id,
external_id=self.external_id,
started_at=started_at if started_at else datetime.now(pytz.UTC),
is_sample_attempt=True,
status=ProctoredExamStudentAttemptStatus.started,
allowed_time_limit_mins=10
)
def _create_started_onboarding_exam_attempt(self, started_at=None):
"""
Creates the ProctoredExamStudentAttempt object.
"""
return ProctoredExamStudentAttempt.objects.create(
proctored_exam_id=self.onboarding_exam_id,
taking_as_proctored=True,
user_id=self.user_id,
external_id=self.external_id,
started_at=started_at if started_at else datetime.now(pytz.UTC),
is_sample_attempt=True,
status=ProctoredExamStudentAttemptStatus.started,
allowed_time_limit_mins=10
)
def _create_review_policy(self, exam_id):
"""
Calls the api's create_exam_review_policy to create an exam review policy object.
"""
return create_exam_review_policy(
exam_id,
self.user.id,
"This is a test review policy."
)
@staticmethod
def _normalize_whitespace(string):
"""
Replaces newlines and multiple spaces with a single space.
"""
return ' '.join(string.replace('\n', '').split())