Example #1
0
    def test_clean_overwrite_url(self):
        user = AnonymousUser()
        user.is_superuser = True
        user.pk = 1
        request = type('Request', (object,), {'user': user})
        with SettingsOverride():
            data = {
                'title': 'TestPage',
                'slug': 'test-page',
                'language': 'en',
                'overwrite_url': '/overwrite/url/',
                'site': Site.objects.get_current().pk,
                'template': get_cms_setting('TEMPLATES')[0][0],
                'published': True
            }

            form = PageForm(data)
            self.assertTrue(form.is_valid(), form.errors.as_text())
            # WTF? WHY DOES form.save() not handle this stuff???
            instance = form.save()
            instance.permission_user_cache = user
            instance.permission_advanced_settings_cache = True
            Title.objects.set_or_create(request, instance, form, 'en')
            form = PageForm(data, instance=instance)
            self.assertTrue(form.is_valid(), form.errors.as_text())
Example #2
0
    def test_reverse_id_error_location(self):
        ''' Test moving the reverse_id validation error to a field specific one '''

        # this is the Reverse ID we'll re-use to break things.
        dupe_id = 'p1'
        site = Site.objects.get_current()
        page1 = create_page('Page 1', 'nav_playground.html', 'en', reverse_id=dupe_id)
        # Assemble a bunch of data to test the page form
        page2_data = {
            'title': 'Page 2',
            'slug': 'page-2',
            'language': 'en',
            'site': site.pk,
            'template': settings.CMS_TEMPLATES[0][0],
            'reverse_id': dupe_id,
        }
        form = PageForm(data=page2_data, files=None)
        self.assertTrue(not form.is_valid())
        # reverse_id is the only item that is in __all__ as every other field
        # has it's own clean method. Moving it to be a field error means
        # __all__ is now not available.
        self.assertTrue('__all__' not in form.errors)
        # In moving it to it's own field, it should be in form.errors, and
        # the values contained therein should match these.
        self.assertTrue('reverse_id' in form.errors)
        self.assertEqual(1, len(form.errors['reverse_id']))
        self.assertEqual([u'A page with this reverse URL id exists already.'],
            form.errors['reverse_id'])

        admin = self._get_guys(admin_only=True)
        # reset some of page2_data so we can use cms.api.create_page
        page2_data['reverse_id'] = None
        page2_data['site'] = site
        page2 = create_page(**page2_data)
        with self.login_user_context(admin):
            # re-reset the page2_data for the admin form instance.
            page2_data['reverse_id'] = dupe_id
            page2_data['site'] = site.pk
            # This is needed to avoid management form tampering errors.
            page2_data['pagepermission_set-TOTAL_FORMS'] = 0
            page2_data['pagepermission_set-INITIAL_FORMS'] = 0
            page2_data['pagepermission_set-MAX_NUM_FORMS'] = 0
            page2_data['pagepermission_set-2-TOTAL_FORMS'] = 0
            page2_data['pagepermission_set-2-INITIAL_FORMS'] = 0
            page2_data['pagepermission_set-2-MAX_NUM_FORMS'] = 0
            # post to the admin change form for page 2, and test that the
            # reverse_id form row has an errors class. Django's admin avoids
            # collapsing these, so that the error is visible.
            resp = self.client.post(base.URL_CMS_PAGE_CHANGE % page2.pk, page2_data)
            self.assertTrue('<div class="form-row errors reverse_id">' in resp.content)
Example #3
0
 def test_existing_overwrite_url(self):
     with SettingsOverride(CMS_MODERATOR=False, CMS_PERMISSION=False):
         create_page('home', 'nav_playground.html', 'en', published=True)
         create_page('boo', 'nav_playground.html', 'en', published=True)
         data = {
             'title': 'foo',
             'overwrite_url': '/boo/',
             'slug': 'foo',
             'language': 'en',
             'template': 'nav_playground.html',
             'site': 1,
         }
         form = PageForm(data)
         self.assertFalse(form.is_valid())
         self.assertTrue('overwrite_url' in form.errors)
Example #4
0
 def test_existing_overwrite_url(self):
     with SettingsOverride(CMS_PERMISSION=False):
         create_page("home", "nav_playground.html", "en", published=True)
         create_page("boo", "nav_playground.html", "en", published=True)
         data = {
             "title": "foo",
             "overwrite_url": "/boo/",
             "slug": "foo",
             "language": "en",
             "template": "nav_playground.html",
             "site": 1,
         }
         form = PageForm(data)
         self.assertFalse(form.is_valid())
         self.assertTrue("overwrite_url" in form.errors)
Example #5
0
 def test_missmatching_site_parent_dotsite(self):
     site0 = Site.objects.create(domain='foo.com', name='foo.com')
     site1 = Site.objects.create(domain='foo.com', name='foo.com')
     parent_page = Page.objects.create(
         template='nav_playground.html',
         site=site0)
     new_page_data = {
         'title': 'Title',
         'slug': 'slug',
         'language': 'en',
         'site': site1.pk,
         'template': get_cms_setting('TEMPLATES')[0][0],
         'reverse_id': '',
         'parent': parent_page.pk,
     }
     form = PageForm(data=new_page_data, files=None)
     self.assertFalse(form.is_valid())
     self.assertIn(u"Site doesn't match the parent's page site",
                   form.errors['__all__'])
 def __create_page(self, page_data, without_h1=False):
     """
     Creates page with h1 or without it
     :param page_data: dict
     :return Page
     """
     if not without_h1:
         page_data["title"] = "Page with titles and META"
         page_data["slug"] = "page-with-data-and-meta"
     # rewrite template name
     page_data['template'] = settings.CMS_TEMPLATES[0][0]
     # Save page
     self.page_form = PageForm(page_data)
     self.assertTrue(self.page_form.is_valid())
     page = self.page_form.save(commit=False)
     return page
class TestPageTitles(CMSTestCase):
    """
    Test Custom page titles
    """

    def setUp(self):
        # Create superuser
        self.superuser = self.get_superuser()

    def __create_page(self, page_data, without_h1=False):
        """
        Creates page with h1 or without it
        :param page_data: dict
        :return Page
        """
        if not without_h1:
            page_data["title"] = "Page with titles and META"
            page_data["slug"] = "page-with-data-and-meta"
        # rewrite template name
        page_data['template'] = settings.CMS_TEMPLATES[0][0]
        # Save page
        self.page_form = PageForm(page_data)
        self.assertTrue(self.page_form.is_valid())
        page = self.page_form.save(commit=False)
        return page

    def __extend_page(self, page, page_data):
        """
        Creates Title and Meta Extension for given page
        :param page: Page
        :param page_data: dict
        """
        # Get Request
        request = HttpRequest()
        request.user = self.superuser
        # Get Title Object
        title = Title.objects.set_or_create(request, page, self.page_form, settings.LANGUAGE_CODE)
        # Extend page
        ext = Metadata(extended_object=title, h1='H1 is ready to use')
        ext.save()

    def test_titles_and_meta(self):
        """
        Test H1 attribute
        """
        from django import template

        with self.login_user_context(self.superuser):

            # Create pages
            page_data = self.get_new_page_data()
            pages = {
                'page_with': self.__create_page(page_data),
                'page_without': self.__create_page(page_data, without_h1=True)
            }

            # Create template
            t = template.Template(
                "{% load cms_tags %}Page: {{ request.current_page.get_title_obj.metadata.h1 }}")

            # Post pages on the site
            for page_type, page in pages.items():
                page.save()
                # If page must to be with extra Titles and META data
                if page_type == 'page_with':
                    self.__extend_page(page, page_data)
                    page.publish(settings.LANGUAGE_CODE)

            # Test page without h1
            req = HttpRequest()
            req.current_page = pages['page_without']
            req.REQUEST = {}
            rendered = t.render(template.Context({"request": req}))
            self.assertEqual(rendered, "Page: ")

            # Test page with h1
            req = HttpRequest()
            req.current_page = pages['page_with']
            req.REQUEST = {}
            rendered = '{0}'.format(t.render(template.Context({"request": req})))
            self.assertEqual(rendered, "Page: H1 is ready to use")