summaryrefslogtreecommitdiff
path: root/django/contrib/sitemaps/tests
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2012-01-29 19:24:32 +0000
committerAymeric Augustin <aymeric.augustin@m4x.org>2012-01-29 19:24:32 +0000
commita4b472dd809c8a696d1488a54dcaf3870ec9e661 (patch)
tree1338453f7de1c9802a571cabf353af364559370f /django/contrib/sitemaps/tests
parent123f567093eb3bd2f9cb295f4553eb62433c2962 (diff)
downloaddjango-a4b472dd809c8a696d1488a54dcaf3870ec9e661.tar.gz
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
Diffstat (limited to 'django/contrib/sitemaps/tests')
-rw-r--r--django/contrib/sitemaps/tests/__init__.py5
-rw-r--r--django/contrib/sitemaps/tests/base.py28
-rw-r--r--django/contrib/sitemaps/tests/flatpages.py37
-rw-r--r--django/contrib/sitemaps/tests/generic.py17
-rw-r--r--django/contrib/sitemaps/tests/http.py (renamed from django/contrib/sitemaps/tests/basic.py)81
-rw-r--r--django/contrib/sitemaps/tests/https.py51
-rw-r--r--django/contrib/sitemaps/tests/urls/__init__.py0
-rw-r--r--django/contrib/sitemaps/tests/urls/http.py (renamed from django/contrib/sitemaps/tests/urls.py)4
-rw-r--r--django/contrib/sitemaps/tests/urls/https.py16
9 files changed, 167 insertions, 72 deletions
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/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, '<loc>%s%s</loc>' % (self.base_url, public.url))
+ # Private flatpage should not be in the sitemap
+ self.assertNotContains(response, '<loc>%s%s</loc>' % (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 += "<url><loc>%s/users/%s/</loc></url>" % (self.base_url, username)
+ self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
+<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+%s
+</urlset>
+""" % expected)
diff --git a/django/contrib/sitemaps/tests/basic.py b/django/contrib/sitemaps/tests/http.py
index b675c3d6fb..3b56aa8d53 100644
--- a/django/contrib/sitemaps/tests/basic.py
+++ b/django/contrib/sitemaps/tests/http.py
@@ -1,39 +1,16 @@
-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
+from .base import SitemapTestsBase
-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
+class HTTPSitemapTests(SitemapTestsBase):
def test_simple_sitemap_index(self):
"A simple sitemap index can be rendered"
@@ -54,6 +31,15 @@ class SitemapTests(TestCase):
</sitemapindex>
""" % 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, """<?xml version="1.0" encoding="UTF-8"?>
+<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
+</urlset>
+""" % (self.base_url, date.today()))
+
def test_simple_sitemap(self):
"A simple sitemap can be rendered"
response = self.client.get('/simple/sitemap.xml')
@@ -88,48 +74,6 @@ class SitemapTests(TestCase):
self.assertContains(response, '<lastmod>%s</lastmod>' % 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 += "<url><loc>%s/users/%s/</loc></url>" % (self.base_url, username)
- self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
-<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
-%s
-</urlset>
-""" % 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, '<loc>%s%s</loc>' % (self.base_url, public.url))
- # Private flatpage should not be in the sitemap
- self.assertNotContains(response, '<loc>%s%s</loc>' % (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
@@ -141,7 +85,8 @@ class SitemapTests(TestCase):
</urlset>
""" % date.today())
- @skipUnless("django.contrib.sites" in settings.INSTALLED_APPS, "django.contrib.sites app not installed.")
+ @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
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, """<?xml version="1.0" encoding="UTF-8"?>
+<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+<sitemap><loc>%s/secure/sitemap-simple.xml</loc></sitemap>
+</sitemapindex>
+""" % 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, """<?xml version="1.0" encoding="UTF-8"?>
+<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
+</urlset>
+""" % (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, """<?xml version="1.0" encoding="UTF-8"?>
+<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+<sitemap><loc>%s/simple/sitemap-simple.xml</loc></sitemap>
+</sitemapindex>
+""" % 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, """<?xml version="1.0" encoding="UTF-8"?>
+<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
+</urlset>
+""" % (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/__init__.py b/django/contrib/sitemaps/tests/urls/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/django/contrib/sitemaps/tests/urls/__init__.py
diff --git a/django/contrib/sitemaps/tests/urls.py b/django/contrib/sitemaps/tests/urls/http.py
index d9ff7200d3..018e46a482 100644
--- a/django/contrib/sitemaps/tests/urls.py
+++ b/django/contrib/sitemaps/tests/urls/http.py
@@ -18,9 +18,7 @@ simple_sitemaps = {
}
generic_sitemaps = {
- 'generic': GenericSitemap({
- 'queryset': User.objects.all()
- }),
+ 'generic': GenericSitemap({'queryset': User.objects.all()}),
}
flatpage_sitemaps = {
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<section>.+)\.xml$', 'sitemap',
+ {'sitemaps': secure_sitemaps}),
+)