Skip to content
test_utils.py 78.5 KiB
Newer Older
# -*- coding: utf-8 -*-

from __future__ import division

__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.
"""

Dong Zhuang's avatar
Dong Zhuang committed
import six
from datetime import datetime
from copy import deepcopy

import unittest
Dong Zhuang's avatar
Dong Zhuang committed
from unittest import skipUnless
from django.test import SimpleTestCase, TestCase, RequestFactory
from django.utils.timezone import now, timedelta
from django.test.utils import override_settings
Dong Zhuang's avatar
Dong Zhuang committed
from django import VERSION as DJANGO_VERSION
from django.utils import translation
Dong Zhuang's avatar
Dong Zhuang committed
from django.utils.translation import ugettext_noop
from django.conf import settings

from relate.utils import (
    localize_datetime, format_datetime_local,
    struct_to_dict, dict_to_struct)
from course import utils
from course.content import parse_date_spec
from course import constants  # noqa
from course.constants import flow_permission as fperm
from tests.constants import QUIZ_FLOW_ID
Dong Zhuang's avatar
Dong Zhuang committed
from tests.base_test_mixins import (
    CoursesTestMixinBase,
    SingleCoursePageTestMixin, SubprocessRunpyContainerMixin,
    SingleCourseTestMixin, FallBackStorageMessageTestMixin,
)
from tests import factories
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
Dong Zhuang's avatar
Dong Zhuang committed
def is_travis_py3():
    import os

    if "RL_TRAVIS_TEST" not in os.environ:
        return False

    if six.PY2:
        return False

    return True


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_ugettext, \
                mock.patch("django.utils.translation.ugettext_lazy") \
                as mock_ugettext_lazy:
            mock_ugettext.side_effect = lambda x: x
            mock_ugettext_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.ugettext, for at least once.
            # Another language description literals (especially "Simplified Chinese")
            # are not called by it.
            self.assertTrue(mock_ugettext.call_count >= 4)
            simplified_chinese_as_arg_count = 0
            my_simplified_chinese_as_arg_count = 0
            for call in mock_ugettext.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):  # noqa
        super(LanguageOverrideTest, cls).setUpTestData()
        cls.c.force_login(cls.instructor_participation.user)
        cls.start_flow(cls.flow_id)

    @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.ugettext("user"), u"Benutzer")

        self.assertEqual(translation.get_language(), "ko")
        self.assertEqual(translation.ugettext("user"), u"사용자")

    @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.ugettext("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.ugettext("user"), u"Benutzer")

        self.assertEqual(translation.get_language(), None)
        self.assertEqual(translation.ugettext("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.ugettext("user"), u"用户")

        self.assertEqual(translation.get_language(), "en-us")

Dong Zhuang's avatar
Dong Zhuang committed
    page_id_literal_dict = {
        "half": {"literals": [ugettext_noop("No answer provided.")]},
        "krylov": {"literals": [ugettext_noop("No answer provided."), ]},
        "ice_cream_toppings": {
            "literals": [ugettext_noop("No answer provided."), ]},
        "inlinemulti": {
            "literals":
                [ugettext_noop("No answer provided."), ]},
        "hgtext": {
            "literals": [ugettext_noop("No answer provided.")]},
        "quarter": {
            "literals": [ugettext_noop("No answer provided."), ]},
        "pymult": {
            "answer": {"answer": "c = ..."},
            "literals": [
                ugettext_noop("Autograder feedback"),
                ugettext_noop("Your answer is not correct.")
            ]},
        "addition": {
            "answer": {"answer": "c = a + b"},
            "literals": [
                ugettext_noop("Your answer is correct."),
                ugettext_noop("It looks like you submitted code that is "
                              "identical to the reference solution. "
                              "This is not allowed."),
                ugettext_noop("Here is some feedback on your code"),
            ]},
        "anyup": {"literals": [ugettext_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 six.iteritems(self.page_id_literal_dict):
            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 six.iteritems(self.page_id_literal_dict):
            with self.subTest(page_id=page_id, course_force_lang=course_force_lang):
                resp = self.c.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.ugettext(literal)
                        self.assertContains(resp, translated_literal)

    @skipUnless(is_travis_py3(), "This is tested only on Travis with PY3.5")
    @override_settings(RELATE_ADMIN_EMAIL_LOCALE="en-us")
    def test_course_no_force_lang_feedback(self):
        self.feedback_test(course_force_lang="")

    @skipUnless(is_travis_py3(), "This is tested only on Travis with PY3.5")
    @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")


class GetCustomPageTypesStopSupportDeadlineTest(unittest.TestCase):
    # test course.utils.get_custom_page_types_stop_support_deadline

    force_deadline = datetime(2019, 1, 1, 0, 0, 0, 0)

    def test_custom_deadline_before_force_deadline(self):
        deadline = datetime(2017, 1, 1, 0, 0, 0, 0)
        with override_settings(
                RELATE_CUSTOM_PAGE_TYPES_REMOVED_DEADLINE=deadline):
            self.assertEqual(
                utils.get_custom_page_types_stop_support_deadline(),
                localize_datetime(deadline))

    def test_custom_deadline_after_force_deadline(self):
        deadline = datetime(2019, 1, 1, 1, 0, 0, 0)
        with override_settings(
                RELATE_CUSTOM_PAGE_TYPES_REMOVED_DEADLINE=deadline):
            self.assertEqual(
                utils.get_custom_page_types_stop_support_deadline(),
                localize_datetime(self.force_deadline))

    def test_custom_deadline_not_configured(self):
        with override_settings():
            del settings.RELATE_CUSTOM_PAGE_TYPES_REMOVED_DEADLINE
            self.assertEqual(
                utils.get_custom_page_types_stop_support_deadline(),
                localize_datetime(self.force_deadline))


class CustomRepoPageStopSupportTest(SingleCourseTestMixin,
                                    FallBackStorageMessageTestMixin, TestCase):

    def setUp(self):
        super(CustomRepoPageStopSupportTest, self).setUp()
        self.current_commit_sha = self.get_course_commit_sha(
            self.instructor_participation)

    force_deadline = datetime(2019, 1, 1, 0, 0, 0, 0)

    custom_page_type = "repo:simple_questions.MyTextQuestion"

    commit_sha_deprecated = b"593a1cdcecc6f4759fd5cadaacec0ba9dd0715a7"

    deprecate_warning_message_pattern = (
        "Custom page type '%(page_type)s' specified. "
        "Custom page types will stop being supported in "
        "RELATE at %(date_time)s.")

    expired_error_message_pattern = (
        "Custom page type '%(page_type)s' specified. "
        "Custom page types were no longer supported in "
        "RELATE since %(date_time)s.")

    def test_custom_page_types_deprecate(self):
        deadline = datetime(2039, 1, 1, 0, 0, 0, 0)

        with override_settings(
                RELATE_CUSTOM_PAGE_TYPES_REMOVED_DEADLINE=deadline):
            resp = self.post_update_course_content(
                commit_sha=self.commit_sha_deprecated)
            self.assertEqual(resp.status_code, 200)

            if datetime.now() <= self.force_deadline:
                expected_message = (
                    self.deprecate_warning_message_pattern
                    % {"page_type": self.custom_page_type,
                       "date_time": format_datetime_local(self.force_deadline)}
                )
                self.assertEqual(
                    self.get_course_commit_sha(self.instructor_participation),
                    self.commit_sha_deprecated)
            else:
                expected_message = (
                    self.expired_error_message_pattern
                    % {"page_type": self.custom_page_type,
                       "date_time": format_datetime_local(self.force_deadline)}
                )
                self.assertEqual(
                    self.get_course_commit_sha(self.instructor_participation),
                    self.current_commit_sha)
            self.assertResponseMessagesContains(resp, expected_message, loose=True)

    def test_custom_page_types_not_supported(self):
        deadline = datetime(2017, 1, 1, 0, 0, 0, 0)
        with override_settings(
                RELATE_CUSTOM_PAGE_TYPES_REMOVED_DEADLINE=deadline):
            resp = self.post_update_course_content(
                commit_sha=self.commit_sha_deprecated)
            self.assertEqual(resp.status_code, 200)
            expected_message = (
                self.expired_error_message_pattern
                % {"page_type": self.custom_page_type,
                   "date_time": format_datetime_local(deadline)}
            )
            self.assertResponseMessagesContains(resp, expected_message, loose=True)
            self.assertEqual(
                self.get_course_commit_sha(self.instructor_participation),
                self.current_commit_sha)

    def test_custom_page_types_deadline_configured_none(self):
        with override_settings(
                RELATE_CUSTOM_PAGE_TYPES_REMOVED_DEADLINE=None):
            resp = self.post_update_course_content(
                commit_sha=self.commit_sha_deprecated)
            self.assertEqual(resp.status_code, 200)

            if datetime.now() <= self.force_deadline:
                expected_message = (
                    self.deprecate_warning_message_pattern
                    % {"page_type": self.custom_page_type,
                       "date_time": format_datetime_local(self.force_deadline)}
                )
                self.assertEqual(
                    self.get_course_commit_sha(self.instructor_participation),
                    self.commit_sha_deprecated)
            else:
                expected_message = (
                    self.expired_error_message_pattern
                    % {"page_type": self.custom_page_type,
                       "date_time": format_datetime_local(self.force_deadline)}
                )
                self.assertEqual(
                    self.get_course_commit_sha(self.instructor_participation),
                    self.current_commit_sha)
            self.assertResponseMessagesContains(resp, expected_message, loose=True)

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

class Foo(object):
    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.assertFalse(
            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.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):  # noqa
        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):

        # emtpy 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)

    @unittest.skipIf(six.PY2, "PY2 doesn't support subTest")
    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)

    @unittest.skipIf(six.PY2, "PY2 doesn't support subTest")
    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