From c9c1a4bc71fe68487b528a552f3c64bcf7930893 Mon Sep 17 00:00:00 2001 From: wajeeha-khalid Date: Wed, 2 Dec 2015 12:04:52 +0500 Subject: [PATCH] jia/MA-1748 patch read states for users --- lms/djangoapps/discussion_api/serializers.py | 2 +- .../discussion_api/tests/test_api.py | 1 + .../discussion_api/tests/test_serializers.py | 2 + .../discussion_api/tests/test_views.py | 78 +++++++++++++++++++ lms/lib/comment_client/models.py | 10 ++- 5 files changed, 90 insertions(+), 3 deletions(-) diff --git a/lms/djangoapps/discussion_api/serializers.py b/lms/djangoapps/discussion_api/serializers.py index ac71e89c8a..9d9763ea60 100644 --- a/lms/djangoapps/discussion_api/serializers.py +++ b/lms/djangoapps/discussion_api/serializers.py @@ -272,7 +272,7 @@ class ThreadSerializer(_ContentSerializer): def update(self, instance, validated_data): for key, val in validated_data.items(): instance[key] = val - instance.save() + instance.save(params={"requested_user_id": self.context["cc_requester"]["id"]}) return instance diff --git a/lms/djangoapps/discussion_api/tests/test_api.py b/lms/djangoapps/discussion_api/tests/test_api.py index 62e6c4306a..3294bd8d7f 100644 --- a/lms/djangoapps/discussion_api/tests/test_api.py +++ b/lms/djangoapps/discussion_api/tests/test_api.py @@ -1968,6 +1968,7 @@ class UpdateThreadTest( "closed": ["False"], "pinned": ["False"], "read": ["False"], + "requested_user_id": [str(self.user.id)], } ) diff --git a/lms/djangoapps/discussion_api/tests/test_serializers.py b/lms/djangoapps/discussion_api/tests/test_serializers.py index 2ffe52317d..2b1dc38b92 100644 --- a/lms/djangoapps/discussion_api/tests/test_serializers.py +++ b/lms/djangoapps/discussion_api/tests/test_serializers.py @@ -562,6 +562,7 @@ class ThreadSerializerDeserializationTest(CommentsServiceMockMixin, UrlResetMixi "pinned": ["False"], "user_id": [str(self.user.id)], "read": ["False"], + "requested_user_id": [str(self.user.id)], } ) @@ -590,6 +591,7 @@ class ThreadSerializerDeserializationTest(CommentsServiceMockMixin, UrlResetMixi "pinned": ["False"], "user_id": [str(self.user.id)], "read": [str(read)], + "requested_user_id": [str(self.user.id)], } ) for key in data: diff --git a/lms/djangoapps/discussion_api/tests/test_views.py b/lms/djangoapps/discussion_api/tests/test_views.py index 343c6658ac..44dcde6def 100644 --- a/lms/djangoapps/discussion_api/tests/test_views.py +++ b/lms/djangoapps/discussion_api/tests/test_views.py @@ -683,6 +683,7 @@ class ThreadViewSetPartialUpdateTest(DiscussionAPIViewTestMixin, ModuleStoreTest "closed": ["False"], "pinned": ["False"], "read": ["False"], + "requested_user_id": [str(self.user.id)], } ) @@ -736,6 +737,83 @@ class ThreadViewSetPartialUpdateTest(DiscussionAPIViewTestMixin, ModuleStoreTest response = self.request_patch(request_data) self.assertEqual(response.status_code, 400) + def test_patch_read_owner_user(self): + self.register_get_user_response(self.user) + self.register_thread() + request_data = {"read": True} + response = self.request_patch(request_data) + self.assertEqual(response.status_code, 200) + response_data = json.loads(response.content) + self.assertEqual( + response_data, + self.expected_response_data({ + "comment_count": 1, + "read": True, + "editable_fields": [ + "abuse_flagged", "following", "raw_body", "read", "title", "topic_id", "type", "voted" + ], + }) + ) + + self.assertEqual( + httpretty.last_request().parsed_body, + { + "course_id": [unicode(self.course.id)], + "commentable_id": ["original_topic"], + "thread_type": ["discussion"], + "title": ["Original Title"], + "body": ["Original body"], + "user_id": [str(self.user.id)], + "anonymous": ["False"], + "anonymous_to_peers": ["False"], + "closed": ["False"], + "pinned": ["False"], + "read": ["True"], + "requested_user_id": [str(self.user.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)}) + + request_data = {"read": True} + response = self.request_patch(request_data) + self.assertEqual(response.status_code, 200) + response_data = json.loads(response.content) + self.assertEqual( + response_data, + self.expected_response_data({ + "author": str(thread_owner_user.username), + "comment_count": 1, + "read": True, + "editable_fields": [ + "abuse_flagged", "following", "read", "voted" + ], + }) + ) + + self.assertEqual( + httpretty.last_request().parsed_body, + { + "course_id": [unicode(self.course.id)], + "commentable_id": ["original_topic"], + "thread_type": ["discussion"], + "title": ["Original Title"], + "body": ["Original body"], + "user_id": [str(thread_owner_user.id)], + "anonymous": ["False"], + "anonymous_to_peers": ["False"], + "closed": ["False"], + "pinned": ["False"], + "read": ["True"], + "requested_user_id": [str(self.user.id)], + } + ) + @httpretty.activate @disable_signal(api, 'thread_deleted') diff --git a/lms/lib/comment_client/models.py b/lms/lib/comment_client/models.py index 149f5cdc04..ed7f5c292b 100644 --- a/lms/lib/comment_client/models.py +++ b/lms/lib/comment_client/models.py @@ -124,14 +124,20 @@ class Model(object): def after_save(cls, instance): pass - def save(self): + def save(self, params=None): + """ + Invokes Forum's POST/PUT service to create/update thread + """ self.before_save(self) if self.id: # if we have id already, treat this as an update + request_params = self.updatable_attributes() + if params: + request_params.update(params) url = self.url(action='put', params=self.attributes) response = perform_request( 'put', url, - self.updatable_attributes(), + request_params, metric_tags=self._metric_tags, metric_action='model.update' )