2017-02-25 20:54:17 +08:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from django.contrib.sitemaps import GenericSitemap
|
2021-05-15 14:11:14 +08:00
|
|
|
from django.test import ignore_warnings, override_settings
|
|
|
|
from django.utils.deprecation import RemovedInDjango50Warning
|
2012-01-30 03:24:32 +08:00
|
|
|
|
2015-02-10 21:11:25 +08:00
|
|
|
from .base import SitemapTestsBase
|
|
|
|
from .models import TestModel
|
2013-03-23 09:57:48 +08:00
|
|
|
|
2012-01-30 03:24:32 +08:00
|
|
|
|
2012-07-01 00:49:11 +08:00
|
|
|
@override_settings(ABSOLUTE_URL_OVERRIDES={})
|
2012-01-30 03:24:32 +08:00
|
|
|
class GenericViewsSitemapTests(SitemapTestsBase):
|
2017-02-25 20:54:17 +08:00
|
|
|
def test_generic_sitemap_attributes(self):
|
|
|
|
datetime_value = datetime.now()
|
|
|
|
queryset = TestModel.objects.all()
|
|
|
|
generic_sitemap = GenericSitemap(
|
|
|
|
info_dict={
|
|
|
|
"queryset": queryset,
|
|
|
|
"date_field": datetime_value,
|
|
|
|
},
|
|
|
|
priority=0.6,
|
|
|
|
changefreq="monthly",
|
2017-02-15 17:36:18 +08:00
|
|
|
protocol="https",
|
2017-02-25 20:54:17 +08:00
|
|
|
)
|
|
|
|
attr_values = (
|
|
|
|
("date_field", datetime_value),
|
|
|
|
("priority", 0.6),
|
|
|
|
("changefreq", "monthly"),
|
2017-02-15 17:36:18 +08:00
|
|
|
("protocol", "https"),
|
2017-02-25 20:54:17 +08:00
|
|
|
)
|
|
|
|
for attr_name, expected_value in attr_values:
|
|
|
|
with self.subTest(attr_name=attr_name):
|
|
|
|
self.assertEqual(getattr(generic_sitemap, attr_name), expected_value)
|
|
|
|
self.assertCountEqual(generic_sitemap.queryset, queryset)
|
|
|
|
|
2012-01-30 03:24:32 +08:00
|
|
|
def test_generic_sitemap(self):
|
|
|
|
"A minimal generic sitemap can be rendered"
|
|
|
|
response = self.client.get("/generic/sitemap.xml")
|
|
|
|
expected = ""
|
2013-03-23 09:57:48 +08:00
|
|
|
for pk in TestModel.objects.values_list("id", flat=True):
|
|
|
|
expected += "<url><loc>%s/testmodel/%s/</loc></url>" % (self.base_url, pk)
|
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
|
|
|
"%s\n"
|
|
|
|
"</urlset>"
|
2012-08-15 02:12:38 +08:00
|
|
|
) % expected
|
2017-02-08 01:05:47 +08:00
|
|
|
self.assertXMLEqual(response.content.decode(), expected_content)
|
2017-03-18 03:25:50 +08:00
|
|
|
|
|
|
|
def test_generic_sitemap_lastmod(self):
|
|
|
|
test_model = TestModel.objects.first()
|
|
|
|
TestModel.objects.update(lastmod=datetime(2013, 3, 13, 10, 0, 0))
|
|
|
|
response = self.client.get("/generic-lastmod/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'
|
2017-03-18 03:25:50 +08:00
|
|
|
"<url><loc>%s/testmodel/%s/</loc><lastmod>2013-03-13</lastmod></url>\n"
|
|
|
|
"</urlset>"
|
|
|
|
) % (
|
|
|
|
self.base_url,
|
|
|
|
test_model.pk,
|
|
|
|
)
|
|
|
|
self.assertXMLEqual(response.content.decode(), expected_content)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(
|
|
|
|
response.headers["Last-Modified"], "Wed, 13 Mar 2013 10:00:00 GMT"
|
|
|
|
)
|
2021-05-21 16:51:17 +08:00
|
|
|
|
|
|
|
def test_get_protocol_defined_in_constructor(self):
|
|
|
|
for protocol in ["http", "https"]:
|
|
|
|
with self.subTest(protocol=protocol):
|
|
|
|
sitemap = GenericSitemap({"queryset": None}, protocol=protocol)
|
|
|
|
self.assertEqual(sitemap.get_protocol(), protocol)
|
|
|
|
|
|
|
|
def test_get_protocol_passed_as_argument(self):
|
|
|
|
sitemap = GenericSitemap({"queryset": None})
|
|
|
|
for protocol in ["http", "https"]:
|
|
|
|
with self.subTest(protocol=protocol):
|
|
|
|
self.assertEqual(sitemap.get_protocol(protocol), protocol)
|
|
|
|
|
2021-05-15 14:11:14 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango50Warning)
|
2021-05-21 16:51:17 +08:00
|
|
|
def test_get_protocol_default(self):
|
|
|
|
sitemap = GenericSitemap({"queryset": None})
|
|
|
|
self.assertEqual(sitemap.get_protocol(), "http")
|
2021-05-15 14:11:14 +08:00
|
|
|
|
|
|
|
def test_get_protocol_default_warning(self):
|
|
|
|
sitemap = GenericSitemap({"queryset": None})
|
|
|
|
msg = (
|
|
|
|
"The default sitemap protocol will be changed from 'http' to "
|
|
|
|
"'https' in Django 5.0. Set Sitemap.protocol to silence this "
|
|
|
|
"warning."
|
|
|
|
)
|
|
|
|
with self.assertWarnsMessage(RemovedInDjango50Warning, msg):
|
|
|
|
sitemap.get_protocol()
|
2020-12-31 00:44:53 +08:00
|
|
|
|
|
|
|
def test_generic_sitemap_index(self):
|
|
|
|
TestModel.objects.update(lastmod=datetime(2013, 3, 13, 10, 0, 0))
|
|
|
|
response = self.client.get("/generic-lastmod/index.xml")
|
|
|
|
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
|
|
<sitemap><loc>http://example.com/simple/sitemap-generic.xml</loc><lastmod>2013-03-13T10:00:00</lastmod></sitemap>
|
|
|
|
</sitemapindex>"""
|
|
|
|
self.assertXMLEqual(response.content.decode("utf-8"), expected_content)
|