示例#1
0
    def test_cms_wizards_category_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
        create_page(
            "y" * 200,
            "richie/fullwidth.html",
            "en",
            reverse_id=Category.ROOT_REVERSE_ID,
        )

        # A category with a slug at the limit length should work
        form = CategoryWizardForm(data={"title": "t" * 255, "slug": "s" * 54})
        self.assertTrue(form.is_valid())
        form.save()

        # A category with a slug too long with regards to the parent's one should raise an error
        form = CategoryWizardForm(data={"title": "t" * 255, "slug": "s" * 55})
        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"
            ),
        )
    def test_cms_wizards_category_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
        page = create_page(
            "y" * 200,
            "richie/single_column.html",
            "en",
            reverse_id=Category.PAGE["reverse_id"],
        )

        # A category with a slug at the limit length should work
        user = UserFactory(is_staff=True, is_superuser=True)
        form = CategoryWizardForm(
            data={
                "title": "t" * 255,
                "slug": "s" * 54
            },
            wizard_language="en",
            wizard_user=user,
            wizard_page=page,
        )
        self.assertTrue(form.is_valid())
        form.save()

        # A category with a slug too long with regards to the parent's one should raise an error
        form = CategoryWizardForm(
            data={
                "title": "t" * 255,
                "slug": "s" * 55
            },
            wizard_language="en",
            wizard_user=user,
            wizard_page=page,
        )
        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"
             ),
        )
示例#3
0
    def test_cms_wizards_category_submit_form_from_any_page(self):
        """
        A user with the required permissions submitting a valid CategoryWizardForm from any page
        should be able to create a category at the top of the category tree and its related page.
        """
        # We want to create the category from an ordinary page
        any_page = create_page("Any page", "richie/single_column.html", "en")

        # A parent page should pre-exist
        parent_page = create_page(
            "Categories",
            "richie/single_column.html",
            "en",
            reverse_id=Category.PAGE["reverse_id"],
        )
        # Create a user with just the required permissions
        user = UserFactory(
            is_staff=True,
            permissions=[
                "courses.add_category", "cms.add_page", "cms.change_page"
            ],
        )

        # We can submit a form with just the title set
        form = CategoryWizardForm(
            data={"title": "My title"},
            wizard_language="en",
            wizard_user=user,
            wizard_page=any_page,
        )
        self.assertTrue(form.is_valid())
        page = form.save()

        # Related page should have been created as draft
        Page.objects.drafts().get(id=page.id)
        Category.objects.get(id=page.category.id, extended_object=page)
        self.assertEqual(page.get_parent_page(), parent_page)

        self.assertEqual(page.get_title(), "My title")
        # The slug should have been automatically set
        self.assertEqual(page.get_slug(), "my-title")
        # The page should be in navigation to appear in the breadcrumb
        self.assertTrue(page.in_navigation)
    def test_cms_wizards_category_submit_form_from_category_page(self):
        """
        Submitting a valid CategoryWizardForm from a category should create a sub category of this
        category and its related page.
        """
        # A parent page should pre-exist
        create_page(
            "Categories",
            "richie/single_column.html",
            "en",
            reverse_id=Category.PAGE["reverse_id"],
        )
        # Create a category when visiting an existing category
        parent_category = CategoryFactory()

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

        # We can submit a form with just the title set
        form = CategoryWizardForm(
            data={"title": "My title"},
            wizard_language="en",
            wizard_user=user,
            wizard_page=parent_category.extended_object,
        )
        self.assertTrue(form.is_valid())
        page = form.save()

        # Related page should have been created as draft
        Page.objects.drafts().get(id=page.id)
        Category.objects.get(id=page.category.id, extended_object=page)
        self.assertEqual(page.get_parent_page(),
                         parent_category.extended_object)

        self.assertEqual(page.get_title(), "My title")
        # The slug should have been automatically set
        self.assertEqual(page.get_slug(), "my-title")
示例#5
0
    def test_cms_wizards_category_submit_form_slugify_long_title(self):
        """
        When generating the slug from the title, we should respect the slug's "max_length"
        """
        # A parent page should pre-exist
        create_page(
            "Categories",
            "richie/fullwidth.html",
            "en",
            reverse_id=Category.ROOT_REVERSE_ID,
        )

        # Submit a title at max length
        data = {"title": "t" * 255}

        form = CategoryWizardForm(data=data)
        self.assertTrue(form.is_valid())
        page = form.save()
        # Check that the slug has been truncated
        self.assertEqual(page.get_slug(), "t" * 200)
    def test_cms_wizards_category_submit_form_slugify_long_title(self):
        """
        When generating the slug from the title, we should respect the slug's "max_length"
        """
        # A parent page should pre-exist
        page = create_page(
            "Categories",
            "richie/single_column.html",
            "en",
            reverse_id=Category.PAGE["reverse_id"],
        )

        # Submit a title at max length
        data = {"title": "t" * 255}
        user = UserFactory(is_staff=True, is_superuser=True)
        form = CategoryWizardForm(
            data=data, wizard_language="en", wizard_user=user, wizard_page=page
        )
        self.assertTrue(form.is_valid())
        page = form.save()

        # Check that the slug has been truncated
        self.assertEqual(page.get_slug(), "t" * 200)
示例#7
0
    def test_cms_wizards_category_submit_form(self):
        """
        Submitting a valid CategoryWizardForm should create a Category page extension and its
        related page.
        """
        # A parent page should pre-exist
        create_page(
            "Categories",
            "richie/fullwidth.html",
            "en",
            reverse_id=Category.ROOT_REVERSE_ID,
        )
        # We can submit a form with just the title set
        form = CategoryWizardForm(data={"title": "My title"})
        self.assertTrue(form.is_valid())
        page = form.save()

        # Related page should have been created as draft
        Page.objects.drafts().get(id=page.id)
        Category.objects.get(id=page.category.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")