Skip to content

Commit

Permalink
Supprime les caractères non supportés par les flux RSS et ATOM (#6329)
Browse files Browse the repository at this point in the history
* Nettoyage de code dans les flux RSS et ATOM
* Ajout de tests pour les flux RSS et ATOM des billets
* Supprime les caractères non supportés par les flux RSS et ATOM lors de leur génération
  • Loading branch information
philippemilink authored Jul 4, 2022
1 parent dfdf6f4 commit b9e68a8
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 58 deletions.
11 changes: 6 additions & 5 deletions zds/forum/feeds.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from django.contrib.syndication.views import Feed

from django.utils.feedgenerator import Atom1Feed
from django.conf import settings
from django.utils.timezone import make_aware
from pytz import AmbiguousTimeError, NonExistentTimeError

from zds.utils.feeds import DropControlCharsRss201rev2Feed, DropControlCharsAtom1Feed
from .models import Post, Topic


Expand Down Expand Up @@ -40,7 +39,8 @@ def request_object(request):
class LastPostsFeedRSS(Feed, ItemMixin):
title = "Derniers messages sur {}".format(settings.ZDS_APP["site"]["literal_name"])
link = "/forums/"
description = "Les derniers messages " "parus sur le forum de {}.".format(settings.ZDS_APP["site"]["literal_name"])
description = "Les derniers messages parus sur le forum de {}.".format(settings.ZDS_APP["site"]["literal_name"])
feed_type = DropControlCharsRss201rev2Feed

def get_object(self, request):
return request_object(request)
Expand All @@ -65,14 +65,15 @@ def item_description(self, item):


class LastPostsFeedATOM(LastPostsFeedRSS):
feed_type = Atom1Feed
feed_type = DropControlCharsAtom1Feed
subtitle = LastPostsFeedRSS.description


class LastTopicsFeedRSS(Feed, ItemMixin):
title = "Derniers sujets sur {}".format(settings.ZDS_APP["site"]["literal_name"])
link = "/forums/"
description = "Les derniers sujets créés sur le forum de {}.".format(settings.ZDS_APP["site"]["literal_name"])
feed_type = DropControlCharsRss201rev2Feed

def get_object(self, request):
return request_object(request)
Expand All @@ -97,5 +98,5 @@ def item_description(self, item):


class LastTopicsFeedATOM(LastTopicsFeedRSS):
feed_type = Atom1Feed
feed_type = DropControlCharsAtom1Feed
subtitle = LastTopicsFeedRSS.description
49 changes: 43 additions & 6 deletions zds/forum/tests/tests_feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from zds.member.tests.factories import ProfileFactory


class LastTopicsFeedRSSTest(TestCase):
class LastTopicsFeedTest(TestCase):
def setUp(self):
# prepare a user and 2 Topic (with and without tags)

Expand All @@ -35,7 +35,7 @@ def test_is_well_setup(self):
self.assertEqual(self.topicfeed.link, "/forums/")
reftitle = "Derniers sujets sur {}".format(settings.ZDS_APP["site"]["literal_name"])
self.assertEqual(self.topicfeed.title, reftitle)
refdescription = "Les derniers sujets créés " "sur le forum de {}.".format(
refdescription = "Les derniers sujets créés sur le forum de {}.".format(
settings.ZDS_APP["site"]["literal_name"]
)
self.assertEqual(self.topicfeed.description, refdescription)
Expand Down Expand Up @@ -76,7 +76,7 @@ def test_items_success(self):
def test_items_bad_cases(self):
"""test that right items are sent back according to obj"""

# test empty values, return value shoulb be empty
# test empty values, return value should be empty
obj = {"forum": -1, "tag": -1}
topics = self.topicfeed.items(obj=obj)
self.assertEqual(len(topics), 0)
Expand Down Expand Up @@ -139,8 +139,26 @@ def test_get_item_link(self):
ret = self.topicfeed.item_link(item=topics[0])
self.assertEqual(ret, ref)

def test_content_control_chars(self):
"""
Test 'control characters' in content of the feed doesn't break it.
The '\u0007' character in the post content belongs to a character
family that is not supported in RSS or Atom feeds and will break their
generation.
"""
buggy_topic = TopicFactory(forum=self.forum2, author=self.user)
buggy_topic.title = "Strange char: \u0007"
buggy_topic.save()

request = self.client.get(reverse("topic-feed-rss"))
self.assertEqual(request.status_code, 200)

request = self.client.get(reverse("topic-feed-atom"))
self.assertEqual(request.status_code, 200)


class LastPostFeedTest(TestCase):
class LastPostsFeedTest(TestCase):
def setUp(self):
# prepare a user and 2 Topic (with and without tags)

Expand All @@ -160,7 +178,7 @@ def setUp(self):
self.topic2.tags.add(self.tag)
self.topic2.save()

# create 2 posts un each forum
# create 2 posts in each forum
PostFactory(topic=self.topic1, author=self.user, position=1)
PostFactory(topic=self.topic1, author=self.user, position=2)
PostFactory(topic=self.topic2, author=self.user, position=1)
Expand All @@ -181,7 +199,7 @@ def test_is_well_setup(self):
self.assertEqual(self.postfeed.link, "/forums/")
reftitle = "Derniers messages sur {}".format(settings.ZDS_APP["site"]["literal_name"])
self.assertEqual(self.postfeed.title, reftitle)
refdescription = "Les derniers messages " "parus sur le forum de {}.".format(
refdescription = "Les derniers messages parus sur le forum de {}.".format(
settings.ZDS_APP["site"]["literal_name"]
)
self.assertEqual(self.postfeed.description, refdescription)
Expand Down Expand Up @@ -284,3 +302,22 @@ def test_get_item_link(self):
posts = self.postfeed.items(obj={"tag": self.tag2.pk})
ret = self.postfeed.item_link(item=posts[0])
self.assertEqual(ret, ref)

def test_content_control_chars(self):
"""
Test 'control characters' in content of the feed doesn't break it.
The '\u0007' character in the post content belongs to a character
family that is not supported in RSS or Atom feeds and will break their
generation.
"""
buggy_topic = TopicFactory(forum=self.forum2, author=self.user)
post = PostFactory(topic=buggy_topic, author=self.user, position=1)
post.update_content("Strange char: \u0007")
post.save()

request = self.client.get(reverse("post-feed-rss"))
self.assertEqual(request.status_code, 200)

request = self.client.get(reverse("post-feed-atom"))
self.assertEqual(request.status_code, 200)
11 changes: 6 additions & 5 deletions zds/tutorialv2/feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from django.contrib.syndication.views import Feed
from django.shortcuts import get_object_or_404
from django.utils.timezone import make_aware
from django.utils.feedgenerator import Atom1Feed
from django.utils.translation import gettext_lazy as _
from pytz import AmbiguousTimeError, NonExistentTimeError

from zds.utils.feeds import DropControlCharsRss201rev2Feed, DropControlCharsAtom1Feed
from zds.utils.models import Category, SubCategory, Tag
from zds.utils.uuslug_wrapper import slugify
from zds.tutorialv2.models.database import PublishedContent
Expand All @@ -21,6 +21,7 @@ class LastContentFeedRSS(Feed):
link = ""
content_type = None
query_params = {}
feed_type = DropControlCharsRss201rev2Feed

def get_object(self, request, *args, **kwargs):
self.query_params = request.GET
Expand Down Expand Up @@ -80,7 +81,7 @@ def item_link(self, item):


class LastContentFeedATOM(LastContentFeedRSS):
feed_type = Atom1Feed
feed_type = DropControlCharsAtom1Feed
subtitle = LastContentFeedRSS.description


Expand All @@ -96,7 +97,7 @@ class LastTutorialsFeedRSS(LastContentFeedRSS):


class LastTutorialsFeedATOM(LastTutorialsFeedRSS):
feed_type = Atom1Feed
feed_type = DropControlCharsAtom1Feed
subtitle = LastTutorialsFeedRSS.description


Expand All @@ -112,7 +113,7 @@ class LastArticlesFeedRSS(LastContentFeedRSS):


class LastArticlesFeedATOM(LastArticlesFeedRSS):
feed_type = Atom1Feed
feed_type = DropControlCharsAtom1Feed
subtitle = LastArticlesFeedRSS.description


Expand All @@ -128,5 +129,5 @@ class LastOpinionsFeedRSS(LastContentFeedRSS):


class LastOpinionsFeedATOM(LastOpinionsFeedRSS):
feed_type = Atom1Feed
feed_type = DropControlCharsAtom1Feed
subtitle = LastOpinionsFeedRSS.description
Loading

0 comments on commit b9e68a8

Please sign in to comment.