From a4b472dd809c8a696d1488a54dcaf3870ec9e661 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sun, 29 Jan 2012 19:24:32 +0000 Subject: Fixed #8995 -- Added support for HTTPS in sitemaps. Modularized tests and did a bit of cleanup while I was in the area. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17409 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/sitemaps/tests/__init__.py | 5 +- django/contrib/sitemaps/tests/base.py | 28 ++++ django/contrib/sitemaps/tests/basic.py | 182 ------------------------- django/contrib/sitemaps/tests/flatpages.py | 37 +++++ django/contrib/sitemaps/tests/generic.py | 17 +++ django/contrib/sitemaps/tests/http.py | 127 +++++++++++++++++ django/contrib/sitemaps/tests/https.py | 51 +++++++ django/contrib/sitemaps/tests/urls.py | 45 ------ django/contrib/sitemaps/tests/urls/__init__.py | 0 django/contrib/sitemaps/tests/urls/http.py | 43 ++++++ django/contrib/sitemaps/tests/urls/https.py | 16 +++ 11 files changed, 323 insertions(+), 228 deletions(-) create mode 100644 django/contrib/sitemaps/tests/base.py delete mode 100644 django/contrib/sitemaps/tests/basic.py create mode 100644 django/contrib/sitemaps/tests/flatpages.py create mode 100644 django/contrib/sitemaps/tests/generic.py create mode 100644 django/contrib/sitemaps/tests/http.py create mode 100644 django/contrib/sitemaps/tests/https.py delete mode 100644 django/contrib/sitemaps/tests/urls.py create mode 100644 django/contrib/sitemaps/tests/urls/__init__.py create mode 100644 django/contrib/sitemaps/tests/urls/http.py create mode 100644 django/contrib/sitemaps/tests/urls/https.py (limited to 'django/contrib/sitemaps/tests') diff --git a/django/contrib/sitemaps/tests/__init__.py b/django/contrib/sitemaps/tests/__init__.py index c5b483cde2..b9cf5f7a7f 100644 --- a/django/contrib/sitemaps/tests/__init__.py +++ b/django/contrib/sitemaps/tests/__init__.py @@ -1 +1,4 @@ -from django.contrib.sitemaps.tests.basic import * +from .flatpages import FlatpagesSitemapTests +from .generic import GenericViewsSitemapTests +from .http import HTTPSitemapTests +from .https import HTTPSSitemapTests, HTTPSDetectionSitemapTests diff --git a/django/contrib/sitemaps/tests/base.py b/django/contrib/sitemaps/tests/base.py new file mode 100644 index 0000000000..f1d6753b3a --- /dev/null +++ b/django/contrib/sitemaps/tests/base.py @@ -0,0 +1,28 @@ +import os + +from django.conf import settings +from django.contrib.auth.models import User +from django.contrib.sites.models import Site +from django.test import TestCase + + +class SitemapTestsBase(TestCase): + protocol = 'http' + domain = 'example.com' if Site._meta.installed else 'testserver' + urls = 'django.contrib.sitemaps.tests.urls.http' + + def setUp(self): + self.base_url = '%s://%s' % (self.protocol, self.domain) + self.old_USE_L10N = settings.USE_L10N + self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS + settings.TEMPLATE_DIRS = ( + os.path.join(os.path.dirname(__file__), 'templates'), + ) + self.old_Site_meta_installed = Site._meta.installed + # Create a user that will double as sitemap content + User.objects.create_user('testuser', 'test@example.com', 's3krit') + + def tearDown(self): + settings.USE_L10N = self.old_USE_L10N + settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS + Site._meta.installed = self.old_Site_meta_installed diff --git a/django/contrib/sitemaps/tests/basic.py b/django/contrib/sitemaps/tests/basic.py deleted file mode 100644 index b675c3d6fb..0000000000 --- a/django/contrib/sitemaps/tests/basic.py +++ /dev/null @@ -1,182 +0,0 @@ -import os -from datetime import date -from django.conf import settings -from django.contrib.auth.models import User -from django.contrib.sitemaps import Sitemap, GenericSitemap -from django.contrib.sites.models import Site -from django.core.exceptions import ImproperlyConfigured -from django.test import TestCase -from django.utils.unittest import skipUnless -from django.utils.formats import localize -from django.utils.translation import activate, deactivate - - -class SitemapTests(TestCase): - urls = 'django.contrib.sitemaps.tests.urls' - - def setUp(self): - if Site._meta.installed: - self.base_url = 'http://example.com' - else: - self.base_url = 'http://testserver' - self.old_USE_L10N = settings.USE_L10N - self.old_Site_meta_installed = Site._meta.installed - self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS - self.old_Site_meta_installed = Site._meta.installed - settings.TEMPLATE_DIRS = ( - os.path.join(os.path.dirname(__file__), 'templates'), - ) - # Create a user that will double as sitemap content - User.objects.create_user('testuser', 'test@example.com', 's3krit') - - def tearDown(self): - settings.USE_L10N = self.old_USE_L10N - Site._meta.installed = self.old_Site_meta_installed - settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS - Site._meta.installed = self.old_Site_meta_installed - - def test_simple_sitemap_index(self): - "A simple sitemap index can be rendered" - response = self.client.get('/simple/index.xml') - self.assertEqual(response.content, """ - -%s/simple/sitemap-simple.xml - -""" % self.base_url) - - def test_simple_sitemap_custom_index(self): - "A simple sitemap index can be rendered with a custom template" - response = self.client.get('/simple/custom-index.xml') - self.assertEqual(response.content, """ - - -%s/simple/sitemap-simple.xml - -""" % self.base_url) - - def test_simple_sitemap(self): - "A simple sitemap can be rendered" - response = self.client.get('/simple/sitemap.xml') - self.assertEqual(response.content, """ - -%s/location/%snever0.5 - -""" % (self.base_url, date.today())) - - def test_simple_custom_sitemap(self): - "A simple sitemap can be rendered with a custom template" - response = self.client.get('/simple/custom-sitemap.xml') - self.assertEqual(response.content, """ - - -%s/location/%snever0.5 - -""" % (self.base_url, date.today())) - - @skipUnless(settings.USE_I18N, "Internationalization is not enabled") - def test_localized_priority(self): - "The priority value should not be localized (Refs #14164)" - # Localization should be active - settings.USE_L10N = True - activate('fr') - self.assertEqual(u'0,3', localize(0.3)) - - # Retrieve the sitemap. Check that priorities - # haven't been rendered in localized format - response = self.client.get('/simple/sitemap.xml') - self.assertContains(response, '0.5') - self.assertContains(response, '%s' % date.today()) - deactivate() - - def test_generic_sitemap(self): - "A minimal generic sitemap can be rendered" - response = self.client.get('/generic/sitemap.xml') - expected = '' - for username in User.objects.values_list("username", flat=True): - expected += "%s/users/%s/" % (self.base_url, username) - self.assertEqual(response.content, """ - -%s - -""" % expected) - - @skipUnless("django.contrib.flatpages" in settings.INSTALLED_APPS, "django.contrib.flatpages app not installed.") - def test_flatpage_sitemap(self): - "Basic FlatPage sitemap test" - - # Import FlatPage inside the test so that when django.contrib.flatpages - # is not installed we don't get problems trying to delete Site - # objects (FlatPage has an M2M to Site, Site.delete() tries to - # delete related objects, but the M2M table doesn't exist. - from django.contrib.flatpages.models import FlatPage - - public = FlatPage.objects.create( - url=u'/public/', - title=u'Public Page', - enable_comments=True, - registration_required=False, - ) - public.sites.add(settings.SITE_ID) - private = FlatPage.objects.create( - url=u'/private/', - title=u'Private Page', - enable_comments=True, - registration_required=True - ) - private.sites.add(settings.SITE_ID) - response = self.client.get('/flatpages/sitemap.xml') - # Public flatpage should be in the sitemap - self.assertContains(response, '%s%s' % (self.base_url, public.url)) - # Private flatpage should not be in the sitemap - self.assertNotContains(response, '%s%s' % (self.base_url, private.url)) - - def test_requestsite_sitemap(self): - # Make sure hitting the flatpages sitemap without the sites framework - # installed doesn't raise an exception - Site._meta.installed = False - response = self.client.get('/simple/sitemap.xml') - self.assertEqual(response.content, """ - -http://testserver/location/%snever0.5 - -""" % date.today()) - - @skipUnless("django.contrib.sites" in settings.INSTALLED_APPS, "django.contrib.sites app not installed.") - def test_sitemap_get_urls_no_site_1(self): - """ - Check we get ImproperlyConfigured if we don't pass a site object to - Sitemap.get_urls and no Site objects exist - """ - Site.objects.all().delete() - self.assertRaises(ImproperlyConfigured, Sitemap().get_urls) - - def test_sitemap_get_urls_no_site_2(self): - """ - Check we get ImproperlyConfigured when we don't pass a site object to - Sitemap.get_urls if Site objects exists, but the sites framework is not - actually installed. - """ - Site._meta.installed = False - self.assertRaises(ImproperlyConfigured, Sitemap().get_urls) - - def test_sitemap_item(self): - """ - Check to make sure that the raw item is included with each - Sitemap.get_url() url result. - """ - user_sitemap = GenericSitemap({'queryset': User.objects.all()}) - def is_user(url): - return isinstance(url['item'], User) - item_in_url_info = all(map(is_user, user_sitemap.get_urls())) - self.assertTrue(item_in_url_info) - - def test_cached_sitemap_index(self): - """ - Check that a cached sitemap index can be rendered (#2713). - """ - response = self.client.get('/cached/index.xml') - self.assertEqual(response.content, """ - -%s/cached/sitemap-simple.xml - -""" % self.base_url) diff --git a/django/contrib/sitemaps/tests/flatpages.py b/django/contrib/sitemaps/tests/flatpages.py new file mode 100644 index 0000000000..a40876e859 --- /dev/null +++ b/django/contrib/sitemaps/tests/flatpages.py @@ -0,0 +1,37 @@ +from django.conf import settings +from django.utils.unittest import skipUnless + +from .base import SitemapTestsBase + +class FlatpagesSitemapTests(SitemapTestsBase): + + @skipUnless("django.contrib.flatpages" in settings.INSTALLED_APPS, + "django.contrib.flatpages app not installed.") + def test_flatpage_sitemap(self): + "Basic FlatPage sitemap test" + + # Import FlatPage inside the test so that when django.contrib.flatpages + # is not installed we don't get problems trying to delete Site + # objects (FlatPage has an M2M to Site, Site.delete() tries to + # delete related objects, but the M2M table doesn't exist. + from django.contrib.flatpages.models import FlatPage + + public = FlatPage.objects.create( + url=u'/public/', + title=u'Public Page', + enable_comments=True, + registration_required=False, + ) + public.sites.add(settings.SITE_ID) + private = FlatPage.objects.create( + url=u'/private/', + title=u'Private Page', + enable_comments=True, + registration_required=True + ) + private.sites.add(settings.SITE_ID) + response = self.client.get('/flatpages/sitemap.xml') + # Public flatpage should be in the sitemap + self.assertContains(response, '%s%s' % (self.base_url, public.url)) + # Private flatpage should not be in the sitemap + self.assertNotContains(response, '%s%s' % (self.base_url, private.url)) diff --git a/django/contrib/sitemaps/tests/generic.py b/django/contrib/sitemaps/tests/generic.py new file mode 100644 index 0000000000..5f8b6b8be0 --- /dev/null +++ b/django/contrib/sitemaps/tests/generic.py @@ -0,0 +1,17 @@ +from django.contrib.auth.models import User + +from .base import SitemapTestsBase + +class GenericViewsSitemapTests(SitemapTestsBase): + + def test_generic_sitemap(self): + "A minimal generic sitemap can be rendered" + response = self.client.get('/generic/sitemap.xml') + expected = '' + for username in User.objects.values_list("username", flat=True): + expected += "%s/users/%s/" % (self.base_url, username) + self.assertEqual(response.content, """ + +%s + +""" % expected) diff --git a/django/contrib/sitemaps/tests/http.py b/django/contrib/sitemaps/tests/http.py new file mode 100644 index 0000000000..3b56aa8d53 --- /dev/null +++ b/django/contrib/sitemaps/tests/http.py @@ -0,0 +1,127 @@ +from datetime import date +from django.conf import settings +from django.contrib.auth.models import User +from django.contrib.sitemaps import Sitemap, GenericSitemap +from django.contrib.sites.models import Site +from django.core.exceptions import ImproperlyConfigured +from django.utils.unittest import skipUnless +from django.utils.formats import localize +from django.utils.translation import activate, deactivate + +from .base import SitemapTestsBase + +class HTTPSitemapTests(SitemapTestsBase): + + def test_simple_sitemap_index(self): + "A simple sitemap index can be rendered" + response = self.client.get('/simple/index.xml') + self.assertEqual(response.content, """ + +%s/simple/sitemap-simple.xml + +""" % self.base_url) + + def test_simple_sitemap_custom_index(self): + "A simple sitemap index can be rendered with a custom template" + response = self.client.get('/simple/custom-index.xml') + self.assertEqual(response.content, """ + + +%s/simple/sitemap-simple.xml + +""" % self.base_url) + + def test_simple_sitemap_section(self): + "A simple sitemap section can be rendered" + response = self.client.get('/simple/sitemap-simple.xml') + self.assertEqual(response.content, """ + +%s/location/%snever0.5 + +""" % (self.base_url, date.today())) + + def test_simple_sitemap(self): + "A simple sitemap can be rendered" + response = self.client.get('/simple/sitemap.xml') + self.assertEqual(response.content, """ + +%s/location/%snever0.5 + +""" % (self.base_url, date.today())) + + def test_simple_custom_sitemap(self): + "A simple sitemap can be rendered with a custom template" + response = self.client.get('/simple/custom-sitemap.xml') + self.assertEqual(response.content, """ + + +%s/location/%snever0.5 + +""" % (self.base_url, date.today())) + + @skipUnless(settings.USE_I18N, "Internationalization is not enabled") + def test_localized_priority(self): + "The priority value should not be localized (Refs #14164)" + # Localization should be active + settings.USE_L10N = True + activate('fr') + self.assertEqual(u'0,3', localize(0.3)) + + # Retrieve the sitemap. Check that priorities + # haven't been rendered in localized format + response = self.client.get('/simple/sitemap.xml') + self.assertContains(response, '0.5') + self.assertContains(response, '%s' % date.today()) + deactivate() + + def test_requestsite_sitemap(self): + # Make sure hitting the flatpages sitemap without the sites framework + # installed doesn't raise an exception + Site._meta.installed = False + response = self.client.get('/simple/sitemap.xml') + self.assertEqual(response.content, """ + +http://testserver/location/%snever0.5 + +""" % date.today()) + + @skipUnless("django.contrib.sites" in settings.INSTALLED_APPS, + "django.contrib.sites app not installed.") + def test_sitemap_get_urls_no_site_1(self): + """ + Check we get ImproperlyConfigured if we don't pass a site object to + Sitemap.get_urls and no Site objects exist + """ + Site.objects.all().delete() + self.assertRaises(ImproperlyConfigured, Sitemap().get_urls) + + def test_sitemap_get_urls_no_site_2(self): + """ + Check we get ImproperlyConfigured when we don't pass a site object to + Sitemap.get_urls if Site objects exists, but the sites framework is not + actually installed. + """ + Site._meta.installed = False + self.assertRaises(ImproperlyConfigured, Sitemap().get_urls) + + def test_sitemap_item(self): + """ + Check to make sure that the raw item is included with each + Sitemap.get_url() url result. + """ + user_sitemap = GenericSitemap({'queryset': User.objects.all()}) + def is_user(url): + return isinstance(url['item'], User) + item_in_url_info = all(map(is_user, user_sitemap.get_urls())) + self.assertTrue(item_in_url_info) + + def test_cached_sitemap_index(self): + """ + Check that a cached sitemap index can be rendered (#2713). + """ + response = self.client.get('/cached/index.xml') + self.assertEqual(response.content, """ + +%s/cached/sitemap-simple.xml + +""" % self.base_url) diff --git a/django/contrib/sitemaps/tests/https.py b/django/contrib/sitemaps/tests/https.py new file mode 100644 index 0000000000..b590bf2215 --- /dev/null +++ b/django/contrib/sitemaps/tests/https.py @@ -0,0 +1,51 @@ +from datetime import date + +from django.test.utils import override_settings + +from .base import SitemapTestsBase + +class HTTPSSitemapTests(SitemapTestsBase): + protocol = 'https' + urls = 'django.contrib.sitemaps.tests.urls.https' + + def test_secure_sitemap_index(self): + "A secure sitemap index can be rendered" + response = self.client.get('/secure/index.xml') + self.assertEqual(response.content, """ + +%s/secure/sitemap-simple.xml + +""" % self.base_url) + + def test_secure_sitemap_section(self): + "A secure sitemap section can be rendered" + response = self.client.get('/secure/sitemap-simple.xml') + self.assertEqual(response.content, """ + +%s/location/%snever0.5 + +""" % (self.base_url, date.today())) + +#@override_settings(SECURE_PROXY_SSL_HEADER=False) +class HTTPSDetectionSitemapTests(SitemapTestsBase): + extra = {'wsgi.url_scheme': 'https'} + + def test_sitemap_index_with_https_request(self): + "A sitemap index requested in HTTPS is rendered with HTTPS links" + response = self.client.get('/simple/index.xml', **self.extra) + self.assertEqual(response.content, """ + +%s/simple/sitemap-simple.xml + +""" % self.base_url.replace('http://', 'https://')) + + def test_sitemap_section_with_https_request(self): + "A sitemap section requested in HTTPS is rendered with HTTPS links" + response = self.client.get('/simple/sitemap-simple.xml', **self.extra) + self.assertEqual(response.content, """ + +%s/location/%snever0.5 + +""" % (self.base_url.replace('http://', 'https://'), date.today())) + +HTTPSDetectionSitemapTests = override_settings(SECURE_PROXY_SSL_HEADER=False)(HTTPSDetectionSitemapTests) diff --git a/django/contrib/sitemaps/tests/urls.py b/django/contrib/sitemaps/tests/urls.py deleted file mode 100644 index d9ff7200d3..0000000000 --- a/django/contrib/sitemaps/tests/urls.py +++ /dev/null @@ -1,45 +0,0 @@ -from datetime import datetime -from django.conf.urls import patterns, url -from django.contrib.sitemaps import Sitemap, GenericSitemap, FlatPageSitemap, views -from django.contrib.auth.models import User -from django.views.decorators.cache import cache_page - -class SimpleSitemap(Sitemap): - changefreq = "never" - priority = 0.5 - location = '/location/' - lastmod = datetime.now() - - def items(self): - return [object()] - -simple_sitemaps = { - 'simple': SimpleSitemap, -} - -generic_sitemaps = { - 'generic': GenericSitemap({ - 'queryset': User.objects.all() - }), -} - -flatpage_sitemaps = { - 'flatpages': FlatPageSitemap, -} - -urlpatterns = patterns('django.contrib.sitemaps.views', - (r'^simple/index\.xml$', 'index', {'sitemaps': simple_sitemaps}), - (r'^simple/custom-index\.xml$', 'index', - {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap_index.xml'}), - (r'^simple/sitemap-(?P
.+)\.xml$', 'sitemap', - {'sitemaps': simple_sitemaps}), - (r'^simple/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}), - (r'^simple/custom-sitemap\.xml$', 'sitemap', - {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap.xml'}), - (r'^generic/sitemap\.xml$', 'sitemap', {'sitemaps': generic_sitemaps}), - (r'^flatpages/sitemap\.xml$', 'sitemap', {'sitemaps': flatpage_sitemaps}), - url(r'^cached/index\.xml$', cache_page(1)(views.index), - {'sitemaps': simple_sitemaps, 'sitemap_url_name': 'cached_sitemap'}), - url(r'^cached/sitemap-(?P
.+)\.xml', cache_page(1)(views.sitemap), - {'sitemaps': simple_sitemaps}, name='cached_sitemap') -) diff --git a/django/contrib/sitemaps/tests/urls/__init__.py b/django/contrib/sitemaps/tests/urls/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django/contrib/sitemaps/tests/urls/http.py b/django/contrib/sitemaps/tests/urls/http.py new file mode 100644 index 0000000000..018e46a482 --- /dev/null +++ b/django/contrib/sitemaps/tests/urls/http.py @@ -0,0 +1,43 @@ +from datetime import datetime +from django.conf.urls import patterns, url +from django.contrib.sitemaps import Sitemap, GenericSitemap, FlatPageSitemap, views +from django.contrib.auth.models import User +from django.views.decorators.cache import cache_page + +class SimpleSitemap(Sitemap): + changefreq = "never" + priority = 0.5 + location = '/location/' + lastmod = datetime.now() + + def items(self): + return [object()] + +simple_sitemaps = { + 'simple': SimpleSitemap, +} + +generic_sitemaps = { + 'generic': GenericSitemap({'queryset': User.objects.all()}), +} + +flatpage_sitemaps = { + 'flatpages': FlatPageSitemap, +} + +urlpatterns = patterns('django.contrib.sitemaps.views', + (r'^simple/index\.xml$', 'index', {'sitemaps': simple_sitemaps}), + (r'^simple/custom-index\.xml$', 'index', + {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap_index.xml'}), + (r'^simple/sitemap-(?P
.+)\.xml$', 'sitemap', + {'sitemaps': simple_sitemaps}), + (r'^simple/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}), + (r'^simple/custom-sitemap\.xml$', 'sitemap', + {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap.xml'}), + (r'^generic/sitemap\.xml$', 'sitemap', {'sitemaps': generic_sitemaps}), + (r'^flatpages/sitemap\.xml$', 'sitemap', {'sitemaps': flatpage_sitemaps}), + url(r'^cached/index\.xml$', cache_page(1)(views.index), + {'sitemaps': simple_sitemaps, 'sitemap_url_name': 'cached_sitemap'}), + url(r'^cached/sitemap-(?P
.+)\.xml', cache_page(1)(views.sitemap), + {'sitemaps': simple_sitemaps}, name='cached_sitemap') +) diff --git a/django/contrib/sitemaps/tests/urls/https.py b/django/contrib/sitemaps/tests/urls/https.py new file mode 100644 index 0000000000..a1b4b939ae --- /dev/null +++ b/django/contrib/sitemaps/tests/urls/https.py @@ -0,0 +1,16 @@ +from django.conf.urls import patterns + +from .http import SimpleSitemap + +class HTTPSSitemap(SimpleSitemap): + protocol = 'https' + +secure_sitemaps = { + 'simple': HTTPSSitemap, +} + +urlpatterns = patterns('django.contrib.sitemaps.views', + (r'^secure/index\.xml$', 'index', {'sitemaps': secure_sitemaps}), + (r'^secure/sitemap-(?P
.+)\.xml$', 'sitemap', + {'sitemaps': secure_sitemaps}), +) -- cgit v1.2.1