Skip to content
test_calendar.py 36.6 KiB
Newer Older
Dong Zhuang's avatar
Dong Zhuang committed
from __future__ import division

__copyright__ = "Copyright (C) 2018 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.
"""

import pytz
import json
import datetime

from django.test import TestCase
from django.utils.timezone import now, timedelta
from django.core.exceptions import ValidationError
Dong Zhuang's avatar
Dong Zhuang committed

from relate.utils import as_local_time

from course.models import Event
Dong Zhuang's avatar
Dong Zhuang committed

from tests.base_test_mixins import (
    SingleCourseTestMixin, MockAddMessageMixing, HackRepoMixin)
from tests.utils import mock
from tests import factories
from tests.constants import DATE_TIME_PICKER_TIME_FORMAT


class CreateRecurringEventsTest(SingleCourseTestMixin,
                                MockAddMessageMixing, TestCase):
    """test course.calendar.create_recurring_events"""
    default_event_kind = "lecture"

    def get_create_recurring_events_url(self, course_identifier=None):
        course_identifier = course_identifier or self.get_default_course_identifier()
        return self.get_course_view_url(
            "relate-create_recurring_events", course_identifier)

    def get_create_recurring_events_view(self, course_identifier=None,
                                         force_login_instructor=True):
        course_identifier = course_identifier or self.get_default_course_identifier()
        if not force_login_instructor:
            user = self.get_logged_in_user()
        else:
            user = self.instructor_participation.user

        with self.temporarily_switch_to_user(user):
            return self.c.get(
                self.get_create_recurring_events_url(course_identifier))

    def post_create_recurring_events_view(self, data, course_identifier=None,
                                          force_login_instructor=True):
        course_identifier = course_identifier or self.get_default_course_identifier()
        if not force_login_instructor:
            user = self.get_logged_in_user()
        else:
            user = self.instructor_participation.user

        with self.temporarily_switch_to_user(user):
            return self.c.post(
                self.get_create_recurring_events_url(course_identifier), data)

    def get_post_create_recur_evt_data(
            self, op="submit", starting_ordinal=None, **kwargs):
        data = {
            "kind": self.default_event_kind,
            "time": now().replace(tzinfo=None).strftime(
                DATE_TIME_PICKER_TIME_FORMAT),
            "interval": "weekly",
            "count": 5,
            op: ''
        }

        if starting_ordinal:
            data["starting_ordinal"] = starting_ordinal

        data.update(kwargs)
        return data

    def test_not_authenticated(self):
        with self.temporarily_switch_to_user(None):
            resp = self.get_create_recurring_events_view(
                force_login_instructor=False)
            self.assertEqual(resp.status_code, 302)

            resp = self.post_create_recurring_events_view(
                data={}, force_login_instructor=False)
            self.assertEqual(resp.status_code, 302)

    def test_no_pperm(self):
        with self.temporarily_switch_to_user(self.student_participation.user):
            resp = self.get_create_recurring_events_view(
                force_login_instructor=False)
            self.assertEqual(resp.status_code, 403)

            resp = self.post_create_recurring_events_view(
                data={}, force_login_instructor=False)
            self.assertEqual(resp.status_code, 403)

    def test_get_success(self):
        resp = self.get_create_recurring_events_view()
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(Event.objects.count(), 0)

    def test_post_form_not_valid(self):
        with mock.patch(
                "course.calendar.RecurringEventForm.is_valid"
        ) as mock_form_valid:
            mock_form_valid.return_value = False
            resp = self.post_create_recurring_events_view(
                data=self.get_post_create_recur_evt_data())
            self.assertEqual(resp.status_code, 200)

    def test_post_success_starting_ordinal_specified(self):
        resp = self.post_create_recurring_events_view(
            data=self.get_post_create_recur_evt_data(starting_ordinal=4))
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(Event.objects.count(), 5)
        self.assertListEqual(
            list(Event.objects.values_list("ordinal", flat=True)),
            [4, 5, 6, 7, 8])

        t = None
        for evt in Event.objects.all():
            if t is None:
                t = evt.time
                continue
            else:
                self.assertTrue(
                        evt.time - t
                        >=
                        (datetime.timedelta(weeks=1) - datetime.timedelta(hours=1)))
                self.assertTrue(
                        evt.time - t
                        <=
                        (datetime.timedelta(weeks=1) + datetime.timedelta(hours=1)))
Dong Zhuang's avatar
Dong Zhuang committed
                t = evt.time

    def test_post_success_starting_ordinal_not_specified(self):
        resp = self.post_create_recurring_events_view(
            data=self.get_post_create_recur_evt_data())
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(Event.objects.count(), 5)
        self.assertListEqual(
            list(Event.objects.values_list("ordinal", flat=True)),
            [1, 2, 3, 4, 5])

    def test_post_success_starting_ordinal_not_specified_skip_exist(self):
        factories.EventFactory(
            course=self.course, kind=self.default_event_kind, ordinal=4)
        resp = self.post_create_recurring_events_view(
            data=self.get_post_create_recur_evt_data())
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(Event.objects.count(), 6)
        self.assertListEqual(
            list(Event.objects.values_list("ordinal", flat=True)),
            [4, 11, 12, 13, 14, 15])

Dong Zhuang's avatar
Dong Zhuang committed
    def test_post_event_already_exists_starting_ordinal_specified(self):
        factories.EventFactory(
            course=self.course, kind=self.default_event_kind, ordinal=7)
        resp = self.post_create_recurring_events_view(
            data=self.get_post_create_recur_evt_data(starting_ordinal=4))
        self.assertEqual(resp.status_code, 200)

        # not created
        self.assertEqual(Event.objects.count(), 1)

        self.assertAddMessageCallCount(1)
        self.assertAddMessageCalledWith(
            "EventAlreadyExists: 'lecture 7' already exists. "
            "No events created.")

    def test_post_event_already_exists_starting_ordinal_not_specified(self):
        factories.EventFactory(
            course=self.course, kind=self.default_event_kind, ordinal=4)
        resp = self.post_create_recurring_events_view(
            data=self.get_post_create_recur_evt_data())
        self.assertEqual(resp.status_code, 200)

        # created and ordinal is not None
        self.assertEqual(
            Event.objects.filter(
                kind=self.default_event_kind, ordinal__isnull=False).count(), 6)

        self.assertAddMessageCallCount(1)
        self.assertAddMessageCalledWith("Events created.")

    def test_event_save_unknown_error(self):
Dong Zhuang's avatar
Dong Zhuang committed
        error_msg = "my unknown error"
        with mock.patch("course.models.Event.save") as mock_event_save:
            mock_event_save.side_effect = RuntimeError(error_msg)
Dong Zhuang's avatar
Dong Zhuang committed
            resp = self.post_create_recurring_events_view(
                data=self.get_post_create_recur_evt_data(starting_ordinal=4))
            self.assertEqual(resp.status_code, 200)

        # not created
        self.assertEqual(Event.objects.count(), 0)
        self.assertAddMessageCallCount(1)
        self.assertAddMessageCalledWith(
            "RuntimeError: %s. No events created." % error_msg)

    def test_event_save_other_validation_error(self):
        error_msg = "my unknown validation error for end_time"
        with mock.patch("course.models.Event.save") as mock_event_save:
            # mock error raised by event.end_time
            mock_event_save.side_effect = (
                ValidationError({"end_time": error_msg}))
            resp = self.post_create_recurring_events_view(
                data=self.get_post_create_recur_evt_data(starting_ordinal=4))
            self.assertEqual(resp.status_code, 200)

        # form error was raised instead of add_message
        self.assertFormErrorLoose(resp, error_msg)
        self.assertAddMessageCallCount(0)

        # not created
        self.assertEqual(Event.objects.count(), 0)

Dong Zhuang's avatar
Dong Zhuang committed
    def test_duration_in_minutes(self):
        resp = self.post_create_recurring_events_view(
            data=self.get_post_create_recur_evt_data(
                starting_ordinal=4, duration_in_minutes=20))
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(Event.objects.count(), 5)
        self.assertListEqual(
            list(Event.objects.values_list("ordinal", flat=True)),
            [4, 5, 6, 7, 8])

        t = None
        for evt in Event.objects.all():
            self.assertEqual(evt.end_time - evt.time, timedelta(minutes=20))
            if t is None:
                t = evt.time
                continue
            else:
                # One hour slack to avoid failure due to daylight savings.
                self.assertTrue(evt.time - t >= (
                    datetime.timedelta(weeks=1) - datetime.timedelta(hours=1)))
                self.assertTrue(evt.time - t <= (
                    datetime.timedelta(weeks=1) + datetime.timedelta(hours=1)))
Dong Zhuang's avatar
Dong Zhuang committed
                t = evt.time

    def test_interval_biweekly(self):
        resp = self.post_create_recurring_events_view(
            data=self.get_post_create_recur_evt_data(
                starting_ordinal=4, interval="biweekly"))
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(Event.objects.count(), 5)
        self.assertListEqual(
            list(Event.objects.values_list("ordinal", flat=True)),
            [4, 5, 6, 7, 8])

        t = None
        for evt in Event.objects.all():
            if t is None:
                t = evt.time
                continue
            else:
                # One hour slack to avoid failure due to daylight savings.
                self.assertTrue(evt.time - t >= (
                    datetime.timedelta(weeks=2) - datetime.timedelta(hours=1)))
                self.assertTrue(evt.time - t <= (
                    datetime.timedelta(weeks=2) + datetime.timedelta(hours=1)))
Dong Zhuang's avatar
Dong Zhuang committed
                t = evt.time


class RecurringEventFormTest(SingleCourseTestMixin, TestCase):
    """test course.calendar.RecurringEventForm"""
    def test_valid(self):
        form_data = {
            "kind": "some_kind",
            "time": now().replace(tzinfo=None).strftime(
                DATE_TIME_PICKER_TIME_FORMAT),
            "interval": "weekly",
            "count": 2
        }
        form = calendar.RecurringEventForm(
            self.course.identifier,
            data=form_data)
        self.assertTrue(form.is_valid())

    def test_negative_duration_in_minutes(self):
        form_data = {
            "kind": "some_kind",
            "time": now().replace(tzinfo=None).strftime(
                DATE_TIME_PICKER_TIME_FORMAT),
            "duration_in_minutes": -1,
            "interval": "weekly",
            "count": 5
        }
        form = calendar.RecurringEventForm(self.course.identifier, data=form_data)
        self.assertFalse(form.is_valid())
        self.assertIn(
            "Ensure this value is greater than or equal to 0.",
            form.errors["duration_in_minutes"])

    def test_negative_event_count(self):
        form_data = {
            "kind": "some_kind",
            "time": now().replace(tzinfo=None).strftime(
                DATE_TIME_PICKER_TIME_FORMAT),
            "interval": "weekly",
            "count": -1
        }
        form = calendar.RecurringEventForm(self.course.identifier, data=form_data)
        self.assertFalse(form.is_valid())
        self.assertIn(
            "Ensure this value is greater than or equal to 0.",
            form.errors["count"])

    def test_available_kind_choices(self):
        factories.EventFactory(
            course=self.course, kind="some_kind1", ordinal=None)
        factories.EventFactory(
            course=self.course, kind="some_kind2", ordinal=1)
        another_course = factories.CourseFactory(identifier="another-course")
        factories.EventFactory(
            course=another_course, kind="some_kind3", ordinal=1)
        factories.EventFactory(
            course=another_course, kind="some_kind4", ordinal=1)
        form = calendar.RecurringEventForm(self.course.identifier)
        self.assertIn(
            '<option value="some_kind1">some_kind1</option>', form.as_p())
        self.assertIn(
            '<option value="some_kind2">some_kind2</option>', form.as_p())

        self.assertNotIn(
            '<option value="some_kind3">some_kind3</option>', form.as_p())
        self.assertNotIn(
            '<option value="some_kind4">some_kind4</option>', form.as_p())


Dong Zhuang's avatar
Dong Zhuang committed
class RenumberEventsTest(SingleCourseTestMixin,
                         MockAddMessageMixing, TestCase):
    """test course.calendar.create_recurring_events"""
    default_event_kind = "lecture"

    def get_renumber_events_events_url(self, course_identifier=None):
        course_identifier = course_identifier or self.get_default_course_identifier()
        return self.get_course_view_url(
            "relate-renumber_events", course_identifier)

    def get_renumber_events_view(self, course_identifier=None,
                                 force_login_instructor=True):
        course_identifier = course_identifier or self.get_default_course_identifier()
        if not force_login_instructor:
            user = self.get_logged_in_user()
        else:
            user = self.instructor_participation.user

        with self.temporarily_switch_to_user(user):
            return self.c.get(
                self.get_renumber_events_events_url(course_identifier))

    def post_renumber_events_view(self, data, course_identifier=None,
                                  force_login_instructor=True):
        course_identifier = course_identifier or self.get_default_course_identifier()
        if not force_login_instructor:
            user = self.get_logged_in_user()
        else:
            user = self.instructor_participation.user

        with self.temporarily_switch_to_user(user):
            return self.c.post(
                self.get_renumber_events_events_url(course_identifier), data)

    def get_post_renumber_evt_data(
            self, starting_ordinal, kind=None, op="submit", **kwargs):

        data = {
            "kind": kind or self.default_event_kind,
            "starting_ordinal": starting_ordinal,
            "preserve_ordinal_order": False}
Dong Zhuang's avatar
Dong Zhuang committed

        data.update(kwargs)
        return data

    @classmethod
    def setUpTestData(cls):  # noqa
        super(RenumberEventsTest, cls).setUpTestData()
        times = []
        now_time = now()
        for i in range(5):
            times.append(now_time)
            now_time += timedelta(weeks=1)
            factories.EventFactory(
                kind=cls.default_event_kind, ordinal=i * 2 + 1, time=now_time)

        cls.evt1, cls.evt2, cls.evt3, cls.evt4, cls.evt5 = Event.objects.all()

        for i in range(2):
            factories.EventFactory(
                kind="another_kind", ordinal=i * 3 + 1, time=now_time)
            now_time += timedelta(weeks=1)

        cls.evt_another_kind1, cls.evt_another_kind2 = (
            Event.objects.filter(kind="another_kind"))
        cls.evt_another_kind1_ordinal = cls.evt_another_kind1.ordinal
        cls.evt_another_kind2_ordinal = cls.evt_another_kind2.ordinal

    def setUp(self):
        super(RenumberEventsTest, self).setUp()
        self.evt1.refresh_from_db()
        self.evt2.refresh_from_db()
        self.evt3.refresh_from_db()
        self.evt4.refresh_from_db()
        self.evt5.refresh_from_db()

    def test_not_authenticated(self):
        with self.temporarily_switch_to_user(None):
            resp = self.get_renumber_events_view(
                force_login_instructor=False)
            self.assertEqual(resp.status_code, 302)

            resp = self.post_renumber_events_view(data={},
                                                  force_login_instructor=False)
            self.assertEqual(resp.status_code, 302)

    def test_no_pperm(self):
        with self.temporarily_switch_to_user(self.student_participation.user):
            resp = self.get_renumber_events_view(
                force_login_instructor=False)
            self.assertEqual(resp.status_code, 403)

            resp = self.post_renumber_events_view(data={},
                                                  force_login_instructor=False)
            self.assertEqual(resp.status_code, 403)

    def test_get_success(self):
        resp = self.get_renumber_events_view()
        self.assertEqual(resp.status_code, 200)

    def test_post_form_not_valid(self):
        with mock.patch(
                "course.calendar.RenumberEventsForm.is_valid"
        ) as mock_form_valid:
            mock_form_valid.return_value = False

            resp = self.post_renumber_events_view(
                data=self.get_post_renumber_evt_data(starting_ordinal=3))
            self.assertEqual(resp.status_code, 200)

        all_default_evts = Event.objects.filter(kind=self.default_event_kind)
        self.assertEqual(all_default_evts.count(), 5)
        self.assertListEqual(
            list(all_default_evts.values_list("ordinal", flat=True)),
            [1, 3, 5, 7, 9])

        t = None
        for evt in all_default_evts:
            if t is None:
                t = evt.time
                continue
            else:
                self.assertEqual(evt.time - t, datetime.timedelta(weeks=1))
                t = evt.time

        # other events also not affected
        self.evt_another_kind1.refresh_from_db()
        self.evt_another_kind2.refresh_from_db()
        self.assertEqual(
            self.evt_another_kind1.ordinal, self.evt_another_kind1_ordinal)
        self.assertEqual(
            self.evt_another_kind2.ordinal, self.evt_another_kind2_ordinal)

    def test_post_success(self):
        resp = self.post_renumber_events_view(
            data=self.get_post_renumber_evt_data(starting_ordinal=3))
        self.assertEqual(resp.status_code, 200)
        all_pks = list(Event.objects.values_list("pk", flat=True))

        # originally 1, 3, 5, 7, 9, now 3, 4, 5, 6, 7

        all_default_evts = Event.objects.filter(kind=self.default_event_kind)
        self.assertEqual(all_default_evts.count(), 5)
        self.assertListEqual(
            list(all_default_evts.values_list("ordinal", flat=True)),
            [3, 4, 5, 6, 7])

        t = None
        for evt in all_default_evts:
            if t is None:
                t = evt.time
                continue
            else:
                self.assertEqual(evt.time - t, datetime.timedelta(weeks=1))
                t = evt.time

        # other events not affected
        self.evt_another_kind1.refresh_from_db()
        self.evt_another_kind2.refresh_from_db()
        self.assertEqual(
            self.evt_another_kind1.ordinal, self.evt_another_kind1_ordinal)
        self.assertEqual(
            self.evt_another_kind2.ordinal, self.evt_another_kind2_ordinal)

        # no new objects created
        self.assertListEqual(
            list(Event.objects.values_list("pk", flat=True)), all_pks)

        self.assertAddMessageCallCount(1)
        self.assertAddMessageCalledWith("Events renumbered.")

    def test_renumber_non_existing_events(self):
        resp = self.post_renumber_events_view(
            data=self.get_post_renumber_evt_data(
                kind="foo_kind", starting_ordinal=3))
        self.assertEqual(resp.status_code, 200)
        expected_errors = ["Select a valid choice. foo_kind is "
                           "not one of the available choices."]
        self.assertFormError(resp, "form", "kind", expected_errors)

    def test_renumber_preserve_ordinal_order(self):

        # make evt1 the latest, evt5 the earliest
        time1, time5 = self.evt1.time, self.evt5.time
        self.evt1.time, self.evt5.time = time5, time1
        self.evt1.save()
        self.evt5.save()
Dong Zhuang's avatar
Dong Zhuang committed

        all_default_evts = Event.objects.filter(kind=self.default_event_kind)
        self.assertEqual(all_default_evts.count(), 5)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertListEqual(
            list(
                all_default_evts.order_by("time").values_list("ordinal", flat=True)),
            [9, 3, 5, 7, 1])
Dong Zhuang's avatar
Dong Zhuang committed

        resp = self.post_renumber_events_view(
            data=self.get_post_renumber_evt_data(
                starting_ordinal=3, preserve_ordinal_order=True))
        self.assertEqual(resp.status_code, 200)

        # originally (ordered by time) 9, 3, 5, 7, 1, now 7, 4, 5, 6, 3
        all_default_evts = Event.objects.filter(kind=self.default_event_kind)
        self.assertEqual(all_default_evts.count(), 5)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertListEqual(
            list(
                all_default_evts.order_by("time").values_list("ordinal", flat=True)),
            [7, 4, 5, 6, 3])
Dong Zhuang's avatar
Dong Zhuang committed

        self.assertAddMessageCallCount(1)
        self.assertAddMessageCalledWith("Events renumbered.")

    def test_no_ordinal_event_not_renumbered(self):

        no_ordinal_event = factories.EventFactory(
            kind=self.default_event_kind, ordinal=None)

        resp = self.post_renumber_events_view(
            data=self.get_post_renumber_evt_data(
                starting_ordinal=3, preserve_ordinal_order=True))
        self.assertEqual(resp.status_code, 200)

        all_evts_with_ordinal = Event.objects.filter(
            kind=self.default_event_kind, ordinal__isnull=False)
        self.assertEqual(all_evts_with_ordinal.count(), 5)
        self.assertListEqual(
            list(all_evts_with_ordinal.values_list("ordinal", flat=True)),
            [3, 4, 5, 6, 7])

        no_ordinal_event.refresh_from_db()
        self.assertEqual(no_ordinal_event.kind, self.default_event_kind)
        self.assertIsNone(no_ordinal_event.ordinal)
Dong Zhuang's avatar
Dong Zhuang committed

        self.assertAddMessageCallCount(1)
        self.assertAddMessageCalledWith("Events renumbered.")
Dong Zhuang's avatar
Dong Zhuang committed
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


class ViewCalendarTest(SingleCourseTestMixin, HackRepoMixin, TestCase):
    """test course.calendar.view_calendar"""

    default_faked_now = datetime.datetime(2019, 1, 1, tzinfo=pytz.UTC)
    default_event_time = default_faked_now - timedelta(hours=12)
    default_event_kind = "lecture"

    def setUp(self):
        super(ViewCalendarTest, self).setUp()
        fake_get_now_or_fake_time = mock.patch(
            "course.views.get_now_or_fake_time")
        self.mock_get_now_or_fake_time = fake_get_now_or_fake_time.start()
        self.mock_get_now_or_fake_time.return_value = now()
        self.addCleanup(fake_get_now_or_fake_time.stop)

        self.addCleanup(factories.EventFactory.reset_sequence)

    def get_course_calender_view(self, course_identifier=None):
        course_identifier = course_identifier or self.get_default_course_identifier()
        return self.c.get(self.get_course_calender_url(course_identifier))

    def switch_to_fake_commit_sha(self):
        self.course.active_git_commit_sha = "my_fake_commit_sha_for_events"
        self.course.events_file = "events.yml"
        self.course.save()

    def test_no_pperm(self):
        with mock.patch(
                "course.utils.CoursePageContext.has_permission"
        ) as mock_has_pperm:
            mock_has_pperm.return_value = False
            resp = self.get_course_calender_view()
            self.assertEqual(resp.status_code, 403)

    def test_neither_events_nor_event_file(self):
        self.mock_get_now_or_fake_time.return_value = self.default_faked_now
        resp = self.get_course_calender_view()
        self.assertEqual(resp.status_code, 200)
        self.assertResponseContextEqual(resp, "events_json", '[]')
        self.assertResponseContextEqual(resp, "event_info_list", [])
        self.assertResponseContextEqual(
            resp, "default_date", self.default_faked_now.date().isoformat())

    def test_no_event_file(self):
        factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            time=self.default_event_time)
        factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            time=self.default_event_time + timedelta(hours=1))

        resp = self.get_course_calender_view()
        self.assertEqual(resp.status_code, 200)

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 2)
        self.assertDictEqual(
            events_json[0],
            {'id': 1, 'start': self.default_event_time.isoformat(),
             'allDay': False,
             'title': 'lecture 0'})
        self.assertDictEqual(
            events_json[1],
            {'id': 2,
             'start': (self.default_event_time + timedelta(hours=1)).isoformat(),
             'allDay': False,
             'title': 'lecture 1'})

        self.assertResponseContextEqual(resp, "event_info_list", [])

    def test_hidden_event_not_shown(self):
        factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            time=self.default_event_time)
        factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            shown_in_calendar=False,
            time=self.default_event_time + timedelta(hours=1))

        resp = self.get_course_calender_view()
        self.assertEqual(resp.status_code, 200)

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 1)
        self.assertDictEqual(
            events_json[0],
            {'id': 1, 'start': self.default_event_time.isoformat(),
             'allDay': False,
             'title': 'lecture 0'})
        self.assertResponseContextEqual(resp, "event_info_list", [])

    def test_event_has_end_time(self):
        factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            time=self.default_event_time,
            end_time=self.default_event_time + timedelta(hours=1))
        resp = self.get_course_calender_view()
        self.assertEqual(resp.status_code, 200)

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 1)
        event_json = events_json[0]
        self.assertDictEqual(
            event_json,
            {'id': 1, 'start': self.default_event_time.isoformat(),
             'allDay': False,
             'title': 'lecture 0',
             'end': (self.default_event_time + timedelta(hours=1)).isoformat(),
             })

        self.assertResponseContextEqual(resp, "event_info_list", [])

    def test_event_course_finished(self):
        self.mock_get_now_or_fake_time.return_value = self.default_faked_now
        self.course.end_date = (self.default_faked_now - timedelta(weeks=1)).date()
        self.course.save()

        resp = self.get_course_calender_view()
        self.assertEqual(resp.status_code, 200)

        self.assertResponseContextEqual(resp, "events_json", '[]')
        self.assertResponseContextEqual(resp, "event_info_list", [])
        self.assertResponseContextEqual(
            resp, "default_date", self.course.end_date.isoformat())

    def test_event_course_not_finished(self):
        self.mock_get_now_or_fake_time.return_value = self.default_faked_now
        self.course.end_date = (self.default_faked_now + timedelta(weeks=1)).date()
        self.course.save()

        resp = self.get_course_calender_view()
        self.assertEqual(resp.status_code, 200)

        self.assertResponseContextEqual(resp, "events_json", '[]')
        self.assertResponseContextEqual(resp, "event_info_list", [])
        self.assertResponseContextEqual(
            resp, "default_date", self.default_faked_now.date().isoformat())

    def test_events_file_no_events(self):
        # make sure it works
        self.switch_to_fake_commit_sha()

        resp = self.get_course_calender_view()

        self.assertResponseContextEqual(resp, "events_json", '[]')
        self.assertResponseContextEqual(resp, "event_info_list", [])

    def test_events_file_with_events_test1(self):
        self.switch_to_fake_commit_sha()

        # lecture 1
        lecture1_start_time = self.default_event_time - timedelta(weeks=1)
        factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            time=lecture1_start_time, ordinal=1)

        # lecture 2
        factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            time=self.default_event_time, ordinal=2)

        resp = self.get_course_calender_view()

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 2)
        self.assertDictEqual(
            events_json[0],
            {'id': 2, 'start': self.default_event_time.isoformat(),
             'allDay': False,
             'title': 'Lecture 2'})

        self.assertDictEqual(
            events_json[1],
            {'id': 1,
             'color': "red",
             'start': lecture1_start_time.isoformat(),
             'allDay': False,
             'title': 'Alternative title for lecture 1',
             'url': '#event-1'
             })

        event_info_list = resp.context["event_info_list"]

        # lecture 2 doesn't create an EventInfo object
        self.assertEqual(len(event_info_list), 1)

        # check the attributes of EventInfo of lecture 1
        evt_info_dict = event_info_list[0].__dict__
        evt_description = evt_info_dict.pop("description")

        self.assertDictEqual(
            evt_info_dict,
            {"id": 1, "human_title": "Alternative title for lecture 1",
             "start_time": lecture1_start_time,
             "end_time": None})

        # make sure markup_to_html is called
        self.assertIn(
            'href="/course/test-course/flow/prequiz-linear-algebra/start/',
            evt_description)

    def test_events_file_with_events_test2(self):
        self.switch_to_fake_commit_sha()

        self.mock_get_now_or_fake_time.return_value = (
                self.default_event_time + timedelta(minutes=5))

        # lecture 2
        factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            time=self.default_event_time, ordinal=2)

        # lecture 3
        lecture3_start_time = self.default_event_time + timedelta(weeks=1)
        factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            time=lecture3_start_time, ordinal=3)

        # test event
        test_start_time = self.default_event_time + timedelta(minutes=1)
        factories.EventFactory(
            kind="test", course=self.course, all_day=True,
            time=test_start_time,
            ordinal=None)

        resp = self.get_course_calender_view()

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 3)

        self.assertDictEqual(
            events_json[0],
            {'id': 2, 'start': (lecture3_start_time).isoformat(),
             'allDay': False,
             'title': 'Lecture 3'})

        self.assertDictEqual(
            events_json[1],
            {'id': 1, 'start': self.default_event_time.isoformat(),
             'allDay': False,
             'title': 'Lecture 2',
             'url': '#event-1'})

        self.assertDictEqual(
            events_json[2],
            {'id': 3, 'start': test_start_time.isoformat(),
             'allDay': True,
             'title': 'test'})

        event_info_list = resp.context["event_info_list"]

        # only lecture 2 create an EventInfo object
        self.assertEqual(len(event_info_list), 1)

        # check the attributes of EventInfo of lecture 1
        evt_info_dict = event_info_list[0].__dict__
        evt_description = evt_info_dict.pop("description")

        self.assertDictEqual(
            evt_info_dict,
            {"id": 1, "human_title": "Lecture 2",
             "start_time": self.default_event_time,
             "end_time": None})

        self.assertIn(
            'Can you see this?',
            evt_description)

        # lecture 2's description exceeded show_description_until
        self.mock_get_now_or_fake_time.return_value = (
                lecture3_start_time + timedelta(minutes=5))

        # no EventInfo object
        resp = self.get_course_calender_view()
        self.assertResponseContextEqual(resp, "event_info_list", [])

    def test_events_file_with_events_test3(self):
        self.switch_to_fake_commit_sha()

        exam_end_time = self.default_event_time + timedelta(hours=2)
        factories.EventFactory(
            kind="exam", course=self.course,
            ordinal=None,
            time=self.default_event_time,
            end_time=exam_end_time)

        resp = self.get_course_calender_view()

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 1)

        self.assertDictEqual(
            events_json[0],
            {'id': 1,
             'start': self.default_event_time.isoformat(),
             'allDay': False,
             'title': 'Exam',
             'color': 'red',
             'end': exam_end_time.isoformat()})

        self.assertResponseContextEqual(resp, "event_info_list", [])

    def test_all_day_event(self):
        self.switch_to_fake_commit_sha()

        # lecture 2, no end_time
        lecture2_start_time = datetime.datetime(2019, 1, 1, tzinfo=pytz.UTC)

        self.mock_get_now_or_fake_time.return_value = (
                lecture2_start_time + timedelta(minutes=5))

        lecture2_evt = factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            all_day=True,
            time=lecture2_start_time, ordinal=2)

        # lecture 3
        lecture3_start_time = lecture2_start_time + timedelta(weeks=1)
        factories.EventFactory(
            kind=self.default_event_kind, course=self.course,
            time=lecture3_start_time, ordinal=3)

        resp = self.get_course_calender_view()

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 2)

        self.assertDictEqual(
            events_json[1],
            {'id': 1, 'start': lecture2_start_time.isoformat(),
             'allDay': True,
             'title': 'Lecture 2',
             'url': '#event-1'})

        # now we add end_time of lecture 2 evt to a time which is not midnight
        lecture2_end_time = lecture2_start_time + timedelta(hours=18)
        lecture2_evt.end_time = lecture2_end_time
        lecture2_evt.save()

        resp = self.get_course_calender_view()

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 2)

        self.assertDictEqual(
            events_json[1],
            {'id': 1, 'start': lecture2_start_time.isoformat(),
             'allDay': True,
             'title': 'Lecture 2',
             'url': '#event-1',
             'end': lecture2_end_time.isoformat()
             })

        # now we update end_time of lecture 2 evt to midnight
        while True:
            local_t = as_local_time(lecture2_end_time)
            end_midnight = datetime.time(tzinfo=local_t.tzinfo)
            if local_t.time() == end_midnight:
                lecture2_evt.end_time = lecture2_end_time
                lecture2_evt.save()
                break

            lecture2_end_time += timedelta(hours=1)

        resp = self.get_course_calender_view()

        events_json = json.loads(resp.context["events_json"])
        self.assertEqual(len(events_json), 2)

        self.assertDictEqual(
            events_json[1],
            {'id': 1, 'start': lecture2_start_time.isoformat(),
             'allDay': True,
             'title': 'Lecture 2',
             'url': '#event-1',
             'end': lecture2_end_time.isoformat()
             })

# vim: fdm=marker