summaryrefslogtreecommitdiff
path: root/tests/sitemaps_tests/test_utils.py
blob: 3aba9b5a1abcb35d2d4dc90c4a4d79d607c0dfad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from unittest import mock
from urllib.parse import urlencode

from django.contrib.sitemaps import SitemapNotFound, _get_sitemap_full_url, ping_google
from django.core.exceptions import ImproperlyConfigured
from django.test import modify_settings, override_settings

from .base import SitemapTestsBase


class PingGoogleTests(SitemapTestsBase):
    @override_settings(ROOT_URLCONF="sitemaps_tests.urls.sitemap_only")
    @mock.patch("django.contrib.sitemaps.urlopen")
    def test_something(self, urlopen):
        ping_google()
        params = urlencode(
            {"sitemap": "https://example.com/sitemap-without-entries/sitemap.xml"}
        )
        full_url = "https://www.google.com/webmasters/tools/ping?%s" % params
        urlopen.assert_called_with(full_url)

    @override_settings(ROOT_URLCONF="sitemaps_tests.urls.sitemap_only")
    def test_get_sitemap_full_url_global(self):
        self.assertEqual(
            _get_sitemap_full_url(None),
            "https://example.com/sitemap-without-entries/sitemap.xml",
        )

    @override_settings(ROOT_URLCONF="sitemaps_tests.urls.index_only")
    def test_get_sitemap_full_url_index(self):
        self.assertEqual(
            _get_sitemap_full_url(None), "https://example.com/simple/index.xml"
        )

    @override_settings(ROOT_URLCONF="sitemaps_tests.urls.empty")
    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):
        self.assertEqual(
            _get_sitemap_full_url("/foo.xml"), "https://example.com/foo.xml"
        )

    def test_get_sitemap_full_url_insecure(self):
        self.assertEqual(
            _get_sitemap_full_url("/foo.xml", sitemap_uses_https=False),
            "http://example.com/foo.xml",
        )

    @modify_settings(INSTALLED_APPS={"remove": "django.contrib.sites"})
    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)