~linuxgoose/bocpress

ref: 42c43cf231328d4ebcb06e2cd0c1276e197e146a bocpress/main/tests/test_sitemap.py -rw-r--r-- 6.1 KiB
42c43cf2Jordan Robinson update API docs 2 months ago
                                                                                
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from datetime import timedelta

from django.test import TestCase
from django.urls import reverse
from django.utils import timezone

from main import models
from mataroa import settings


class SitemapFormatTestCase(TestCase):
    def setUp(self):
        self.user = models.User.objects.create(username="alice")

    def test_sitemap_valid(self):
        response = self.client.get(
            reverse("sitemap"),
            # needs HTTP_HOST because we need to request it on the subdomain
            HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST,
        )
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, '<?xml version="1.0" encoding="UTF-8"?>')
        self.assertContains(
            response,
            '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">',
        )


class SitemapIndexTestCase(TestCase):
    def setUp(self):
        self.user = models.User.objects.create(username="alice")

    def test_index_exists(self):
        response = self.client.get(
            reverse("sitemap"),
            # needs HTTP_HOST because we need to request it on the subdomain
            HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST,
        )
        # do not include <loc> so the assertion matches all protocols
        self.assertContains(
            response, f"//{self.user.username}.{settings.CANONICAL_HOST}/</loc>"
        )


class SitemapPostTestCase(TestCase):
    def setUp(self):
        self.user = models.User.objects.create(username="alice")
        self.client.force_login(self.user)
        self.data = {
            "slug": "new-post",
        }
        self.post = models.Post.objects.create(owner=self.user, **self.data)

    def test_post_exists(self):
        response = self.client.get(
            reverse("sitemap"),
            # needs HTTP_HOST because we need to request it on the subdomain
            HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST,
        )
        # do not include <loc> so the assertion matches all protocols
        self.assertContains(
            response,
            f"//{self.user.username}.{settings.CANONICAL_HOST}/blog/{self.data['slug']}/</loc>",
        )


class SitemapFuturePostTestCase(TestCase):
    def setUp(self):
        self.user = models.User.objects.create(username="alice")
        self.client.force_login(self.user)
        self.data = {
            "slug": "new-future-post",
            "published_at": timezone.now() + timedelta(1),
        }
        self.post = models.Post.objects.create(owner=self.user, **self.data)

    def test_future_post_hidden(self):
        response = self.client.get(
            reverse("sitemap"),
            # needs HTTP_HOST because we need to request it on the subdomain
            HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST,
        )
        # do not include <loc> so the assertion matches all protocols
        self.assertNotContains(
            response,
            f"//{self.user.username}.{settings.CANONICAL_HOST}/blog/{self.data['slug']}/</loc>",
        )


class SitemapPostDraftTestCase(TestCase):
    def setUp(self):
        self.user = models.User.objects.create(username="alice")
        self.client.force_login(self.user)
        self.data = {"slug": "new-draft", "published_at": None}
        self.post = models.Post.objects.create(owner=self.user, **self.data)

    def test_draft_hidden(self):
        response = self.client.get(
            reverse("sitemap"),
            # needs HTTP_HOST because we need to request it on the subdomain
            HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST,
        )
        # do not include <loc> so the assertion matches all protocols
        self.assertNotContains(
            response,
            f"//{self.user.username}.{settings.CANONICAL_HOST}/blog/{self.data['slug']}/</loc>",
        )


class SitemapPageTestCase(TestCase):
    def setUp(self):
        self.user = models.User.objects.create(username="alice")
        self.client.force_login(self.user)
        self.data = {
            "slug": "new-page",
        }
        self.page = models.Page.objects.create(owner=self.user, **self.data)

    def test_page_exists(self):
        response = self.client.get(
            reverse("sitemap"),
            # needs HTTP_HOST because we need to request it on the subdomain
            HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST,
        )
        # do not include <loc> so the assertion matches all protocols
        self.assertContains(
            response,
            f"//{self.user.username}.{settings.CANONICAL_HOST}/{self.data['slug']}/</loc>",
        )


class SitemapHiddenPageTestCase(TestCase):
    def setUp(self):
        self.user = models.User.objects.create(username="alice")
        self.client.force_login(self.user)
        self.data = {"slug": "new-hidden-page", "is_hidden": True}
        self.page = models.Page.objects.create(owner=self.user, **self.data)

    def test_hidden_page_hidden(self):
        response = self.client.get(
            reverse("sitemap"),
            # needs HTTP_HOST because we need to request it on the subdomain
            HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST,
        )
        # do not include <loc> so the assertion matches all protocols
        self.assertNotContains(
            response,
            f"//{self.user.username}.{settings.CANONICAL_HOST}/{self.data['slug']}/</loc>",
        )


class SitemapCustomDomainTestCase(TestCase):
    def setUp(self):
        self.user = models.User.objects.create(
            username="alice", custom_domain="blog.test"
        )
        self.client.force_login(self.user)
        self.data = {
            "slug": "new-post",
        }
        self.post = models.Post.objects.create(owner=self.user, **self.data)

    def test_custom_baseurl(self):
        response = self.client.get(
            reverse("sitemap"),
            # needs HTTP_HOST because we need to request it on the subdomain
            HTTP_HOST=self.user.custom_domain,
        )
        self.assertContains(response, f"//blog.test/blog/{self.data['slug']}/</loc>")