def serialize(self, comment, thread_data=None): """ Create a serializer with an appropriate context and use it to serialize the given comment, returning the result. """ context = get_context(self.course, self.request, make_minimal_cs_thread(thread_data)) return CommentSerializer(comment, context=context).data
def test_create_parent_id_too_deep(self, max_depth): with mock.patch("lms.djangoapps.discussion.django_comment_client.utils.MAX_COMMENT_DEPTH", max_depth): data = self.minimal_data.copy() context = get_context(self.course, self.request, make_minimal_cs_thread()) if max_depth is None or max_depth >= 0: if max_depth != 0: self.register_get_comment_response({ "id": "not_too_deep", "thread_id": "test_thread", "depth": max_depth - 1 if max_depth else 100 }) data["parent_id"] = "not_too_deep" else: data["parent_id"] = None serializer = CommentSerializer(data=data, context=context) self.assertTrue(serializer.is_valid(), serializer.errors) if max_depth is not None: if max_depth >= 0: self.register_get_comment_response({ "id": "too_deep", "thread_id": "test_thread", "depth": max_depth }) data["parent_id"] = "too_deep" else: data["parent_id"] = None serializer = CommentSerializer(data=data, context=context) self.assertFalse(serializer.is_valid()) self.assertEqual(serializer.errors, {"non_field_errors": ["Comment level is too deep."]})
def setUp(self): super().setUp() httpretty.reset() httpretty.enable() self.addCleanup(httpretty.reset) self.addCleanup(httpretty.disable) self.user = UserFactory.create() self.register_get_user_response(self.user) self.request = RequestFactory().get("/dummy") self.request.user = self.user self.minimal_data = { "course_id": str(self.course.id), "topic_id": "test_topic", "type": "discussion", "title": "Test Title", "raw_body": "Test body", } self.existing_thread = Thread( **make_minimal_cs_thread({ "id": "existing_thread", "course_id": str(self.course.id), "commentable_id": "original_topic", "thread_type": "discussion", "title": "Original Title", "body": "Original body", "user_id": str(self.user.id), "username": self.user.username, "read": "False", "endorsed": "False" }))
def test_create_parent_id_too_deep(self, max_depth): with mock.patch("lms.djangoapps.discussion.django_comment_client.utils.MAX_COMMENT_DEPTH", max_depth): data = self.minimal_data.copy() context = get_context(self.course, self.request, make_minimal_cs_thread()) if max_depth is None or max_depth >= 0: if max_depth != 0: self.register_get_comment_response({ "id": "not_too_deep", "thread_id": "test_thread", "depth": max_depth - 1 if max_depth else 100 }) data["parent_id"] = "not_too_deep" else: data["parent_id"] = None serializer = CommentSerializer(data=data, context=context) assert serializer.is_valid(), serializer.errors if max_depth is not None: if max_depth >= 0: self.register_get_comment_response({ "id": "too_deep", "thread_id": "test_thread", "depth": max_depth }) data["parent_id"] = "too_deep" else: data["parent_id"] = None serializer = CommentSerializer(data=data, context=context) assert not serializer.is_valid() assert serializer.errors == {'non_field_errors': ['Comment level is too deep.']}
def setUp(self): super(ThreadSerializerDeserializationTest, self).setUp() httpretty.reset() httpretty.enable() self.addCleanup(httpretty.reset) self.addCleanup(httpretty.disable) self.user = UserFactory.create() self.register_get_user_response(self.user) self.request = RequestFactory().get("/dummy") self.request.user = self.user self.minimal_data = { "course_id": six.text_type(self.course.id), "topic_id": "test_topic", "type": "discussion", "title": "Test Title", "raw_body": "Test body", } self.existing_thread = Thread(**make_minimal_cs_thread({ "id": "existing_thread", "course_id": six.text_type(self.course.id), "commentable_id": "original_topic", "thread_type": "discussion", "title": "Original Title", "body": "Original body", "user_id": str(self.user.id), "username": self.user.username, "read": "False", "endorsed": "False" }))
def test_create_missing_field(self): for field in self.minimal_data: data = self.minimal_data.copy() data.pop(field) serializer = CommentSerializer(data=data, context=get_context( self.course, self.request, make_minimal_cs_thread())) assert not serializer.is_valid() assert serializer.errors == {field: ['This field is required.']}
def test_create_parent_id_wrong_thread(self): self.register_get_comment_response({"thread_id": "different_thread", "id": "test_parent"}) data = self.minimal_data.copy() data["parent_id"] = "test_parent" context = get_context(self.course, self.request, make_minimal_cs_thread()) serializer = CommentSerializer(data=data, context=context) assert not serializer.is_valid() assert serializer.errors == { 'non_field_errors': ['parent_id does not identify a comment in the thread identified by thread_id.'] }
def test_basic(self): thread = make_minimal_cs_thread({ "id": "test_thread", "course_id": six.text_type(self.course.id), "commentable_id": "test_topic", "user_id": str(self.author.id), "username": self.author.username, "title": "Test Title", "body": "Test body", "pinned": True, "votes": { "up_count": 4 }, "comments_count": 5, "unread_comments_count": 3, }) expected = self.expected_thread_data({ "author": self.author.username, "vote_count": 4, "comment_count": 6, "unread_comment_count": 3, "pinned": True, "editable_fields": ["abuse_flagged", "following", "read", "voted"], }) self.assertEqual(self.serialize(thread), expected) thread["thread_type"] = "question" expected.update({ "type": "question", "comment_list_url": None, "endorsed_comment_list_url": ("http://testserver/api/discussion/v1/comments/?thread_id=test_thread&endorsed=True" ), "non_endorsed_comment_list_url": ("http://testserver/api/discussion/v1/comments/?thread_id=test_thread&endorsed=False" ), }) self.assertEqual(self.serialize(thread), expected)
def test_create_missing_field(self): for field in self.minimal_data: data = self.minimal_data.copy() data.pop(field) serializer = CommentSerializer( data=data, context=get_context(self.course, self.request, make_minimal_cs_thread()) ) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors, {field: ["This field is required."]} )
def make_cs_content(self, overrides): """ Create a thread with the given overrides, plus some useful test data. """ merged_overrides = { "course_id": six.text_type(self.course.id), "user_id": str(self.author.id), "username": self.author.username, "read": True, "endorsed": True, "resp_total": 0, } merged_overrides.update(overrides) return make_minimal_cs_thread(merged_overrides)
def make_cs_content(self, overrides): """ Create a thread with the given overrides, plus some useful test data. """ merged_overrides = { "course_id": str(self.course.id), "user_id": str(self.author.id), "username": self.author.username, "read": True, "endorsed": True, "resp_total": 0, } merged_overrides.update(overrides) return make_minimal_cs_thread(merged_overrides)
def test_create_parent_id_nonexistent(self): self.register_get_comment_error_response("bad_parent", 404) data = self.minimal_data.copy() data["parent_id"] = "bad_parent" context = get_context(self.course, self.request, make_minimal_cs_thread()) serializer = CommentSerializer(data=data, context=context) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors, { "non_field_errors": [ "parent_id does not identify a comment in the thread identified by thread_id." ] })
def test_create_parent_id_wrong_thread(self): self.register_get_comment_response({"thread_id": "different_thread", "id": "test_parent"}) data = self.minimal_data.copy() data["parent_id"] = "test_parent" context = get_context(self.course, self.request, make_minimal_cs_thread()) serializer = CommentSerializer(data=data, context=context) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors, { "non_field_errors": [ "parent_id does not identify a comment in the thread identified by thread_id." ] } )
def save_and_reserialize(self, data, instance=None): """ Create a serializer with the given data, ensure that it is valid, save the result, and return the full comment data from the serializer. """ context = get_context( self.course, self.request, make_minimal_cs_thread({"course_id": str(self.course.id)})) serializer = CommentSerializer(instance, data=data, partial=(instance is not None), context=context) assert serializer.is_valid() serializer.save() return serializer.data
def save_and_reserialize(self, data, instance=None): """ Create a serializer with the given data, ensure that it is valid, save the result, and return the full comment data from the serializer. """ context = get_context( self.course, self.request, make_minimal_cs_thread({"course_id": six.text_type(self.course.id)}) ) serializer = CommentSerializer( instance, data=data, partial=(instance is not None), context=context ) self.assertTrue(serializer.is_valid()) serializer.save() return serializer.data
def test_basic(self): thread = make_minimal_cs_thread({ "id": "test_thread", "course_id": six.text_type(self.course.id), "commentable_id": "test_topic", "user_id": str(self.author.id), "username": self.author.username, "title": "Test Title", "body": "Test body", "pinned": True, "votes": {"up_count": 4}, "comments_count": 5, "unread_comments_count": 3, }) expected = self.expected_thread_data({ "author": self.author.username, "vote_count": 4, "comment_count": 6, "unread_comment_count": 3, "pinned": True, "editable_fields": ["abuse_flagged", "following", "read", "voted"], }) self.assertEqual(self.serialize(thread), expected) thread["thread_type"] = "question" expected.update({ "type": "question", "comment_list_url": None, "endorsed_comment_list_url": ( "http://testserver/api/discussion/v1/comments/?thread_id=test_thread&endorsed=True" ), "non_endorsed_comment_list_url": ( "http://testserver/api/discussion/v1/comments/?thread_id=test_thread&endorsed=False" ), }) self.assertEqual(self.serialize(thread), expected)