def test_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 test_anonymous_user_public_for_none(self): # default of when to show pages to anonymous user doesn't take # global permissions into account url = self.page_b.get_absolute_url() with SettingsOverride(POSER_PUBLIC_FOR=None): response = self.client.get(url) self.assertEqual(response.status_code, 404)
def test_include_urlconf(self): with SettingsOverride(ROOT_URLCONF='poser.test_utils.project.second_urls_for_apphook_tests'): apphook_pool.clear() superuser = User.objects.create_superuser('admin', '*****@*****.**', 'admin') child_child_page = create_page("child_child_page", "nav_playground.html", created_by=superuser, published=True, apphook='SampleApp') child_child_page.publish() path = reverse('extra_second') response = self.client.get(path) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'sampleapp/extra.html') self.assertContains(response, "test included urlconf") path = reverse('extra_first') response = self.client.get(path) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'sampleapp/extra.html') self.assertContains(response, "test urlconf") path = reverse('extra_first') response = self.client.get(path) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'sampleapp/extra.html') self.assertContains(response, "test urlconf") path = reverse('extra_second') response = self.client.get(path) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'sampleapp/extra.html') self.assertContains(response, "test included urlconf") apphook_pool.clear()
def test_public_for_all_num_queries(self): with SettingsOverride(POSER_PUBLIC_FOR='all'): user = User.objects.create_user('user', '*****@*****.**', 'user') request = self.get_request(user) page = create_page("test-public-for-all", "nav_playground.html") with self.assertNumQueries(0): page.has_view_permission(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_unauthed_num_queries(self): with SettingsOverride(POSER_PUBLIC_FOR='all'): request = self.get_request() site = Site() site.pk = 1 page = create_page("test-public-for-all", "nav_playground.html") with self.assertNumQueries(0): page.has_view_permission(request)
def test_site_framework(self): #Test the site framework, and test if it's possible to disable it with SettingsOverride(SITE_ID=self.site2.pk): create_page("page_2a", "nav_playground.html", site=self.site2) create_page("page_3b", "nav_playground.html", site=self.site3) with SettingsOverride(SITE_ID=self.site3.pk): create_page("page_3a", "nav_playground.html", site=self.site3) # with param self.assertEqual(Page.objects.on_site(self.site2.pk).count(), 1) self.assertEqual(Page.objects.on_site(self.site3.pk).count(), 2) self.assertEqual(Page.objects.on_site().count(), 2) with SettingsOverride(SITE_ID=self.site2.pk): # without param self.assertEqual(Page.objects.on_site().count(), 1)
def test_page_permissions_view_groups(self): with SettingsOverride(POSER_PUBLIC_FOR='staff'): user = User.objects.create_user('user', '*****@*****.**', 'user') group = Group.objects.create(name='testgroup') group.user_set.add(user) request = self.get_request(user) page = create_page('A', 'nav_playground.html') assign_user_to_page(page, group, grant_all=True) self.assertTrue(page.has_view_permission(request))
def test_page_permissions(self): with SettingsOverride(POSER_PUBLIC_FOR='staff'): user = User.objects.create_user('user', '*****@*****.**', 'user') user.user_permissions.add( Permission.objects.get(codename="view_page")) user.save() request = self.get_request(user) page = create_page('A', 'nav_playground.html') self.assertTrue(page.has_view_permission(request))
def test_home_slug_not_accessible(self): with SettingsOverride(POSER_PERMISSION=False): page = create_page('page', 'nav_playground.html', published=True) self.assertEqual(page.get_absolute_url(), '/page/') resp = self.client.get('/') self.assertEqual(resp.status_code, HttpResponseNotFound.status_code) resp = self.client.get('/page/') self.assertEqual(resp.status_code, HttpResponse.status_code)
def test_poser_templates_none(self): ''' In fixing #814, POSER_TEMPLATES default after patching changes from None to an empty tuple. As such, If the user has decided to set None for some reason, this test lets us know what to expect. As it stands, we should get a TypeError because post_patch attempts to turn None into a tuple explicitly. ''' with SettingsOverride(DEBUG=True, POSER_TEMPLATES=None): self.assertRaises(ImproperlyConfigured, post_patch_check)
def test_apphook_on_root_reverse(self): with SettingsOverride(ROOT_URLCONF='poser.test_utils.project.urls_for_apphook_tests'): apphook_pool.clear() superuser = User.objects.create_superuser('admin', '*****@*****.**', 'admin') page = create_page("apphooked-page", "nav_playground.html", created_by=superuser, published=True, apphook="SampleApp") self.assertTrue(page.publish()) self.assertFalse(reverse('sample-settings').startswith('//')) apphook_pool.clear()
def test_poser_templates_length(self): ''' Ensure that the correct exception is raised when POSER_TEMPLATES is configured with an empty tuple or the magic value 'INHERIT' ''' improperly_configured_template_tests = ( # don't allow 0 length (), ) for value_to_test in improperly_configured_template_tests: with SettingsOverride(DEBUG=True, POSER_TEMPLATES=value_to_test): self.assertRaises(ImproperlyConfigured, post_patch_check)
def test_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 = '/' self.assertRaises(Http404, _handle_no_page, request, slug)
def test_list_apphooks(self): out = StringIO() apps = ["poser", "poser.test_utils.project.sampleapp"] with SettingsOverride(INSTALLED_APPS=apps): create_page('Hello Page', "nav_playground.html", apphook=APPHOOK) self.assertEqual( Page.objects.filter(application_urls=APPHOOK).count(), 1) command = poser.Command() command.stdout = out command.handle("list", "apphooks", interactive=False) self.assertEqual(out.getvalue(), "SampleApp\n")
def test_user_normal_can_view(self): url = self.page_b.get_absolute_url() with self.login_user_context(self.user_normal): response = self.client.get(url) self.assertEqual(response.status_code, 200) with self.login_user_context(self.user_non_global): response = self.client.get(url) self.assertEqual(response.status_code, 200) with SettingsOverride(POSER_PUBLIC_FOR=None): # non logged in user response = self.client.get(url) self.assertEqual(response.status_code, 404)
def test_page_already_expired(self): """ Test that a page which has a end date in the past gives a 404, not a 500. """ yesterday = datetime.date.today() - datetime.timedelta(days=1) with SettingsOverride(POSER_MODERATOR=False, POSER_PERMISSION=False): page = create_page('page', 'nav_playground.html', publication_end_date=yesterday, published=True) resp = self.client.get(page.get_absolute_url()) self.assertEqual(resp.status_code, 404)
def test_apphook_by_class(self): if APP_MODULE in sys.modules: del sys.modules[APP_MODULE] apphooks = ( '%s.%s' % (APP_MODULE, APP_NAME), ) with SettingsOverride(POSER_APPHOOKS=apphooks): apphook_pool.clear() apphook = apphook_pool.get_apphook(APP_NAME) page = create_page(apphook=apphook, **self._get_default_create_page_arguments()) self.assertEqual(page.get_application_urls(), APP_NAME)
def test_implicit_apphooks(self): """ Test implicit apphook loading with INSTALLED_APPS + poser_app.py """ apps = ['poser.test_utils.project.sampleapp'] with SettingsOverride(INSTALLED_APPS=apps, ROOT_URLCONF='poser.test_utils.project.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_explicit_apphooks(self): """ Test explicit apphook loading with the POSER_APPHOOKS setting. """ apphooks = ( '%s.%s' % (APP_MODULE, APP_NAME), ) with SettingsOverride(POSER_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_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), ) create_page("page2", "nav_playground.html", published=True) with SettingsOverride(POSER_APPHOOKS=apphooks): apphook_pool.clear() response = self.client.get('/') self.assertEqual(response.status_code, 404) apphook_pool.clear()
def test_authed_basic_perm_num_queries(self): with SettingsOverride(POSER_PUBLIC_FOR='staff'): user = User.objects.create_user('user', '*****@*****.**', 'user') user.user_permissions.add( Permission.objects.get(codename="view_page")) user.save() request = self.get_request(user) page = create_page("test-public-for-all", "nav_playground.html") with self.assertNumQueries(0): """ The queries are: Generic django permission lookup content type lookup by permission lookup """ page.has_view_permission(request)
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 self.site2 = Site.objects.create(domain="sample2.com", name="sample2.com") self.site3 = Site.objects.create(domain="sample3.com", name="sample3.com") self._login_context = self.login_user_context(u) self._login_context.__enter__()
def test_apphook_on_root(self): with SettingsOverride(ROOT_URLCONF='poser.test_utils.project.urls_for_apphook_tests'): apphook_pool.clear() superuser = User.objects.create_superuser('admin', '*****@*****.**', 'admin') page = create_page("apphooked-page", "nav_playground.html", created_by=superuser, published=True, apphook="SampleApp") blank_page = create_page("not-apphooked-page", "nav_playground.html", created_by=superuser, published=True, apphook="", slug='blankapp') self.assertTrue(page.publish()) self.assertTrue(blank_page.publish()) response = self.client.get("/apphooked-page/") self.assertTemplateUsed(response, 'sampleapp/home.html') response = self.client.get('/blankapp/') self.assertTemplateUsed(response, 'nav_playground.html') apphook_pool.clear()
def test_poser_templates_valid(self): ''' These are all formats that should be valid, thus return nothing when DEBUG is True. ''' success_template_tests = ( # one valid template ( ('col_two.html', 'two columns'), ), # two valid templates ( ('col_two.html', 'two columns'), ('col_three.html', 'three columns'), ), # three valid templates ( ('col_two.html', 'two columns'), ('col_three.html', 'three columns'), ('nav_playground.html', 'navigation examples'), ), # three valid templates + inheritance ( ('col_two.html', 'two columns'), ('col_three.html', 'three columns'), ('nav_playground.html', 'navigation examples'), ), # same valid templates as above, ensuring we don't short circuit when inheritance # magic comes first. ( ('col_two.html', 'two columns'), ('col_three.html', 'three columns'), ('nav_playground.html', 'navigation examples'), ), ) for value_to_test in success_template_tests: with SettingsOverride(DEBUG=True, POSER_TEMPLATES=value_to_test): self.assertEqual(None, post_patch_check())
def test_slug_url_overwrite_clash(self): """ Tests if a URL-Override clashes with a normal page url """ with SettingsOverride(POSER_PERMISSION=False): bar = create_page('bar', 'nav_playground.html', published=False) foo = create_page('foo', 'nav_playground.html', published=True) # Tests to assure is_valid_url is ok on plain pages self.assertTrue(is_valid_url(bar.get_absolute_url(), bar)) self.assertTrue(is_valid_url(foo.get_absolute_url(), foo)) # Set url_overwrite for page foo foo.has_url_overwrite = True foo.path = '/bar/' foo.save() try: url = is_valid_url(bar.get_absolute_url(), bar) except ValidationError: url = False if url: bar.published = True bar.save() self.assertFalse(bar.published)
def test_get_page_for_apphook(self): with SettingsOverride(ROOT_URLCONF='poser.test_utils.project.second_urls_for_apphook_tests'): apphook_pool.clear() superuser = User.objects.create_superuser('admin', '*****@*****.**', 'admin') child_child_page = create_page("child_child_page", "nav_playground.html", created_by=superuser, published=True, apphook='SampleApp') child_child_page.publish() # publisher_public is set to draft on publish, issue with onetoone reverse child_child_page = self.reload(child_child_page) en_title = child_child_page.get_title() path = reverse('sample-settings') response = self.client.get(path) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'sampleapp/home.html') self.assertContains(response, en_title) apphook_pool.clear()
def test_apphook_breaking_under_home_with_new_path_caching(self): with SettingsOverride(POSER_PERMISSION=False): create_page("subchild", "nav_playground.html", published=True, apphook='SampleApp') urlpatterns = get_app_patterns() resolver = urlpatterns[0] url = resolver.reverse('sample-root')
def test_authed_no_access(self): with SettingsOverride(POSER_PUBLIC_FOR='staff'): user = User.objects.create_user('user', '*****@*****.**', 'user') request = self.get_request(user) page = create_page("test-public-for-all", "nav_playground.html") self.assertFalse(page.has_view_permission(request))
def test_unauthed_no_access(self): with SettingsOverride(POSER_PUBLIC_FOR='staff'): request = self.get_request() page = create_page("test-public-for-all", "nav_playground.html") self.assertFalse(page.has_view_permission(request))