def test_cms_wizards_organization_submit_form_slugify_long_title(self):
        """
        When generating the slug from the title, we should respect the slug's "max_length"
        """
        # A parent page to list organizations should pre-exist
        create_page(
            "Organizations",
            "richie/single_column.html",
            "en",
            reverse_id="organizations",
        )

        # Submit a title at max length
        data = {"title": "t" * 255}
        user = UserFactory(is_staff=True, is_superuser=True)
        form = OrganizationWizardForm(data=data,
                                      wizard_language="en",
                                      wizard_user=user)
        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_organization_submit_form_slug_too_long(self):
        """
        Trying to set a slug that is too long should make the form invalid
        """
        # A parent page to list organizations should pre-exist
        create_page(
            "Organizations",
            "richie/single_column.html",
            "en",
            reverse_id="organizations",
        )
        # Submit a slug that is too long and a title that is ok
        invalid_data = {"title": "t" * 255, "slug": "s" * 201}
        user = UserFactory(is_staff=True, is_superuser=True)
        form = OrganizationWizardForm(
            data=invalid_data, wizard_language="en", wizard_user=user
        )

        self.assertFalse(form.is_valid())
        # Check that the slug being too long is a cause for the invalid form
        self.assertEqual(
            form.errors["slug"],
            ["Ensure this value has at most 200 characters (it has 201)."],
        )
    def test_cms_wizards_organization_submit_form(self, *_):
        """
        A user with the required permissions submitting a valid OrganizationWizardForm should be
        able to create a page and its related extension. Admin permissions should be automatically
        assigned to a new group.
        """
        any_page = create_page("page", "richie/single_column.html", "en")

        # A parent page to list organizations should pre-exist
        create_page(
            "Organizations",
            "richie/single_column.html",
            "en",
            reverse_id="organizations",
            published=True,
        )

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

        # We can submit a form with just the title set
        form = OrganizationWizardForm(
            data={"title": "My title"},
            wizard_language="en",
            wizard_user=user,
            wizard_page=any_page,
        )

        self.assertTrue(form.is_valid())

        role_dict = {
            "django_permissions": ["cms.change_page"],
            "organization_page_permissions": {
                "can_change": random.choice([True, False]),
                "can_add": random.choice([True, False]),
                "can_delete": random.choice([True, False]),
                "can_change_advanced_settings": random.choice([True, False]),
                "can_publish": random.choice([True, False]),
                "can_change_permissions": random.choice([True, False]),
                "can_move_page": random.choice([True, False]),
                "can_view": False,  # can_view = True would make it a view restriction...
                "grant_on": random.randint(1, 5),
            },
            "organization_folder_permissions": {
                "can_read": random.choice([True, False]),
                "can_edit": random.choice([True, False]),
                "can_add_children": random.choice([True, False]),
                "type": random.randint(0, 2),
            },
        }
        with mock.patch.dict(defaults.ORGANIZATION_ADMIN_ROLE, role_dict):
            page = form.save()

        organization = page.organization

        # The page and its related extension have been created as draft
        self.assertEqual(Page.objects.count(), 4)
        self.assertEqual(Page.objects.drafts().count(), 3)
        self.assertEqual(page.get_title(), "My title")
        # The slug should have been automatically set
        self.assertEqual(page.get_slug(), "my-title")
        # The code is left blank in this case
        self.assertIsNone(organization.code)
        # The page should be in navigation to appear in the breadcrumb
        self.assertTrue(page.in_navigation)

        # A page role should have been created
        self.assertEqual(page.roles.count(), 1)
        role = page.roles.get(role="ADMIN")
        self.assertEqual(role.group.name, "Admin | My title")
        self.assertEqual(role.group.permissions.count(), 1)
        self.assertEqual(role.folder.name, "Admin | My title")

        # All expected permissions should have been assigned to the group:
        # - Django permissions
        self.assertEqual(role.group.permissions.first().codename, "change_page")
        # - DjangoCMS page permissions
        self.assertEqual(PagePermission.objects.filter(group=role.group).count(), 1)
        page_permission = PagePermission.objects.get(group=role.group)
        for key, value in role_dict["organization_page_permissions"].items():
            self.assertEqual(getattr(page_permission, key), value)
        # The Django Filer folder permissions
        self.assertEqual(
            FolderPermission.objects.filter(group_id=role.group_id).count(), 1
        )
        folder_permission = FolderPermission.objects.get(group_id=role.group_id)
        for key, value in role_dict["organization_folder_permissions"].items():
            self.assertEqual(getattr(folder_permission, key), value)

        # The page should be public
        page.publish("en")
        response = self.client.get(page.get_absolute_url())
        self.assertEqual(response.status_code, 200)