def test_has_page_add_permission_with_target(self): page = create_page('Test', 'nav_playground.html', 'en') user = self._create_user('user') request = RequestFactory().get('/', data={'target': page.pk}) request.user = user has_perm = has_page_add_permission(request) self.assertFalse(has_perm)
def get_request(self, path=None, language=None, post_data=None, enforce_csrf_checks=False): factory = RequestFactory() if not path: path = self.get_pages_root() if not language: language = settings.LANGUAGES[0][0] if post_data: request = factory.post(path, post_data) else: request = factory.get(path) request.session = self.client.session request.user = getattr(self, 'user', AnonymousUser()) request.LANGUAGE_CODE = language request._dont_enforce_csrf_checks = not enforce_csrf_checks return request
class ToolbarNoModeratorTests(SettingsOverrideTestCase, ToolbarUserMixin): settings_overrides = {'CMS_MODERATOR': False} def setUp(self): self.request_factory = RequestFactory() def test_toolbar_no_moderate_button(self): page = create_page('test', 'nav_playground.html', 'en', published=True) request = self.request_factory.get(page.get_absolute_url() + '?edit') request.user = self.get_staff() request.current_page = page toolbar = CMSToolbar(request) self.assertTrue(toolbar.edit_mode) items = toolbar.get_items({}) # Logo + edit-mode + templates + page-menu + admin-menu + logout self.assertEqual(len(items), 6)
def test_emulate_admin_index(self): """ Call methods that emulate the adminsite instance's index. This test was basically the reason for the new manager, in light of the problem highlighted in ticket #1120, which asserts that giving a user no site-specific rights when creating a GlobalPagePermission should allow access to all sites. """ # create and then ignore this user. superuser = self._create_user("super", is_staff=True, is_active=True, is_superuser=True) superuser.set_password("super") superuser.save() # create 2 staff users SITES = [ Site.objects.get(pk=1), Site.objects.create(domain='example2.com', name='example2.com'), ] USERS = [ self._create_user("staff", is_staff=True, is_active=True), self._create_user("staff_2", is_staff=True, is_active=True), ] for user in USERS: user.set_password('staff') # re-use the same methods the UserPage form does. # Note that it internally calls .save(), as we've not done so. save_permissions({ 'can_add_page': True, 'can_change_page': True, 'can_delete_page': False }, user) GlobalPagePermission.objects.create(can_add=True, can_change=True, can_delete=False, user=USERS[0]) # we're querying here to ensure that even though we've created two users # above, we should have successfully filtered to just one perm. self.assertEqual(1, GlobalPagePermission.objects.with_user(USERS[0]).count()) # this will confirm explicit permissions still work, by adding the first # site instance to the many2many relationship 'sites' GlobalPagePermission.objects.create(can_add=True, can_change=True, can_delete=False, user=USERS[1]).sites.add(SITES[0]) self.assertEqual(1, GlobalPagePermission.objects.with_user(USERS[1]).count()) homepage = create_page(title="master", template="nav_playground.html", language="en", in_navigation=True, slug='/') publish_page(page=homepage, user=superuser, language='en') with SettingsOverride(CMS_PERMISSION=True): # for all users, they should have access to site 1 request = RequestFactory().get(path='/', data={'site__exact': 1}) # we need a session attribute for current_site(request), which is # used by has_page_add_permission and has_page_change_permission request.session = {} for user in USERS: # has_page_add_permission and has_page_change_permission both test # for this explicitly, to see if it's a superuser. request.user = user # Note, the query count is inflated by doing additional lookups # because there's a site param in the request. with self.assertNumQueries(FuzzyInt(6,7)): # PageAdmin swaps out the methods called for permissions # if the setting is true, it makes use of cms.utils.permissions self.assertTrue(has_page_add_permission(request)) self.assertTrue(has_page_change_permission(request)) # internally this calls PageAdmin.has_[add|change|delete]_permission() self.assertEqual({'add': True, 'change': True, 'delete': False}, site._registry[Page].get_model_perms(request)) # can't use the above loop for this test, as we're testing that # user 1 has access, but user 2 does not, as they are only assigned # to site 1 request = RequestFactory().get('/', data={'site__exact': 2}) request.session = {} # As before, the query count is inflated by doing additional lookups # because there's a site param in the request with self.assertNumQueries(FuzzyInt(11, 20)): # this user shouldn't have access to site 2 request.user = USERS[1] self.assertTrue(not has_page_add_permission(request)) self.assertTrue(not has_page_change_permission(request)) self.assertEqual({'add': False, 'change': False, 'delete': False}, site._registry[Page].get_model_perms(request)) # but, going back to the first user, they should. request = RequestFactory().get('/', data={'site__exact': 2}) request.user = USERS[0] self.assertTrue(has_page_add_permission(request)) self.assertTrue(has_page_change_permission(request)) self.assertEqual({'add': True, 'change': True, 'delete': False}, site._registry[Page].get_model_perms(request))
def setUp(self): self.request_factory = RequestFactory()
class ToolbarTests(SettingsOverrideTestCase, ToolbarUserMixin): settings_overrides = {'CMS_MODERATOR': False} def setUp(self): self.request_factory = RequestFactory() def test_toolbar_no_page_anon(self): request = self.request_factory.get('/') request.user = self.get_anon() request.current_page = None toolbar = CMSToolbar(request) items = toolbar.get_items({}) self.assertEqual(len(items), 2) # Logo + login # check the logo is there logo = items[0] self.assertTrue(isinstance(logo, Anchor)) # check the login form is there login = items[1] self.assertTrue(isinstance(login, TemplateHTML)) self.assertEqual(login.template, 'cms/toolbar/items/login.html') def test_toolbar_no_page_staff(self): request = self.request_factory.get('/') request.user = self.get_staff() request.current_page = None toolbar = CMSToolbar(request) items = toolbar.get_items({}) # Logo + edit-mode + admin-menu + logout self.assertEqual(len(items), 4) # check the logo is there logo = items[0] self.assertTrue(isinstance(logo, Anchor)) # check the edit-mode switcher is there and the switcher is turned off edit = items[1] self.assertTrue(isinstance(edit, Switcher)) self.assertFalse(toolbar.edit_mode) # check the admin-menu admin = items[2] self.assertTrue(isinstance(admin, List)) self.assertEqual(len(admin.raw_items), 1) # only the link to main admin self.assertTrue(isinstance(admin.raw_items[0], ListItem)) # check the logout button logout = items[3] self.assertTrue(isinstance(logout, GetButton)) self.assertEqual(logout.url, '?cms-toolbar-logout') def test_toolbar_no_page_superuser(self): request = self.request_factory.get('/') request.user = self.get_superuser() request.current_page = None toolbar = CMSToolbar(request) items = toolbar.get_items({}) # Logo + edit-mode + admin-menu + logout self.assertEqual(len(items), 4) # check the logo is there logo = items[0] self.assertTrue(isinstance(logo, Anchor)) # check the edit-mode switcher is there and the switcher is turned off edit = items[1] self.assertTrue(isinstance(edit, Switcher)) self.assertFalse(toolbar.edit_mode) # check the admin-menu admin = items[2] self.assertTrue(isinstance(admin, List)) self.assertEqual(len(admin.raw_items), 1) # only the link to main admin self.assertTrue(isinstance(admin.raw_items[0], ListItem)) # check the logout button logout = items[3] self.assertTrue(isinstance(logout, GetButton)) self.assertEqual(logout.url, '?cms-toolbar-logout') def test_toolbar_anon(self): page = create_page('test', 'nav_playground.html', 'en') request = self.request_factory.get('/') request.user = self.get_anon() request.current_page = page toolbar = CMSToolbar(request) items = toolbar.get_items({}) self.assertEqual(len(items), 2) # Logo + login # check the logo is there logo = items[0] self.assertTrue(isinstance(logo, Anchor)) # check the login form is there login = items[1] self.assertTrue(isinstance(login, TemplateHTML)) self.assertEqual(login.template, 'cms/toolbar/items/login.html') def test_toolbar_staff(self): page = create_page('test', 'nav_playground.html', 'en', published=True) request = self.request_factory.get(page.get_absolute_url()) request.user = self.get_staff() request.current_page = page toolbar = CMSToolbar(request) items = toolbar.get_items({}) # Logo + edit-mode + templates + page-menu + admin-menu + logout self.assertEqual(len(items), 6) # check the logo is there logo = items[0] self.assertTrue(isinstance(logo, Anchor)) # check the edit-mode switcher is there and the switcher is turned off edit = items[1] self.assertTrue(isinstance(edit, Switcher)) self.assertFalse(toolbar.edit_mode) # check templates templates = items[2] self.assertTrue(isinstance(templates, List)) self.assertEqual(len(templates.raw_items), len(settings.CMS_TEMPLATES)) base = reverse('admin:cms_page_change_template', args=(page.pk,)) for item, template in zip(templates.raw_items, settings.CMS_TEMPLATES): self.assertEqual(item.url, '%s?template=%s' % (base, template[0])) # check page menu pagemenu = items[3] self.assertTrue(isinstance(pagemenu, List)) self.assertEqual(len(pagemenu.raw_items), 4) overview, addchild, addsibling, delete = pagemenu.raw_items self.assertEqual(overview.url, reverse('admin:cms_page_changelist')) self.assertEqual(addchild.serialize_url({}, toolbar), reverse('admin:cms_page_add') + '?position=last-child&target=%s' % page.pk) self.assertEqual(addsibling.serialize_url({}, toolbar), reverse('admin:cms_page_add') + '?position=last-child') self.assertEqual(delete.serialize_url({}, toolbar), reverse('admin:cms_page_delete', args=(page.pk,))) # check the admin-menu admin = items[4] self.assertTrue(isinstance(admin, List)) self.assertEqual(len(admin.raw_items), 1) # only the link to main admin self.assertTrue(isinstance(admin.raw_items[0], ListItem)) # check the logout button logout = items[5] self.assertTrue(isinstance(logout, GetButton)) self.assertEqual(logout.url, '?cms-toolbar-logout') def test_toolbar_markup(self): superuser = self.get_superuser() create_page("toolbar-page", "nav_playground.html", "en", created_by=superuser, published=True) with self.login_user_context(superuser): response = self.client.get('/?edit') self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'nav_playground.html') self.assertContains(response, '<div id="cms_toolbar"') self.assertContains(response, 'cms.placeholders.js') self.assertContains(response, 'cms.placeholders.css')
from cms.api import create_page from cms.models.pagemodel import Page from cms.test_utils.util.request_factory import RequestFactory from django import template from django.contrib.auth.models import AnonymousUser from djangocmsbench.utils import run_benchmark rf = RequestFactory() request = rf.get('/') request.user = AnonymousUser() context = template.Context({ 'request': request }) tpl = template.Template("{% load menu_tags %}{% show_menu %}") def benchmark(): tpl.render(context) def setup(): """ Build tree: 3 levels 5 per level/branch """ names = ['a', 'b', 'c', 'd', 'e'] languages = ['de', 'it', 'fr', 'rm'] for lang in languages: for name in names: page = create_page(name, 'template.html', lang, published=True, in_navigation=True) for name in names:
def request_factory(self): return RequestFactory()