| 1 |
from datetime import datetime |
| 2 |
|
| 3 |
from django.contrib.sitemaps import GenericSitemap |
| 4 |
from django.test import override_settings |
| 5 |
|
| 6 |
from .base import SitemapTestsBase |
| 7 |
from .models import TestModel |
| 8 |
|
| 9 |
|
| 10 |
@override_settings(ABSOLUTE_URL_OVERRIDES={}) |
| 11 |
class GenericViewsSitemapTests(SitemapTestsBase): |
| 12 |
def test_generic_sitemap_attributes(self): |
| 13 |
datetime_value = datetime.now() |
| 14 |
queryset = TestModel.objects.all() |
| 15 |
generic_sitemap = GenericSitemap( |
| 16 |
info_dict={ |
| 17 |
"queryset": queryset, |
| 18 |
"date_field": datetime_value, |
| 19 |
}, |
| 20 |
priority=0.6, |
| 21 |
changefreq="monthly", |
| 22 |
protocol="https", |
| 23 |
) |
| 24 |
attr_values = ( |
| 25 |
("date_field", datetime_value), |
| 26 |
("priority", 0.6), |
| 27 |
("changefreq", "monthly"), |
| 28 |
("protocol", "https"), |
| 29 |
) |
| 30 |
for attr_name, expected_value in attr_values: |
| 31 |
with self.subTest(attr_name=attr_name): |
| 32 |
self.assertEqual(getattr(generic_sitemap, attr_name), expected_value) |
| 33 |
self.assertCountEqual(generic_sitemap.queryset, queryset) |
| 34 |
|
| 35 |
def test_generic_sitemap(self): |
| 36 |
"A minimal generic sitemap can be rendered" |
| 37 |
response = self.client.get("/generic/sitemap.xml") |
| 38 |
expected = "" |
| 39 |
for pk in TestModel.objects.values_list("id", flat=True): |
| 40 |
expected += "<url><loc>%s/testmodel/%s/</loc></url>" % (self.base_url, pk) |
| 41 |
expected_content = ( |
| 42 |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 43 |
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' |
| 44 |
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' |
| 45 |
"%s\n" |
| 46 |
"</urlset>" |
| 47 |
) % expected |
| 48 |
self.assertXMLEqual(response.text, expected_content) |
| 49 |
|
| 50 |
def test_generic_sitemap_lastmod(self): |
| 51 |
test_model = TestModel.objects.first() |
| 52 |
TestModel.objects.update(lastmod=datetime(2013, 3, 13, 10, 0, 0)) |
| 53 |
response = self.client.get("/generic-lastmod/sitemap.xml") |
| 54 |
expected_content = ( |
| 55 |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 56 |
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' |
| 57 |
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' |
| 58 |
"<url><loc>%s/testmodel/%s/</loc><lastmod>2013-03-13</lastmod></url>\n" |
| 59 |
"</urlset>" |
| 60 |
) % ( |
| 61 |
self.base_url, |
| 62 |
test_model.pk, |
| 63 |
) |
| 64 |
self.assertXMLEqual(response.text, expected_content) |
| 65 |
self.assertEqual( |
| 66 |
response.headers["Last-Modified"], "Wed, 13 Mar 2013 10:00:00 GMT" |
| 67 |
) |
| 68 |
|
| 69 |
def test_get_protocol_defined_in_constructor(self): |
| 70 |
for protocol in ["http", "https"]: |
| 71 |
with self.subTest(protocol=protocol): |
| 72 |
sitemap = GenericSitemap({"queryset": None}, protocol=protocol) |
| 73 |
self.assertEqual(sitemap.get_protocol(), protocol) |
| 74 |
|
| 75 |
def test_get_protocol_passed_as_argument(self): |
| 76 |
sitemap = GenericSitemap({"queryset": None}) |
| 77 |
for protocol in ["http", "https"]: |
| 78 |
with self.subTest(protocol=protocol): |
| 79 |
self.assertEqual(sitemap.get_protocol(protocol), protocol) |
| 80 |
|
| 81 |
def test_get_protocol_default(self): |
| 82 |
sitemap = GenericSitemap({"queryset": None}) |
| 83 |
self.assertEqual(sitemap.get_protocol(), "https") |
| 84 |
|
| 85 |
def test_generic_sitemap_index(self): |
| 86 |
TestModel.objects.update(lastmod=datetime(2013, 3, 13, 10, 0, 0)) |
| 87 |
response = self.client.get("/generic-lastmod/index.xml") |
| 88 |
expected_content = """<?xml version="1.0" encoding="UTF-8"?> |
| 89 |
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 90 |
<sitemap><loc>http://example.com/simple/sitemap-generic.xml</loc><lastmod>2013-03-13T10:00:00</lastmod></sitemap> |
| 91 |
</sitemapindex>""" |
| 92 |
self.assertXMLEqual(response.text, expected_content) |