Files
edx-platform/lms/djangoapps/discussion/rest_api/tests/test_api.py
Taimoor Ahmed 4c051378d0 feat!: remove last calls to cs_comments_service (#37376)
This removes the last remaining code that called out to the
cs_comments_service. All forums backend logic now uses the v2 API from
the forum repo (https://github.com/openedx/forum). This does NOT remove
MongoDB support.

This also implements the endpoint to retrieve all comments for a user
using the new forum backend. This is not actually called from any known
frontend code, but it has not been formally deprecated as an endpoint,
and therefore needs to be supported.

As part of the cleanup, the ENABLE_FORUM_V2 course waffle flag has also
been removed, along with all remaining switching logic that used to
route between the Python API in the forum repo and service calls to the
cs_comments_service Ruby service.

Other endpoints affected (switching logic removed):

* get course commentable counts
* get/update course user stats
* update comment/thread/user
* delete thread (implementation moved to forum repo)
* follow
* retire user

This is part of the following overall DEPR ticket:
  https://github.com/openedx/cs_comments_service/issues/437
2025-10-08 11:36:52 -04:00

146 lines
4.9 KiB
Python

"""
Tests for Discussion API internal interface
"""
from unittest import mock
import ddt
import pytest
from django.contrib.auth import get_user_model
from django.test.client import RequestFactory
from opaque_keys.edx.keys import CourseKey
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory
from common.djangoapps.student.tests.factories import (
UserFactory
)
from lms.djangoapps.discussion.django_comment_client.tests.utils import ForumsEnableMixin
from lms.djangoapps.discussion.rest_api.api import get_user_comments
from lms.djangoapps.discussion.rest_api.tests.utils import (
ForumMockUtilsMixin,
make_minimal_cs_comment,
)
from openedx.core.lib.exceptions import CourseNotFoundError, PageNotFoundError
User = get_user_model()
@ddt.ddt
@mock.patch.dict("django.conf.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True})
class GetUserCommentsTest(ForumsEnableMixin, ForumMockUtilsMixin, SharedModuleStoreTestCase):
"""
Tests for get_user_comments.
"""
@classmethod
def setUpClass(cls):
super().setUpClass()
super().setUpClassAndForumMock()
@classmethod
def tearDownClass(cls):
super().tearDownClass()
super().disposeForumMocks()
@mock.patch.dict("django.conf.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True})
def setUp(self):
super().setUp()
self.course = CourseFactory.create()
# create staff user so that we don't need to worry about
# permissions here
self.user = UserFactory.create(is_staff=True)
self.register_get_user_response(self.user)
self.request = RequestFactory().get(f'/api/discussion/v1/users/{self.user.username}/{self.course.id}')
self.request.user = self.user
def test_call_with_single_results_page(self):
"""
Assert that a minimal call with valid inputs, and single result,
returns the expected response structure.
"""
self.register_get_comments_response(
[make_minimal_cs_comment()],
page=1,
num_pages=1,
)
response = get_user_comments(
request=self.request,
author=self.user,
course_key=self.course.id,
)
assert "results" in response.data
assert "pagination" in response.data
assert response.data["pagination"]["count"] == 1
assert response.data["pagination"]["num_pages"] == 1
assert response.data["pagination"]["next"] is None
assert response.data["pagination"]["previous"] is None
@ddt.data(1, 2, 3)
def test_call_with_paginated_results(self, page):
"""
Assert that paginated results return the correct pagination
information at the pagination boundaries.
"""
self.register_get_comments_response(
[make_minimal_cs_comment() for _ in range(30)],
page=page,
num_pages=3,
)
response = get_user_comments(
request=self.request,
author=self.user,
course_key=self.course.id,
page=page,
)
assert "pagination" in response.data
assert response.data["pagination"]["count"] == 30
assert response.data["pagination"]["num_pages"] == 3
if page in (1, 2):
assert response.data["pagination"]["next"] is not None
assert f"page={page+1}" in response.data["pagination"]["next"]
if page in (2, 3):
assert response.data["pagination"]["previous"] is not None
assert f"page={page-1}" in response.data["pagination"]["previous"]
if page == 1:
assert response.data["pagination"]["previous"] is None
if page == 3:
assert response.data["pagination"]["next"] is None
def test_call_with_invalid_page(self):
"""
Assert that calls for pages that exceed the existing number of
results pages raise PageNotFoundError.
"""
self.register_get_comments_response([], page=2, num_pages=1)
with pytest.raises(PageNotFoundError):
get_user_comments(
request=self.request,
author=self.user,
course_key=self.course.id,
page=2,
)
def test_call_with_non_existent_course(self):
"""
Assert that calls for comments in a course that doesn't exist
result in a CourseNotFoundError error.
"""
self.register_get_comments_response(
[make_minimal_cs_comment()],
page=1,
num_pages=1,
)
with pytest.raises(CourseNotFoundError):
get_user_comments(
request=self.request,
author=self.user,
course_key=CourseKey.from_string("course-v1:x+y+z"),
page=2,
)