Skip to content

Commit

Permalink
fix: tests, add mocks for getting course_id APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Faraz Maqsood committed Oct 11, 2024
1 parent af8e057 commit 43bf218
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 25 deletions.
29 changes: 21 additions & 8 deletions lms/djangoapps/discussion/django_comment_client/base/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ class CreateThreadGroupIdTestCase(
):
cs_endpoint = "/threads"

def setUp(self):
super().setUp()
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.models.forum_api.get_course_id_by_comment"
)
self.mock_get_course_id_by_comment = patcher.start()
self.addCleanup(patcher.stop)

def call_view(self, mock_is_forum_v2_enabled, mock_request, commentable_id, user, group_id, pass_group_id=True):
mock_is_forum_v2_enabled.return_value = False
self._set_mock_request_data(mock_request, {})
Expand Down Expand Up @@ -140,7 +153,7 @@ def call_view(
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)
mock_is_forum_v2_enabled.return_value = False
self._set_mock_request_data(
Expand Down Expand Up @@ -415,7 +428,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)

def count_queries(func): # pylint: disable=no-self-argument
Expand Down Expand Up @@ -497,7 +510,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)
# Patch the comment client user save method so it does not try
# to create a new cc user when creating a django user
Expand Down Expand Up @@ -1175,7 +1188,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)

def test_pin_thread_as_student(self, mock_is_forum_v2_enabled, mock_request):
Expand Down Expand Up @@ -1321,7 +1334,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)

@classmethod
Expand Down Expand Up @@ -1375,7 +1388,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)

@classmethod
Expand Down Expand Up @@ -1705,7 +1718,7 @@ def _setup_mock(self, user, mock_is_forum_v2_enabled, mock_request, data):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)
self.client.login(username=user.username, password=self.password)

Expand Down Expand Up @@ -1920,7 +1933,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)

@classmethod
Expand Down
14 changes: 7 additions & 7 deletions lms/djangoapps/discussion/rest_api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)
self.maxDiff = None # pylint: disable=invalid-name
self.user = UserFactory.create()
Expand Down Expand Up @@ -2225,7 +2225,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)
self.user = UserFactory.create()
self.register_get_user_response(self.user)
Expand Down Expand Up @@ -2624,7 +2624,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)

self.user = UserFactory.create()
Expand Down Expand Up @@ -3201,7 +3201,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)
self.user = UserFactory.create()
self.register_get_user_response(self.user)
Expand Down Expand Up @@ -3731,7 +3731,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)
self.user = UserFactory.create()
self.register_get_user_response(self.user)
Expand Down Expand Up @@ -3897,7 +3897,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)
self.user = UserFactory.create()
self.register_get_user_response(self.user)
Expand Down Expand Up @@ -4073,7 +4073,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)
self.user = UserFactory.create()
self.register_get_user_response(self.user)
Expand Down
18 changes: 9 additions & 9 deletions lms/djangoapps/discussion/rest_api/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)

def test_basic(self):
Expand Down Expand Up @@ -1623,7 +1623,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)

def test_basic(self):
Expand Down Expand Up @@ -1976,7 +1976,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)

def create_source_comment(self, overrides=None):
Expand Down Expand Up @@ -2443,7 +2443,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)

def test_basic(self):
Expand Down Expand Up @@ -2496,7 +2496,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)

def test_basic(self):
Expand Down Expand Up @@ -2611,7 +2611,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)
self.register_get_user_response(self.user)
self.url = reverse("comment-detail", kwargs={"comment_id": "test_comment"})
Expand Down Expand Up @@ -2739,14 +2739,14 @@ def setUp(self):
patcher.start()
self.addCleanup(patcher.stop)
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
"openedx.core.djangoapps.django_comment_common.comment_client.models.forum_api.get_course_id_by_comment"
)
self.mock_get_course_id_by_comment = patcher.start()
self.addCleanup(patcher.stop)
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)

def test_basic(self):
Expand Down Expand Up @@ -2812,7 +2812,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)

def make_comment_data(self, comment_id, parent_id=None, children=[]): # pylint: disable=W0102
Expand Down
2 changes: 1 addition & 1 deletion lms/djangoapps/discussion/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def setUp(self):
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_comment = patcher.start()
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)

def tearDown(self):
Expand Down
23 changes: 23 additions & 0 deletions lms/djangoapps/discussion/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,11 @@ def setUp(self):
patcher = mock.patch('lms.djangoapps.discussion.toggles.ENABLE_FORUM_V2.is_enabled', return_value=False)
patcher.start()
self.addCleanup(patcher.stop)
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.models.forum_api.get_course_id_by_comment"
)
self.mock_get_course_id_by_comment = patcher.start()
self.addCleanup(patcher.stop)
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
Expand Down Expand Up @@ -1255,6 +1260,11 @@ class UserProfileDiscussionGroupIdTestCase(CohortedTestCase, CohortedTopicGroupI

def setUp(self):
super().setUp()
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.models.forum_api.get_course_id_by_comment"
)
self.mock_get_course_id_by_comment = patcher.start()
self.addCleanup(patcher.stop)
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
Expand Down Expand Up @@ -1707,6 +1717,19 @@ class CommentsServiceRequestHeadersTestCase(ForumsEnableMixin, UrlResetMixin, Mo
@patch.dict("django.conf.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True})
def setUp(self):
super().setUp()
patcher = mock.patch('lms.djangoapps.discussion.toggles.ENABLE_FORUM_V2.is_enabled', return_value=False)
patcher.start()
self.addCleanup(patcher.stop)
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.models.forum_api.get_course_id_by_comment"
)
self.mock_get_course_id_by_comment = patcher.start()
self.addCleanup(patcher.stop)
patcher = mock.patch(
"openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread"
)
self.mock_get_course_id_by_thread = patcher.start()
self.addCleanup(patcher.stop)

username = "foo"
password = "bar"
Expand Down

0 comments on commit 43bf218

Please sign in to comment.