Commit c9c1a4bc by wajeeha-khalid

jia/MA-1748 patch read states for users

parent f2c4a3de
......@@ -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
......
......@@ -1968,6 +1968,7 @@ class UpdateThreadTest(
"closed": ["False"],
"pinned": ["False"],
"read": ["False"],
"requested_user_id": [str(self.user.id)],
}
)
......
......@@ -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:
......
......@@ -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')
......
......@@ -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'
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment