diff --git a/src/forum/backends/mysql/api.py b/src/forum/backends/mysql/api.py index f47692d1..2fa06b67 100644 --- a/src/forum/backends/mysql/api.py +++ b/src/forum/backends/mysql/api.py @@ -940,7 +940,7 @@ def threads_presentor( is_read, unread_count = read_states.get( str(thread.pk), (False, thread.comment_count) ) - is_endorsed = threads_endorsed.get(thread.pk, False) + is_endorsed = threads_endorsed.get(str(thread.pk), False) abuse_flagged_count = threads_flagged.get(str(thread.pk), 0) presenters.append( cls.prepare_thread( diff --git a/tests/test_backends/test_mysql/test_api.py b/tests/test_backends/test_mysql/test_api.py index d89a537f..c30e9138 100644 --- a/tests/test_backends/test_mysql/test_api.py +++ b/tests/test_backends/test_mysql/test_api.py @@ -7,7 +7,7 @@ from django.contrib.auth import get_user_model from forum.backends.mysql.api import MySQLBackend as backend -from forum.backends.mysql.models import AbuseFlagger, CommentThread, CourseStat +from forum.backends.mysql.models import AbuseFlagger, Comment, CommentThread, CourseStat from forum.serializers.thread import ThreadSerializer User = get_user_model() @@ -175,6 +175,54 @@ def test_update_stats_for_course_calls_build_course_stats() -> None: mock_build_course_stats.assert_called_once_with(str(user.pk), course_id) +@pytest.mark.django_db +def test_threads_presentor_includes_endorsed_status() -> None: + """Test that threads_presentor marks threads with an endorsed response as endorsed.""" + user = User.objects.create(username="testuser") + course_id = "course123" + answered_thread = CommentThread.objects.create( + author=user, + course_id=course_id, + title="Answered question", + body="This question has an endorsed response", + thread_type="question", + context="course", + ) + unanswered_thread = CommentThread.objects.create( + author=user, + course_id=course_id, + title="Unanswered question", + body="This question has no endorsed response", + thread_type="question", + context="course", + ) + Comment.objects.create( + author=user, + course_id=course_id, + body="Endorsed response", + comment_thread=answered_thread, + endorsed=True, + ) + Comment.objects.create( + author=user, + course_id=course_id, + body="Unendorsed response", + comment_thread=unanswered_thread, + ) + + presented = backend.threads_presentor( + [str(answered_thread.pk), str(unanswered_thread.pk)], + str(user.pk), + course_id, + ) + + endorsed_by_id = {str(thread["_id"]): thread["endorsed"] for thread in presented} + assert endorsed_by_id == { + str(answered_thread.pk): True, + str(unanswered_thread.pk): False, + } + + @pytest.mark.django_db class TestMongoAPI(unittest.TestCase): """