forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_visibility_enroll.py
764 lines (710 loc) · 36.7 KB
/
test_visibility_enroll.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
from datetime import timedelta
import logging
from django.contrib.auth.models import User
from django.urls import reverse
from django.test import TestCase
from django.utils import timezone
from django.conf import settings
from course.models import Course, CourseInstance, CourseModule, \
LearningObjectCategory
from exercise.exercise_models import BaseExercise, CourseChapter, LearningObject
from exercise.submission_models import Submission
class CourseVisibilityTest(TestCase):
"""Tests for course visibility and access control.
There are also some tests about enrollment.
"""
@classmethod
def setUpTestData(cls):
cls.user = User(username="testUser") # not enrolled in the course
cls.user.set_password("testUser")
cls.user.save()
cls.user.userprofile.student_id = '123456'
cls.user.userprofile.organization = settings.LOCAL_ORGANIZATION
cls.user.userprofile.save()
cls.student = User(username="student") # enrolled in the course
cls.student.set_password("student")
cls.student.save()
cls.student.userprofile.student_id = '654321'
cls.student.userprofile.organization = settings.LOCAL_ORGANIZATION
cls.student.userprofile.save()
cls.course = Course.objects.create(
name="Test course",
code="123456",
url="Course-Url",
)
cls.today = timezone.now()
cls.tomorrow = cls.today + timedelta(days=1)
cls.two_days_from_now = cls.tomorrow + timedelta(days=1)
cls.yesterday = cls.today - timedelta(days=1)
# course instances with different view_access_to settings
cls.public_course_instance = CourseInstance.objects.create(
instance_name="Public",
starting_time=cls.yesterday,
ending_time=cls.tomorrow,
course=cls.course,
url="public",
view_content_to=CourseInstance.VIEW_ACCESS.PUBLIC,
enrollment_audience=CourseInstance.ENROLLMENT_AUDIENCE.INTERNAL_USERS,
)
cls.all_regist_course_instance = CourseInstance.objects.create(
instance_name="All registered users",
starting_time=cls.yesterday,
ending_time=cls.tomorrow,
course=cls.course,
url="allregistered",
view_content_to=CourseInstance.VIEW_ACCESS.ALL_REGISTERED,
enrollment_audience=CourseInstance.ENROLLMENT_AUDIENCE.INTERNAL_USERS,
)
cls.enroll_audience_course_instance = CourseInstance.objects.create(
instance_name="Enrollment audience",
starting_time=cls.yesterday,
ending_time=cls.two_days_from_now,
course=cls.course,
url="enrollmentaudience",
view_content_to=CourseInstance.VIEW_ACCESS.ENROLLMENT_AUDIENCE,
enrollment_audience=CourseInstance.ENROLLMENT_AUDIENCE.INTERNAL_USERS,
)
cls.enrolled_course_instance = CourseInstance.objects.create(
instance_name="Enrolled",
starting_time=cls.yesterday,
ending_time=cls.two_days_from_now,
course=cls.course,
url="enrolled",
view_content_to=CourseInstance.VIEW_ACCESS.ENROLLED,
enrollment_audience=CourseInstance.ENROLLMENT_AUDIENCE.INTERNAL_USERS,
)
cls.course_instances = [cls.public_course_instance, cls.all_regist_course_instance,
cls.enroll_audience_course_instance, cls.enrolled_course_instance]
# enrollment
for instance in cls.course_instances:
instance.enroll_student(cls.student)
# module/exercise round for each course instance
cls.course_modules = {}
for instance in cls.course_instances:
cls.course_modules[instance.id] = CourseModule.objects.create(
name="Test module",
url="test-module",
points_to_pass=10,
course_instance=instance,
opening_time=cls.today,
closing_time=cls.tomorrow,
)
# category
cls.categories = {}
for instance in cls.course_instances:
cls.categories[instance.id] = LearningObjectCategory.objects.create(
name="Test category",
course_instance=instance,
points_to_pass=0,
)
# learning objects
cls.learning_objects = {}
for instance in cls.course_instances:
lobjects = []
chapter = CourseChapter.objects.create(
name="Test chapter",
course_module=cls.course_modules[instance.id],
category=cls.categories[instance.id],
url='chapter1',
)
lobjects.append(chapter)
lobjects.append(BaseExercise.objects.create(
name="Embedded exercise",
parent=chapter,
status=LearningObject.STATUS.UNLISTED,
course_module=cls.course_modules[instance.id],
category=cls.categories[instance.id],
url='embedexercise',
max_submissions=10,
max_points=10,
points_to_pass=0,
))
lobjects.append(BaseExercise.objects.create(
name="Normal exercise",
course_module=cls.course_modules[instance.id],
category=cls.categories[instance.id],
url='normalexercise',
max_submissions=10,
max_points=10,
points_to_pass=0,
))
cls.learning_objects[instance.id] = lobjects
# submissions
cls.submissions = {}
for _course_instance_id, exercises in cls.learning_objects.items():
for exercise in exercises:
if not exercise.is_submittable:
continue
cls.submissions[exercise.id] = []
submission = Submission.objects.create(
exercise=exercise,
)
submission.submitters.add(cls.student.userprofile)
cls.submissions[exercise.id].append(submission)
# disable all logging
logging.disable(logging.CRITICAL)
def test_redirect_to_enroll(self):
url = self.enrolled_course_instance.get_absolute_url()
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in user should be redirected to the enroll page
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertRedirects(response, self.enrolled_course_instance.get_url('enroll'))
self.client.logout()
# enrolled students should open the course front page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
def test_course_home(self):
url = self.enroll_audience_course_instance.get_absolute_url()
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in user should see the course home page
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# enrolled students should open the course front page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# course instance: all registered/logged-in users may see the course
url = self.all_regist_course_instance.get_absolute_url()
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in user should see the course home page
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# enrolled students should open the course front page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# public course instance
url = self.public_course_instance.get_absolute_url()
# anonymous user should see the course front page
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# unenrolled logged-in user should see the course home page
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# enrolled students should open the course front page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# course content visible to the enrollment audience, but user is
# not in the enrollment audience
ext_instance = CourseInstance.objects.create(
instance_name="Enrollment audience external",
starting_time=self.yesterday,
ending_time=self.tomorrow,
course=self.course,
url="extaudience",
view_content_to=CourseInstance.VIEW_ACCESS.ENROLLMENT_AUDIENCE,
enrollment_audience=CourseInstance.ENROLLMENT_AUDIENCE.EXTERNAL_USERS,
)
url = ext_instance.get_absolute_url()
self.assertTrue(self.client.login(username=self.user.username, password="testUser"))
response = self.client.get(url)
self.assertEqual(response.status_code, 403) # Forbidden
response = self.client.get(ext_instance.get_url('enroll'))
self.assertEqual(response.status_code, 200) # allowed to see the enrollment page
response = self.client.post(ext_instance.get_url('enroll'))
self.assertEqual(response.status_code, 403) # may not enroll
self.client.logout()
# course content visible to registered users (logged-in users), but user is
# not in the enrollment audience
ext_regist_instance = CourseInstance.objects.create( # pylint: disable=unused-variable
instance_name="Enrollment audience external - view registered users",
starting_time=self.yesterday,
ending_time=self.tomorrow,
course=self.course,
url="extaudience-registusers",
view_content_to=CourseInstance.VIEW_ACCESS.ALL_REGISTERED,
enrollment_audience=CourseInstance.ENROLLMENT_AUDIENCE.EXTERNAL_USERS,
)
url = ext_instance.get_absolute_url()
self.assertTrue(self.client.login(username=self.user.username, password="testUser"))
response = self.client.get(url)
self.assertEqual(response.status_code, 403) # Forbidden
response = self.client.get(ext_instance.get_url('enroll'))
self.assertEqual(response.status_code, 200) # allowed to see the enrollment page
response = self.client.post(ext_instance.get_url('enroll'))
self.assertEqual(response.status_code, 403) # may not enroll
self.client.logout()
def test_course_module(self):
url = self.course_modules[self.enrolled_course_instance.id].get_absolute_url()
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in user should be redirected to enrollment page
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertRedirects(response, self.enrolled_course_instance.get_url('enroll'))
self.client.logout()
# enrolled students should open the course module page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# course instance: access to enrollment audience (logged-in internal users)
url = self.course_modules[self.enroll_audience_course_instance.id].get_absolute_url()
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in user should see the module page
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# enrolled students should open the course module page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# course instance: access to registered users (any logged-in users)
url = self.course_modules[self.all_regist_course_instance.id].get_absolute_url()
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in user should see the module page
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# enrolled students should open the course module page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# course instance: access to anyone (anonymous)
url = self.course_modules[self.public_course_instance.id].get_absolute_url()
# anonymous user can open the module page
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# unenrolled logged-in user should see the module page
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# enrolled students should open the course module page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# course content visible to the enrollment audience, but user is
# not in the enrollment audience
ext_instance = CourseInstance.objects.create(
instance_name="Enrollment audience external",
starting_time=self.yesterday,
ending_time=self.tomorrow,
course=self.course,
url="extaudience",
view_content_to=CourseInstance.VIEW_ACCESS.ENROLLMENT_AUDIENCE,
enrollment_audience=CourseInstance.ENROLLMENT_AUDIENCE.EXTERNAL_USERS,
)
ext_module = CourseModule.objects.create(
name="Test module",
url="test-module",
points_to_pass=10,
course_instance=ext_instance,
opening_time=self.today,
closing_time=self.tomorrow,
)
url = ext_module.get_absolute_url()
self.assertTrue(self.client.login(username=self.user.username, password="testUser"))
response = self.client.get(url)
self.assertEqual(response.status_code, 403) # Forbidden
self.client.logout()
def test_chapter_enrolled_only(self):
url = self.learning_objects[self.enrolled_course_instance.id][0].get_display_url()
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in user should be redirected to enrollment page
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertRedirects(response, self.enrolled_course_instance.get_url('enroll'))
self.client.logout()
# enrolled students should open the chapter page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# chapter exercise
chapter_exercise = self.learning_objects[self.enrolled_course_instance.id][1] # pylint: disable=unused-variable
url = '/Course-Url/enrolled/test-module/chapter1/embedexercise/plain/'
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in user should be redirected to enrollment page
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertRedirects(response, self.enrolled_course_instance.get_url('enroll'))
self.client.logout()
# enrolled students should open the chapter page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# normal exercise (not inside chapter)
exercise = self.learning_objects[self.enrolled_course_instance.id][2]
url = reverse('exercise', kwargs={
'exercise_path': exercise.url,
'module_slug': exercise.course_module.url,
'instance_slug': exercise.course_module.course_instance.url,
'course_slug': exercise.course_module.course_instance.course.url,
})
self.assertEqual(url, '/Course-Url/enrolled/test-module/normalexercise/')
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in user should be redirected to enrollment page
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertRedirects(response, self.enrolled_course_instance.get_url('enroll'))
self.client.logout()
# enrolled students should open the chapter page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
def test_chapter_enroll_audience(self):
url = self.learning_objects[self.enroll_audience_course_instance.id][0].get_display_url()
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in internal user should see the chapter page
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# enrolled students should open the chapter page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# chapter exercise
# pylint: disable-next=unused-variable
chapter_exercise = self.learning_objects[self.enroll_audience_course_instance.id][1]
url = '/Course-Url/enrollmentaudience/test-module/chapter1/embedexercise/plain/'
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in internal user should see the exercise
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# enrolled students should open the chapter page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# normal exercise (not inside chapter)
exercise = self.learning_objects[self.enroll_audience_course_instance.id][2]
url = reverse('exercise', kwargs={
'exercise_path': exercise.url,
'module_slug': exercise.course_module.url,
'instance_slug': exercise.course_module.course_instance.url,
'course_slug': exercise.course_module.course_instance.course.url,
})
self.assertEqual(url, '/Course-Url/enrollmentaudience/test-module/normalexercise/')
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in user should see the exercise
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# enrolled students should open the exercise page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# course content visible to the enrollment audience, but user is
# not in the enrollment audience
ext_instance = CourseInstance.objects.create(
instance_name="Enrollment audience external",
starting_time=self.yesterday,
ending_time=self.tomorrow,
course=self.course,
url="extaudience",
view_content_to=CourseInstance.VIEW_ACCESS.ENROLLMENT_AUDIENCE,
enrollment_audience=CourseInstance.ENROLLMENT_AUDIENCE.EXTERNAL_USERS,
)
ext_module = CourseModule.objects.create(
name="Test module",
url="test-module",
points_to_pass=10,
course_instance=ext_instance,
opening_time=self.today,
closing_time=self.tomorrow,
)
ext_category = LearningObjectCategory.objects.create(
name="External test category",
course_instance=ext_instance,
points_to_pass=0,
)
ext_chapter = CourseChapter.objects.create(
name="External test chapter",
course_module=ext_module,
category=ext_category,
url='extchapter1',
)
url = ext_chapter.get_display_url()
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in internal user should NOT see the chapter page
# (user is not external and the course is visible to external users only)
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 403)
self.client.logout()
def test_chapter_all_registered(self):
url = self.learning_objects[self.all_regist_course_instance.id][0].get_display_url()
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in internal user should see the chapter page
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# enrolled students should open the chapter page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# chapter exercise
# pylint: disable-next=unused-variable
chapter_exercise = self.learning_objects[self.all_regist_course_instance.id][1]
url = '/Course-Url/allregistered/test-module/chapter1/embedexercise/plain/'
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in internal user should see the exercise
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# enrolled students should open the chapter page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# normal exercise (not inside chapter)
exercise = self.learning_objects[self.all_regist_course_instance.id][2]
url = reverse('exercise', kwargs={
'exercise_path': exercise.url,
'module_slug': exercise.course_module.url,
'instance_slug': exercise.course_module.course_instance.url,
'course_slug': exercise.course_module.course_instance.course.url,
})
self.assertEqual(url, '/Course-Url/allregistered/test-module/normalexercise/')
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in user should see the exercise
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# enrolled students should open the exercise page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
def test_chapter_public(self):
url = self.learning_objects[self.public_course_instance.id][0].get_display_url()
# anonymous user can open the chapter
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# unenrolled logged-in internal user should see the chapter page
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# enrolled students should open the chapter page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# chapter exercise
chapter_exercise = self.learning_objects[self.public_course_instance.id][1] # pylint: disable=unused-variable
url = '/Course-Url/public/test-module/chapter1/embedexercise/plain/'
# anonymous user can open the chapter exercise
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# unenrolled logged-in internal user should see the exercise
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# enrolled students should open the chapter exercise normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# normal exercise (not inside chapter)
exercise = self.learning_objects[self.public_course_instance.id][2]
url = reverse('exercise', kwargs={
'exercise_path': exercise.url,
'module_slug': exercise.course_module.url,
'instance_slug': exercise.course_module.course_instance.url,
'course_slug': exercise.course_module.course_instance.course.url,
})
self.assertEqual(url, '/Course-Url/public/test-module/normalexercise/')
# anonymous user can open the exercise
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# unenrolled logged-in user should see the exercise
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
# enrolled students should open the exercise page normally
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
def test_submission(self):
# submission in the chapter exercise
chapter_exercise = self.learning_objects[self.enrolled_course_instance.id][1]
submission = self.submissions[chapter_exercise.id][0]
url = submission.get_absolute_url()
self.assertEqual(url,
'/Course-Url/enrolled/test-module/chapter1/embedexercise/submissions/{0}/'.format(
submission.id))
# should redirect to A+ login
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# unenrolled logged-in user should be redirected to enrollment page
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertRedirects(response, self.enrolled_course_instance.get_url('enroll'))
self.client.logout()
# enrolled non-submitter user should not see the submission
self.enrolled_course_instance.enroll_student(self.user)
self.assertTrue(self.client.login(username=self.user.username, password='testUser'))
response = self.client.get(url)
self.assertEqual(response.status_code, 403)
self.client.logout()
self.enrolled_course_instance.get_enrollment_for(self.user).delete()
# the submitter should see her submission
self.assertTrue(self.client.login(username=self.student.username, password="student"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.client.logout()
def test_enroll(self):
self.assertTrue(self.enrolled_course_instance.is_enrollable(self.user))
self.assertTrue(self.enrolled_course_instance.is_enrollment_open())
# course instance is hidden from students
new_instance = CourseInstance.objects.create(
instance_name="Hidden course instance",
starting_time=self.yesterday,
ending_time=self.tomorrow,
course=self.course,
url="hiddencourse",
view_content_to=CourseInstance.VIEW_ACCESS.PUBLIC,
enrollment_audience=CourseInstance.ENROLLMENT_AUDIENCE.INTERNAL_USERS,
visible_to_students=False,
)
url = new_instance.get_url('enroll')
# anonymous user accesses a hidden course: redirect to login
response = self.client.post(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
response = self.client.get(url)
self.assertRedirects(response, '/accounts/login/?next=' + url)
# enrollment closed
new_instance.visible_to_students = True
new_instance.enrollment_starting_time = self.yesterday
new_instance.enrollment_ending_time = self.yesterday + timedelta(hours=1)
new_instance.save()
url = new_instance.get_url('enroll')
self.assertTrue(new_instance.is_enrollable(self.user))
self.assertFalse(new_instance.is_enrollment_open())
self.assertTrue(self.client.login(username=self.user.username, password="testUser"))
response = self.client.get(url)
# can open the enrollment page
self.assertEqual(response.status_code, 200)
# can not enroll
response = self.client.post(url)
self.assertEqual(response.status_code, 403)
self.client.logout()
def test_enrollment_exercise(self):
instance = self.enrolled_course_instance
enroll_exercise = self.learning_objects[instance.id][2]
enroll_exercise.status = LearningObject.STATUS.ENROLLMENT
enroll_exercise.save()
enroll_url = instance.get_url('enroll')
exercise_url = reverse('exercise', kwargs={
'exercise_path': enroll_exercise.url,
'module_slug': enroll_exercise.course_module.url,
'instance_slug': enroll_exercise.course_module.course_instance.url,
'course_slug': enroll_exercise.course_module.course_instance.course.url,
})
# anonymous may not open the exercise nor enroll
response = self.client.post(enroll_url)
self.assertRedirects(response, '/accounts/login/?next=' + enroll_url)
response = self.client.get(exercise_url)
self.assertRedirects(response, '/accounts/login/?next=' + exercise_url)
response = self.client.post(exercise_url)
self.assertRedirects(response, '/accounts/login/?next=' + exercise_url)
# the course has only one enrollment (made in setUp())
self.assertEqual(instance.students.count(), 1)
# logged-in user may open the exercise and submit
self.assertTrue(self.client.login(username=self.user.username, password="testUser"))
response = self.client.post(enroll_url)
self.assertRedirects(response, exercise_url) # redirects to the enrollment exercise
response = self.client.get(exercise_url)
self.assertEqual(response.status_code, 200)
# Since there is no exercise service running in the unit test environment,
# we can not make test submissions to the exercise.
success_flag, alerts, _students = enroll_exercise.check_submission_allowed(self.user.userprofile)
self.assertEqual(success_flag, BaseExercise.SUBMIT_STATUS.ALLOWED)
self.assertEqual(len(alerts['error_messages'] + alerts['warning_messages'] + alerts['info_messages']), 0)
instance.enroll_student(self.user)
self.assertEqual(instance.students.count(), 2)
self.assertTrue(instance.is_student(self.user))
self.client.logout()
def test_enrollment_exercise_external_users(self):
# only external users may enroll
instance = self.enrolled_course_instance
instance.enrollment_audience = CourseInstance.ENROLLMENT_AUDIENCE.EXTERNAL_USERS
instance.save()
enroll_exercise = self.learning_objects[instance.id][2]
enroll_exercise.status = LearningObject.STATUS.ENROLLMENT_EXTERNAL
enroll_exercise.save()
enroll_url = instance.get_url('enroll')
exercise_url = reverse('exercise', kwargs={
'exercise_path': enroll_exercise.url,
'module_slug': enroll_exercise.course_module.url,
'instance_slug': enroll_exercise.course_module.course_instance.url,
'course_slug': enroll_exercise.course_module.course_instance.course.url,
})
# internal user may not enroll
self.assertTrue(self.client.login(username=self.user.username, password="testUser"))
response = self.client.post(enroll_url)
self.assertEqual(response.status_code, 403)
response = self.client.get(exercise_url)
self.assertEqual(response.status_code, 403)
response = self.client.post(exercise_url)
self.assertEqual(response.status_code, 403)
self.assertFalse(instance.is_student(self.user))
self.client.logout()
def tearDown(self):
# return previous logging settings
logging.disable(logging.NOTSET)