def test_03_is_media_url(self): with SettingsOverride(MEDIA_URL='/media/'): request = self.get_request('/media/') self.assertTrue(urlutils.is_media_request(request)) request = self.get_request('/no-media/') self.assertFalse(urlutils.is_media_request(request)) with SettingsOverride(MEDIA_URL='http://testserver2.com/'): request = self.get_request('/') self.assertFalse(urlutils.is_media_request(request)) with SettingsOverride(MEDIA_URL='http://testserver/media/'): request = self.get_request('/media/') self.assertTrue(urlutils.is_media_request(request)) request = self.get_request('/no-media/') self.assertFalse(urlutils.is_media_request(request))
def setUp(self): admin = 'django.contrib.admin' noadmin_apps = [ app for app in settings.INSTALLED_APPS if not app == admin ] self._ctx = SettingsOverride(INSTALLED_APPS=noadmin_apps) self._ctx.__enter__()
def test_03_multilingual_middleware_get_lang_from_request(self): class Mock: pass middle = MultilingualURLMiddleware() with SettingsOverride(CMS_LANGUAGES={'klingon': 'Klingon'}): request = Mock() setattr(request, 'session', {}) setattr(request, 'path_info', '/en/whatever') setattr(request, 'path', '/en/whatever') result = middle.get_language_from_request(request) self.assertEqual(result, 'en') setattr(request, 'path_info', 'whatever') setattr(request, 'path', 'whatever') setattr(request, 'session', {'django_language': 'klingon'}) setattr(request, 'COOKIES', {}) setattr(request, 'META', {}) result = middle.get_language_from_request(request) self.assertEqual(result, 'klingon') # the session's language. Nerd. request = Mock() setattr(request, 'path_info', 'whatever') setattr(request, 'path', 'whatever') setattr(request, 'COOKIES', {'django_language': 'klingon'}) setattr(request, 'META', {}) result = middle.get_language_from_request(request) self.assertEqual(result, 'klingon') # the cookies language. # Now the following should revert to the default language (en) setattr(request, 'COOKIES', {'django_language': 'elvish'}) result = middle.get_language_from_request(request) self.assertEqual(result, 'en') # The default
def test_11_detail_view_fallsback_language(self): ''' Ask for a page in elvish (doesn't exist), and assert that it fallsback to English ''' with SettingsOverride( TEMPLATE_CONTEXT_PROCESSORS=[], CMS_LANGUAGE_FALLBACK=True, CMS_DBGETTEXT=False, CMS_LANGUAGE_CONF={'elvish': [ 'klingon', 'en', ]}, CMS_LANGUAGES=[('klingon', 'Klingon'), ('elvish', 'Elvish')]): from cms.views import details class Mock: pass request = Mock() setattr(request, 'REQUEST', {'language': 'elvish'}) setattr(request, 'GET', []) setattr(request, 'session', {}) setattr(request, 'path', '/') setattr(request, 'user', self.user) setattr(request, 'current_page', None) response = details(request, slug=self.test_page.get_slug()) self.assertTrue(isinstance(response, HttpResponseRedirect))
def test_01_dbgettext_deprecation(self): with SettingsOverride(CMS_DBGETTEXT_SLUGS=True): self.assertWarns( DeprecationWarning, "CMS_DBGETTEXT_SLUGS (and general support for django-dbggettext " "for CMS contents) will be deprecated in django CMS 2.2.", post_patch)
def test_03_apphook_on_root(self): if APP_MODULE in sys.modules: del sys.modules[APP_MODULE] with SettingsOverride(ROOT_URLCONF='testapp.urls_for_apphook_tests'): apphook_pool.clear() superuser = User.objects.create_superuser('admin', '*****@*****.**', 'admin') page = self.create_page(user=superuser, published=True) english_title = page.title_set.all()[0] self.assertEquals(english_title.language, 'en') Title.objects.create( language='de', title='%s DE' % english_title.title, slug=english_title.slug, path=english_title.path, page=page, ) page.title_set.all().update(application_urls='SampleApp') self.assertTrue(page.publish()) response = self.client.get(self.get_pages_root()) self.assertTemplateUsed(response, 'sampleapp/home.html') apphook_pool.clear()
def test_14_flat_urls(self): with SettingsOverride(CMS_FLAT_URLS=True): home_slug = "home" child_slug = "child" grandchild_slug = "grandchild" home = self.create_page(title=home_slug, published=True, in_navigation=True) home.publish() child = self.create_page(parent_page=home, title=child_slug, published=True, in_navigation=True) child.publish() grandchild = self.create_page(parent_page=child, title=grandchild_slug, published=True, in_navigation=True) grandchild.publish() response = self.client.get(home.get_absolute_url()) self.assertEqual(response.status_code, 200) response = self.client.get(child.get_absolute_url()) self.assertEqual(response.status_code, 200) response = self.client.get(grandchild.get_absolute_url()) self.assertEqual(response.status_code, 200) self.assertFalse( child.get_absolute_url() in grandchild.get_absolute_url())
def test_02_show_menu(self): with SettingsOverride(CMS_MODERATOR = False): context = self.get_context() tpl = Template("{% load menu_tags %}{% show_menu %}") tpl.render(context) nodes = context['children'] self.assertEqual(nodes[0].get_absolute_url(), self.get_pages_root()) self.assertEqual(nodes[0].get_absolute_url(), "/content/")
def setUp(self): with SettingsOverride(CMS_MODERATOR = False): u = User(username="******", is_staff = True, is_active = True, is_superuser = True) u.set_password("test") u.save() self.login_user(u) self.create_some_pages()
def test_03_command_line_should_raise_when_moderator_false(self): with SettingsOverride(CMS_MODERATOR=False): raised = False try: com = publisher_publish.Command() com.handle_noargs() except CommandError: raised = True self.assertTrue(raised)
def test_03_show_breadcrumb(self): with SettingsOverride(CMS_MODERATOR = False): page2 = Page.objects.get(pk=self.page2.pk) context = self.get_context(path=self.page2.get_absolute_url()) tpl = Template("{% load menu_tags %}{% show_breadcrumb %}") tpl.render(context) nodes = context['ancestors'] self.assertEqual(nodes[0].get_absolute_url(), self.get_pages_root()) self.assertEqual(nodes[0].get_absolute_url(), "/content/") self.assertEqual(isinstance(nodes[0], NavigationNode), True) self.assertEqual(nodes[1].get_absolute_url(), page2.get_absolute_url())
def test_05_incorrect_nav_extender_in_db(self): with SettingsOverride(CMS_MODERATOR=False): self.create_some_nodes() page2 = Page.objects.get(pk=self.page2.pk) page2.navigation_extenders = "SomethingWrong" page2.save() context = self.get_context() tpl = Template("{% load menu_tags %}{% show_menu %}") tpl.render(context) nodes = context['children'] self.assertEqual(len(nodes), 2)
def test_02_language_fallback(self): """ Test language fallbacks in details view """ self.create_page(published=True, language='en') request = self.get_request('/', 'de') response = details(request, '') self.assertEqual(response.status_code, 302) self.assertEqual(response['Location'], '/en/') with SettingsOverride(CMS_LANGUAGE_FALLBACK=False): self.assertRaises(Http404, details, request, '')
def test_01_handle_no_page(self): """ Test handle nopage correctly works with DEBUG=True """ request = self.get_request('/') slug = '' self.assertRaises(Http404, _handle_no_page, request, slug) with SettingsOverride(DEBUG=True): request = self.get_request('/') slug = '' response = _handle_no_page(request, slug) self.assertEqual(response.status_code, 200)
def test_01_site_framework(self): #Test the site framework, and test if it's possible to disable it with SettingsOverride(SITE_ID=2): page_2a = self.create_page(site=2) response = self.client.get("/admin/cms/page/?site__exact=3") self.assertEqual(response.status_code, 200) page_3b = self.create_page(site=3) with SettingsOverride(SITE_ID=3): page_3a = self.create_page(site=3) # with param self.assertEqual(Page.objects.on_site(2).count(), 1) self.assertEqual(Page.objects.on_site(3).count(), 2) self.assertEqual(Page.objects.drafts().on_site().count(), 2) with SettingsOverride(SITE_ID=2): # without param self.assertEqual(Page.objects.drafts().on_site().count(), 1)
def setUp(self): with SettingsOverride(SITE_ID=1): u = User(username="******", is_staff = True, is_active = True, is_superuser = True) u.set_password("test") u.save() # setup sites Site(domain="sample2.com", name="sample2.com").save() # pk 2 Site(domain="sample3.com", name="sample3.com").save() # pk 3 self.login_user(u)
def test_00_details_view(self): """ Tests that the `detail` view is working. """ with SettingsOverride(**self.render_settings()): from cms.views import details response = details(self.get_request(), slug=self.test_page.get_slug()) r = self.strip_rendered(response.content) self.assertEqual( r, u'|' + self.test_data['text_main'] + u'|' + self.test_data['text_sub'] + u'|')
def test_03_extenders_on_root_child(self): with SettingsOverride(CMS_MODERATOR=False): self.create_some_nodes() page4 = Page.objects.get(pk=self.page4.pk) page4.navigation_extenders = "TestMenu" page4.save() context = self.get_context() tpl = Template("{% load menu_tags %}{% show_menu 0 100 100 100 %}") tpl.render(context) nodes = context['children'] self.assertEqual(len(nodes), 2) self.assertEqual(len(nodes[1].children), 4)
def test_01_explicit_apphooks(self): """ Test explicit apphook loading with the CMS_APPHOOKS setting. """ if APP_MODULE in sys.modules: del sys.modules[APP_MODULE] apphooks = ('%s.%s' % (APP_MODULE, APP_NAME), ) with SettingsOverride(CMS_APPHOOKS=apphooks): apphook_pool.clear() hooks = apphook_pool.get_apphooks() app_names = [hook[0] for hook in hooks] self.assertEqual(len(hooks), 1) self.assertEqual(app_names, [APP_NAME]) apphook_pool.clear()
def test_04_get_site_choices_without_moderator(self): with SettingsOverride(CMS_MODERATOR=False): # boilerplate (creating a page) user_super = User(username="******", is_staff=True, is_active=True, is_superuser=True) user_super.set_password("super") user_super.save() self.login_user(user_super) self.create_page(title="home", user=user_super) # The proper test result = get_site_choices() self.assertEquals(result, [(1, 'example.com')])
def test_03_apphook_not_hooked(self): """ Test details view when apphook pool has apphooks, but they're not actually hooked """ if APP_MODULE in sys.modules: del sys.modules[APP_MODULE] apphooks = ('%s.%s' % (APP_MODULE, APP_NAME), ) self.create_page(published=True, language='en') with SettingsOverride(CMS_APPHOOKS=apphooks): apphook_pool.clear() response = self.client.get('/') self.assertEqual(response.status_code, 200) apphook_pool.clear()
def test_discover2(self): """ Same as test_discover, tests that those tests don't leak """ _reset(replacer) # reset against globally INSTALLED_APPS with SettingsOverride(INSTALLED_APPS=['minitrue.testdata']): _reset(replacer) # reset against overwritten INSTALLED_APPS self.assertFalse(replacer._discovered) self.assertEqual(len(replacer.models), 0) replacer.discover() self.assertTrue(replacer._discovered) self.assertEqual(len(replacer.models), 1) self.assertTrue(Title in replacer.models) self.assertEqual(replacer.models[Title].fields, ['title', 'redirect'])
def test_05_login_required(self): self.create_page( published=True, language='en', login_required=True, ) request = self.get_request('/') response = details(request, '') self.assertEqual(response.status_code, 302) self.assertEqual(response['Location'], '%s?next=/en/' % settings.LOGIN_URL) with SettingsOverride(i18n_installed=False): request = self.get_request('/') response = details(request, '') self.assertEqual(response.status_code, 302) self.assertEqual(response['Location'], '%s?next=/' % settings.LOGIN_URL)
def test_discover(self): """ Test automated (explicit) discovery. """ _reset(replacer) # reset against globally INSTALLED_APPS with SettingsOverride(INSTALLED_APPS=['minitrue.testdata']): _reset(replacer) # reset against overwritten INSTALLED_APPS self.assertFalse(replacer._discovered) self.assertEqual(len(replacer.models), 0) replacer.discover() self.assertTrue(replacer._discovered) self.assertEqual(len(replacer.models), 1) self.assertTrue(Title in replacer.models) self.assertEqual(replacer.models[Title].fields, ['title', 'redirect'])
def setUp(self): """ All tests are run against different INSTALLED_APPS """ self.data = Namespace() # namespace all test data self.data.template = 'testtemplate.html' apps = [ 'minitrue.testdata', 'django.contrib.admin', 'minitrue', 'project', 'django.contrib.sessions' ] templates = [(self.data.template, 'testtemplate')] self.settings_ctx = SettingsOverride(INSTALLED_APPS=apps, CMS_TEMPLATES=templates) self.settings_ctx.__enter__() self._populate_database()
def test_02_implicit_apphooks(self): """ Test implicit apphook loading with INSTALLED_APPS + cms_app.py """ if APP_MODULE in sys.modules: del sys.modules[APP_MODULE] apps = ['testapp.sampleapp'] with SettingsOverride(INSTALLED_APPS=apps, ROOT_URLCONF='testapp.urls_for_apphook_tests'): apphook_pool.clear() hooks = apphook_pool.get_apphooks() app_names = [hook[0] for hook in hooks] self.assertEqual(len(hooks), 1) self.assertEqual(app_names, [APP_NAME]) apphook_pool.clear()
def test_04_extenders_on_child(self): with SettingsOverride(CMS_MODERATOR=False): self.create_some_nodes() page1 = Page.objects.get(pk=self.page1.pk) page1.in_navigation = False page1.save() page2 = Page.objects.get(pk=self.page2.pk) page2.navigation_extenders = "TestMenu" page2.save() context = self.get_context() tpl = Template("{% load menu_tags %}{% show_menu 0 100 100 100 %}") tpl.render(context) nodes = context['children'] self.assertEqual(len(nodes), 2) self.assertEqual(len(nodes[0].children), 4) self.assertEqual(nodes[0].children[1].get_absolute_url(), "/")
def create_some_nodes(self): with SettingsOverride(CMS_MODERATOR=False): self.page1 = self.create_page(parent_page=None, published=True, in_navigation=True) self.page2 = self.create_page(parent_page=self.page1, published=True, in_navigation=True) self.page3 = self.create_page(parent_page=self.page2, published=True, in_navigation=True) self.page4 = self.create_page(parent_page=None, published=True, in_navigation=True) self.page5 = self.create_page(parent_page=self.page4, published=True, in_navigation=True)
def setUp(self): with SettingsOverride(CMS_MODERATOR=False): u = User(username="******", is_staff=True, is_active=True, is_superuser=True) u.set_password("test") u.save() self.login_user(u) menu_pool.clear(settings.SITE_ID) if not menu_pool.discovered: menu_pool.discover_menus() self.old_menu = menu_pool.menus menu_pool.menus = { 'CMSMenu': self.old_menu['CMSMenu'], 'TestMenu': TestMenu() }
def test_02_test_type_limit(self): placeholder = self.get_placeholder() admin = self.get_admin() data = { 'plugin_type': 'TextPlugin', 'placeholder': placeholder.pk, 'language': 'en', } superuser = self.get_superuser() with UserLoginContext(self, superuser): with SettingsOverride(CMS_PLACEHOLDER_CONF=self.placeholderconf): request = self.get_post_request(data) response = admin.add_plugin(request) # first self.assertEqual(response.status_code, 200) response = admin.add_plugin(request) # second self.assertEqual(response.status_code, 400) self.assertEqual( response.content, "This placeholder already has the maximum number (1) of TextPlugin plugins." )