~linuxgoose/bocpress

ref: 80848bb7529efcba5b8a7b03c1fcd81de2138168 bocpress/main/tests/test_feeds.py -rw-r--r-- 5.1 KiB
80848bb7Jordan Robinson add signup email notification to admin 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
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 FeedTestCase(TestCase):
    def setUp(self):
        self.user = models.User.objects.create(username="alice")
        self.client.force_login(self.user)
        self.data = {
            "title": "Welcome post",
            "slug": "welcome-post",
            "body": "Content sentence.",
            "published_at": timezone.now(),
        }
        self.post = models.Post.objects.create(owner=self.user, **self.data)

    def test_rss_feed(self):
        for feed in ["rss", "atom"]:
            response = self.client.get(
                reverse(f"{feed}_feed"),
                HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST,
            )
            self.assertEqual(response.status_code, 200)
            self.assertEqual(response["Content-Type"], f"application/{feed}+xml; charset=utf-8")
            self.assertContains(response, self.data["title"])
            self.assertContains(response, self.data["slug"])
            self.assertContains(response, self.data["body"])
            self.assertContains(
                response,
                f"//{self.user.username}.{settings.CANONICAL_HOST}/blog/{self.data['slug']}/",
            )


class FeedDraftsTestCase(TestCase):
    """Tests draft posts do not appear in the RSS feed."""

    def setUp(self):
        self.user = models.User.objects.create(username="alice")
        self.client.force_login(self.user)
        self.post_published = {
            "title": "Welcome post",
            "slug": "welcome-post",
            "body": "Content sentence.",
            "published_at": timezone.now(),
        }
        models.Post.objects.create(owner=self.user, **self.post_published)
        self.post_draft = {
            "title": "Hidden post",
            "slug": "hidden-post",
            "body": "Hidden sentence.",
            "published_at": None,
        }
        models.Post.objects.create(owner=self.user, **self.post_draft)

    def test_rss_feed(self):
        for feed in ["rss", "atom"]:
            response = self.client.get(
                reverse(f"{feed}_feed"),
                HTTP_HOST=self.user.username + "." + settings.CANONICAL_HOST,
            )
            self.assertEqual(response.status_code, 200)
            self.assertEqual(response["Content-Type"], f"application/{feed}+xml; charset=utf-8")
            self.assertContains(response, self.post_published["title"])
            self.assertContains(response, self.post_published["slug"])
            self.assertContains(response, self.post_published["body"])
            self.assertNotContains(response, self.post_draft["title"])
            self.assertNotContains(response, self.post_draft["slug"])
            self.assertNotContains(response, self.post_draft["body"])
            self.assertNotContains(
                response,
                f"//{self.user.username}.{settings.CANONICAL_HOST}/blog/{self.post_draft['slug']}/</link>",
            )


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

    def test_future_post_hidden(self):
        for feed in ["rss", "atom"]:
            response = self.client.get(
                reverse(f"{feed}_feed"),
                # 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.assertEqual(response["Content-Type"], f"application/{feed}+xml; charset=utf-8")
            self.assertNotContains(response, self.data["title"])
            self.assertNotContains(response, self.data["slug"])
            self.assertNotContains(response, self.data["body"])
            self.assertNotContains(
                response,
                f"//{self.user.username}.{settings.CANONICAL_HOST}/blog/{self.data['slug']}/</link>",
            )


class FeedFormatTestCase(TestCase):
    def setUp(self):
        self.user = models.User.objects.create(
            username="alice", blog_title="test title", blog_byline="test about text"
        )

    def test_feed_valid(self):
        for feed in ["rss", "atom"]:
            response = self.client.get(
                reverse(f"{feed}_feed"),
                # 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, f"<title>{self.user.blog_title}</title>")