2017-01-19 17:16:04 +00:00
|
|
|
from unittest import mock
|
2017-01-07 11:11:46 +00:00
|
|
|
from urllib.parse import urlencode
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
from django.contrib.sitemaps import SitemapNotFound, _get_sitemap_full_url, ping_google
|
2016-11-22 11:56:22 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2017-01-19 17:16:04 +00:00
|
|
|
from django.test import modify_settings, override_settings
|
2016-11-22 11:56:22 +00:00
|
|
|
|
|
|
|
from .base import SitemapTestsBase
|
|
|
|
|
|
|
|
|
|
|
|
class PingGoogleTests(SitemapTestsBase):
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(ROOT_URLCONF="sitemaps_tests.urls.sitemap_only")
|
|
|
|
@mock.patch("django.contrib.sitemaps.urlopen")
|
2016-11-22 11:56:22 +00:00
|
|
|
def test_something(self, urlopen):
|
|
|
|
ping_google()
|
2022-02-03 19:24:19 +00:00
|
|
|
params = urlencode(
|
|
|
|
{"sitemap": "https://example.com/sitemap-without-entries/sitemap.xml"}
|
|
|
|
)
|
|
|
|
full_url = "https://www.google.com/webmasters/tools/ping?%s" % params
|
2016-11-22 11:56:22 +00:00
|
|
|
urlopen.assert_called_with(full_url)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(ROOT_URLCONF="sitemaps_tests.urls.sitemap_only")
|
2016-11-22 11:56:22 +00:00
|
|
|
def test_get_sitemap_full_url_global(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
_get_sitemap_full_url(None),
|
|
|
|
"https://example.com/sitemap-without-entries/sitemap.xml",
|
|
|
|
)
|
2016-11-22 11:56:22 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(ROOT_URLCONF="sitemaps_tests.urls.index_only")
|
2016-11-22 11:56:22 +00:00
|
|
|
def test_get_sitemap_full_url_index(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
_get_sitemap_full_url(None), "https://example.com/simple/index.xml"
|
|
|
|
)
|
2016-11-22 11:56:22 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(ROOT_URLCONF="sitemaps_tests.urls.empty")
|
2016-11-22 11:56:22 +00:00
|
|
|
def test_get_sitemap_full_url_not_detected(self):
|
|
|
|
msg = "You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected."
|
|
|
|
with self.assertRaisesMessage(SitemapNotFound, msg):
|
|
|
|
_get_sitemap_full_url(None)
|
|
|
|
|
|
|
|
def test_get_sitemap_full_url_exact_url(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
_get_sitemap_full_url("/foo.xml"), "https://example.com/foo.xml"
|
|
|
|
)
|
2019-01-10 10:00:00 +00:00
|
|
|
|
|
|
|
def test_get_sitemap_full_url_insecure(self):
|
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
_get_sitemap_full_url("/foo.xml", sitemap_uses_https=False),
|
|
|
|
"http://example.com/foo.xml",
|
2019-01-10 10:00:00 +00:00
|
|
|
)
|
2016-11-22 11:56:22 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@modify_settings(INSTALLED_APPS={"remove": "django.contrib.sites"})
|
2016-11-22 11:56:22 +00:00
|
|
|
def test_get_sitemap_full_url_no_sites(self):
|
|
|
|
msg = "ping_google requires django.contrib.sites, which isn't installed."
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
_get_sitemap_full_url(None)
|