From fa45caa138ca0b5257b39a804794c8d2de244ab8 Mon Sep 17 00:00:00 2001 From: Dmytro <98233552+DmytroAlipov@users.noreply.github.com> Date: Thu, 24 Aug 2023 09:12:52 +0300 Subject: [PATCH] fix: float " " symbol in the preview_body (#32484) Co-authored-by: Edward Zarecor --- lms/djangoapps/discussion/rest_api/serializers.py | 5 ++--- .../discussion/rest_api/tests/test_serializers.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lms/djangoapps/discussion/rest_api/serializers.py b/lms/djangoapps/discussion/rest_api/serializers.py index 7ede0a5df5..65778a34f1 100644 --- a/lms/djangoapps/discussion/rest_api/serializers.py +++ b/lms/djangoapps/discussion/rest_api/serializers.py @@ -427,10 +427,9 @@ class ThreadSerializer(_ContentSerializer): def get_preview_body(self, obj): """ - Returns a cleaned and truncated version of the thread's body to display in a - preview capacity. + Returns a cleaned version of the thread's body to display in a preview capacity. """ - return strip_tags(self.get_rendered_body(obj)).replace('\n', ' ') + return strip_tags(self.get_rendered_body(obj)).replace('\n', ' ').replace(' ', ' ') def get_close_reason(self, obj): """ diff --git a/lms/djangoapps/discussion/rest_api/tests/test_serializers.py b/lms/djangoapps/discussion/rest_api/tests/test_serializers.py index 570c7852e3..d0ded0f013 100644 --- a/lms/djangoapps/discussion/rest_api/tests/test_serializers.py +++ b/lms/djangoapps/discussion/rest_api/tests/test_serializers.py @@ -356,6 +356,21 @@ class ThreadSerializerSerializationTest(SerializerTestMixin, SharedModuleStoreTe }) assert self.serialize(thread) == expected + def test_get_preview_body(self): + """ + Test for the 'get_preview_body' method. + + This test verifies that the 'get_preview_body' method returns a cleaned + version of the thread's body that is suitable for display as a preview. + The test specifically focuses on handling the presence of multiple + spaces within the body. + """ + thread_data = self.make_cs_content( + {"body": "

This is a test thread body with some text.

"} + ) + serialized = self.serialize(thread_data) + assert serialized['preview_body'] == "This is a test thread body with some text." + @ddt.ddt class CommentSerializerTest(SerializerTestMixin, SharedModuleStoreTestCase):