Newer
Older
from __future__ import annotations
__copyright__ = "Copyright (C) 2017 Dong Zhuang"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from django.conf import settings
from django.test import Client, RequestFactory, SimpleTestCase, TestCase
from django.test.utils import override_settings
from django.utils import translation
from django.utils.translation import gettext_noop
from course.constants import flow_permission as fperm
from course.content import parse_date_spec
from relate.utils import dict_to_struct, struct_to_dict
from tests import factories
CoursesTestMixinBase,
MockAddMessageMixing,
SingleCoursePageTestMixin,
SingleCourseQuizPageTestMixin,
SingleCourseTestMixin,
from tests.utils import mock
if DJANGO_VERSION < (2, 0):
REAL_TRANSLATION_FUNCTION_TO_MOCK = (
"django.utils.translation.trans_real.do_translate")
real_trans_side_effect = lambda x, y: x # noqa
else:
# "do_translate(message, translation_function)" was refactored to
# "gettext(message)" since Django >= 2.0
REAL_TRANSLATION_FUNCTION_TO_MOCK = (
"django.utils.translation._trans.gettext")
real_trans_side_effect = lambda x: x # noqa
class GetCourseSpecificLanguageChoicesTest(SimpleTestCase):
# test course.utils.get_course_specific_language_choices
LANGUAGES_CONF1 = [
("en", "English"),
("zh-hans", "Simplified Chinese"),
("de", "German")]
("en", "English"),
("zh-hans", "Simplified Chinese"),
("zh-hans", "my Simplified Chinese"),
("de", "German")]
@override_settings(USE_I18N=False, LANGUAGES=LANGUAGES_CONF1,
choices = utils.get_course_specific_language_choices()
self.assertTrue(choices[0][1].startswith("Default:"))
self.assertNotIn("disabled", choices[0][1])
self.assertEqual(len(choices), 4)
self.assertIn("(ko)", choices[0][1])
@override_settings(USE_I18N=False, LANGUAGES=LANGUAGES_CONF1,
def test_i18n_disabled_lang_items_has_same_lang_code_with_language_code(self):
choices = utils.get_course_specific_language_choices()
self.assertTrue(choices[0][1].startswith("Default:"))
self.assertNotIn("disabled", choices[0][1])
self.assertEqual(len(choices), 3)
@override_settings(USE_I18N=False, LANGUAGES=LANGUAGES_CONF2,
def test_i18n_disabled_lang_items_having_duplicated_lang_code(self):
choices = utils.get_course_specific_language_choices()
self.assertTrue(choices[0][1].startswith("Default:"))
self.assertNotIn("disabled", choices[0][1])
self.assertEqual(len(choices), 4)
@override_settings(USE_I18N=True, LANGUAGES=LANGUAGES_CONF1,
choices = utils.get_course_specific_language_choices()
self.assertTrue(choices[0][1].startswith("Default: disabled"))
self.assertEqual(len(choices), 5)
self.assertIn("(ko)", choices[1][1])
@override_settings(USE_I18N=True, LANGUAGES=LANGUAGES_CONF1,
def test_i18n_enabled_lang_items_has_same_lang_code_with_language_code(self):
choices = utils.get_course_specific_language_choices()
self.assertTrue(choices[0][1].startswith("Default: disabled"))
self.assertEqual(len(choices), 4)
@override_settings(USE_I18N=True, LANGUAGES=LANGUAGES_CONF2,
def test_i18n_enabled_lang_items_having_duplicated_lang_code(self):
choices = utils.get_course_specific_language_choices()
self.assertEqual(len(choices), 5)
self.assertTrue(choices[0][1].startswith("Default: disabled"))
def lang_descr_get_translated(self, choice_count):
with mock.patch("course.utils._") as mock_gettext, \
mock.patch("django.utils.translation.gettext_lazy") \
as mock_gettext_lazy:
mock_gettext.side_effect = lambda x: x
mock_gettext_lazy.side_effect = lambda x: x
choices = utils.get_course_specific_language_choices()
self.assertEqual(len(choices), choice_count)
# "English", "Default", "my Simplified Chinese" and "German" are
# called by django.utils.translation.gettext, for at least once.
# Another language description literals (especially "Simplified Chinese")
# are not called by it.
self.assertTrue(mock_gettext.call_count >= 4)
simplified_chinese_as_arg_count = 0
my_simplified_chinese_as_arg_count = 0
for call in mock_gettext.call_args_list:
arg, kwargs = call
if "my Simplified Chinese" in arg:
my_simplified_chinese_as_arg_count += 1
if "Simplified Chinese" in arg:
simplified_chinese_as_arg_count += 1
self.assertEqual(simplified_chinese_as_arg_count, 0)
self.assertTrue(my_simplified_chinese_as_arg_count > 0)
def test_lang_descr_translated(self):
with override_settings(USE_I18N=True, LANGUAGES=self.LANGUAGES_CONF2,
self.lang_descr_get_translated(choice_count=5)
with override_settings(USE_I18N=True, LANGUAGES=self.LANGUAGES_CONF2,
self.lang_descr_get_translated(choice_count=5)
def test_user_customized_lang_code_as_settings_language_code(self):
with override_settings(USE_I18N=True, LANGUAGES=self.LANGUAGES_CONF2,
with self.assertRaises(IOError):
# because there's no file named "user_customized_lang_code.mo"
utils.get_course_specific_language_choices()
with mock.patch(REAL_TRANSLATION_FUNCTION_TO_MOCK) as mock_gettext:
mock_gettext.side_effect = real_trans_side_effect
choices = utils.get_course_specific_language_choices()
# The language description is the language_code, because it can't
# be found in django.conf.locale.LANG_INFO
self.assertEqual(choices[1][1], "user_customized_lang_code")
with override_settings(USE_I18N=False, LANGUAGES=self.LANGUAGES_CONF2,
with mock.patch(REAL_TRANSLATION_FUNCTION_TO_MOCK) as mock_gettext:
mock_gettext.side_effect = real_trans_side_effect
choices = utils.get_course_specific_language_choices()
# The language description is the language_code, because it can't
# be found in django.conf.locale.LANG_INFO
self.assertIn("user_customized_lang_code", choices[0][1])
class LanguageOverrideTest(SingleCoursePageTestMixin,
SubprocessRunpyContainerMixin, TestCase):
# test course.utils.LanguageOverride
client = Client()
client.force_login(cls.student_participation.user)
cls.start_flow(client, cls.flow_id)
super().setUp()
self.client.force_login(self.instructor_participation.user)
@override_settings(RELATE_ADMIN_EMAIL_LOCALE="de", LANGUAGE_CODE="ko")
def test_language_override_no_course_force_lang(self):
if self.course.force_lang:
self.course.force_lang = ""
self.course.save()
with utils.LanguageOverride(course=self.course):
self.assertEqual(translation.get_language(), "de")
self.assertEqual(translation.gettext("user"), "Benutzer")
self.assertEqual(translation.get_language(), "ko")
self.assertEqual(translation.gettext("user"), "사용자")
@override_settings(RELATE_ADMIN_EMAIL_LOCALE="de", LANGUAGE_CODE="ko")
def test_language_override_course_has_force_lang(self):
self.course.force_lang = "zh-hans"
self.course.save()
with utils.LanguageOverride(course=self.course):
self.assertEqual(translation.get_language(), "zh-hans")
self.assertEqual(translation.get_language(), "ko")
@override_settings(RELATE_ADMIN_EMAIL_LOCALE=None)
def test_language_override_no_course_force_lang_no_admin_lang(self):
if self.course.force_lang:
self.course.force_lang = ""
self.course.save()
with utils.LanguageOverride(course=self.course):
self.assertEqual(translation.get_language(), None)
self.assertEqual(translation.gettext("whatever"), "whatever")
self.assertEqual(translation.get_language(), "en-us")
@override_settings(RELATE_ADMIN_EMAIL_LOCALE="de")
def test_language_override_no_course_force_lang_no_langcode(self):
if self.course.force_lang:
self.course.force_lang = ""
self.course.save()
translation.deactivate_all()
with utils.LanguageOverride(course=self.course):
self.assertEqual(translation.get_language(), "de")
self.assertEqual(translation.gettext("user"), "Benutzer")
self.assertEqual(translation.get_language(), None)
self.assertEqual(translation.gettext("whatever"), "whatever")
@override_settings(RELATE_ADMIN_EMAIL_LOCALE="de")
def test_language_override_deactivate(self):
self.course.force_lang = "zh-hans"
self.course.save()
with utils.LanguageOverride(course=self.course, deactivate=True):
self.assertEqual(translation.get_language(), "zh-hans")
self.assertEqual(translation.gettext("user"), "用户")
self.assertEqual(translation.get_language(), "en-us")
"half": {"literals": [gettext_noop("No answer provided.")]},
"krylov": {"literals": [gettext_noop("No answer provided."), ]},
"literals": [gettext_noop("No answer provided."), ]},
[gettext_noop("No answer provided."), ]},
"literals": [gettext_noop("No answer provided.")]},
"literals": [gettext_noop("No answer provided."), ]},
"pymult": {
"answer": {"answer": "c = ..."},
"literals": [
gettext_noop("Autograder feedback"),
gettext_noop("Your answer is not correct.")
]},
"addition": {
"answer": {"answer": "c = a + b"},
"literals": [
gettext_noop("Your answer is correct."),
gettext_noop("It looks like you submitted code that is "
"identical to the reference solution. "
"This is not allowed."),
gettext_noop("Here is some feedback on your code"),
"anyup": {"literals": [gettext_noop("No answer provided.")]},
}
def feedback_test(self, course_force_lang):
self.course.force_lang = course_force_lang
self.course.save()
for page_id, v in self.page_id_literal_dict.items():
if "answer" not in v:
continue
self.post_answer_by_page_id(page_id, answer_data=v["answer"])
self.end_flow()
for page_id, v in self.page_id_literal_dict.items():
with self.subTest(page_id=page_id, course_force_lang=course_force_lang):
resp = self.client.get(self.get_page_url_by_page_id(page_id))
for literal in v["literals"]:
if not course_force_lang:
self.assertContains(resp, literal)
else:
with translation.override(course_force_lang):
translated_literal = translation.gettext(literal)
self.assertContains(resp, translated_literal)
@override_settings(RELATE_ADMIN_EMAIL_LOCALE="en-us")
def test_course_no_force_lang_feedback(self):
self.feedback_test(course_force_lang="")
@override_settings(RELATE_ADMIN_EMAIL_LOCALE="en-us")
def test_course_force_lang_zh_hans_feedback(self):
self.feedback_test(course_force_lang="zh-hans")
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
def __init__(self, a=None):
self.a = a
class GetattrWithFallbackTest(unittest.TestCase):
# test utils.getattr_with_fallback
def test_result_found(self):
aggregates = [Foo(), Foo(1), Foo(None)]
self.assertEqual(utils.getattr_with_fallback(aggregates, "a", None), 1)
def test_fallbacked(self):
aggregates = [Foo(), Foo(), Foo(None)]
self.assertEqual(utils.getattr_with_fallback(aggregates, "a", 2), 2)
class FlowSessionAccessRuleText(unittest.TestCase):
# test utils.FlowSessionAccessRule
def test_human_readable_permissions(self):
arule = utils.FlowSessionAccessRule(
permissions=frozenset([fperm.end_session, fperm.see_correctness])
)
result = arule.human_readable_permissions()
self.assertIsInstance(result, list)
self.assertEqual(len(result), 2)
class EvalGenericConditionsTest(unittest.TestCase):
# test utils._eval_generic_conditions
def setUp(self):
self.course = mock.MagicMock()
self.participation = mock.MagicMock()
self.login_exam_ticket = mock.MagicMock()
self.flow_id = mock.MagicMock()
fake_parse_date_spec = mock.patch("course.utils.parse_date_spec")
self.mock_parse_date_spec = fake_parse_date_spec.start()
self.mock_parse_date_spec.return_value = now() - timedelta(days=1)
self.addCleanup(fake_parse_date_spec.stop)
fake_get_participation_role_identifiers = mock.patch(
"course.enrollment.get_participation_role_identifiers")
self.mock_get_participation_role_identifiers = (
fake_get_participation_role_identifiers.start()
)
self.mock_get_participation_role_identifiers.return_value = (
["student", "ta"])
self.addCleanup(fake_get_participation_role_identifiers.stop)
def test_if_before(self):
rule = utils.FlowSessionRuleBase()
rule.if_before = mock.MagicMock()
now_datetime = now() + timedelta(days=2)
self.assertFalse(
utils._eval_generic_conditions(
rule, self.course, self.participation,
now_datetime, self.flow_id, self.login_exam_ticket))
now_datetime = now() - timedelta(days=2)
self.assertTrue(
utils._eval_generic_conditions(
rule, self.course, self.participation,
now_datetime, self.flow_id, self.login_exam_ticket))
def test_if_after(self):
rule = utils.FlowSessionRuleBase()
rule.if_after = mock.MagicMock()
now_datetime = now() - timedelta(days=2)
self.assertFalse(
utils._eval_generic_conditions(
rule, self.course, self.participation,
now_datetime, self.flow_id, self.login_exam_ticket))
now_datetime = now() + timedelta(days=2)
self.assertTrue(
utils._eval_generic_conditions(
rule, self.course, self.participation,
now_datetime, self.flow_id, self.login_exam_ticket))
def test_if_has_role(self):
rule = utils.FlowSessionRuleBase()
rule.if_has_role = ["instructor"]
now_datetime = now() - timedelta(days=2)
self.assertFalse(
utils._eval_generic_conditions(
rule, self.course, self.participation,
now_datetime, self.flow_id, self.login_exam_ticket))
rule.if_has_role = ["student"]
self.assertTrue(
utils._eval_generic_conditions(
rule, self.course, self.participation,
now_datetime, self.flow_id, self.login_exam_ticket))
def test_if_signed_in_with_matching_exam_ticket(self):
rule = utils.FlowSessionRuleBase()
rule.if_signed_in_with_matching_exam_ticket = True
now_datetime = now() - timedelta(days=2)
# login_exam_ticket is None
self.assertFalse(
utils._eval_generic_conditions(
rule, self.course, self.participation,
now_datetime, self.flow_id, None))
# flow_id not match
self.flow_id = "bar"
self.login_exam_ticket.exam = mock.MagicMock()
self.login_exam_ticket.exam.flow_id = "foo"
self.login_exam_ticket.participation = self.participation
self.assertFalse(
utils._eval_generic_conditions(
rule, self.course, self.participation,
now_datetime, self.flow_id, self.login_exam_ticket))
# participation does not match
self.flow_id = "foo"
self.login_exam_ticket.exam = mock.MagicMock()
self.login_exam_ticket.exam.flow_id = "foo"
self.assertTrue(
utils._eval_generic_conditions(
rule, self.course, self.participation,
now_datetime, self.flow_id, self.login_exam_ticket))
# flow_id matched
self.flow_id = "foo"
self.login_exam_ticket.exam = mock.MagicMock()
self.login_exam_ticket.exam.flow_id = "foo"
self.login_exam_ticket.participation = self.participation
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
self.assertTrue(
utils._eval_generic_conditions(
rule, self.course, self.participation,
now_datetime, self.flow_id, self.login_exam_ticket))
class EvalGenericSessionConditionsTest(unittest.TestCase):
def setUp(self):
self.session = mock.MagicMock()
fake_parse_date_spec = mock.patch("course.utils.parse_date_spec")
self.mock_parse_date_spec = fake_parse_date_spec.start()
self.mock_parse_date_spec.return_value = now() + timedelta(days=1)
self.addCleanup(fake_parse_date_spec.stop)
def test_true(self):
rule = utils.FlowSessionRuleBase()
self.assertTrue(
utils._eval_generic_session_conditions(rule, self.session, now()))
def test_if_has_tag(self):
rule = utils.FlowSessionRuleBase()
rule.if_has_tag = "foo"
now_datetime = now()
self.session.access_rules_tag = "bar"
self.assertFalse(
utils._eval_generic_session_conditions(rule, self.session, now_datetime)
)
self.session.access_rules_tag = "foo"
self.assertTrue(
utils._eval_generic_session_conditions(rule, self.session, now_datetime)
)
def test_if_started_before(self):
rule = utils.FlowSessionRuleBase()
rule.if_started_before = mock.MagicMock()
now_datetime = now()
self.session.start_time = now()
self.assertTrue(
utils._eval_generic_session_conditions(rule, self.session, now_datetime)
)
self.session.start_time = now() + timedelta(days=2)
self.assertFalse(
utils._eval_generic_session_conditions(rule, self.session, now_datetime)
)
class EvalParticipationTagsConditionsTest(CoursesTestMixinBase, TestCase):
# test utils._eval_participation_tags_conditions
@classmethod
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
course = factories.CourseFactory()
cls.participation1 = factories.ParticipationFactory(
course=course)
tag1 = factories.ParticipationTagFactory(
course=course,
name="tag1")
tag2 = factories.ParticipationTagFactory(
course=course,
name="tag2")
tag3 = factories.ParticipationTagFactory(
course=course,
name="tag3")
cls.participation2 = factories.ParticipationFactory(
course=course)
cls.participation2.tags.set([tag1, tag2])
cls.participation3 = factories.ParticipationFactory(
course=course)
cls.participation3.tags.set([tag1, tag2, tag3])
def test_no_participation(self):
rule = utils.FlowSessionRuleBase()
rule.if_has_participation_tags_any = ["tag1"]
self.assertFalse(
utils._eval_participation_tags_conditions(rule, None))
def test_true(self):
rule = utils.FlowSessionRuleBase()
self.assertTrue(
utils._eval_participation_tags_conditions(rule, None))
self.assertTrue(
utils._eval_participation_tags_conditions(rule, self.participation1))
self.assertTrue(
utils._eval_participation_tags_conditions(rule, self.participation2))
self.assertTrue(
utils._eval_participation_tags_conditions(rule, self.participation3))
def test_if_has_participation_tags_any(self):
rule = utils.FlowSessionRuleBase()
rule.if_has_participation_tags_any = ["tag1", "tag3"]
self.assertFalse(
utils._eval_participation_tags_conditions(rule, self.participation1))
self.assertTrue(
utils._eval_participation_tags_conditions(rule, self.participation2))
self.assertTrue(
utils._eval_participation_tags_conditions(rule, self.participation3))
rule.if_has_participation_tags_any = ["foo"]
self.assertFalse(
utils._eval_participation_tags_conditions(rule, self.participation3))
def test_if_has_participation_tags_all(self):
rule = utils.FlowSessionRuleBase()
rule.if_has_participation_tags_all = ["tag1", "tag3"]
self.assertFalse(
utils._eval_participation_tags_conditions(rule, self.participation1))
self.assertFalse(
utils._eval_participation_tags_conditions(rule, self.participation2))
self.assertTrue(
utils._eval_participation_tags_conditions(rule, self.participation3))
class GetFlowRulesTest(SingleCourseTestMixin, TestCase):
# test utils.get_flow_rules
flow_id = QUIZ_FLOW_ID
def test_no_rules(self):
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
flow_desc = self.get_hacked_flow_desc(del_rules=True)
default_rules_desc = [mock.MagicMock(), mock.MagicMock()]
result = utils.get_flow_rules(
flow_desc, constants.flow_rule_kind.start,
self.student_participation,
self.flow_id,
now(),
consider_exceptions=False,
default_rules_desc=default_rules_desc
)
self.assertListEqual(result, default_rules_desc)
result = utils.get_flow_rules(
flow_desc, constants.flow_rule_kind.start,
self.student_participation,
self.flow_id,
now(),
consider_exceptions=True,
default_rules_desc=default_rules_desc
)
self.assertListEqual(result, default_rules_desc)
def test_rules_with_given_kind(self):
# use real rules
flow_desc = self.get_hacked_flow_desc()
default_rules_desc = [mock.MagicMock(), mock.MagicMock()]
for kind in dict(constants.FLOW_RULE_KIND_CHOICES).keys():
with self.subTest(missing_kind=kind):
result = utils.get_flow_rules(
flow_desc, kind,
self.student_participation,
self.flow_id,
now(),
consider_exceptions=False,
default_rules_desc=default_rules_desc
)
# there are existing rule for those kind
self.assertNotEqual(result, default_rules_desc)
def test_rules_with_no_given_kind(self):
flow_desc_dict = self.get_hacked_flow_desc(as_dict=True)
default_rules_desc = [mock.MagicMock(), mock.MagicMock()]
for kind in dict(constants.FLOW_RULE_KIND_CHOICES).keys():
flow_desc_dict_copy = deepcopy(flow_desc_dict)
rules_dict = struct_to_dict(flow_desc_dict_copy["rules"])
# delete kind from flow_desc
rules_dict.pop(kind)
flow_desc_dict_copy["rules"] = dict_to_struct(rules_dict)
flow_desc = dict_to_struct(flow_desc_dict_copy)
assert not hasattr(flow_desc.rules, kind)
with self.subTest(missing_kind=kind):
result = utils.get_flow_rules(
flow_desc, kind,
self.student_participation,
self.flow_id,
now(),
consider_exceptions=False,
default_rules_desc=default_rules_desc
)
self.assertListEqual(result, default_rules_desc)
def test_not_consider_exist_exceptions(self):
# use real rules
flow_desc = self.get_hacked_flow_desc()
default_rules_desc = [mock.MagicMock(), mock.MagicMock()]
factories.FlowRuleExceptionFactory(
flow_id=self.flow_id,
participation=self.student_participation,
kind=constants.flow_rule_kind.start,
rule={
"if_after": "end_week 1"
}
)
result = utils.get_flow_rules(
flow_desc, constants.flow_rule_kind.start,
self.student_participation,
self.flow_id,
now(),
consider_exceptions=False, # NOT consider
default_rules_desc=default_rules_desc
)
exist_start_rule = flow_desc.rules.start
self.assertNotEqual(result, default_rules_desc)
self.assertEqual(exist_start_rule, result)
def test_consider_exist_exceptions_is_default_to_true(self):
# use real rules
flow_desc = self.get_hacked_flow_desc()
exist_start_rule = flow_desc.rules.start
default_rules_desc = [mock.MagicMock(), mock.MagicMock()]
# creating 1 rules without expiration
factories.FlowRuleExceptionFactory(
flow_id=self.flow_id,
participation=self.student_participation,
kind=constants.flow_rule_kind.start,
creation_time=now() - timedelta(days=1),
rule={
"if_after": "end_week 1"
}
)
# consider_exceptions not specified
result = utils.get_flow_rules(
flow_desc, constants.flow_rule_kind.start,
self.student_participation,
self.flow_id,
now(),
default_rules_desc=default_rules_desc
)
self.assertNotEqual(result, default_rules_desc)
self.assertEqual(len(result), len(exist_start_rule) + 1)
def test_consider_exist_exceptions(self):
# use real rules
flow_desc = self.get_hacked_flow_desc()
default_rules_desc = [mock.MagicMock(), mock.MagicMock()]
# creating 2 rules without expiration
factories.FlowRuleExceptionFactory(
flow_id=self.flow_id,
participation=self.student_participation,
kind=constants.flow_rule_kind.start,
creation_time=now() - timedelta(days=1),
rule={
"if_after": "end_week 1"
}
)
factories.FlowRuleExceptionFactory(
flow_id=self.flow_id,
participation=self.student_participation,
kind=constants.flow_rule_kind.start,
rule={
"if_before": "end_week 2"
},
creation_time=now() - timedelta(minutes=3),
)
result = utils.get_flow_rules(
flow_desc, constants.flow_rule_kind.start,
self.student_participation,
self.flow_id,
now(),
consider_exceptions=True,
default_rules_desc=default_rules_desc
)
exist_start_rule = flow_desc.rules.start
self.assertNotEqual(result, default_rules_desc)
self.assertEqual(len(result), len(exist_start_rule) + 2)
self.assertEqual(exist_start_rule, result[2:])
# last create ordered first
self.assertEqual(result[0].if_before, "end_week 2")
def test_consider_exist_exceptions_rule_expiration(self):
# use real rules
flow_desc = self.get_hacked_flow_desc()
exist_start_rule = flow_desc.rules.start
default_rules_desc = [mock.MagicMock(), mock.MagicMock()]
# creating 2 rules without expiration
factories.FlowRuleExceptionFactory(
flow_id=self.flow_id,
participation=self.student_participation,
expiration=now() - timedelta(hours=12),
kind=constants.flow_rule_kind.start,
creation_time=now() - timedelta(days=1),
rule={
"if_after": "end_week 1"
}
)
factories.FlowRuleExceptionFactory(
flow_id=self.flow_id,
participation=self.student_participation,
expiration=now() + timedelta(hours=12),
kind=constants.flow_rule_kind.start,
rule={
"if_before": "end_week 2"
},
creation_time=now() - timedelta(minutes=3),
)
# {{{ all exceptions not due
now_datetime = now() - timedelta(days=3)
result = utils.get_flow_rules(
flow_desc, constants.flow_rule_kind.start,
self.student_participation,
self.flow_id,
now_datetime,
consider_exceptions=True,
default_rules_desc=default_rules_desc
)
self.assertEqual(len(result), len(exist_start_rule) + 2)
self.assertEqual(exist_start_rule, result[2:])
# last create ordered first
self.assertEqual(result[0].if_before, "end_week 2")
# }}}
# {{{ one exception expired
now_datetime = now()
result = utils.get_flow_rules(
flow_desc, constants.flow_rule_kind.start,
self.student_participation,
self.flow_id,
now_datetime,
consider_exceptions=True,
default_rules_desc=default_rules_desc
)
self.assertEqual(len(result), len(exist_start_rule) + 1)
self.assertEqual(exist_start_rule, result[1:])
# last create ordered first
self.assertEqual(result[0].if_before, "end_week 2")
# }}}
# {{{ all exceptions expired
now_datetime = now() + timedelta(days=5)
result = utils.get_flow_rules(
flow_desc, constants.flow_rule_kind.start,
self.student_participation,
self.flow_id,
now_datetime,
consider_exceptions=True,
default_rules_desc=default_rules_desc
)
self.assertEqual(len(result), len(exist_start_rule))
self.assertEqual(exist_start_rule, result)
# }}}
my_mock_event_time = mock.MagicMock()
my_test_event_1_time = now() - timedelta(days=2)
my_test_event_2_time = now()
my_test_event_3_time = now() + timedelta(days=1)
def parse_date_spec_get_rule_test_side_effect(
course, datespec, vctx=None, location=None):
if datespec == "my_mock_event_time":
return my_mock_event_time
if datespec == "my_test_event 1":
return my_test_event_1_time
if datespec == "my_test_event 2":
return my_test_event_2_time
if datespec == "my_test_event 3":
return my_test_event_3_time
return parse_date_spec(course, datespec, vctx, location)
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
flow_id = QUIZ_FLOW_ID
@property
def call_func(self):
raise NotImplementedError()
def get_updated_kwargs(self, **extra_kwargs):
kwargs = deepcopy(self.default_kwargs)
kwargs.update(extra_kwargs)
return kwargs
@property
def default_kwargs(self):
raise NotImplementedError()
@property
def fallback_rule(self):
raise NotImplementedError()
def get_result(self, **extra_kwargs):
raise NotImplementedError()
def assertRuleEqual(self, rule, expected_rule): # noqa
self.assertIsInstance(rule, self.rule_klass)
rule_dict = struct_to_dict(rule)
if isinstance(expected_rule, dict):
expected_rule_dict = expected_rule
else:
self.assertIsInstance(expected_rule, self.rule_klass)
expected_rule_dict = struct_to_dict(expected_rule)
self.assertDictEqual(rule_dict, expected_rule_dict)
def setUp(self):
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
fake_get_flow_rules = mock.patch("course.utils.get_flow_rules")
self.mock_get_flow_rules = fake_get_flow_rules.start()
self.addCleanup(fake_get_flow_rules.stop)
fake_eval_generic_conditions = mock.patch(
"course.utils._eval_generic_conditions")
self.mock_eval_generic_conditions = fake_eval_generic_conditions.start()
self.addCleanup(fake_eval_generic_conditions.stop)
fake_eval_participation_tags_conditions = mock.patch(
"course.utils._eval_participation_tags_conditions")
self.mock_eval_participation_tags_conditions = (
fake_eval_participation_tags_conditions.start())
self.addCleanup(fake_eval_participation_tags_conditions.stop)
fake_eval_generic_session_conditions = mock.patch(
"course.utils._eval_generic_session_conditions")
self.mock_eval_generic_session_conditions = (
fake_eval_generic_session_conditions.start())
self.addCleanup(fake_eval_generic_session_conditions.stop)
fake_get_participation_role_identifiers = mock.patch(
"course.enrollment.get_participation_role_identifiers")
self.mock_get_participation_role_identifiers = (
fake_get_participation_role_identifiers.start())
self.mock_get_participation_role_identifiers.return_value = ["student"]
self.addCleanup(fake_get_participation_role_identifiers.stop)
fake_parse_date_spec = mock.patch("course.utils.parse_date_spec")
self.mock_parse_date_spec = fake_parse_date_spec.start()
self.mock_parse_date_spec.side_effect = (
parse_date_spec_get_rule_test_side_effect)
self.addCleanup(fake_parse_date_spec.stop)
class GetSessionStartRuleTest(GetSessionRuleMixin, SingleCourseTestMixin, TestCase):
# test utils.get_session_start_rule
call_func = utils.get_session_start_rule
rule_klass = utils.FlowSessionStartRule
fallback_rule = utils.FlowSessionStartRule(
may_list_existing_sessions=False,
may_start_new_session=False)
@property
def default_kwargs(self):
return {
"course": self.course,
"participation": self.student_participation,
"flow_id": self.flow_id,
"flow_desc": mock.MagicMock(),
"now_datetime": now(),
"facilities": None,
"for_rollover": False,
"login_exam_ticket": None,
}
def get_result(self, **extra_kwargs):
kwargs = self.get_updated_kwargs(**extra_kwargs)
return utils.get_session_start_rule(**kwargs)
def get_default_rule(self, **kwargs):
defaults = {
"tag_session": None,
"may_start_new_session": True,
"may_list_existing_sessions": True,
"default_expiration_mode": None,
}
defaults.update(kwargs)
return utils.FlowSessionStartRule(**defaults)
def test_no_rules(self):
self.mock_get_flow_rules.return_value = []
result = self.get_result()
self.assertRuleEqual(self.fallback_rule, result)
# make sure get_flow_rules is called with expected default_rules_desc
self.assertEqual(self.mock_get_flow_rules.call_count, 1)
self.assertIn("default_rules_desc", self.mock_get_flow_rules.call_args[1])
default_rules_desc = (
self.mock_get_flow_rules.call_args[1]["default_rules_desc"])
self.assertTrue(default_rules_desc[0].may_start_new_session)