From 9ec4a50cd0116c48f9ee3c178b930497d4847e23 Mon Sep 17 00:00:00 2001 From: Muhammad Faraz Maqsood Date: Fri, 11 Oct 2024 16:42:38 +0500 Subject: [PATCH] temp: code progress --- .../rest_api/tests/test_views_v2.py | 377 +++++++++--------- .../discussion/rest_api/tests/utils_v2.py | 53 +-- 2 files changed, 201 insertions(+), 229 deletions(-) diff --git a/lms/djangoapps/discussion/rest_api/tests/test_views_v2.py b/lms/djangoapps/discussion/rest_api/tests/test_views_v2.py index d3e817d1c9c8..8870f1c20f5e 100644 --- a/lms/djangoapps/discussion/rest_api/tests/test_views_v2.py +++ b/lms/djangoapps/discussion/rest_api/tests/test_views_v2.py @@ -134,42 +134,29 @@ def setUp(self): self.user.profile.save() CourseEnrollmentFactory.create(user=self.user, course_id=self.course.id) self.client.login(username=self.user.username, password=self.password) - - patcher = mock.patch( + mock.patch( + "lms.djangoapps.discussion.toggles.ENABLE_FORUM_V2.is_enabled", + return_value=True, + ).start() + self.mock_get_thread = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_thread" - ) - self.mock_get_thread = patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_update_thread = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.models.forum_api.update_thread" - ) - self.mock_update_thread = patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_get_parent_comment = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.models.forum_api.get_parent_comment" - ) - self.mock_get_parent_comment = patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_update_comment = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.models.forum_api.update_comment" - ) - self.mock_update_comment = patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_create_parent_comment = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.models.forum_api.create_parent_comment" - ) - self.mock_create_parent_comment = patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_create_child_comment = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.models.forum_api.create_child_comment" - ) - self.mock_create_child_comment = patcher.start() - self.addCleanup(patcher.stop) + ).start() + self.addCleanup(mock.patch.stopall) def assert_response_correct(self, response, expected_status, expected_content): """ @@ -258,18 +245,14 @@ def setUp(self): ) self.url = reverse("upload_file", kwargs={"course_id": str(self.course.id)}) - patcher = mock.patch( + mock.patch( "lms.djangoapps.discussion.toggles.ENABLE_FORUM_V2.is_enabled", return_value=True, - ) - patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_get_user = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.user.forum_api.get_user" - ) - self.mock_get_user = patcher.start() - self.addCleanup(patcher.stop) + ).start() + self.addCleanup(mock.patch.stopall) def user_login(self): """ @@ -424,30 +407,20 @@ def setUp(self): self.addCleanup(httpretty.reset) self.addCleanup(httpretty.disable) - patcher = mock.patch( + mock.patch( "lms.djangoapps.discussion.toggles.ENABLE_FORUM_V2.is_enabled", return_value=True, - ) - patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_get_user_threads = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.user.forum_api.get_user_threads" - ) - self.mock_get_user_threads = patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_get_thread = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_thread" - ) - self.mock_get_thread = patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_get_user = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.user.forum_api.get_user" - ) - self.mock_get_user = patcher.start() - self.addCleanup(patcher.stop) + ).start() + self.addCleanup(mock.patch.stopall) self.user = UserFactory.create(password=self.TEST_PASSWORD) self.register_get_user_response(self.user) @@ -661,19 +634,14 @@ def setUp(self): self.url = reverse( "discussion_course", kwargs={"course_id": str(self.course.id)} ) - - patcher = mock.patch( + mock.patch( "lms.djangoapps.discussion.toggles.ENABLE_FORUM_V2.is_enabled", return_value=True, - ) - patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_get_user = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.user.forum_api.get_user" - ) - self.mock_get_user = patcher.start() - self.addCleanup(patcher.stop) + ).start() + self.addCleanup(mock.patch.stopall) def test_404(self): response = self.client.get( @@ -740,24 +708,17 @@ def setUp(self): self.retired_username = get_retired_username_by_username(self.user.username) self.url = reverse("retire_discussion_user") - patcher = mock.patch( + mock.patch( "lms.djangoapps.discussion.toggles.ENABLE_FORUM_V2.is_enabled", return_value=True, - ) - patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_get_user = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.user.forum_api.get_user" - ) - self.mock_get_user = patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_retire_user = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.user.forum_api.retire_user" - ) - self.mock_retire_user = patcher.start() - self.addCleanup(patcher.stop) + ).start() + self.addCleanup(mock.patch.stopall) def assert_response_correct(self, response, expected_status, expected_content): """ @@ -857,24 +818,17 @@ def setUp(self): self.new_username = "test_username_replacement" self.url = reverse("replace_discussion_username") - patcher = mock.patch( + mock.patch( "lms.djangoapps.discussion.toggles.ENABLE_FORUM_V2.is_enabled", return_value=True, - ) - patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_get_user = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.user.forum_api.get_user" - ) - self.mock_get_user = patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_update_username = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.user.forum_api.update_username" - ) - self.mock_update_username = patcher.start() - self.addCleanup(patcher.stop) + ).start() + self.addCleanup(mock.patch.stopall) def assert_response_correct(self, response, expected_status, expected_content): """ @@ -1023,28 +977,21 @@ def setUp(self) -> None: }, topic_ids[0]: dict(discussion=0, question=0), } - patcher = mock.patch( + mock.patch( "lms.djangoapps.discussion.rest_api.api.get_course_commentable_counts", mock.Mock(return_value=self.topic_stats), - ) - patcher.start() - self.addCleanup(patcher.stop) + ).start() self.url = reverse( "course_topics_v3", kwargs={"course_id": str(self.course.id)} ) - - patcher = mock.patch( + mock.patch( "lms.djangoapps.discussion.toggles.ENABLE_FORUM_V2.is_enabled", return_value=True, - ) - patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_get_user = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.user.forum_api.get_user" - ) - self.mock_get_user = patcher.start() - self.addCleanup(patcher.stop) + ).start() + self.addCleanup(mock.patch.stopall) def test_basic(self): response = self.client.get(self.url) @@ -1094,29 +1041,20 @@ def setUp(self): super().setUp() self.author = UserFactory.create() self.url = reverse("thread-list") - - patcher = mock.patch( + mock.patch( "lms.djangoapps.discussion.toggles.ENABLE_FORUM_V2.is_enabled", return_value=True, - ) - patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_get_user = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.user.forum_api.get_user" - ) - self.mock_get_user = patcher.start() - self.addCleanup(patcher.stop) - patcher = mock.patch( + ).start() + self.mock_get_user_threads = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.user.forum_api.get_user_threads" - ) - self.mock_get_user_threads = patcher.start() - self.addCleanup(patcher.stop) - patcher = mock.patch( + ).start() + self.mock_search_threads = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.search_threads" - ) - self.mock_search_threads = patcher.start() - self.addCleanup(patcher.stop) + ).start() + self.addCleanup(mock.patch.stopall) def create_source_thread(self, overrides=None): """ @@ -1512,22 +1450,17 @@ class ThreadViewSetCreateTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): def setUp(self): super().setUp() self.url = reverse("thread-list") - patcher = mock.patch( + mock.patch( "lms.djangoapps.discussion.toggles.ENABLE_FORUM_V2.is_enabled", return_value=True, - ) - patcher.start() - self.addCleanup(patcher.stop) - patcher = mock.patch( + ).start() + self.mock_get_user = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.user.forum_api.get_user" - ) - self.mock_get_user = patcher.start() - self.addCleanup(patcher.stop) - patcher = mock.patch( + ).start() + self.mock_create_thread = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.models.forum_api.create_thread" - ) - self.mock_create_thread = patcher.start() - self.addCleanup(patcher.stop) + ).start() + self.addCleanup(mock.patch.stopall) def test_basic(self): self.register_get_user_response(self.user) @@ -1550,15 +1483,14 @@ def test_basic(self): self.url, json.dumps(request_data), content_type="application/json" ) self.mock_create_thread.assert_called_once_with( - "Test Title", - "# Test \n This is a very long body but will not be truncated for the preview.", - str(self.course.id), - str(self.user.id), - False, - False, - "test_topic", - "discussion", - None, + title="Test Title", + body="# Test \n This is a very long body but will not be truncated for the preview.", + course_id=str(self.course.id), + user_id=str(self.user.id), + anonymous=False, + anonymous_to_peers=False, + commentable_id="test_topic", + thread_type="discussion", ) def test_error(self): @@ -1614,43 +1546,32 @@ def setUp(self): } ) ) - patcher = mock.patch( + mock.patch( "lms.djangoapps.discussion.toggles.ENABLE_FORUM_V2.is_enabled", return_value=True, - ) - patcher.start() - self.addCleanup(patcher.stop) - - patcher = mock.patch( + ).start() + self.mock_get_user = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.user.forum_api.get_user" - ) - self.mock_get_user = patcher.start() - self.addCleanup(patcher.stop) - patcher = mock.patch( + ).start() + self.mock_get_course_id_by_thread = 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.addCleanup(patcher.stop) - patcher = mock.patch( + ).start() + self.mock_get_thread = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_thread" - ) - self.mock_get_thread = patcher.start() - self.addCleanup(patcher.stop) - patcher = mock.patch( + ).start() + self.mock_update_thread = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.models.forum_api.update_thread" - ) - self.mock_update_thread = patcher.start() - self.addCleanup(patcher.stop) - patcher = mock.patch( + ).start() + self.mock_update_thread_flag = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.update_thread_flag" - ) - self.mock_update_thread_flag = patcher.start() - self.addCleanup(patcher.stop) - patcher = mock.patch( + ).start() + self.mock_update_thread_flag_in_comment = mock.patch( "openedx.core.djangoapps.django_comment_common.comment_client.comment.forum_api.update_thread_flag" - ) - self.mock_update_thread_flag_in_comment = patcher.start() - self.addCleanup(patcher.stop) + ).start() + self.mock_mark_thread_as_read = mock.patch( + "openedx.core.djangoapps.django_comment_common.comment_client.user.forum_api.mark_thread_as_read" + ).start() + self.addCleanup(mock.patch.stopall) def test_basic(self): self.register_get_user_response(self.user) @@ -1741,3 +1662,103 @@ def test_closed_thread_error(self, field, value): request_data = {field: value} response = self.request_patch(request_data) assert response.status_code == 400 + + def test_patch_read_owner_user(self): + self.register_get_user_response(self.user) + self.register_thread({"resp_total": 2}) + self.register_read_response(self.user, "thread", "test_thread") + request_data = {"read": True} + + response = self.request_patch(request_data) + assert response.status_code == 200 + response_data = json.loads(response.content.decode("utf-8")) + assert response_data == self.expected_thread_data( + { + "comment_count": 1, + "read": True, + "editable_fields": [ + "abuse_flagged", + "anonymous", + "copy_link", + "following", + "raw_body", + "read", + "title", + "topic_id", + "type", + ], + "response_count": 2, + } + ) + self.mock_mark_thread_as_read.assert_called_once_with( + str(self.user.id), "test_thread", course_id=str(self.course.id) + ) + + def test_patch_read_non_owner_user(self): + self.register_get_user_response(self.user) + thread_owner_user = UserFactory.create(password=self.password) + CourseEnrollmentFactory.create(user=thread_owner_user, course_id=self.course.id) + self.register_get_user_response(thread_owner_user) + self.register_thread( + { + "username": thread_owner_user.username, + "user_id": str(thread_owner_user.id), + "resp_total": 2, + } + ) + self.register_read_response(self.user, "thread", "test_thread") + + request_data = {"read": True} + self.request_patch(request_data) + self.mock_mark_thread_as_read.assert_called_once_with( + str(thread_owner_user.id), "test_thread", course_id=str(self.course.id) + ) + + +# @httpretty.activate +# @disable_signal(api, 'thread_deleted') +# @mock.patch.dict("django.conf.settings.FEATURES", {"ENABLE_DISCUSSION_SERVICE": True}) +# class ThreadViewSetDeleteTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase): +# """Tests for ThreadViewSet delete""" + +# def setUp(self): +# super().setUp() +# self.url = reverse("thread-detail", kwargs={"thread_id": "test_thread"}) +# self.thread_id = "test_thread" +# mock.patch( +# "lms.djangoapps.discussion.toggles.ENABLE_FORUM_V2.is_enabled", +# return_value=True, +# ).start() +# self.mock_get_user = mock.patch( +# "openedx.core.djangoapps.django_comment_common.comment_client.user.forum_api.get_user" +# ).start() +# self.mock_get_course_id_by_thread = mock.patch( +# "openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_course_id_by_thread" +# ).start() +# self.mock_get_thread = mock.patch( +# "openedx.core.djangoapps.django_comment_common.comment_client.thread.forum_api.get_thread" +# ).start() +# self.mock_delete_thread = mock.patch( +# "openedx.core.djangoapps.django_comment_common.comment_client.models.forum_api.delete_thread" +# ).start() +# self.addCleanup(mock.patch.stopall) + +# def test_basic(self): +# self.register_get_user_response(self.user) +# cs_thread = make_minimal_cs_thread({ +# "id": self.thread_id, +# "course_id": str(self.course.id), +# "username": self.user.username, +# "user_id": str(self.user.id), +# }) +# self.register_get_thread_response(cs_thread) +# self.register_delete_thread_response(self.thread_id) +# response = self.client.delete(self.url) +# assert response.status_code == 204 +# assert response.content == b'' +# self.mock_delete_thread.assert_called_once_with(self.thread_id) + +# def test_delete_nonexistent_thread(self): +# self.register_get_thread_error_response(self.thread_id, 404) +# response = self.client.delete(self.url) +# assert response.status_code == 404 diff --git a/lms/djangoapps/discussion/rest_api/tests/utils_v2.py b/lms/djangoapps/discussion/rest_api/tests/utils_v2.py index 57b52354792d..58c38b722e60 100644 --- a/lms/djangoapps/discussion/rest_api/tests/utils_v2.py +++ b/lms/djangoapps/discussion/rest_api/tests/utils_v2.py @@ -58,42 +58,6 @@ def callback(request, _uri, headers): return callback -def _get_comment_callback(comment_data, thread_id, parent_id): - """ - Get a callback function that will return a comment containing the given data - plus necessary dummy data, overridden by the content of the POST/PUT - request. - """ - - def callback(request, _uri, headers): - """ - Simulate the comment creation or update endpoint as described above. - """ - response_data = make_minimal_cs_comment(comment_data) - original_data = response_data.copy() - # thread_id and parent_id are not included in request payload but - # are returned by the comments service - response_data["thread_id"] = thread_id - response_data["parent_id"] = parent_id - for key, val_list in parsed_body(request).items(): - val = val_list[0] - if key in ["anonymous", "anonymous_to_peers", "endorsed"]: - response_data[key] = val == "True" - elif key == "edit_reason_code": - response_data["edit_history"] = [ - { - "original_body": original_data["body"], - "author": comment_data.get("username"), - "reason_code": val, - }, - ] - else: - response_data[key] = val - return response_data - - return callback - - class CommentsServiceMockMixin: """Mixin with utility methods for mocking the comments service""" @@ -347,14 +311,7 @@ def register_read_response(self, user, content_type, content_id): """ Register a mock response for POST on the CS 'read' endpoint """ - assert httpretty.is_enabled(), "httpretty must be enabled to mock calls." - httpretty.register_uri( - httpretty.POST, - f"http://localhost:4567/api/v1/users/{user.id}/read", - params={"source_type": content_type, "source_id": content_id}, - body=json.dumps({}), # body is unused - status=200, - ) + self.mock_mark_thread_as_read.return_value = {} def register_thread_flag_response(self, thread_id): """Register a mock response for PUT on the CS thread flag endpoints""" @@ -368,13 +325,7 @@ def register_delete_thread_response(self, thread_id): """ Register a mock response for DELETE on the CS thread instance endpoint """ - assert httpretty.is_enabled(), "httpretty must be enabled to mock calls." - httpretty.register_uri( - httpretty.DELETE, - f"http://localhost:4567/api/v1/threads/{thread_id}", - body=json.dumps({}), # body is unused - status=200, - ) + self.mock_delete_thread.return_value = {} def register_delete_comment_response(self, comment_id): """