| 1 |
import os |
| 2 |
from datetime import date |
| 3 |
|
| 4 |
from django.contrib.sitemaps import Sitemap |
| 5 |
from django.contrib.sites.models import Site |
| 6 |
from django.core.exceptions import ImproperlyConfigured |
| 7 |
from django.test import modify_settings, override_settings |
| 8 |
from django.utils import translation |
| 9 |
from django.utils.formats import localize |
| 10 |
|
| 11 |
from .base import SitemapTestsBase |
| 12 |
from .models import I18nTestModel, TestModel |
| 13 |
|
| 14 |
|
| 15 |
class HTTPSitemapTests(SitemapTestsBase): |
| 16 |
use_sitemap_err_msg = ( |
| 17 |
"To use sitemaps, either enable the sites framework or pass a " |
| 18 |
"Site/RequestSite object in your view." |
| 19 |
) |
| 20 |
|
| 21 |
def test_simple_sitemap_index(self): |
| 22 |
"A simple sitemap index can be rendered" |
| 23 |
response = self.client.get("/simple/index.xml") |
| 24 |
expected_content = """<?xml version="1.0" encoding="UTF-8"?> |
| 25 |
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 26 |
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod></sitemap> |
| 27 |
</sitemapindex> |
| 28 |
""" % ( |
| 29 |
self.base_url, |
| 30 |
date.today(), |
| 31 |
) |
| 32 |
self.assertXMLEqual(response.text, expected_content) |
| 33 |
|
| 34 |
def test_sitemap_not_callable(self): |
| 35 |
"""A sitemap may not be callable.""" |
| 36 |
response = self.client.get("/simple-not-callable/index.xml") |
| 37 |
expected_content = """<?xml version="1.0" encoding="UTF-8"?> |
| 38 |
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 39 |
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod></sitemap> |
| 40 |
</sitemapindex> |
| 41 |
""" % ( |
| 42 |
self.base_url, |
| 43 |
date.today(), |
| 44 |
) |
| 45 |
self.assertXMLEqual(response.text, expected_content) |
| 46 |
|
| 47 |
def test_paged_sitemap(self): |
| 48 |
"""A sitemap may have multiple pages.""" |
| 49 |
response = self.client.get("/simple-paged/index.xml") |
| 50 |
expected_content = """<?xml version="1.0" encoding="UTF-8"?> |
| 51 |
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 52 |
<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> |
| 53 |
</sitemapindex> |
| 54 |
""".format(self.base_url, date.today()) |
| 55 |
self.assertXMLEqual(response.text, expected_content) |
| 56 |
|
| 57 |
@override_settings( |
| 58 |
TEMPLATES=[ |
| 59 |
{ |
| 60 |
"BACKEND": "django.template.backends.django.DjangoTemplates", |
| 61 |
"DIRS": [os.path.join(os.path.dirname(__file__), "templates")], |
| 62 |
} |
| 63 |
] |
| 64 |
) |
| 65 |
def test_simple_sitemap_custom_lastmod_index(self): |
| 66 |
"A simple sitemap index can be rendered with a custom template" |
| 67 |
response = self.client.get("/simple/custom-lastmod-index.xml") |
| 68 |
expected_content = """<?xml version="1.0" encoding="UTF-8"?> |
| 69 |
<!-- This is a customised template --> |
| 70 |
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 71 |
<sitemap><loc>%s/simple/sitemap-simple.xml</loc><lastmod>%s</lastmod></sitemap> |
| 72 |
</sitemapindex> |
| 73 |
""" % ( |
| 74 |
self.base_url, |
| 75 |
date.today(), |
| 76 |
) |
| 77 |
self.assertXMLEqual(response.text, expected_content) |
| 78 |
|
| 79 |
def test_simple_sitemap_section(self): |
| 80 |
"A simple sitemap section can be rendered" |
| 81 |
response = self.client.get("/simple/sitemap-simple.xml") |
| 82 |
expected_content = ( |
| 83 |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 84 |
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' |
| 85 |
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' |
| 86 |
"<url><loc>%s/location/</loc><lastmod>%s</lastmod>" |
| 87 |
"<changefreq>never</changefreq><priority>0.5</priority></url>\n" |
| 88 |
"</urlset>" |
| 89 |
) % ( |
| 90 |
self.base_url, |
| 91 |
date.today(), |
| 92 |
) |
| 93 |
self.assertXMLEqual(response.text, expected_content) |
| 94 |
|
| 95 |
def test_no_section(self): |
| 96 |
response = self.client.get("/simple/sitemap-simple2.xml") |
| 97 |
self.assertEqual( |
| 98 |
str(response.context["exception"]), |
| 99 |
"No sitemap available for section: 'simple2'", |
| 100 |
) |
| 101 |
self.assertEqual(response.status_code, 404) |
| 102 |
|
| 103 |
def test_empty_page(self): |
| 104 |
response = self.client.get("/simple/sitemap-simple.xml?p=0") |
| 105 |
self.assertEqual(str(response.context["exception"]), "Page 0 empty") |
| 106 |
self.assertEqual(response.status_code, 404) |
| 107 |
|
| 108 |
def test_page_not_int(self): |
| 109 |
response = self.client.get("/simple/sitemap-simple.xml?p=test") |
| 110 |
self.assertEqual(str(response.context["exception"]), "No page 'test'") |
| 111 |
self.assertEqual(response.status_code, 404) |
| 112 |
|
| 113 |
def test_simple_sitemap(self): |
| 114 |
"A simple sitemap can be rendered" |
| 115 |
response = self.client.get("/simple/sitemap.xml") |
| 116 |
expected_content = ( |
| 117 |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 118 |
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' |
| 119 |
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' |
| 120 |
"<url><loc>%s/location/</loc><lastmod>%s</lastmod>" |
| 121 |
"<changefreq>never</changefreq><priority>0.5</priority></url>\n" |
| 122 |
"</urlset>" |
| 123 |
) % ( |
| 124 |
self.base_url, |
| 125 |
date.today(), |
| 126 |
) |
| 127 |
self.assertXMLEqual(response.text, expected_content) |
| 128 |
|
| 129 |
@override_settings( |
| 130 |
TEMPLATES=[ |
| 131 |
{ |
| 132 |
"BACKEND": "django.template.backends.django.DjangoTemplates", |
| 133 |
"DIRS": [os.path.join(os.path.dirname(__file__), "templates")], |
| 134 |
} |
| 135 |
] |
| 136 |
) |
| 137 |
def test_simple_custom_sitemap(self): |
| 138 |
"A simple sitemap can be rendered with a custom template" |
| 139 |
response = self.client.get("/simple/custom-sitemap.xml") |
| 140 |
expected_content = """<?xml version="1.0" encoding="UTF-8"?> |
| 141 |
<!-- This is a customised template --> |
| 142 |
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 143 |
<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> |
| 144 |
</urlset> |
| 145 |
""" % ( |
| 146 |
self.base_url, |
| 147 |
date.today(), |
| 148 |
) |
| 149 |
self.assertXMLEqual(response.text, expected_content) |
| 150 |
|
| 151 |
def test_sitemap_last_modified(self): |
| 152 |
"Last-Modified header is set correctly" |
| 153 |
response = self.client.get("/lastmod/sitemap.xml") |
| 154 |
self.assertEqual( |
| 155 |
response.headers["Last-Modified"], "Wed, 13 Mar 2013 10:00:00 GMT" |
| 156 |
) |
| 157 |
|
| 158 |
def test_sitemap_last_modified_date(self): |
| 159 |
""" |
| 160 |
The Last-Modified header should be support dates (without time). |
| 161 |
""" |
| 162 |
response = self.client.get("/lastmod/date-sitemap.xml") |
| 163 |
self.assertEqual( |
| 164 |
response.headers["Last-Modified"], "Wed, 13 Mar 2013 00:00:00 GMT" |
| 165 |
) |
| 166 |
|
| 167 |
def test_sitemap_last_modified_tz(self): |
| 168 |
""" |
| 169 |
The Last-Modified header should be converted from timezone aware dates |
| 170 |
to GMT. |
| 171 |
""" |
| 172 |
response = self.client.get("/lastmod/tz-sitemap.xml") |
| 173 |
self.assertEqual( |
| 174 |
response.headers["Last-Modified"], "Wed, 13 Mar 2013 15:00:00 GMT" |
| 175 |
) |
| 176 |
|
| 177 |
def test_sitemap_last_modified_missing(self): |
| 178 |
"Last-Modified header is missing when sitemap has no lastmod" |
| 179 |
response = self.client.get("/generic/sitemap.xml") |
| 180 |
self.assertFalse(response.has_header("Last-Modified")) |
| 181 |
|
| 182 |
def test_sitemap_last_modified_mixed(self): |
| 183 |
"Last-Modified header is omitted when lastmod not on all items" |
| 184 |
response = self.client.get("/lastmod-mixed/sitemap.xml") |
| 185 |
self.assertFalse(response.has_header("Last-Modified")) |
| 186 |
|
| 187 |
def test_sitemaps_lastmod_mixed_ascending_last_modified_missing(self): |
| 188 |
""" |
| 189 |
The Last-Modified header is omitted when lastmod isn't found in all |
| 190 |
sitemaps. Test sitemaps are sorted by lastmod in ascending order. |
| 191 |
""" |
| 192 |
response = self.client.get("/lastmod-sitemaps/mixed-ascending.xml") |
| 193 |
self.assertFalse(response.has_header("Last-Modified")) |
| 194 |
|
| 195 |
def test_sitemaps_lastmod_mixed_descending_last_modified_missing(self): |
| 196 |
""" |
| 197 |
The Last-Modified header is omitted when lastmod isn't found in all |
| 198 |
sitemaps. Test sitemaps are sorted by lastmod in descending order. |
| 199 |
""" |
| 200 |
response = self.client.get("/lastmod-sitemaps/mixed-descending.xml") |
| 201 |
self.assertFalse(response.has_header("Last-Modified")) |
| 202 |
|
| 203 |
def test_sitemaps_lastmod_ascending(self): |
| 204 |
""" |
| 205 |
The Last-Modified header is set to the most recent sitemap lastmod. |
| 206 |
Test sitemaps are sorted by lastmod in ascending order. |
| 207 |
""" |
| 208 |
response = self.client.get("/lastmod-sitemaps/ascending.xml") |
| 209 |
self.assertEqual( |
| 210 |
response.headers["Last-Modified"], "Sat, 20 Apr 2013 05:00:00 GMT" |
| 211 |
) |
| 212 |
|
| 213 |
def test_sitemaps_lastmod_descending(self): |
| 214 |
""" |
| 215 |
The Last-Modified header is set to the most recent sitemap lastmod. |
| 216 |
Test sitemaps are sorted by lastmod in descending order. |
| 217 |
""" |
| 218 |
response = self.client.get("/lastmod-sitemaps/descending.xml") |
| 219 |
self.assertEqual( |
| 220 |
response.headers["Last-Modified"], "Sat, 20 Apr 2013 05:00:00 GMT" |
| 221 |
) |
| 222 |
|
| 223 |
def test_sitemap_get_latest_lastmod_none(self): |
| 224 |
""" |
| 225 |
sitemapindex.lastmod is omitted when Sitemap.lastmod is |
| 226 |
callable and Sitemap.get_latest_lastmod is not implemented |
| 227 |
""" |
| 228 |
response = self.client.get("/lastmod/get-latest-lastmod-none-sitemap.xml") |
| 229 |
self.assertNotContains(response, "<lastmod>") |
| 230 |
|
| 231 |
def test_sitemap_get_latest_lastmod(self): |
| 232 |
""" |
| 233 |
sitemapindex.lastmod is included when Sitemap.lastmod is |
| 234 |
attribute and Sitemap.get_latest_lastmod is implemented |
| 235 |
""" |
| 236 |
response = self.client.get("/lastmod/get-latest-lastmod-sitemap.xml") |
| 237 |
self.assertContains(response, "<lastmod>2013-03-13T10:00:00</lastmod>") |
| 238 |
|
| 239 |
def test_sitemap_latest_lastmod_timezone(self): |
| 240 |
""" |
| 241 |
lastmod datestamp shows timezones if Sitemap.get_latest_lastmod |
| 242 |
returns an aware datetime. |
| 243 |
""" |
| 244 |
response = self.client.get("/lastmod/latest-lastmod-timezone-sitemap.xml") |
| 245 |
self.assertContains(response, "<lastmod>2013-03-13T10:00:00-05:00</lastmod>") |
| 246 |
|
| 247 |
def test_localized_priority(self): |
| 248 |
"""The priority value should not be localized.""" |
| 249 |
with translation.override("fr"): |
| 250 |
self.assertEqual("0,3", localize(0.3)) |
| 251 |
# Priorities aren't rendered in localized format. |
| 252 |
response = self.client.get("/simple/sitemap.xml") |
| 253 |
self.assertContains(response, "<priority>0.5</priority>") |
| 254 |
self.assertContains(response, "<lastmod>%s</lastmod>" % date.today()) |
| 255 |
|
| 256 |
@modify_settings(INSTALLED_APPS={"remove": "django.contrib.sites"}) |
| 257 |
def test_requestsite_sitemap(self): |
| 258 |
# Hitting the flatpages sitemap without the sites framework installed |
| 259 |
# doesn't raise an exception. |
| 260 |
response = self.client.get("/simple/sitemap.xml") |
| 261 |
expected_content = ( |
| 262 |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 263 |
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' |
| 264 |
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' |
| 265 |
"<url><loc>http://testserver/location/</loc><lastmod>%s</lastmod>" |
| 266 |
"<changefreq>never</changefreq><priority>0.5</priority></url>\n" |
| 267 |
"</urlset>" |
| 268 |
) % date.today() |
| 269 |
self.assertXMLEqual(response.text, expected_content) |
| 270 |
|
| 271 |
def test_sitemap_get_urls_no_site_1(self): |
| 272 |
""" |
| 273 |
Check we get ImproperlyConfigured if we don't pass a site object to |
| 274 |
Sitemap.get_urls and no Site objects exist |
| 275 |
""" |
| 276 |
Site.objects.all().delete() |
| 277 |
with self.assertRaisesMessage(ImproperlyConfigured, self.use_sitemap_err_msg): |
| 278 |
Sitemap().get_urls() |
| 279 |
|
| 280 |
@modify_settings(INSTALLED_APPS={"remove": "django.contrib.sites"}) |
| 281 |
def test_sitemap_get_urls_no_site_2(self): |
| 282 |
""" |
| 283 |
Check we get ImproperlyConfigured when we don't pass a site object to |
| 284 |
Sitemap.get_urls if Site objects exists, but the sites framework is not |
| 285 |
actually installed. |
| 286 |
""" |
| 287 |
with self.assertRaisesMessage(ImproperlyConfigured, self.use_sitemap_err_msg): |
| 288 |
Sitemap().get_urls() |
| 289 |
|
| 290 |
def test_sitemap_item(self): |
| 291 |
""" |
| 292 |
Check to make sure that the raw item is included with each |
| 293 |
Sitemap.get_url() url result. |
| 294 |
""" |
| 295 |
test_sitemap = Sitemap() |
| 296 |
test_sitemap.items = TestModel.objects.order_by("pk").all |
| 297 |
|
| 298 |
def is_testmodel(url): |
| 299 |
return isinstance(url["item"], TestModel) |
| 300 |
|
| 301 |
item_in_url_info = all(map(is_testmodel, test_sitemap.get_urls())) |
| 302 |
self.assertTrue(item_in_url_info) |
| 303 |
|
| 304 |
def test_cached_sitemap_index(self): |
| 305 |
""" |
| 306 |
A cached sitemap index can be rendered (#2713). |
| 307 |
""" |
| 308 |
response = self.client.get("/cached/index.xml") |
| 309 |
expected_content = """<?xml version="1.0" encoding="UTF-8"?> |
| 310 |
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 311 |
<sitemap><loc>%s/cached/sitemap-simple.xml</loc><lastmod>%s</lastmod></sitemap> |
| 312 |
</sitemapindex> |
| 313 |
""" % ( |
| 314 |
self.base_url, |
| 315 |
date.today(), |
| 316 |
) |
| 317 |
self.assertXMLEqual(response.text, expected_content) |
| 318 |
|
| 319 |
def test_x_robots_sitemap(self): |
| 320 |
response = self.client.get("/simple/index.xml") |
| 321 |
self.assertEqual(response.headers["X-Robots-Tag"], "noindex, noodp, noarchive") |
| 322 |
|
| 323 |
response = self.client.get("/simple/sitemap.xml") |
| 324 |
self.assertEqual(response.headers["X-Robots-Tag"], "noindex, noodp, noarchive") |
| 325 |
|
| 326 |
def test_empty_sitemap(self): |
| 327 |
response = self.client.get("/empty/sitemap.xml") |
| 328 |
self.assertEqual(response.status_code, 200) |
| 329 |
|
| 330 |
@override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese"))) |
| 331 |
def test_simple_i18n_sitemap_index(self): |
| 332 |
""" |
| 333 |
A simple i18n sitemap index can be rendered, without logging variable |
| 334 |
lookup errors. |
| 335 |
""" |
| 336 |
with self.assertNoLogs("django.template", "DEBUG"): |
| 337 |
response = self.client.get("/simple/i18n.xml") |
| 338 |
expected_content = ( |
| 339 |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 340 |
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' |
| 341 |
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' |
| 342 |
"<url><loc>{0}/en/i18n/testmodel/{1}/</loc><changefreq>never</changefreq>" |
| 343 |
"<priority>0.5</priority></url><url><loc>{0}/pt/i18n/testmodel/{1}/</loc>" |
| 344 |
"<changefreq>never</changefreq><priority>0.5</priority></url>\n" |
| 345 |
"</urlset>" |
| 346 |
).format(self.base_url, self.i18n_model.pk) |
| 347 |
self.assertXMLEqual(response.text, expected_content) |
| 348 |
|
| 349 |
@override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese"))) |
| 350 |
def test_alternate_i18n_sitemap_index(self): |
| 351 |
""" |
| 352 |
A i18n sitemap with alternate/hreflang links can be rendered. |
| 353 |
""" |
| 354 |
response = self.client.get("/alternates/i18n.xml") |
| 355 |
url, pk = self.base_url, self.i18n_model.pk |
| 356 |
expected_urls = f""" |
| 357 |
<url><loc>{url}/en/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority> |
| 358 |
<xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/> |
| 359 |
<xhtml:link rel="alternate" hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/> |
| 360 |
</url> |
| 361 |
<url><loc>{url}/pt/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority> |
| 362 |
<xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/> |
| 363 |
<xhtml:link rel="alternate" hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/> |
| 364 |
</url> |
| 365 |
""".replace("\n", "") |
| 366 |
expected_content = ( |
| 367 |
f'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 368 |
f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' |
| 369 |
f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' |
| 370 |
f"{expected_urls}\n" |
| 371 |
f"</urlset>" |
| 372 |
) |
| 373 |
self.assertXMLEqual(response.text, expected_content) |
| 374 |
|
| 375 |
@override_settings( |
| 376 |
LANGUAGES=(("en", "English"), ("pt", "Portuguese"), ("es", "Spanish")) |
| 377 |
) |
| 378 |
def test_alternate_i18n_sitemap_limited(self): |
| 379 |
""" |
| 380 |
A i18n sitemap index with limited languages can be rendered. |
| 381 |
""" |
| 382 |
response = self.client.get("/limited/i18n.xml") |
| 383 |
url, pk = self.base_url, self.i18n_model.pk |
| 384 |
expected_urls = f""" |
| 385 |
<url><loc>{url}/en/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority> |
| 386 |
<xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/> |
| 387 |
<xhtml:link rel="alternate" hreflang="es" href="{url}/es/i18n/testmodel/{pk}/"/> |
| 388 |
</url> |
| 389 |
<url><loc>{url}/es/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority> |
| 390 |
<xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/> |
| 391 |
<xhtml:link rel="alternate" hreflang="es" href="{url}/es/i18n/testmodel/{pk}/"/> |
| 392 |
</url> |
| 393 |
""".replace("\n", "") |
| 394 |
expected_content = ( |
| 395 |
f'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 396 |
f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' |
| 397 |
f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' |
| 398 |
f"{expected_urls}\n" |
| 399 |
f"</urlset>" |
| 400 |
) |
| 401 |
self.assertXMLEqual(response.text, expected_content) |
| 402 |
|
| 403 |
@override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese"))) |
| 404 |
def test_alternate_i18n_sitemap_xdefault(self): |
| 405 |
""" |
| 406 |
A i18n sitemap index with x-default can be rendered. |
| 407 |
""" |
| 408 |
response = self.client.get("/x-default/i18n.xml") |
| 409 |
url, pk = self.base_url, self.i18n_model.pk |
| 410 |
expected_urls = f""" |
| 411 |
<url><loc>{url}/en/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority> |
| 412 |
<xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/> |
| 413 |
<xhtml:link rel="alternate" hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/> |
| 414 |
<xhtml:link rel="alternate" hreflang="x-default" href="{url}/i18n/testmodel/{pk}/"/> |
| 415 |
</url> |
| 416 |
<url><loc>{url}/pt/i18n/testmodel/{pk}/</loc><changefreq>never</changefreq><priority>0.5</priority> |
| 417 |
<xhtml:link rel="alternate" hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/> |
| 418 |
<xhtml:link rel="alternate" hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/> |
| 419 |
<xhtml:link rel="alternate" hreflang="x-default" href="{url}/i18n/testmodel/{pk}/"/> |
| 420 |
</url> |
| 421 |
""".replace("\n", "") |
| 422 |
expected_content = ( |
| 423 |
f'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 424 |
f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' |
| 425 |
f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' |
| 426 |
f"{expected_urls}\n" |
| 427 |
f"</urlset>" |
| 428 |
) |
| 429 |
self.assertXMLEqual(response.text, expected_content) |
| 430 |
|
| 431 |
@override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese"))) |
| 432 |
def test_language_for_item_i18n_sitemap(self): |
| 433 |
""" |
| 434 |
A i18n sitemap index in which item can be chosen to be displayed for a |
| 435 |
lang or not. |
| 436 |
""" |
| 437 |
only_pt = I18nTestModel.objects.create(name="Only for PT") |
| 438 |
response = self.client.get("/item-by-lang/i18n.xml") |
| 439 |
url, pk, only_pt_pk = self.base_url, self.i18n_model.pk, only_pt.pk |
| 440 |
expected_urls = ( |
| 441 |
f"<url><loc>{url}/en/i18n/testmodel/{pk}/</loc>" |
| 442 |
f"<changefreq>never</changefreq><priority>0.5</priority></url>" |
| 443 |
f"<url><loc>{url}/pt/i18n/testmodel/{pk}/</loc>" |
| 444 |
f"<changefreq>never</changefreq><priority>0.5</priority></url>" |
| 445 |
f"<url><loc>{url}/pt/i18n/testmodel/{only_pt_pk}/</loc>" |
| 446 |
f"<changefreq>never</changefreq><priority>0.5</priority></url>" |
| 447 |
) |
| 448 |
expected_content = ( |
| 449 |
f'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 450 |
f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' |
| 451 |
f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' |
| 452 |
f"{expected_urls}\n" |
| 453 |
f"</urlset>" |
| 454 |
) |
| 455 |
self.assertXMLEqual(response.text, expected_content) |
| 456 |
|
| 457 |
@override_settings(LANGUAGES=(("en", "English"), ("pt", "Portuguese"))) |
| 458 |
def test_alternate_language_for_item_i18n_sitemap(self): |
| 459 |
""" |
| 460 |
A i18n sitemap index in which item can be chosen to be displayed for a |
| 461 |
lang or not. |
| 462 |
""" |
| 463 |
only_pt = I18nTestModel.objects.create(name="Only for PT") |
| 464 |
response = self.client.get("/item-by-lang-alternates/i18n.xml") |
| 465 |
url, pk, only_pt_pk = self.base_url, self.i18n_model.pk, only_pt.pk |
| 466 |
expected_urls = ( |
| 467 |
f"<url><loc>{url}/en/i18n/testmodel/{pk}/</loc>" |
| 468 |
f"<changefreq>never</changefreq><priority>0.5</priority>" |
| 469 |
f'<xhtml:link rel="alternate" ' |
| 470 |
f'hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>' |
| 471 |
f'<xhtml:link rel="alternate" ' |
| 472 |
f'hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/>' |
| 473 |
f'<xhtml:link rel="alternate" ' |
| 474 |
f'hreflang="x-default" href="{url}/i18n/testmodel/{pk}/"/></url>' |
| 475 |
f"<url><loc>{url}/pt/i18n/testmodel/{pk}/</loc>" |
| 476 |
f"<changefreq>never</changefreq><priority>0.5</priority>" |
| 477 |
f'<xhtml:link rel="alternate" ' |
| 478 |
f'hreflang="en" href="{url}/en/i18n/testmodel/{pk}/"/>' |
| 479 |
f'<xhtml:link rel="alternate" ' |
| 480 |
f'hreflang="pt" href="{url}/pt/i18n/testmodel/{pk}/"/>' |
| 481 |
f'<xhtml:link rel="alternate" ' |
| 482 |
f'hreflang="x-default" href="{url}/i18n/testmodel/{pk}/"/></url>' |
| 483 |
f"<url><loc>{url}/pt/i18n/testmodel/{only_pt_pk}/</loc>" |
| 484 |
f"<changefreq>never</changefreq><priority>0.5</priority>" |
| 485 |
f'<xhtml:link rel="alternate" ' |
| 486 |
f'hreflang="pt" href="{url}/pt/i18n/testmodel/{only_pt_pk}/"/></url>' |
| 487 |
) |
| 488 |
expected_content = ( |
| 489 |
f'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 490 |
f'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' |
| 491 |
f'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' |
| 492 |
f"{expected_urls}\n" |
| 493 |
f"</urlset>" |
| 494 |
) |
| 495 |
self.assertXMLEqual(response.text, expected_content) |
| 496 |
|
| 497 |
def test_sitemap_without_entries(self): |
| 498 |
response = self.client.get("/sitemap-without-entries/sitemap.xml") |
| 499 |
expected_content = ( |
| 500 |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 501 |
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' |
| 502 |
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n\n' |
| 503 |
"</urlset>" |
| 504 |
) |
| 505 |
self.assertXMLEqual(response.text, expected_content) |
| 506 |
|
| 507 |
def test_callable_sitemod_partial(self): |
| 508 |
""" |
| 509 |
Not all items have `lastmod`. Therefore the `Last-Modified` header |
| 510 |
is not set by the detail or index sitemap view. |
| 511 |
""" |
| 512 |
index_response = self.client.get("/callable-lastmod-partial/index.xml") |
| 513 |
sitemap_response = self.client.get("/callable-lastmod-partial/sitemap.xml") |
| 514 |
self.assertNotIn("Last-Modified", index_response) |
| 515 |
self.assertNotIn("Last-Modified", sitemap_response) |
| 516 |
expected_content_index = """<?xml version="1.0" encoding="UTF-8"?> |
| 517 |
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 518 |
<sitemap><loc>http://example.com/simple/sitemap-callable-lastmod.xml</loc></sitemap> |
| 519 |
</sitemapindex> |
| 520 |
""" |
| 521 |
expected_content_sitemap = ( |
| 522 |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 523 |
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' |
| 524 |
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' |
| 525 |
"<url><loc>http://example.com/location/</loc>" |
| 526 |
"<lastmod>2013-03-13</lastmod></url><url>" |
| 527 |
"<loc>http://example.com/location/</loc></url>\n" |
| 528 |
"</urlset>" |
| 529 |
) |
| 530 |
self.assertXMLEqual(index_response.text, expected_content_index) |
| 531 |
self.assertXMLEqual(sitemap_response.text, expected_content_sitemap) |
| 532 |
|
| 533 |
def test_callable_sitemod_full(self): |
| 534 |
""" |
| 535 |
All items in the sitemap have `lastmod`. The `Last-Modified` header |
| 536 |
is set for the detail and index sitemap view. |
| 537 |
""" |
| 538 |
index_response = self.client.get("/callable-lastmod-full/index.xml") |
| 539 |
sitemap_response = self.client.get("/callable-lastmod-full/sitemap.xml") |
| 540 |
self.assertEqual( |
| 541 |
index_response.headers["Last-Modified"], "Thu, 13 Mar 2014 10:00:00 GMT" |
| 542 |
) |
| 543 |
self.assertEqual( |
| 544 |
sitemap_response.headers["Last-Modified"], "Thu, 13 Mar 2014 10:00:00 GMT" |
| 545 |
) |
| 546 |
expected_content_index = """<?xml version="1.0" encoding="UTF-8"?> |
| 547 |
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 548 |
<sitemap><loc>http://example.com/simple/sitemap-callable-lastmod.xml</loc><lastmod>2014-03-13T10:00:00</lastmod></sitemap> |
| 549 |
</sitemapindex> |
| 550 |
""" |
| 551 |
expected_content_sitemap = ( |
| 552 |
'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 553 |
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' |
| 554 |
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' |
| 555 |
"<url><loc>http://example.com/location/</loc>" |
| 556 |
"<lastmod>2013-03-13</lastmod></url>" |
| 557 |
"<url><loc>http://example.com/location/</loc>" |
| 558 |
"<lastmod>2014-03-13</lastmod></url>\n" |
| 559 |
"</urlset>" |
| 560 |
) |
| 561 |
self.assertXMLEqual(index_response.text, expected_content_index) |
| 562 |
self.assertXMLEqual(sitemap_response.text, expected_content_sitemap) |
| 563 |
|
| 564 |
def test_callable_sitemod_no_items(self): |
| 565 |
index_response = self.client.get("/callable-lastmod-no-items/index.xml") |
| 566 |
self.assertNotIn("Last-Modified", index_response) |
| 567 |
expected_content_index = """<?xml version="1.0" encoding="UTF-8"?> |
| 568 |
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 569 |
<sitemap><loc>http://example.com/simple/sitemap-callable-lastmod.xml</loc></sitemap> |
| 570 |
</sitemapindex> |
| 571 |
""" |
| 572 |
self.assertXMLEqual(index_response.text, expected_content_index) |