Example #1
0
    def test_cms_wizards_course_run_submit_form_max_lengths(self):
        """
        Check that the form correctly raises an error when the slug is too long. The path built
        by combining the slug of the page with the slug of its parent page, should not exceed
        255 characters in length.
        """
        # A parent page with a very long slug
        root_page = create_page("p" * 100,
                                "richie/fullwidth.html",
                                "en",
                                reverse_id=Course.ROOT_REVERSE_ID)
        course = CourseFactory(page_title="c" * 100, page_parent=root_page)

        # A course run with a slug at the limit length should work
        form = CourseRunWizardForm(data={
            "title": "t" * 53,
            "course": course.id,
            "languages": ["en"]
        })
        self.assertTrue(form.is_valid())
        form.save()

        # A course run with a slug too long with regards to the parent's one should raise an error
        form = CourseRunWizardForm(data={
            "title": "t" * 54,
            "course": course.id
        })
        self.assertFalse(form.is_valid())
        self.assertEqual(
            form.errors["slug"][0],
            ("This slug is too long. The length of the path built by prepending the slug of "
             "the parent page would be 256 characters long and it should be less than 255"
             ),
        )
Example #2
0
    def test_cms_wizards_course_run_snapshot(self, mock_snapshot):
        """
        Requesting a snapshot from the form to create a new course run should call the
        help function twice: once for validation and once to actually create the snapshot.
        """
        course = CourseFactory()
        user = UserFactory()

        # Submit a valid form with the snapshot flag enabled
        user = UserFactory(is_staff=True, is_superuser=True)
        form = CourseRunWizardForm(
            data={
                "title": "My title",
                "should_snapshot_course": True
            },
            wizard_language="en",
            wizard_user=user,
            wizard_page=course.extended_object,
        )

        self.assertTrue(form.is_valid())
        self.assertEqual(mock_snapshot.call_count, 1)

        form.save()
        self.assertEqual(
            mock_snapshot.call_args_list,
            [
                mock.call(course.extended_object, user, simulate_only=True),
                mock.call(course.extended_object, user),
            ],
        )
Example #3
0
    def test_cms_wizards_course_run_submit_form_slugify_long_title(
            self, mock_snapshot):
        """
        When generating the slug from the title, we should respect the slug's "max_length"
        """
        # A course should pre-exist
        course = CourseFactory()

        # Submit a title at max length
        data = {"title": "t" * 255}
        user = UserFactory(is_staff=True, is_superuser=True)
        form = CourseRunWizardForm(
            data=data,
            wizard_language="en",
            wizard_user=user,
            wizard_page=course.extended_object,
        )

        self.assertTrue(form.is_valid())
        page = form.save()
        # Check that the slug has been truncated
        self.assertEqual(page.get_slug(), "t" * 200)

        # Snapshot was not request and should not have been triggered
        self.assertFalse(mock_snapshot.called)
Example #4
0
    def test_cms_wizards_course_run_submit_form_success(self):
        """
        Submitting a valid CourseRunWizardForm should create a course run and its
        related page.
        """
        # A course should pre-exist
        course = CourseFactory()

        # Submit a valid form
        languages = [random.choice(["en", "fr"])]
        form = CourseRunWizardForm(data={
            "title": "My title",
            "course": course.id,
            "languages": languages
        })
        self.assertTrue(form.is_valid())
        page = form.save()
        course_run = page.courserun

        # The course run and its related page should have been created as draft
        Page.objects.drafts().get(id=page.id)
        CourseRun.objects.get(id=course_run.id, extended_object=page)

        self.assertEqual(page.get_title(), "My title")
        # The slug should have been automatically set
        self.assertEqual(page.get_slug(), "my-title")

        # The course run should be a child of the course page
        self.assertEqual(course_run.extended_object.parent_page,
                         course.extended_object)

        # The languages field should have been set
        self.assertEqual(course_run.languages, languages)
Example #5
0
    def test_cms_wizards_course_run_submit_form_max_lengths(
            self, mock_snapshot):
        """
        Check that the form correctly raises an error when the slug is too long. The path built
        by combining the slug of the page with the slug of its parent page, should not exceed
        255 characters in length.
        """
        # A parent page with a very long slug
        root_page = create_page(
            "p" * 100,
            "richie/single_column.html",
            "en",
            reverse_id=Course.PAGE["reverse_id"],
        )
        course = CourseFactory(page_title="c" * 100, page_parent=root_page)

        # A course run with a slug at the limit length should work
        user = UserFactory(is_staff=True, is_superuser=True)
        form = CourseRunWizardForm(
            data={"title": "t" * 53},
            wizard_language="en",
            wizard_user=user,
            wizard_page=course.extended_object,
        )
        self.assertTrue(form.is_valid())
        form.save()

        # A course run with a slug too long with regards to the parent's one should raise an error
        form = CourseRunWizardForm(
            data={"title": "t" * 54},
            wizard_language="en",
            wizard_user=user,
            wizard_page=course.extended_object,
        )

        self.assertFalse(form.is_valid())
        self.assertEqual(
            form.errors["slug"][0],
            ("This slug is too long. The length of the path built by prepending the slug of "
             "the parent page would be 256 characters long and it should be less than 255"
             ),
        )

        # Snapshot was not request and should not have been triggered
        self.assertFalse(mock_snapshot.called)
Example #6
0
    def test_cms_wizards_course_run_submit_form_slugify_long_title(self):
        """
        When generating the slug from the title, we should respect the slug's "max_length"
        """
        # A course should pre-exist
        course = CourseFactory()

        # Submit a title at max length
        data = {"title": "t" * 255, "course": course.id, "languages": ["en"]}
        form = CourseRunWizardForm(data=data)
        self.assertTrue(form.is_valid())
        page = form.save()
        # Check that the slug has been truncated
        self.assertEqual(page.get_slug(), "t" * 200)
Example #7
0
    def test_cms_wizards_course_run_submit_form_success(self, mock_snapshot):
        """
        A user with the required permissions submitting a valid CourseRunWizardForm should be able
        to create a course run and its related page.
        """
        course = CourseFactory()

        # Create a user with just the required permissions
        user = UserFactory(
            is_staff=True,
            permissions=[
                "courses.add_courserun", "cms.add_page", "cms.change_page"
            ],
        )

        # Submit a valid form
        form = CourseRunWizardForm(
            data={"title": "My title"},
            wizard_language="en",
            wizard_user=user,
            wizard_page=course.extended_object,
        )

        self.assertTrue(form.is_valid())
        page = form.save()
        course_run = page.courserun

        # The course run and its related page should have been created as draft
        Page.objects.drafts().get(id=page.id)
        CourseRun.objects.get(id=course_run.id, extended_object=page)

        self.assertEqual(page.get_title(), "My title")
        # The slug should have been automatically set
        self.assertEqual(page.get_slug(), "my-title")

        # The course run should be a child of the course page
        self.assertEqual(course_run.extended_object.parent_page,
                         course.extended_object)

        # The languages field should have been set
        self.assertEqual(course_run.languages, ["en"])

        # Snapshot was not request and should not have been triggered
        self.assertFalse(mock_snapshot.called)
Example #8
0
    def test_cms_wizards_course_run_language_active(self, *_):
        """
        The language should be set to the active language.
        """
        course = CourseFactory()

        # Submit a valid form
        user = UserFactory(is_staff=True, is_superuser=True)
        form = CourseRunWizardForm(
            data={"title": "My title"},
            wizard_language="en",
            wizard_user=user,
            wizard_page=course.extended_object,
        )

        self.assertTrue(form.is_valid())
        with translation.override("fr"):
            page = form.save()

        # The language field should have been set to the active language
        self.assertEqual(page.courserun.languages, ["fr"])
Example #9
0
    def test_cms_wizards_course_run_language_active_not_in_all_languages(
            self, *_):
        """
        If the ALL_LANGUAGES setting does not include the full active language, it should match
        on the simple language prefix.
        """
        course = CourseFactory(page_title={"fr-ca": "my title"})

        # Submit a valid form
        user = UserFactory(is_staff=True, is_superuser=True)
        form = CourseRunWizardForm(
            data={"title": "My title"},
            wizard_language="en",
            wizard_user=user,
            wizard_page=course.extended_object,
        )

        self.assertTrue(form.is_valid())
        with translation.override("fr-ca"):
            page = form.save()

        # The language field should have been set to the active language
        self.assertEqual(page.courserun.languages, ["fr"])