Skip to content
test_utils.py 71.1 KiB
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.
"""

Andreas Klöckner's avatar
Andreas Klöckner committed
import unittest
from copy import deepcopy
Dong Zhuang's avatar
Dong Zhuang committed
from django import VERSION as DJANGO_VERSION
Andreas Klöckner's avatar
Andreas Klöckner committed
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
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.utils.timezone import now, timedelta
from django.utils.translation import gettext_noop
from course import (
    constants,
    utils,
)
from course.constants import flow_permission as fperm
Andreas Klöckner's avatar
Andreas Klöckner committed
from course.content import parse_date_spec
from relate.utils import dict_to_struct, struct_to_dict
from tests import factories
Dong Zhuang's avatar
Dong Zhuang committed
from tests.base_test_mixins import (
    CoursesTestMixinBase,
    MockAddMessageMixing,
    SingleCoursePageTestMixin,
    SingleCourseQuizPageTestMixin,
    SingleCourseTestMixin,
Andreas Klöckner's avatar
Andreas Klöckner committed
    SubprocessRunpyContainerMixin,
Andreas Klöckner's avatar
Andreas Klöckner committed
from tests.constants import QUIZ_FLOW_ID
Dong Zhuang's avatar
Dong Zhuang committed

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")]
    LANGUAGES_CONF2 = [
        ("en", "English"),
        ("zh-hans", "Simplified Chinese"),
        ("zh-hans", "my Simplified Chinese"),
        ("de", "German")]

    @override_settings(USE_I18N=False, LANGUAGES=LANGUAGES_CONF1,
                       LANGUAGE_CODE="ko")
    def test_i18n_disabled(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)
        self.assertIn("(ko)", choices[0][1])

    @override_settings(USE_I18N=False, LANGUAGES=LANGUAGES_CONF1,
                       LANGUAGE_CODE="en")
    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,
                       LANGUAGE_CODE="en-us")
    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,
                       LANGUAGE_CODE="ko")
    def test_i18n_enabled(self):
        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,
                       LANGUAGE_CODE="en")
    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,
                       LANGUAGE_CODE="en-us")
    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,
                       LANGUAGE_CODE="en-us"):
            self.lang_descr_get_translated(choice_count=5)

        with override_settings(USE_I18N=True, LANGUAGES=self.LANGUAGES_CONF2,
                       LANGUAGE_CODE="en-us"):
            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,
                       LANGUAGE_CODE="user_customized_lang_code"):
            with self.assertRaises(IOError):
                # because there's no file named "user_customized_lang_code.mo"
                utils.get_course_specific_language_choices()
Dong Zhuang's avatar
Dong Zhuang committed
            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,
                               LANGUAGE_CODE="user_customized_lang_code"):
Dong Zhuang's avatar
Dong Zhuang committed
            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])

Dong Zhuang's avatar
Dong Zhuang committed
class LanguageOverrideTest(SingleCoursePageTestMixin,
                           SubprocessRunpyContainerMixin, TestCase):
Dong Zhuang's avatar
Dong Zhuang committed
    force_login_student_for_each_test = False

Dong Zhuang's avatar
Dong Zhuang committed
    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()

        client = Client()
        client.force_login(cls.student_participation.user)
        cls.start_flow(client, cls.flow_id)

    def setUp(self):
        super().setUp()
        self.client.force_login(self.instructor_participation.user)
Dong Zhuang's avatar
Dong Zhuang committed

    @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")

Dong Zhuang's avatar
Dong Zhuang committed
    page_id_literal_dict = {
        "half": {"literals": [gettext_noop("No answer provided.")]},
        "krylov": {"literals": [gettext_noop("No answer provided."), ]},
Dong Zhuang's avatar
Dong Zhuang committed
        "ice_cream_toppings": {
            "literals": [gettext_noop("No answer provided."), ]},
Dong Zhuang's avatar
Dong Zhuang committed
        "inlinemulti": {
            "literals":
                [gettext_noop("No answer provided."), ]},
Dong Zhuang's avatar
Dong Zhuang committed
        "hgtext": {
            "literals": [gettext_noop("No answer provided.")]},
Dong Zhuang's avatar
Dong Zhuang committed
        "quarter": {
            "literals": [gettext_noop("No answer provided."), ]},
Dong Zhuang's avatar
Dong Zhuang committed
        "pymult": {
            "answer": {"answer": "c = ..."},
            "literals": [
                gettext_noop("Autograder feedback"),
                gettext_noop("Your answer is not correct.")
Dong Zhuang's avatar
Dong Zhuang committed
            ]},
        "addition": {
            "answer": {"answer": "c = a + b"},
            "literals": [
                gettext_noop("Your answer is correct."),
                gettext_noop("It looks like you submitted code that is "
Dong Zhuang's avatar
Dong Zhuang committed
                              "identical to the reference solution. "
                              "This is not allowed."),
                gettext_noop("Here is some feedback on your code"),
Dong Zhuang's avatar
Dong Zhuang committed
            ]},
        "anyup": {"literals": [gettext_noop("No answer provided.")]},
Dong Zhuang's avatar
Dong Zhuang committed
    }

    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():
Dong Zhuang's avatar
Dong Zhuang committed
            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():
Dong Zhuang's avatar
Dong Zhuang committed
            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))
Dong Zhuang's avatar
Dong Zhuang committed
                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)
Dong Zhuang's avatar
Dong Zhuang committed
                        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")


    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
        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
    def setUpTestData(cls):
        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):

        # empty rules
        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)


class GetSessionRuleMixin:

    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):
        super().setUp()

        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)