2012-06-03 03:24:18 +08:00
|
|
|
import os
|
2010-08-27 23:16:04 +08:00
|
|
|
from datetime import date
|
2012-06-03 03:24:18 +08:00
|
|
|
|
2017-02-25 20:54:17 +08:00
|
|
|
from django.contrib.sitemaps import Sitemap
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
from django.contrib.sites.models import Site
|
2010-10-11 22:34:42 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2023-01-12 19:34:42 +08:00
|
|
|
from django.test import modify_settings, override_settings
|
2020-05-23 20:58:17 +08:00
|
|
|
from django.utils import translation
|
2010-08-27 23:16:04 +08:00
|
|
|
from django.utils.formats import localize
|
|
|
|
|
2015-02-10 21:11:25 +08:00
|
|
|
from .base import SitemapTestsBase
|
2022-10-21 23:36:44 +08:00
|
|
|
from .models import I18nTestModel, TestModel
|
2010-08-27 23:16:04 +08:00
|
|
|
|
2012-06-03 03:24:18 +08:00
|
|
|
|
2012-01-30 03:24:32 +08:00
|
|
|
class HTTPSitemapTests(SitemapTestsBase):
|
2017-05-29 03:37:21 +08:00
|
|
|
use_sitemap_err_msg = (
|
|
|
|
"To use sitemaps, either enable the sites framework or pass a "
|
|
|
|
"Site/RequestSite object in your view."
|
|
|
|
)
|
2010-12-13 06:56:29 +08:00
|
|
|
|
|
|
|
def test_simple_sitemap_index(self):
|
|
|
|
"A simple sitemap index can be rendered"
|
2014-12-22 04:19:05 +08:00
|
|
|
response = self.client.get("/simple/index.xml")
|
2012-08-15 02:12:38 +08:00
|
|
|
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
2010-12-13 06:56:29 +08:00
|
|
|
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
2020-12-31 00:44:53 +08:00
|
|
|
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod></sitemap>
|
2010-12-13 06:56:29 +08:00
|
|
|
</sitemapindex>
|
2020-12-31 00:44:53 +08:00
|
|
|
""" % (
|
|
|
|
self.base_url,
|
|
|
|
date.today(),
|
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2010-12-13 06:56:29 +08:00
|
|
|
|
2017-03-18 03:25:50 +08:00
|
|
|
def test_sitemap_not_callable(self):
|
|
|
|
"""A sitemap may not be callable."""
|
|
|
|
response = self.client.get("/simple-not-callable/index.xml")
|
|
|
|
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
2020-12-31 00:44:53 +08:00
|
|
|
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod></sitemap>
|
2017-03-18 03:25:50 +08:00
|
|
|
</sitemapindex>
|
2020-12-31 00:44:53 +08:00
|
|
|
""" % (
|
|
|
|
self.base_url,
|
|
|
|
date.today(),
|
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2017-03-18 03:25:50 +08:00
|
|
|
|
|
|
|
def test_paged_sitemap(self):
|
|
|
|
"""A sitemap may have multiple pages."""
|
|
|
|
response = self.client.get("/simple-paged/index.xml")
|
|
|
|
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
2020-12-31 00:44:53 +08:00
|
|
|
<sitemap><loc>{0}/simple/sitemap-simple.xml</loc><lastmod>{1}</lastmod></sitemap><sitemap><loc>{0}/simple/sitemap-simple.xml?p=2</loc><lastmod>{1}</lastmod></sitemap>
|
2017-03-18 03:25:50 +08:00
|
|
|
</sitemapindex>
|
2020-12-31 00:44:53 +08:00
|
|
|
""".format(
|
|
|
|
self.base_url, date.today()
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2017-03-18 03:25:50 +08:00
|
|
|
|
2014-12-18 05:51:42 +08:00
|
|
|
@override_settings(
|
|
|
|
TEMPLATES=[
|
|
|
|
{
|
|
|
|
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
2017-01-20 21:01:02 +08:00
|
|
|
"DIRS": [os.path.join(os.path.dirname(__file__), "templates")],
|
2014-12-18 05:51:42 +08:00
|
|
|
}
|
2022-02-04 03:24:19 +08:00
|
|
|
]
|
2014-12-18 05:51:42 +08:00
|
|
|
)
|
2020-12-31 00:44:53 +08:00
|
|
|
def test_simple_sitemap_custom_lastmod_index(self):
|
2010-12-13 06:56:29 +08:00
|
|
|
"A simple sitemap index can be rendered with a custom template"
|
2020-12-31 00:44:53 +08:00
|
|
|
response = self.client.get("/simple/custom-lastmod-index.xml")
|
2012-08-15 02:12:38 +08:00
|
|
|
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
2010-12-13 06:56:29 +08:00
|
|
|
<!-- This is a customised template -->
|
|
|
|
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
2020-12-31 00:44:53 +08:00
|
|
|
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod></sitemap>
|
2010-12-13 06:56:29 +08:00
|
|
|
</sitemapindex>
|
2020-12-31 00:44:53 +08:00
|
|
|
""" % (
|
|
|
|
self.base_url,
|
|
|
|
date.today(),
|
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2012-08-15 02:12:38 +08:00
|
|
|
|
2012-01-30 03:24:32 +08:00
|
|
|
def test_simple_sitemap_section(self):
|
|
|
|
"A simple sitemap section can be rendered"
|
|
|
|
response = self.client.get("/simple/sitemap-simple.xml")
|
2012-08-15 02:12:38 +08:00
|
|
|
expected_content = (
|
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>\n'
|
2020-07-29 16:30:55 +08:00
|
|
|
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
|
|
|
|
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
|
2012-01-30 03:24:32 +08:00
|
|
|
"<url><loc>%s/location/</loc><lastmod>%s</lastmod>"
|
|
|
|
"<changefreq>never</changefreq><priority>0.5</priority></url>\n"
|
|
|
|
"</urlset>"
|
2012-08-15 02:12:38 +08:00
|
|
|
) % (
|
|
|
|
self.base_url,
|
|
|
|
date.today(),
|
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2012-01-30 03:24:32 +08:00
|
|
|
|
2017-03-18 03:25:50 +08:00
|
|
|
def test_no_section(self):
|
|
|
|
response = self.client.get("/simple/sitemap-simple2.xml")
|
|
|
|
self.assertEqual(
|
|
|
|
str(response.context["exception"]),
|
|
|
|
"No sitemap available for section: 'simple2'",
|
|
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
|
|
def test_empty_page(self):
|
|
|
|
response = self.client.get("/simple/sitemap-simple.xml?p=0")
|
|
|
|
self.assertEqual(str(response.context["exception"]), "Page 0 empty")
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
|
|
def test_page_not_int(self):
|
|
|
|
response = self.client.get("/simple/sitemap-simple.xml?p=test")
|
|
|
|
self.assertEqual(str(response.context["exception"]), "No page 'test'")
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
2010-08-27 23:16:04 +08:00
|
|
|
def test_simple_sitemap(self):
|
|
|
|
"A simple sitemap can be rendered"
|
2010-08-30 23:09:12 +08:00
|
|
|
response = self.client.get("/simple/sitemap.xml")
|
2012-08-15 02:12:38 +08:00
|
|
|
expected_content = (
|
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>\n'
|
2020-07-29 16:30:55 +08:00
|
|
|
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
|
|
|
|
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
|
2011-02-05 11:49:03 +08:00
|
|
|
"<url><loc>%s/location/</loc><lastmod>%s</lastmod>"
|
|
|
|
"<changefreq>never</changefreq><priority>0.5</priority></url>\n"
|
2010-12-13 06:56:29 +08:00
|
|
|
"</urlset>"
|
2012-08-15 02:12:38 +08:00
|
|
|
) % (
|
|
|
|
self.base_url,
|
|
|
|
date.today(),
|
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2010-12-13 06:56:29 +08:00
|
|
|
|
2014-12-18 05:51:42 +08:00
|
|
|
@override_settings(
|
|
|
|
TEMPLATES=[
|
|
|
|
{
|
|
|
|
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
2017-01-20 21:01:02 +08:00
|
|
|
"DIRS": [os.path.join(os.path.dirname(__file__), "templates")],
|
2014-12-18 05:51:42 +08:00
|
|
|
}
|
2022-02-04 03:24:19 +08:00
|
|
|
]
|
2014-12-18 05:51:42 +08:00
|
|
|
)
|
2010-12-13 06:56:29 +08:00
|
|
|
def test_simple_custom_sitemap(self):
|
|
|
|
"A simple sitemap can be rendered with a custom template"
|
|
|
|
response = self.client.get("/simple/custom-sitemap.xml")
|
2012-08-15 02:12:38 +08:00
|
|
|
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
2010-12-13 06:56:29 +08:00
|
|
|
<!-- This is a customised template -->
|
|
|
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
2011-02-05 11:49:03 +08:00
|
|
|
<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
|
2010-08-27 23:16:04 +08:00
|
|
|
</urlset>
|
2012-08-15 02:12:38 +08:00
|
|
|
""" % (
|
|
|
|
self.base_url,
|
|
|
|
date.today(),
|
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2010-08-27 23:16:04 +08:00
|
|
|
|
2013-07-23 22:25:21 +08:00
|
|
|
def test_sitemap_last_modified(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
"Last-Modified header is set correctly"
|
2013-07-23 22:25:21 +08:00
|
|
|
response = self.client.get("/lastmod/sitemap.xml")
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(
|
|
|
|
response.headers["Last-Modified"], "Wed, 13 Mar 2013 10:00:00 GMT"
|
|
|
|
)
|
2013-07-23 22:25:21 +08:00
|
|
|
|
2014-09-10 01:34:40 +08:00
|
|
|
def test_sitemap_last_modified_date(self):
|
|
|
|
"""
|
|
|
|
The Last-Modified header should be support dates (without time).
|
|
|
|
"""
|
|
|
|
response = self.client.get("/lastmod/date-sitemap.xml")
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(
|
|
|
|
response.headers["Last-Modified"], "Wed, 13 Mar 2013 00:00:00 GMT"
|
|
|
|
)
|
2014-09-10 01:34:40 +08:00
|
|
|
|
|
|
|
def test_sitemap_last_modified_tz(self):
|
|
|
|
"""
|
|
|
|
The Last-Modified header should be converted from timezone aware dates
|
|
|
|
to GMT.
|
|
|
|
"""
|
|
|
|
response = self.client.get("/lastmod/tz-sitemap.xml")
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(
|
|
|
|
response.headers["Last-Modified"], "Wed, 13 Mar 2013 15:00:00 GMT"
|
|
|
|
)
|
2014-09-10 01:34:40 +08:00
|
|
|
|
2013-07-23 22:25:21 +08:00
|
|
|
def test_sitemap_last_modified_missing(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
"Last-Modified header is missing when sitemap has no lastmod"
|
2013-07-23 22:25:21 +08:00
|
|
|
response = self.client.get("/generic/sitemap.xml")
|
|
|
|
self.assertFalse(response.has_header("Last-Modified"))
|
|
|
|
|
|
|
|
def test_sitemap_last_modified_mixed(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
"Last-Modified header is omitted when lastmod not on all items"
|
2013-07-23 22:25:21 +08:00
|
|
|
response = self.client.get("/lastmod-mixed/sitemap.xml")
|
|
|
|
self.assertFalse(response.has_header("Last-Modified"))
|
|
|
|
|
2015-12-27 02:01:25 +08:00
|
|
|
def test_sitemaps_lastmod_mixed_ascending_last_modified_missing(self):
|
|
|
|
"""
|
|
|
|
The Last-Modified header is omitted when lastmod isn't found in all
|
|
|
|
sitemaps. Test sitemaps are sorted by lastmod in ascending order.
|
|
|
|
"""
|
|
|
|
response = self.client.get("/lastmod-sitemaps/mixed-ascending.xml")
|
|
|
|
self.assertFalse(response.has_header("Last-Modified"))
|
|
|
|
|
|
|
|
def test_sitemaps_lastmod_mixed_descending_last_modified_missing(self):
|
|
|
|
"""
|
|
|
|
The Last-Modified header is omitted when lastmod isn't found in all
|
|
|
|
sitemaps. Test sitemaps are sorted by lastmod in descending order.
|
|
|
|
"""
|
|
|
|
response = self.client.get("/lastmod-sitemaps/mixed-descending.xml")
|
|
|
|
self.assertFalse(response.has_header("Last-Modified"))
|
|
|
|
|
|
|
|
def test_sitemaps_lastmod_ascending(self):
|
|
|
|
"""
|
|
|
|
The Last-Modified header is set to the most recent sitemap lastmod.
|
|
|
|
Test sitemaps are sorted by lastmod in ascending order.
|
|
|
|
"""
|
|
|
|
response = self.client.get("/lastmod-sitemaps/ascending.xml")
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(
|
|
|
|
response.headers["Last-Modified"], "Sat, 20 Apr 2013 05:00:00 GMT"
|
|
|
|
)
|
2015-12-27 02:01:25 +08:00
|
|
|
|
|
|
|
def test_sitemaps_lastmod_descending(self):
|
|
|
|
"""
|
|
|
|
The Last-Modified header is set to the most recent sitemap lastmod.
|
|
|
|
Test sitemaps are sorted by lastmod in descending order.
|
|
|
|
"""
|
|
|
|
response = self.client.get("/lastmod-sitemaps/descending.xml")
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(
|
|
|
|
response.headers["Last-Modified"], "Sat, 20 Apr 2013 05:00:00 GMT"
|
|
|
|
)
|
2015-12-27 02:01:25 +08:00
|
|
|
|
2020-12-31 00:44:53 +08:00
|
|
|
def test_sitemap_get_latest_lastmod_none(self):
|
|
|
|
"""
|
2022-07-11 14:25:04 +08:00
|
|
|
sitemapindex.lastmod is omitted when Sitemap.lastmod is
|
2020-12-31 00:44:53 +08:00
|
|
|
callable and Sitemap.get_latest_lastmod is not implemented
|
|
|
|
"""
|
|
|
|
response = self.client.get("/lastmod/get-latest-lastmod-none-sitemap.xml")
|
|
|
|
self.assertNotContains(response, "<lastmod>")
|
|
|
|
|
|
|
|
def test_sitemap_get_latest_lastmod(self):
|
|
|
|
"""
|
|
|
|
sitemapindex.lastmod is included when Sitemap.lastmod is
|
|
|
|
attribute and Sitemap.get_latest_lastmod is implemented
|
|
|
|
"""
|
|
|
|
response = self.client.get("/lastmod/get-latest-lastmod-sitemap.xml")
|
|
|
|
self.assertContains(response, "<lastmod>2013-03-13T10:00:00</lastmod>")
|
|
|
|
|
|
|
|
def test_sitemap_latest_lastmod_timezone(self):
|
|
|
|
"""
|
|
|
|
lastmod datestamp shows timezones if Sitemap.get_latest_lastmod
|
|
|
|
returns an aware datetime.
|
|
|
|
"""
|
|
|
|
response = self.client.get("/lastmod/latest-lastmod-timezone-sitemap.xml")
|
|
|
|
self.assertContains(response, "<lastmod>2013-03-13T10:00:00-05:00</lastmod>")
|
|
|
|
|
2010-08-27 23:16:04 +08:00
|
|
|
def test_localized_priority(self):
|
2020-05-23 20:58:17 +08:00
|
|
|
"""The priority value should not be localized."""
|
|
|
|
with translation.override("fr"):
|
|
|
|
self.assertEqual("0,3", localize(0.3))
|
|
|
|
# Priorities aren't rendered in localized format.
|
|
|
|
response = self.client.get("/simple/sitemap.xml")
|
|
|
|
self.assertContains(response, "<priority>0.5</priority>")
|
|
|
|
self.assertContains(response, "<lastmod>%s</lastmod>" % date.today())
|
2010-08-30 23:09:12 +08:00
|
|
|
|
2013-12-23 17:37:34 +08:00
|
|
|
@modify_settings(INSTALLED_APPS={"remove": "django.contrib.sites"})
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
def test_requestsite_sitemap(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
# Hitting the flatpages sitemap without the sites framework installed
|
|
|
|
# doesn't raise an exception.
|
2013-12-23 17:37:34 +08:00
|
|
|
response = self.client.get("/simple/sitemap.xml")
|
|
|
|
expected_content = (
|
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>\n'
|
2020-07-29 16:30:55 +08:00
|
|
|
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
|
|
|
|
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
"<url><loc>http://testserver/location/</loc><lastmod>%s</lastmod>"
|
|
|
|
"<changefreq>never</changefreq><priority>0.5</priority></url>\n"
|
|
|
|
"</urlset>"
|
2012-08-15 02:12:38 +08:00
|
|
|
) % date.today()
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2010-10-11 22:34:42 +08:00
|
|
|
|
|
|
|
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()
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, self.use_sitemap_err_msg):
|
2016-01-17 19:26:39 +08:00
|
|
|
Sitemap().get_urls()
|
2010-10-11 22:34:42 +08:00
|
|
|
|
2013-12-23 17:37:34 +08:00
|
|
|
@modify_settings(INSTALLED_APPS={"remove": "django.contrib.sites"})
|
2010-10-11 22:34:42 +08:00
|
|
|
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.
|
|
|
|
"""
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, self.use_sitemap_err_msg):
|
2016-01-17 19:26:39 +08:00
|
|
|
Sitemap().get_urls()
|
2011-06-28 18:16:34 +08:00
|
|
|
|
|
|
|
def test_sitemap_item(self):
|
|
|
|
"""
|
|
|
|
Check to make sure that the raw item is included with each
|
|
|
|
Sitemap.get_url() url result.
|
|
|
|
"""
|
2017-02-25 20:54:17 +08:00
|
|
|
test_sitemap = Sitemap()
|
|
|
|
test_sitemap.items = TestModel.objects.order_by("pk").all
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-03-23 09:57:48 +08:00
|
|
|
def is_testmodel(url):
|
|
|
|
return isinstance(url["item"], TestModel)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2013-03-23 09:57:48 +08:00
|
|
|
item_in_url_info = all(map(is_testmodel, test_sitemap.get_urls()))
|
2011-06-28 18:16:34 +08:00
|
|
|
self.assertTrue(item_in_url_info)
|
2012-01-29 17:30:28 +08:00
|
|
|
|
|
|
|
def test_cached_sitemap_index(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
A cached sitemap index can be rendered (#2713).
|
2012-01-29 17:30:28 +08:00
|
|
|
"""
|
|
|
|
response = self.client.get("/cached/index.xml")
|
2012-08-15 02:12:38 +08:00
|
|
|
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
2012-01-29 17:30:28 +08:00
|
|
|
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
2020-12-31 00:44:53 +08:00
|
|
|
<sitemap><loc>%s/cached/sitemap-simple.xml</loc><lastmod>%s</lastmod></sitemap>
|
2012-01-29 17:30:28 +08:00
|
|
|
</sitemapindex>
|
2020-12-31 00:44:53 +08:00
|
|
|
""" % (
|
|
|
|
self.base_url,
|
|
|
|
date.today(),
|
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2013-05-04 18:08:15 +08:00
|
|
|
|
|
|
|
def test_x_robots_sitemap(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
response = self.client.get("/simple/index.xml")
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(response.headers["X-Robots-Tag"], "noindex, noodp, noarchive")
|
2013-05-04 18:08:15 +08:00
|
|
|
|
|
|
|
response = self.client.get("/simple/sitemap.xml")
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(response.headers["X-Robots-Tag"], "noindex, noodp, noarchive")
|
2013-09-17 22:21:11 +08:00
|
|
|
|
|
|
|
def test_empty_sitemap(self):
|
|
|
|
response = self.client.get("/empty/sitemap.xml")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2014-06-07 02:47:15 +08:00
|
|
|
|
|
|
|
@override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese")))
|
2020-07-29 16:33:20 +08:00
|
|
|
def test_simple_i18n_sitemap_index(self):
|
|
|
|
"""
|
2021-04-14 20:50:01 +08:00
|
|
|
A simple i18n sitemap index can be rendered, without logging variable
|
|
|
|
lookup errors.
|
2020-07-29 16:33:20 +08:00
|
|
|
"""
|
2021-04-14 20:50:01 +08:00
|
|
|
with self.assertNoLogs("django.template", "DEBUG"):
|
|
|
|
response = self.client.get("/simple/i18n.xml")
|
2014-06-07 02:47:15 +08:00
|
|
|
expected_content = (
|
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>\n'
|
2020-07-29 16:30:55 +08:00
|
|
|
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
|
|
|
|
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
|
2014-06-21 05:53:18 +08:00
|
|
|
"<url><loc>{0}/en/i18n/testmodel/{1}/</loc><changefreq>never</changefreq>"
|
|
|
|
"<priority>0.5</priority></url><url><loc>{0}/pt/i18n/testmodel/{1}/</loc>"
|
|
|
|
"<changefreq>never</changefreq><priority>0.5</priority></url>\n"
|
2014-06-07 02:47:15 +08:00
|
|
|
"</urlset>"
|
2014-06-21 05:53:18 +08:00
|
|
|
).format(self.base_url, self.i18n_model.pk)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2016-01-25 03:35:46 +08:00
|
|
|
|
2020-07-29 16:33:20 +08:00
|
|
|
@override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese")))
|
|
|
|
def test_alternate_i18n_sitemap_index(self):
|
|
|
|
"""
|
|
|
|
A i18n sitemap with alternate/hreflang links can be rendered.
|
|
|
|
"""
|
|
|
|
response = self.client.get("/alternates/i18n.xml")
|
|
|
|
url, pk = self.base_url, self.i18n_model.pk
|
|
|
|
expected_urls = f"""
|
|
|
|
<url><loc>{url}/en/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority>
|
|
|
|
<xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>
|
|
|
|
<xhtml:link rel="alternate" hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/>
|
|
|
|
</url>
|
|
|
|
<url><loc>{url}/pt/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority>
|
|
|
|
<xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>
|
|
|
|
<xhtml:link rel="alternate" hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/>
|
|
|
|
</url>
|
|
|
|
""".replace(
|
|
|
|
"\n", ""
|
|
|
|
)
|
|
|
|
expected_content = (
|
|
|
|
f'<?xml version="1.0" encoding="UTF-8"?>\n'
|
|
|
|
f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
|
|
|
|
f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
|
|
|
|
f"{expected_urls}\n"
|
|
|
|
f"</urlset>"
|
2022-02-04 15:08:27 +08:00
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2020-07-29 16:33:20 +08:00
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
LANGUAGES=(("en", "English"), ("pt", "Portuguese"), ("es", "Spanish"))
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2020-07-29 16:33:20 +08:00
|
|
|
def test_alternate_i18n_sitemap_limited(self):
|
|
|
|
"""
|
|
|
|
A i18n sitemap index with limited languages can be rendered.
|
|
|
|
"""
|
|
|
|
response = self.client.get("/limited/i18n.xml")
|
|
|
|
url, pk = self.base_url, self.i18n_model.pk
|
|
|
|
expected_urls = f"""
|
|
|
|
<url><loc>{url}/en/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority>
|
|
|
|
<xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>
|
|
|
|
<xhtml:link rel="alternate" hreflang="es" href="{url}/es/i18n/testmodel/{pk}/"/>
|
|
|
|
</url>
|
|
|
|
<url><loc>{url}/es/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority>
|
|
|
|
<xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>
|
|
|
|
<xhtml:link rel="alternate" hreflang="es" href="{url}/es/i18n/testmodel/{pk}/"/>
|
|
|
|
</url>
|
|
|
|
""".replace(
|
|
|
|
"\n", ""
|
|
|
|
)
|
|
|
|
expected_content = (
|
|
|
|
f'<?xml version="1.0" encoding="UTF-8"?>\n'
|
|
|
|
f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
|
|
|
|
f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
|
|
|
|
f"{expected_urls}\n"
|
|
|
|
f"</urlset>"
|
2022-02-04 15:08:27 +08:00
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2020-07-29 16:33:20 +08:00
|
|
|
|
|
|
|
@override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese")))
|
|
|
|
def test_alternate_i18n_sitemap_xdefault(self):
|
|
|
|
"""
|
|
|
|
A i18n sitemap index with x-default can be rendered.
|
|
|
|
"""
|
|
|
|
response = self.client.get("/x-default/i18n.xml")
|
|
|
|
url, pk = self.base_url, self.i18n_model.pk
|
|
|
|
expected_urls = f"""
|
|
|
|
<url><loc>{url}/en/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority>
|
|
|
|
<xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>
|
|
|
|
<xhtml:link rel="alternate" hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/>
|
|
|
|
<xhtml:link rel="alternate" hreflang="x-default" href="{url}/i18n/testmodel/{pk}/"/>
|
|
|
|
</url>
|
|
|
|
<url><loc>{url}/pt/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority>
|
|
|
|
<xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>
|
|
|
|
<xhtml:link rel="alternate" hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/>
|
|
|
|
<xhtml:link rel="alternate" hreflang="x-default" href="{url}/i18n/testmodel/{pk}/"/>
|
|
|
|
</url>
|
|
|
|
""".replace(
|
|
|
|
"\n", ""
|
|
|
|
)
|
|
|
|
expected_content = (
|
|
|
|
f'<?xml version="1.0" encoding="UTF-8"?>\n'
|
|
|
|
f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
|
|
|
|
f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
|
|
|
|
f"{expected_urls}\n"
|
|
|
|
f"</urlset>"
|
2022-02-04 15:08:27 +08:00
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2020-07-29 16:33:20 +08:00
|
|
|
|
2022-10-21 23:36:44 +08:00
|
|
|
@override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese")))
|
|
|
|
def test_language_for_item_i18n_sitemap(self):
|
|
|
|
"""
|
|
|
|
A i18n sitemap index in which item can be chosen to be displayed for a
|
|
|
|
lang or not.
|
|
|
|
"""
|
|
|
|
only_pt = I18nTestModel.objects.create(name="Only for PT")
|
|
|
|
response = self.client.get("/item-by-lang/i18n.xml")
|
|
|
|
url, pk, only_pt_pk = self.base_url, self.i18n_model.pk, only_pt.pk
|
|
|
|
expected_urls = (
|
|
|
|
f"<url><loc>{url}/en/i18n/testmodel/{pk}/</loc>"
|
|
|
|
f"<changefreq>never</changefreq><priority>0.5</priority></url>"
|
|
|
|
f"<url><loc>{url}/pt/i18n/testmodel/{pk}/</loc>"
|
|
|
|
f"<changefreq>never</changefreq><priority>0.5</priority></url>"
|
|
|
|
f"<url><loc>{url}/pt/i18n/testmodel/{only_pt_pk}/</loc>"
|
|
|
|
f"<changefreq>never</changefreq><priority>0.5</priority></url>"
|
|
|
|
)
|
|
|
|
expected_content = (
|
|
|
|
f'<?xml version="1.0" encoding="UTF-8"?>\n'
|
|
|
|
f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
|
|
|
|
f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
|
|
|
|
f"{expected_urls}\n"
|
|
|
|
f"</urlset>"
|
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2022-10-21 23:36:44 +08:00
|
|
|
|
|
|
|
@override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese")))
|
|
|
|
def test_alternate_language_for_item_i18n_sitemap(self):
|
|
|
|
"""
|
|
|
|
A i18n sitemap index in which item can be chosen to be displayed for a
|
|
|
|
lang or not.
|
|
|
|
"""
|
|
|
|
only_pt = I18nTestModel.objects.create(name="Only for PT")
|
|
|
|
response = self.client.get("/item-by-lang-alternates/i18n.xml")
|
|
|
|
url, pk, only_pt_pk = self.base_url, self.i18n_model.pk, only_pt.pk
|
|
|
|
expected_urls = (
|
|
|
|
f"<url><loc>{url}/en/i18n/testmodel/{pk}/</loc>"
|
|
|
|
f"<changefreq>never</changefreq><priority>0.5</priority>"
|
|
|
|
f'<xhtml:link rel="alternate" '
|
|
|
|
f'hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>'
|
|
|
|
f'<xhtml:link rel="alternate" '
|
|
|
|
f'hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/>'
|
|
|
|
f'<xhtml:link rel="alternate" '
|
|
|
|
f'hreflang="x-default" href="{url}/i18n/testmodel/{pk}/"/></url>'
|
|
|
|
f"<url><loc>{url}/pt/i18n/testmodel/{pk}/</loc>"
|
|
|
|
f"<changefreq>never</changefreq><priority>0.5</priority>"
|
|
|
|
f'<xhtml:link rel="alternate" '
|
|
|
|
f'hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>'
|
|
|
|
f'<xhtml:link rel="alternate" '
|
|
|
|
f'hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/>'
|
|
|
|
f'<xhtml:link rel="alternate" '
|
|
|
|
f'hreflang="x-default" href="{url}/i18n/testmodel/{pk}/"/></url>'
|
|
|
|
f"<url><loc>{url}/pt/i18n/testmodel/{only_pt_pk}/</loc>"
|
|
|
|
f"<changefreq>never</changefreq><priority>0.5</priority>"
|
|
|
|
f'<xhtml:link rel="alternate" '
|
|
|
|
f'hreflang="pt" href="{url}/pt/i18n/testmodel/{only_pt_pk}/"/></url>'
|
|
|
|
)
|
|
|
|
expected_content = (
|
|
|
|
f'<?xml version="1.0" encoding="UTF-8"?>\n'
|
|
|
|
f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
|
|
|
|
f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
|
|
|
|
f"{expected_urls}\n"
|
|
|
|
f"</urlset>"
|
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2022-10-21 23:36:44 +08:00
|
|
|
|
2016-01-25 03:35:46 +08:00
|
|
|
def test_sitemap_without_entries(self):
|
|
|
|
response = self.client.get("/sitemap-without-entries/sitemap.xml")
|
|
|
|
expected_content = (
|
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>\n'
|
2020-07-29 16:30:55 +08:00
|
|
|
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
|
|
|
|
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n\n'
|
2016-01-25 03:35:46 +08:00
|
|
|
"</urlset>"
|
2022-02-04 15:08:27 +08:00
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(response.text, expected_content)
|
2021-10-10 01:04:05 +08:00
|
|
|
|
|
|
|
def test_callable_sitemod_partial(self):
|
|
|
|
"""
|
|
|
|
Not all items have `lastmod`. Therefore the `Last-Modified` header
|
2020-12-31 00:44:53 +08:00
|
|
|
is not set by the detail or index sitemap view.
|
2021-10-10 01:04:05 +08:00
|
|
|
"""
|
|
|
|
index_response = self.client.get("/callable-lastmod-partial/index.xml")
|
|
|
|
sitemap_response = self.client.get("/callable-lastmod-partial/sitemap.xml")
|
|
|
|
self.assertNotIn("Last-Modified", index_response)
|
|
|
|
self.assertNotIn("Last-Modified", sitemap_response)
|
|
|
|
expected_content_index = """<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
|
|
<sitemap><loc>http://example.com/simple/sitemap-callable-lastmod.xml</loc></sitemap>
|
|
|
|
</sitemapindex>
|
|
|
|
"""
|
|
|
|
expected_content_sitemap = (
|
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>\n'
|
|
|
|
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
|
|
|
|
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
|
|
|
|
"<url><loc>http://example.com/location/</loc>"
|
|
|
|
"<lastmod>2013-03-13</lastmod></url><url>"
|
|
|
|
"<loc>http://example.com/location/</loc></url>\n"
|
|
|
|
"</urlset>"
|
2022-02-04 15:08:27 +08:00
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(index_response.text, expected_content_index)
|
|
|
|
self.assertXMLEqual(sitemap_response.text, expected_content_sitemap)
|
2021-10-10 01:04:05 +08:00
|
|
|
|
|
|
|
def test_callable_sitemod_full(self):
|
|
|
|
"""
|
|
|
|
All items in the sitemap have `lastmod`. The `Last-Modified` header
|
2020-12-31 00:44:53 +08:00
|
|
|
is set for the detail and index sitemap view.
|
2021-10-10 01:04:05 +08:00
|
|
|
"""
|
|
|
|
index_response = self.client.get("/callable-lastmod-full/index.xml")
|
|
|
|
sitemap_response = self.client.get("/callable-lastmod-full/sitemap.xml")
|
2020-12-31 00:44:53 +08:00
|
|
|
self.assertEqual(
|
|
|
|
index_response.headers["Last-Modified"], "Thu, 13 Mar 2014 10:00:00 GMT"
|
|
|
|
)
|
2021-10-10 01:04:05 +08:00
|
|
|
self.assertEqual(
|
|
|
|
sitemap_response.headers["Last-Modified"], "Thu, 13 Mar 2014 10:00:00 GMT"
|
|
|
|
)
|
|
|
|
expected_content_index = """<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
2020-12-31 00:44:53 +08:00
|
|
|
<sitemap><loc>http://example.com/simple/sitemap-callable-lastmod.xml</loc><lastmod>2014-03-13T10:00:00</lastmod></sitemap>
|
2021-10-10 01:04:05 +08:00
|
|
|
</sitemapindex>
|
|
|
|
"""
|
|
|
|
expected_content_sitemap = (
|
|
|
|
'<?xml version="1.0" encoding="UTF-8"?>\n'
|
|
|
|
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '
|
|
|
|
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n'
|
|
|
|
"<url><loc>http://example.com/location/</loc>"
|
|
|
|
"<lastmod>2013-03-13</lastmod></url>"
|
|
|
|
"<url><loc>http://example.com/location/</loc>"
|
|
|
|
"<lastmod>2014-03-13</lastmod></url>\n"
|
|
|
|
"</urlset>"
|
2022-02-04 15:08:27 +08:00
|
|
|
)
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(index_response.text, expected_content_index)
|
|
|
|
self.assertXMLEqual(sitemap_response.text, expected_content_sitemap)
|
2020-12-31 00:44:53 +08:00
|
|
|
|
2022-11-04 21:43:40 +08:00
|
|
|
def test_callable_sitemod_no_items(self):
|
|
|
|
index_response = self.client.get("/callable-lastmod-no-items/index.xml")
|
|
|
|
self.assertNotIn("Last-Modified", index_response)
|
|
|
|
expected_content_index = """<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
|
|
<sitemap><loc>http://example.com/simple/sitemap-callable-lastmod.xml</loc></sitemap>
|
|
|
|
</sitemapindex>
|
|
|
|
"""
|
2024-10-16 00:11:46 +08:00
|
|
|
self.assertXMLEqual(index_response.text, expected_content_index)
|