1. from unittest import mock
    
  2. from urllib.parse import urlencode
    
  3. 
    
  4. from django.contrib.sitemaps import SitemapNotFound, _get_sitemap_full_url, ping_google
    
  5. from django.core.exceptions import ImproperlyConfigured
    
  6. from django.test import modify_settings, override_settings
    
  7. 
    
  8. from .base import SitemapTestsBase
    
  9. 
    
  10. 
    
  11. class PingGoogleTests(SitemapTestsBase):
    
  12.     @override_settings(ROOT_URLCONF="sitemaps_tests.urls.sitemap_only")
    
  13.     @mock.patch("django.contrib.sitemaps.urlopen")
    
  14.     def test_something(self, urlopen):
    
  15.         ping_google()
    
  16.         params = urlencode(
    
  17.             {"sitemap": "https://example.com/sitemap-without-entries/sitemap.xml"}
    
  18.         )
    
  19.         full_url = "https://www.google.com/webmasters/tools/ping?%s" % params
    
  20.         urlopen.assert_called_with(full_url)
    
  21. 
    
  22.     @override_settings(ROOT_URLCONF="sitemaps_tests.urls.sitemap_only")
    
  23.     def test_get_sitemap_full_url_global(self):
    
  24.         self.assertEqual(
    
  25.             _get_sitemap_full_url(None),
    
  26.             "https://example.com/sitemap-without-entries/sitemap.xml",
    
  27.         )
    
  28. 
    
  29.     @override_settings(ROOT_URLCONF="sitemaps_tests.urls.index_only")
    
  30.     def test_get_sitemap_full_url_index(self):
    
  31.         self.assertEqual(
    
  32.             _get_sitemap_full_url(None), "https://example.com/simple/index.xml"
    
  33.         )
    
  34. 
    
  35.     @override_settings(ROOT_URLCONF="sitemaps_tests.urls.empty")
    
  36.     def test_get_sitemap_full_url_not_detected(self):
    
  37.         msg = (
    
  38.             "You didn't provide a sitemap_url, and the sitemap URL couldn't be "
    
  39.             "auto-detected."
    
  40.         )
    
  41.         with self.assertRaisesMessage(SitemapNotFound, msg):
    
  42.             _get_sitemap_full_url(None)
    
  43. 
    
  44.     def test_get_sitemap_full_url_exact_url(self):
    
  45.         self.assertEqual(
    
  46.             _get_sitemap_full_url("/foo.xml"), "https://example.com/foo.xml"
    
  47.         )
    
  48. 
    
  49.     def test_get_sitemap_full_url_insecure(self):
    
  50.         self.assertEqual(
    
  51.             _get_sitemap_full_url("/foo.xml", sitemap_uses_https=False),
    
  52.             "http://example.com/foo.xml",
    
  53.         )
    
  54. 
    
  55.     @modify_settings(INSTALLED_APPS={"remove": "django.contrib.sites"})
    
  56.     def test_get_sitemap_full_url_no_sites(self):
    
  57.         msg = "ping_google requires django.contrib.sites, which isn't installed."
    
  58.         with self.assertRaisesMessage(ImproperlyConfigured, msg):
    
  59.             _get_sitemap_full_url(None)