forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
1515 lines (1361 loc) · 70.7 KB
/
tests.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
import json
import urllib
from datetime import datetime, timedelta
from io import BytesIO, StringIO
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.sessions.middleware import SessionMiddleware
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.test.client import RequestFactory
from django.utils import timezone
from django.utils.datastructures import MultiValueDict
from course.models import Course, CourseInstance, CourseHook, CourseModule, \
LearningObjectCategory
from deviations.models import DeadlineRuleDeviation, \
MaxSubmissionsRuleDeviation
from exercise.cache.points import ExercisePoints
from exercise.exercise_models import build_upload_dir
from exercise.models import BaseExercise, StaticExercise, \
ExerciseWithAttachment, Submission, SubmittedFile, LearningObject, \
RevealRule, CourseChapter
from exercise.protocol.exercise_page import ExercisePage
from exercise.reveal_states import ExerciseRevealState, ModuleRevealState
from exercise.submission_models import build_upload_dir as build_upload_dir_for_submission_model
from lib.helpers import build_aplus_url
class ExerciseTestBase(TestCase):
@classmethod
def setUpTestData(cls): # pylint: disable=too-many-statements
cls.user = User(username="testUser", first_name="First", last_name="Last")
cls.user.set_password("testPassword")
cls.user.save()
cls.user.userprofile.student_id = '123456'
cls.user.userprofile.organization = settings.LOCAL_ORGANIZATION
cls.user.userprofile.save()
cls.grader = User(username="grader")
cls.grader.set_password("graderPassword")
cls.grader.save()
cls.teacher = User(username="staff", is_staff=True)
cls.teacher.set_password("staffPassword")
cls.teacher.save()
cls.user2 = User(username="testUser2", first_name="Strange", last_name="Fellow")
cls.user2.set_password("testPassword2")
cls.user2.save()
cls.user2.userprofile.student_id = '654321'
cls.user2.userprofile.organization = settings.LOCAL_ORGANIZATION
cls.user2.userprofile.save()
cls.course = Course.objects.create(
name="test course",
code="123456",
url="Course-Url"
)
cls.today = timezone.now()
cls.yesterday = cls.today - timedelta(days=1)
cls.tomorrow = cls.today + timedelta(days=1)
cls.two_days_from_now = cls.tomorrow + timedelta(days=1)
cls.three_days_from_now = cls.two_days_from_now + timedelta(days=1)
cls.course_instance = CourseInstance.objects.create(
instance_name="Fall 2011 day 1",
enrollment_starting_time=cls.yesterday,
starting_time=cls.today,
ending_time=cls.tomorrow,
course=cls.course,
url="T-00.1000_d1",
view_content_to=CourseInstance.VIEW_ACCESS.ENROLLMENT_AUDIENCE,
)
cls.course_instance.add_teacher(cls.teacher.userprofile)
cls.course_instance.add_assistant(cls.grader.userprofile)
cls.course_module = CourseModule.objects.create(
name="test module",
url="test-module",
points_to_pass=15,
course_instance=cls.course_instance,
opening_time=cls.today,
closing_time=cls.tomorrow
)
cls.course_module_with_late_submissions_allowed = CourseModule.objects.create(
name="test module",
url="test-module-late",
points_to_pass=50,
course_instance=cls.course_instance,
opening_time=cls.today,
closing_time=cls.tomorrow,
late_submissions_allowed=True,
late_submission_deadline=cls.two_days_from_now,
late_submission_penalty=0.2
)
cls.old_course_module = CourseModule.objects.create(
name="test module",
url="test-module-old",
points_to_pass=15,
course_instance=cls.course_instance,
opening_time=cls.yesterday,
closing_time=cls.today
)
cls.reading_open_course_module = CourseModule.objects.create(
name="test module",
url="test-module-reading-open",
points_to_pass=15,
course_instance=cls.course_instance,
reading_opening_time=cls.yesterday,
opening_time=cls.tomorrow,
closing_time=cls.two_days_from_now
)
cls.learning_object_category = LearningObjectCategory.objects.create(
name="test category",
course_instance=cls.course_instance,
points_to_pass=5
)
cls.hidden_learning_object_category = LearningObjectCategory.objects.create(
name="hidden category",
course_instance=cls.course_instance
)
#cls.hidden_learning_object_category.hidden_to.add(cls.user.userprofile)
cls.learning_object = LearningObject.objects.create(
name="test learning object",
course_module=cls.course_module,
category=cls.learning_object_category,
url="l1",
)
# Learning object names are not unique, so this object really is not
# broken despite the variable name.
cls.broken_learning_object = LearningObject.objects.create(
name="test learning object",
course_module=cls.course_module_with_late_submissions_allowed,
category=cls.learning_object_category,
url="l2",
)
cls.base_exercise = BaseExercise.objects.create(
order=1,
name="test exercise",
course_module=cls.course_module,
category=cls.learning_object_category,
url="b1",
max_submissions=1,
)
cls.static_exercise = StaticExercise.objects.create(
order=2,
name="test exercise 2",
course_module=cls.course_module,
category=cls.learning_object_category,
url="s2",
max_points=50,
points_to_pass=50,
service_url="/testServiceURL",
exercise_page_content="test_page_content",
submission_page_content="test_submission_content"
)
cls.exercise_with_attachment = ExerciseWithAttachment.objects.create(
order=3,
name="test exercise 3",
course_module=cls.course_module,
category=cls.learning_object_category,
url="a1",
max_points=50,
points_to_pass=50,
max_submissions=0,
files_to_submit="test1.txt|test2.txt|img.png",
content="test_instructions"
)
cls.old_base_exercise = BaseExercise.objects.create(
name="test exercise",
course_module=cls.old_course_module,
category=cls.learning_object_category,
url="b2",
max_submissions=1
)
cls.exercise_in_reading_time = BaseExercise.objects.create(
order=1,
name="test exercise",
course_module=cls.reading_open_course_module,
category=cls.learning_object_category,
url="b1",
max_submissions=1,
)
cls.base_exercise_with_late_submission_allowed = BaseExercise.objects.create(
name="test exercise with late submissions allowed",
course_module=cls.course_module_with_late_submissions_allowed,
category=cls.learning_object_category,
url="b3",
)
cls.enrollment_exercise = BaseExercise.objects.create(
name="test enrollment exercise",
course_module=cls.old_course_module,
category=cls.learning_object_category,
url="enroll-exercise",
max_submissions=1,
status="enrollment",
)
cls.submission = Submission.objects.create(
exercise=cls.base_exercise,
grader=cls.grader.userprofile
)
cls.submission.submitters.add(cls.user.userprofile)
cls.submission_with_two_submitters = Submission.objects.create(
exercise=cls.base_exercise,
grader=cls.grader.userprofile
)
cls.submission_with_two_submitters.submitters.add(cls.user.userprofile)
cls.submission_with_two_submitters.submitters.add(cls.user2.userprofile)
cls.late_submission = Submission.objects.create(
exercise=cls.base_exercise,
grader=cls.grader.userprofile
)
cls.late_submission.submission_time = cls.two_days_from_now
cls.late_submission.submitters.add(cls.user.userprofile)
cls.submission_when_late_allowed = Submission.objects.create(
exercise=cls.base_exercise_with_late_submission_allowed,
grader=cls.grader.userprofile
)
cls.submission_when_late_allowed.submitters.add(cls.user.userprofile)
cls.late_submission_when_late_allowed = Submission.objects.create(
exercise=cls.base_exercise_with_late_submission_allowed,
grader=cls.grader.userprofile
)
cls.late_submission_when_late_allowed.submission_time = cls.two_days_from_now
cls.late_submission_when_late_allowed.submitters.add(cls.user.userprofile)
cls.late_late_submission_when_late_allowed = Submission.objects.create(
exercise=cls.base_exercise_with_late_submission_allowed,
grader=cls.grader.userprofile
)
cls.late_late_submission_when_late_allowed.submission_time = cls.three_days_from_now
cls.late_late_submission_when_late_allowed.submitters.add(cls.user.userprofile)
cls.course_hook = CourseHook.objects.create(
hook_url="http://localhost/test_hook_url",
course_instance=cls.course_instance
)
cls.deadline_rule_deviation = DeadlineRuleDeviation.objects.create(
exercise=cls.exercise_with_attachment,
submitter=cls.user.userprofile,
granter=cls.teacher.userprofile,
extra_seconds=24*60*60 # One day
)
class ExerciseTest(ExerciseTestBase):
def test_learning_object_category_unicode_string(self):
self.assertEqual("test category", str(self.learning_object_category))
self.assertEqual("hidden category", str(self.hidden_learning_object_category))
#def test_learning_object_category_hiding(self):
# self.assertFalse(self.learning_object_category.is_hidden_to(self.user.userprofile))
# self.assertFalse(self.learning_object_category.is_hidden_to(self.grader.userprofile))
# self.assertTrue(self.hidden_learning_object_category.is_hidden_to(self.user.userprofile))
# self.assertFalse(self.hidden_learning_object_category.is_hidden_to(self.grader.userprofile))
# self.hidden_learning_object_category.set_hidden_to(self.user.userprofile, False)
# self.hidden_learning_object_category.set_hidden_to(self.grader.userprofile)
# self.assertFalse(self.hidden_learning_object_category.is_hidden_to(self.user.userprofile))
# self.assertTrue(self.hidden_learning_object_category.is_hidden_to(self.grader.userprofile))
# self.hidden_learning_object_category.set_hidden_to(self.user.userprofile, True)
# self.hidden_learning_object_category.set_hidden_to(self.grader.userprofile, False)
# self.assertTrue(self.hidden_learning_object_category.is_hidden_to(self.user.userprofile))
# self.assertFalse(self.hidden_learning_object_category.is_hidden_to(self.grader.userprofile))
def test_learning_object_clean(self):
try:
self.learning_object.clean()
self.broken_learning_object.clean() # should validate since it really is not broken
except ValidationError:
self.fail()
def test_learning_object_course_instance(self):
self.assertEqual(self.course_instance, self.learning_object.course_instance)
self.assertEqual(self.course_instance, self.broken_learning_object.course_instance)
def test_base_exercise_one_has_submissions(self):
self.assertFalse(self.base_exercise.one_has_submissions([self.user.userprofile])[0])
self.assertTrue(self.static_exercise.one_has_submissions([self.user.userprofile])[0])
self.assertTrue(self.exercise_with_attachment.one_has_submissions([self.user.userprofile])[0])
self.submission.set_error()
self.submission.save()
self.submission_with_two_submitters.set_error()
self.submission_with_two_submitters.save()
self.late_submission.set_error()
self.late_submission.save()
self.assertTrue(self.base_exercise.one_has_submissions([self.user.userprofile])[0])
def test_base_exercise_max_submissions(self):
self.assertEqual(1, self.base_exercise.max_submissions_for_student(self.user.userprofile))
self.assertEqual(10, self.static_exercise.max_submissions_for_student(self.user.userprofile))
self.assertEqual(0, self.exercise_with_attachment.max_submissions_for_student(self.user.userprofile))
def test_base_exercise_submissions_for_student(self):
self.assertEqual(3, len(self.base_exercise.get_submissions_for_student(self.user.userprofile)))
self.assertEqual(0, len(self.static_exercise.get_submissions_for_student(self.user.userprofile)))
self.assertEqual(0, len(self.exercise_with_attachment.get_submissions_for_student(self.user.userprofile)))
self.submission.set_error()
self.submission.save()
self.assertEqual(3, len(self.base_exercise.get_submissions_for_student(self.user.userprofile)))
self.assertEqual(2, len(self.base_exercise.get_submissions_for_student(self.user.userprofile, True)))
def test_base_exercise_is_open(self):
self.assertTrue(self.base_exercise.is_open())
self.assertTrue(self.static_exercise.is_open())
self.assertTrue(self.exercise_with_attachment.is_open())
self.assertFalse(self.old_base_exercise.is_open())
self.assertFalse(self.base_exercise.is_open(self.yesterday))
self.assertFalse(self.static_exercise.is_open(self.yesterday))
self.assertFalse(self.exercise_with_attachment.is_open(self.yesterday))
self.assertTrue(self.old_base_exercise.is_open(self.yesterday))
self.assertTrue(self.base_exercise.is_open(self.tomorrow))
self.assertTrue(self.static_exercise.is_open(self.tomorrow))
self.assertTrue(self.exercise_with_attachment.is_open(self.tomorrow))
self.assertFalse(self.old_base_exercise.is_open(self.tomorrow))
self.assertFalse(self.exercise_in_reading_time.is_open())
self.assertTrue(self.exercise_in_reading_time.is_open(self.tomorrow))
def test_enrollment_exercise_access(self):
# The module has closed now.
# Enrollment exercise should be submittable even after the module closing time.
self.assertEqual(self.enrollment_exercise.check_submission_allowed(self.user.userprofile)[0],
BaseExercise.SUBMIT_STATUS.ALLOWED)
# The module closed yesterday.
self.old_course_module.closing_time = self.yesterday
self.old_course_module.save()
self.assertEqual(self.enrollment_exercise.check_submission_allowed(self.user.userprofile)[0],
BaseExercise.SUBMIT_STATUS.ALLOWED)
# The module is currently open.
self.old_course_module.opening_time = self.yesterday
self.old_course_module.closing_time = self.tomorrow
self.old_course_module.save()
self.assertEqual(self.enrollment_exercise.check_submission_allowed(self.user.userprofile)[0],
BaseExercise.SUBMIT_STATUS.ALLOWED)
# The module has not yet opened.
self.old_course_module.opening_time = self.tomorrow
self.old_course_module.closing_time = self.two_days_from_now
self.old_course_module.save()
self.assertEqual(self.enrollment_exercise.check_submission_allowed(self.user.userprofile)[0],
BaseExercise.SUBMIT_STATUS.ALLOWED)
def test_base_exercise_one_has_access(self):
self.assertTrue(self.base_exercise.one_has_access([self.user.userprofile])[0])
self.assertTrue(self.static_exercise.one_has_access([self.user.userprofile])[0])
self.assertTrue(self.exercise_with_attachment.one_has_access([self.user.userprofile])[0])
self.assertFalse(self.old_base_exercise.one_has_access([self.user.userprofile])[0])
self.assertFalse(self.base_exercise.one_has_access([self.user.userprofile], self.yesterday)[0])
self.assertFalse(self.static_exercise.one_has_access([self.user.userprofile], self.yesterday)[0])
self.assertFalse(self.exercise_with_attachment.one_has_access([self.user.userprofile], self.yesterday)[0])
self.assertTrue(self.old_base_exercise.one_has_access([self.user.userprofile], self.yesterday)[0])
self.assertTrue(self.base_exercise.one_has_access([self.user.userprofile], self.tomorrow)[0])
self.assertTrue(self.static_exercise.one_has_access([self.user.userprofile], self.tomorrow)[0])
self.assertTrue(self.exercise_with_attachment.one_has_access([self.user.userprofile], self.tomorrow)[0])
self.assertFalse(self.old_base_exercise.one_has_access([self.user.userprofile], self.tomorrow)[0])
self.assertFalse(self.exercise_in_reading_time.one_has_access([self.user.userprofile], self.today)[0])
self.assertTrue(self.exercise_in_reading_time.one_has_access([self.user.userprofile], self.tomorrow)[0])
def test_base_exercise_submission_allowed(self):
status, alerts, _students = (
self.base_exercise.check_submission_allowed(self.user.userprofile))
self.assertNotEqual(status, self.base_exercise.SUBMIT_STATUS.ALLOWED)
self.assertEqual(len(alerts['error_messages'] + alerts['warning_messages'] + alerts['info_messages']), 1)
json.dumps(str(alerts))
self.assertNotEqual(
self.static_exercise.check_submission_allowed(self.user.userprofile)[0],
self.static_exercise.SUBMIT_STATUS.ALLOWED)
self.course_instance.enroll_student(self.user)
self.assertEqual(
self.static_exercise.check_submission_allowed(self.user.userprofile)[0],
self.static_exercise.SUBMIT_STATUS.ALLOWED)
self.assertEqual(
self.exercise_with_attachment.check_submission_allowed(self.user.userprofile)[0],
self.static_exercise.SUBMIT_STATUS.ALLOWED)
self.assertNotEqual(
self.old_base_exercise.check_submission_allowed(self.user.userprofile)[0],
self.old_base_exercise.SUBMIT_STATUS.ALLOWED)
self.assertEqual(
self.base_exercise.check_submission_allowed(self.grader.userprofile)[0],
self.base_exercise.SUBMIT_STATUS.ALLOWED)
self.assertEqual(
self.static_exercise.check_submission_allowed(self.grader.userprofile)[0],
self.static_exercise.SUBMIT_STATUS.ALLOWED)
self.assertEqual(
self.exercise_with_attachment \
.check_submission_allowed(self.grader.userprofile)[0],
self.exercise_with_attachment.SUBMIT_STATUS.ALLOWED)
self.assertEqual(
self.old_base_exercise.check_submission_allowed(self.grader.userprofile)[0],
self.old_base_exercise.SUBMIT_STATUS.ALLOWED)
def test_base_exercise_submission_deviation(self):
self.assertFalse(self.base_exercise.one_has_submissions([self.user.userprofile])[0])
deviation = MaxSubmissionsRuleDeviation.objects.create( # pylint: disable=unused-variable
exercise=self.base_exercise,
submitter=self.user.userprofile,
granter=self.teacher.userprofile,
extra_submissions=3
)
self.assertTrue(self.base_exercise.one_has_submissions([self.user.userprofile])[0])
def test_base_exercise_deadline_deviation(self):
self.assertFalse(self.old_base_exercise.one_has_access([self.user.userprofile])[0])
deviation = DeadlineRuleDeviation.objects.create( # pylint: disable=unused-variable
exercise=self.old_base_exercise,
submitter=self.user.userprofile,
granter=self.teacher.userprofile,
extra_seconds=10*24*60*60 # Ten days
)
self.assertTrue(self.old_base_exercise.one_has_access([self.user.userprofile])[0])
def test_base_exercise_total_submission_count(self):
self.assertEqual(self.base_exercise.get_total_submitter_count(), 2)
self.assertEqual(self.static_exercise.get_total_submitter_count(), 0)
self.assertEqual(self.exercise_with_attachment.get_total_submitter_count(), 0)
def test_base_exercise_unicode_string(self):
self.assertEqual("1.1 test exercise", str(self.base_exercise))
self.assertEqual("1.2 test exercise 2", str(self.static_exercise))
self.assertEqual("1.3 test exercise 3", str(self.exercise_with_attachment))
def test_base_exercise_absolute_url(self):
self.assertEqual("/Course-Url/T-00.1000_d1/test-module/b1/", self.base_exercise.get_absolute_url())
self.assertEqual("/Course-Url/T-00.1000_d1/test-module/s2/", self.static_exercise.get_absolute_url())
self.assertEqual("/Course-Url/T-00.1000_d1/test-module/a1/", self.exercise_with_attachment.get_absolute_url())
def test_base_exercise_async_url(self):
language = 'en'
# the order of the parameters in the returned service url is non-deterministic,
# so we check the parameters separately
split_base_exercise_service_url = (
self.base_exercise.
_build_service_url(language, [self.user.userprofile], 1, 'exercise', 'service')
.split("?")
)
split_static_exercise_service_url = (
self.static_exercise
._build_service_url(language, [self.user.userprofile], 1, 'exercise', 'service')
.split("?")
)
self.assertEqual("", split_base_exercise_service_url[0])
self.assertEqual("/testServiceURL", split_static_exercise_service_url[0])
# a quick hack to check whether the parameters are URL encoded
self.assertFalse("/" in split_base_exercise_service_url[1] or ":" in split_base_exercise_service_url[1])
self.assertFalse("/" in split_static_exercise_service_url[1] or ":" in split_static_exercise_service_url[1])
# create dictionaries from the parameters and check each value.
# Note: parse_qs changes encoding back to regular utf-8
base_exercise_url_params = urllib.parse.parse_qs(split_base_exercise_service_url[1])
static_exercise_url_params = urllib.parse.parse_qs(split_static_exercise_service_url[1])
self.assertEqual(['100'], base_exercise_url_params['max_points'])
expected = build_aplus_url("service")
self.assertEqual(expected, base_exercise_url_params['submission_url'][0][:40])
self.assertEqual(['50'], static_exercise_url_params['max_points'])
self.assertEqual([expected], static_exercise_url_params['submission_url'])
def test_static_exercise_load(self):
request = RequestFactory().request(SERVER_NAME='localhost', SERVER_PORT='8001')
static_exercise_page = self.static_exercise.load(request, [self.user.userprofile])
self.assertIsInstance(static_exercise_page, ExercisePage)
self.assertEqual("test_page_content", static_exercise_page.content)
def test_static_exercise_grade(self):
request = RequestFactory().request(SERVER_NAME='localhost', SERVER_PORT='8001')
def dummy_get_response(request):
return None
SessionMiddleware(dummy_get_response).process_request(request)
request.session.save()
sub = Submission.objects.create_from_post(self.static_exercise, [self.user.userprofile], request)
static_exercise_page = self.static_exercise.grade(request, sub)
self.assertIsInstance(static_exercise_page, ExercisePage)
self.assertTrue(static_exercise_page.is_accepted)
self.assertEqual("test_submission_content", static_exercise_page.content)
def test_exercise_upload_dir(self):
self.assertEqual("course_instance_1/exercise_attachment_5/test_file_name",
build_upload_dir(self.exercise_with_attachment, "test_file_name"))
def test_exercise_with_attachment_files_to_submit(self):
files = self.exercise_with_attachment.get_files_to_submit()
self.assertEqual(3, len(files))
self.assertEqual("test1.txt", files[0])
self.assertEqual("test2.txt", files[1])
self.assertEqual("img.png", files[2])
def test_exercise_with_attachment_load(self):
request = RequestFactory().request(SERVER_NAME='localhost', SERVER_PORT='8001')
exercise_with_attachment_page = self.exercise_with_attachment.load(request, [self.user.userprofile])
self.assertIsInstance(exercise_with_attachment_page, ExercisePage)
c = exercise_with_attachment_page.content
self.assertTrue('test_instructions' in c)
self.assertTrue('test1.txt' in c and 'test2.txt' in c and "img.png" in c)
def test_submission_files(self):
self.assertEqual(0, len(self.submission.files.all()))
self.submission.add_files(MultiValueDict({
"key1": ["test_file1.txt", "test_file2.txt"],
"key2": ["test_image.png", "test_audio.wav", "test_pdf.pdf"]
}))
self.assertEqual(5, len(self.submission.files.all()))
def test_submission_points(self):
try:
self.submission.set_points(10, 5)
self.fail("Should not be able to set points higher than max points!")
except AssertionError:
self.submission.set_points(5, 10)
self.assertEqual(50, self.submission.grade)
self.late_submission_when_late_allowed.set_points(10, 10)
self.assertEqual(80, self.late_submission_when_late_allowed.grade)
def test_submission_late_penalty_applied(self):
self.submission.set_points(5, 10)
self.late_submission.set_points(5, 10)
self.submission_when_late_allowed.set_points(5, 10)
self.late_submission_when_late_allowed.set_points(5, 10)
self.late_late_submission_when_late_allowed.set_points(5, 10)
self.assertFalse(self.submission.late_penalty_applied)
self.assertTrue(self.late_submission.late_penalty_applied is not None)
self.assertAlmostEqual(self.late_submission.late_penalty_applied, 0.0)
self.assertEqual(self.late_submission.service_points, 5)
self.assertEqual(self.late_submission.grade, 50)
self.assertFalse(self.submission_when_late_allowed.late_penalty_applied)
self.assertTrue(self.late_submission_when_late_allowed.late_penalty_applied)
self.assertTrue(self.late_late_submission_when_late_allowed.late_penalty_applied)
self.assertAlmostEqual(self.late_late_submission_when_late_allowed.late_penalty_applied, 0.2)
self.assertEqual(self.late_late_submission_when_late_allowed.service_points, 5)
self.assertEqual(self.late_late_submission_when_late_allowed.grade, 40)
deviation = DeadlineRuleDeviation.objects.create(
exercise=self.base_exercise_with_late_submission_allowed,
submitter=self.user.userprofile,
granter=self.teacher.userprofile,
extra_seconds=10*24*60*60,
without_late_penalty=True
)
self.late_late_submission_when_late_allowed.set_points(5, 10)
self.assertFalse(self.late_late_submission_when_late_allowed.late_penalty_applied)
deviation.without_late_penalty=False
deviation.save()
self.late_late_submission_when_late_allowed.set_points(5, 10)
self.assertAlmostEqual(self.late_late_submission_when_late_allowed.late_penalty_applied, 0.2)
def test_submission_late_conversion(self):
convert_submission_url = self.late_submission.get_url('submission-approve')
response = self.client.get(convert_submission_url)
self.assertEqual(response.status_code, 302)
self.assertTrue(self.late_submission.late_penalty_applied is None)
def test_early_submission(self):
self.course_module_with_late_submissions_allowed.opening_time = self.tomorrow
submission = Submission.objects.create(
exercise=self.base_exercise_with_late_submission_allowed,
grader=self.grader.userprofile
)
submission.submitters.add(self.grader.userprofile)
submission.set_points(10, 10)
self.assertFalse(submission.late_penalty_applied)
def test_unofficial_submission(self):
self.course_module_with_late_submissions_allowed.late_submissions_allowed = False
self.course_module_with_late_submissions_allowed.save()
self.learning_object_category.accept_unofficial_submits = True
self.learning_object_category.save()
self.late_submission_when_late_allowed.set_points(10, 10)
self.late_submission_when_late_allowed.set_ready()
self.late_submission_when_late_allowed.save()
self.assertEqual(self.late_submission_when_late_allowed.grade, 100)
self.assertEqual(self.late_submission_when_late_allowed.status, Submission.STATUS.UNOFFICIAL)
summary = ExercisePoints.get(self.base_exercise_with_late_submission_allowed, self.user)
self.assertEqual(summary.submission_count, 2)
self.assertEqual(summary.official_points, 0) # unofficial points are not shown here
self.assertFalse(summary.graded)
self.assertTrue(summary.unofficial)
self.submission_when_late_allowed.set_points(5, 10)
self.submission_when_late_allowed.set_ready()
self.submission_when_late_allowed.save()
self.assertEqual(self.submission_when_late_allowed.grade, 50)
self.assertEqual(self.submission_when_late_allowed.status, Submission.STATUS.READY)
summary = ExercisePoints.get(self.base_exercise_with_late_submission_allowed, self.user)
self.assertEqual(summary.submission_count, 2)
self.assertEqual(summary.official_points, 50)
self.assertTrue(summary.graded)
self.assertFalse(summary.unofficial)
sub = Submission.objects.create(
exercise=self.base_exercise_with_late_submission_allowed,
grader=self.grader.userprofile
)
sub.submission_time = self.two_days_from_now + timedelta(days = 1)
sub.save()
sub.submitters.add(self.user.userprofile)
sub.set_points(10, 10)
sub.save()
summary = ExercisePoints.get(self.base_exercise_with_late_submission_allowed, self.user)
self.assertEqual(summary.submission_count, 2)
self.assertEqual(summary.official_points, 50)
self.assertTrue(summary.graded)
self.assertFalse(summary.unofficial)
def test_unofficial_max_submissions(self):
self.learning_object_category.accept_unofficial_submits = True
self.learning_object_category.save()
res = self.base_exercise.one_has_submissions([self.user.userprofile])
self.assertFalse(res[0] and len(res[1]) == 0)
self.submission.set_points(1, 10)
self.submission.set_ready()
self.submission.save()
self.assertEqual(self.submission.status, Submission.STATUS.UNOFFICIAL)
def test_submission_unicode_string(self):
self.assertEqual("1", str(self.submission))
self.assertEqual("2", str(self.submission_with_two_submitters))
self.assertEqual("3", str(self.late_submission))
self.assertEqual("4", str(self.submission_when_late_allowed))
self.assertEqual("5", str(self.late_submission_when_late_allowed))
self.assertEqual("6", str(self.late_late_submission_when_late_allowed))
def test_submission_status(self):
self.assertEqual("initialized", self.submission.status)
self.assertFalse(self.submission.is_graded)
self.submission.set_error()
self.assertEqual("error", self.submission.status)
self.assertFalse(self.submission.is_graded)
self.submission.set_waiting()
self.assertEqual("waiting", self.submission.status)
self.assertFalse(self.submission.is_graded)
self.submission.set_error()
self.assertEqual("error", self.submission.status)
self.assertFalse(self.submission.is_graded)
self.assertEqual(None, self.submission.grading_time)
self.submission.set_ready()
self.assertIsInstance(self.submission.grading_time, datetime)
self.assertEqual("ready", self.submission.status)
self.assertTrue(self.submission.is_graded)
def test_submission_absolute_url(self):
self.assertEqual(
"/Course-Url/T-00.1000_d1/test-module/b1/submissions/1/",
self.submission.get_absolute_url()
)
self.assertEqual(
"/Course-Url/T-00.1000_d1/test-module/b1/submissions/3/",
self.late_submission.get_absolute_url()
)
def test_submission_upload_dir(self):
submitted_file1 = SubmittedFile.objects.create(
submission=self.submission,
param_name="testParam"
)
submitted_file2 = SubmittedFile.objects.create(
submission=self.submission_with_two_submitters,
param_name="testParam2"
)
self.assertEqual(
"course_instance_1/submissions/exercise_3/users_1/submission_1/test_file_name",
build_upload_dir_for_submission_model(submitted_file1, "test_file_name")
)
self.assertEqual(
"course_instance_1/submissions/exercise_3/users_1-4/submission_2/test_file_name",
build_upload_dir_for_submission_model(submitted_file2, "test_file_name")
)
def test_exercise_views(self):
upcoming_module = CourseModule.objects.create(
name="upcoming module",
url="upcoming-module",
points_to_pass=15,
course_instance=self.course_instance,
opening_time=self.two_days_from_now,
closing_time=self.three_days_from_now
)
upcoming_static_exercise = StaticExercise.objects.create(
name="upcoming exercise",
course_module=upcoming_module,
category=self.learning_object_category,
url="sssss",
max_points=50,
points_to_pass=50,
service_url="/testServiceURL",
exercise_page_content="test_page_content",
submission_page_content="test_submission_content"
)
self.submission_with_two_submitters.submitters.remove(self.user.userprofile)
response = self.client.get(self.static_exercise.get_absolute_url())
self.assertEqual(response.status_code, 302)
self.client.login(username="testUser", password="testPassword")
response = self.client.get(self.static_exercise.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["exercise"], self.static_exercise)
response = self.client.get(upcoming_static_exercise.get_absolute_url())
self.assertEqual(response.status_code, 403)
response = self.client.get(self.submission.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["submission"], self.submission)
response = self.client.get(self.submission_with_two_submitters.get_absolute_url())
self.assertEqual(response.status_code, 403)
self.client.login(username="staff", password="staffPassword")
response = self.client.get(upcoming_static_exercise.get_absolute_url())
self.assertEqual(response.status_code, 200)
response = self.client.get(self.submission.get_absolute_url())
self.assertEqual(response.status_code, 200)
response = self.client.get(self.submission_with_two_submitters.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.client.login(username="grader", password="graderPassword")
response = self.client.get(upcoming_static_exercise.get_absolute_url())
self.assertEqual(response.status_code, 200)
response = self.client.get(self.submission.get_absolute_url())
self.assertEqual(response.status_code, 200)
response = self.client.get(self.submission_with_two_submitters.get_absolute_url())
self.assertEqual(response.status_code, 200)
def test_exercise_staff_views(self) -> None:
self.other_instance = CourseInstance.objects.create(
instance_name="Another",
starting_time=self.today,
ending_time=self.tomorrow,
course=self.course,
url="another"
)
assessment_data = {
"points": 0,
"mark_as_final": False,
"assistant_feedback": "",
"feedback": "",
}
self.other_instance.add_assistant(self.grader.userprofile)
list_submissions_url = self.base_exercise.get_submission_list_url()
inspect_submission_url = self.submission.get_url('submission-inspect')
response = self.client.get(list_submissions_url)
self.assertEqual(response.status_code, 302)
self.client.login(username="testUser", password="testPassword")
response = self.client.get(list_submissions_url)
self.assertEqual(response.status_code, 403)
response = self.client.get(inspect_submission_url)
self.assertEqual(response.status_code, 403)
response = self.client.post(inspect_submission_url, assessment_data)
self.assertEqual(response.status_code, 403)
self.client.login(username="staff", password="staffPassword")
response = self.client.get(list_submissions_url)
self.assertEqual(response.status_code, 200)
response = self.client.get(inspect_submission_url)
self.assertEqual(response.status_code, 200)
response = self.client.post(inspect_submission_url, assessment_data)
self.assertEqual(response.status_code, 302)
self.client.login(username="grader", password="graderPassword")
response = self.client.get(list_submissions_url)
self.assertEqual(response.status_code, 200)
response = self.client.get(inspect_submission_url)
self.assertEqual(response.status_code, 200)
response = self.client.post(inspect_submission_url, assessment_data)
self.assertEqual(response.status_code, 403)
self.base_exercise.allow_assistant_grading = True
self.base_exercise.save()
response = self.client.post(inspect_submission_url, assessment_data)
self.assertEqual(response.status_code, 302)
self.course_instance.clear_assistants()
response = self.client.get(list_submissions_url)
self.assertEqual(response.status_code, 403)
def test_uploading_and_viewing_file(self):
exercise = BaseExercise.objects.create(
order=4,
name="test exercise 4",
course_module=self.course_module,
category=self.learning_object_category,
url="bbb",
max_points=50,
points_to_pass=50,
max_submissions=0,
service_url="http://grader.invalid/testServiceURL",
)
self.course_instance.enroll_student(self.user)
self.client.login(username="testUser", password="testPassword")
png_file = BytesIO(
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x05\x00\x00\x00\x05\x08\x02\x00\x00\x00\x02\r'
b'\xb1\xb2\x00\x00\x00\x01sRGB\x00\xae\xce\x1c\xe9\x00\x00\x00\x15IDAT\x08\xd7c`\xc0\n\xfe\xff'
b'\xff\x8f\xce\xc1"\x84\x05\x00\x00\xde\x7f\x0b\xf5<|+\x98\x00\x00\x00\x00IEND\xaeB`\x82'
)
png_file.name = 'test.png'
py_file = StringIO('print("Tekijät ja Hyyppö")')
py_file.name = 'test.py'
with png_file, py_file:
response = self.client.post(exercise.get_absolute_url(), {
"key": "value",
"file1": png_file,
"file2": py_file,
})
self.assertEqual(response.status_code, 302)
subs = self.user.userprofile.submissions.filter(exercise=exercise.id)
self.assertEqual(subs.count(), 1)
sub = subs.first()
self.assertEqual(sub.submission_data[0], ["key", "value"])
self.assertEqual(sub.files.count(), 2)
files = sub.files.all().order_by("param_name")
self.assertEqual(files[0].param_name, "file1")
response = self.client.get(files[0].get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "image/png")
self.assertEqual(files[1].param_name, "file2")
response = self.client.get(files[1].get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], 'text/plain; charset="UTF-8"')
response = self.client.get(files[1].get_absolute_url() + "?download=1")
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "application/octet-stream")
self.assertTrue(response["Content-Disposition"].startswith("attachment; filename="))
exercise.delete()
def test_can_show_model_solutions(self):
course_module_with_late_submissions_open = CourseModule.objects.create(
name="test module late open",
url="test-module-late-open",
points_to_pass=50,
course_instance=self.course_instance,
opening_time=self.yesterday - timedelta(days=1),
closing_time=self.yesterday,
late_submissions_allowed=True,
late_submission_deadline=self.tomorrow,
late_submission_penalty=0.4,
)
course_module_with_late_submissions_closed = CourseModule.objects.create(
name="test module late closed",
url="test-module-late-closed",
points_to_pass=50,
course_instance=self.course_instance,
opening_time=self.yesterday - timedelta(days=1),
closing_time=self.yesterday,
late_submissions_allowed=True,
late_submission_deadline=self.today - timedelta(hours=1),
late_submission_penalty=0.4,
)
base_exercise_with_late_open = BaseExercise.objects.create(
name="test exercise late open",
course_module=course_module_with_late_submissions_open,
category=self.learning_object_category,
url="blateopen",
max_submissions=5,
)
base_exercise_with_late_closed = BaseExercise.objects.create(
name="test exercise late closed",
course_module=course_module_with_late_submissions_closed,
category=self.learning_object_category,
url="blateclosed",
max_submissions=5,
)
self.assertFalse(self.base_exercise.can_show_model_solutions_to_student(self.user)) # module is open
self.assertTrue(self.old_base_exercise.can_show_model_solutions_to_student(self.user)) # module is closed
self.assertFalse(
self.base_exercise_with_late_submission_allowed.can_show_model_solutions_to_student(self.user)
) # module is open
self.assertFalse(base_exercise_with_late_open.can_show_model_solutions_to_student(self.user))
self.assertTrue(base_exercise_with_late_closed.can_show_model_solutions_to_student(self.user))
# The user has submitted alone and has no deadline extension.
self.assertEqual(self.old_base_exercise.get_submissions_for_student(self.user.userprofile).count(), 0)
submission1 = Submission.objects.create(
exercise=self.old_base_exercise,
)
submission1.submitters.add(self.user.userprofile)
self.assertTrue(self.old_base_exercise.can_show_model_solutions_to_student(self.user)) # module is closed
# Add a deadline extension that is still active.
deadline_rule_deviation_old_base_exercise = DeadlineRuleDeviation.objects.create(
exercise=self.old_base_exercise,
submitter=self.user.userprofile,
granter=self.teacher.userprofile,
extra_seconds=24*60*60, # One day
)
self.assertFalse(self.old_base_exercise.can_show_model_solutions_to_student(self.user))
# Change the deadline extension so that it is not active anymore.
self.old_course_module.closing_time = self.today - timedelta(hours=2)
self.old_course_module.save()
deadline_rule_deviation_old_base_exercise.delete()
deadline_rule_deviation_old_base_exercise = DeadlineRuleDeviation.objects.create(
exercise=self.old_base_exercise,
submitter=self.user.userprofile,
granter=self.teacher.userprofile,
extra_seconds=10*60,
)
self.assertTrue(self.old_base_exercise.can_show_model_solutions_to_student(self.user))
# Group submission
submission2 = Submission.objects.create(
exercise=base_exercise_with_late_closed,
)
submission2.submitters.add(self.user.userprofile, self.user2.userprofile)
self.assertTrue(base_exercise_with_late_closed.can_show_model_solutions_to_student(self.user))
self.assertTrue(base_exercise_with_late_closed.can_show_model_solutions_to_student(self.user2))
# Add a deadline extension to one group member. It affects all group members.
# Note: deadline deviations are relative to the module closing time, not the late submission deadline.
deadline_rule_deviation_ex_late_closed = DeadlineRuleDeviation.objects.create(
exercise=base_exercise_with_late_closed,
submitter=self.user.userprofile,
granter=self.teacher.userprofile,
extra_seconds=2*24*60*60, # Two days
)
self.assertFalse(base_exercise_with_late_closed.can_show_model_solutions_to_student(self.user))
self.assertFalse(base_exercise_with_late_closed.can_show_model_solutions_to_student(self.user2))
# Change the deadline extension so that it is not active anymore.
deadline_rule_deviation_ex_late_closed.delete()
deadline_rule_deviation_ex_late_closed = DeadlineRuleDeviation.objects.create(
exercise=base_exercise_with_late_closed,
submitter=self.user.userprofile,
granter=self.teacher.userprofile,
extra_seconds=10*60,
)
self.assertTrue(base_exercise_with_late_closed.can_show_model_solutions_to_student(self.user))
self.assertTrue(base_exercise_with_late_closed.can_show_model_solutions_to_student(self.user2))
def test_can_be_shown_as_module_model_solution(self):
chapter = CourseChapter.objects.create(
name="test course chapter",
course_module=self.course_module,
category=self.learning_object_category,
url="c1",
)
deadline_deviation_old_base_exercise = DeadlineRuleDeviation.objects.create(
exercise=self.old_base_exercise,
submitter=self.user.userprofile,
granter=self.teacher.userprofile,
extra_seconds=24*60*60, # One day
)
reveal_rule = RevealRule.objects.create(
trigger=RevealRule.TRIGGER.DEADLINE,
)
self.old_course_module.model_answer = chapter
self.old_course_module.model_answer_reveal_rule = reveal_rule
self.old_course_module.save()
self.base_exercise.parent = chapter
self.base_exercise.save()
self.static_exercise.parent = self.base_exercise
self.static_exercise.save()
# Chapter is model answer to a closed module with a deadline extension
self.assertFalse(chapter.can_be_shown_as_module_model_solution(self.user))
# Unrevealed chapter's child
self.assertFalse(self.base_exercise.can_be_shown_as_module_model_solution(self.user))
# Unrevealed chapter's grandchild
self.assertFalse(self.static_exercise.can_be_shown_as_module_model_solution(self.user))
self.assertTrue(chapter.can_be_shown_as_module_model_solution(self.user2))
deadline_deviation_old_base_exercise.extra_seconds = 0
deadline_deviation_old_base_exercise.save()
self.assertTrue(chapter.can_be_shown_as_module_model_solution(self.user))
self.assertTrue(self.base_exercise.can_be_shown_as_module_model_solution(self.user))
self.assertTrue(self.static_exercise.can_be_shown_as_module_model_solution(self.user))
def test_reveal_rule(self):
reveal_rule = RevealRule.objects.create(
trigger=RevealRule.TRIGGER.MANUAL,
)