def test_basic(self): self.register_get_user_response(self.user) self.register_get_thread_response( make_minimal_cs_thread({ "id": "test_thread", "course_id": unicode(self.course.id), "commentable_id": "test_topic", }) ) self.register_post_comment_response( { "id": "test_comment", "username": self.user.username, "created_at": "2015-05-27T00:00:00Z", "updated_at": "2015-05-27T00:00:00Z", }, thread_id="test_thread" ) request_data = { "thread_id": "test_thread", "raw_body": "Test body", } expected_response_data = { "id": "test_comment", "thread_id": "test_thread", "parent_id": None, "author": self.user.username, "author_label": None, "created_at": "2015-05-27T00:00:00Z", "updated_at": "2015-05-27T00:00:00Z", "raw_body": "Test body", "endorsed": False, "endorsed_by": None, "endorsed_by_label": None, "endorsed_at": None, "abuse_flagged": False, "voted": False, "vote_count": 0, "children": [], } response = self.client.post( self.url, json.dumps(request_data), content_type="application/json" ) self.assertEqual(response.status_code, 200) response_data = json.loads(response.content) self.assertEqual(response_data, expected_response_data) self.assertEqual( urlparse(httpretty.last_request().path).path, "/api/v1/threads/test_thread/comments" ) self.assertEqual( httpretty.last_request().parsed_body, { "course_id": [unicode(self.course.id)], "body": ["Test body"], "user_id": [str(self.user.id)], } )
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": unicode(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": unicode(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 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_basic(self): self.register_get_user_response(self.user) cs_comment_child = self.make_comment_data("test_child_comment", self.comment_id, children=[]) cs_comment = self.make_comment_data(self.comment_id, None, [cs_comment_child]) cs_thread = make_minimal_cs_thread( {"id": self.thread_id, "course_id": unicode(self.course.id), "children": [cs_comment]} ) self.register_get_thread_response(cs_thread) self.register_get_comment_response(cs_comment) expected_response_data = { "id": "test_child_comment", "parent_id": self.comment_id, "thread_id": self.thread_id, "author": self.user.username, "author_label": None, "raw_body": "Original body", "rendered_body": "<p>Original body</p>", "created_at": "2015-06-03T00:00:00Z", "updated_at": "2015-06-03T00:00:00Z", "children": [], "endorsed_at": None, "endorsed": False, "endorsed_by": None, "endorsed_by_label": None, "voted": False, "vote_count": 0, "abuse_flagged": False, "editable_fields": ["abuse_flagged", "raw_body", "voted"], } response = self.client.get(self.url) self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(response.content)["results"][0], expected_response_data)
def test_create_parent_id_too_deep(self, max_depth): with mock.patch("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 test_pagination(self): """ Test that pagination parameters are correctly plumbed through to the comments service and that a 404 is correctly returned if a page past the end is requested """ self.register_get_user_response(self.user) self.register_get_thread_response(make_minimal_cs_thread({ "id": self.thread_id, "course_id": unicode(self.course.id), "thread_type": "discussion", "children": [], "resp_total": 10, })) response = self.client.get( self.url, {"thread_id": self.thread_id, "page": "18", "page_size": "4"} ) self.assert_response_correct( response, 404, {"developer_message": "Not found."} ) self.assert_query_params_equal( httpretty.httpretty.latest_requests[-2], { "recursive": ["True"], "resp_skip": ["68"], "resp_limit": ["4"], "user_id": [str(self.user.id)], "mark_as_read": ["True"], } )
def test_order_by(self, http_query, cc_query): """ Tests the order_by parameter Arguments: http_query (str): Query string sent in the http request cc_query (str): Query string used for the comments client service """ threads = [make_minimal_cs_thread()] self.register_get_user_response(self.user) self.register_get_threads_response(threads, page=1, num_pages=1) self.client.get( self.url, { "course_id": unicode(self.course.id), "order_by": http_query, } ) self.assert_last_query_params({ "user_id": [unicode(self.user.id)], "course_id": [unicode(self.course.id)], "sort_order": ["desc"], "recursive": ["False"], "page": ["1"], "per_page": ["10"], "sort_key": [cc_query], })
def setUp(self): super(CommentViewSetPartialUpdateTest, self).setUp() httpretty.reset() httpretty.enable() self.addCleanup(httpretty.disable) self.register_get_user_response(self.user) self.url = reverse("comment-detail", kwargs={"comment_id": "test_comment"}) cs_thread = make_minimal_cs_thread({ "id": "test_thread", "course_id": unicode(self.course.id), }) self.register_get_thread_response(cs_thread) cs_comment = make_minimal_cs_comment({ "id": "test_comment", "course_id": cs_thread["course_id"], "thread_id": cs_thread["id"], "username": self.user.username, "user_id": str(self.user.id), "created_at": "2015-06-03T00:00:00Z", "updated_at": "2015-06-03T00:00:00Z", "body": "Original body", }) self.register_get_comment_response(cs_comment) self.register_put_comment_response(cs_comment)
def test_pagination(self): """ Test that pagination parameters are correctly plumbed through to the comments service and that a 404 is correctly returned if a page past the end is requested """ self.register_get_user_response(self.user) self.register_get_thread_response( make_minimal_cs_thread({ "id": self.thread_id, "course_id": unicode(self.course.id), "thread_type": "discussion", "children": [], "resp_total": 10, })) response = self.client.get(self.url, { "thread_id": self.thread_id, "page": "18", "page_size": "4" }) self.assert_response_correct(response, 404, {"developer_message": "Not found."}) self.assert_query_params_equal( httpretty.httpretty.latest_requests[-2], { "recursive": ["False"], "resp_skip": ["68"], "resp_limit": ["4"], "user_id": [str(self.user.id)], "mark_as_read": ["False"], })
def test_basic(self): self.register_get_user_response(self.user) cs_thread = make_minimal_cs_thread({ "id": "test_thread", "course_id": unicode(self.course.id), }) self.register_get_thread_response(cs_thread) cs_comment = make_minimal_cs_comment({ "id": self.comment_id, "course_id": cs_thread["course_id"], "thread_id": cs_thread["id"], "username": self.user.username, "user_id": str(self.user.id), }) self.register_get_comment_response(cs_comment) self.register_delete_comment_response(self.comment_id) response = self.client.delete(self.url) self.assertEqual(response.status_code, 204) self.assertEqual(response.content, "") self.assertEqual( urlparse(httpretty.last_request().path).path, "/api/v1/comments/{}".format(self.comment_id)) self.assertEqual(httpretty.last_request().method, "DELETE")
def test_nonexistent_course(self): self.register_get_thread_response( make_minimal_cs_thread({"id": "test_thread", "course_id": "non/existent/course"}) ) with self.assertRaises(ValidationError) as assertion: create_comment(self.request, self.minimal_data) self.assertEqual(assertion.exception.message_dict, {"thread_id": ["Invalid value."]})
def make_minimal_cs_thread(self, overrides=None): """ Create a thread with the given overrides, plus the course_id if not already in overrides. """ overrides = overrides.copy() if overrides else {} overrides.setdefault("course_id", unicode(self.course.id)) return make_minimal_cs_thread(overrides)
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 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": unicode(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): self.register_get_user_response(self.user) self.register_get_thread_response( make_minimal_cs_thread({ "id": "test_thread", "course_id": unicode(self.course.id), "commentable_id": "test_topic", })) self.register_post_comment_response( { "id": "test_comment", "username": self.user.username, "created_at": "2015-05-27T00:00:00Z", "updated_at": "2015-05-27T00:00:00Z", }, thread_id="test_thread") request_data = { "thread_id": "test_thread", "raw_body": "Test body", } expected_response_data = { "id": "test_comment", "thread_id": "test_thread", "parent_id": None, "author": self.user.username, "author_label": None, "created_at": "2015-05-27T00:00:00Z", "updated_at": "2015-05-27T00:00:00Z", "raw_body": "Test body", "rendered_body": "<p>Test body</p>", "endorsed": False, "endorsed_by": None, "endorsed_by_label": None, "endorsed_at": None, "abuse_flagged": False, "voted": False, "vote_count": 0, "children": [], "editable_fields": ["abuse_flagged", "raw_body", "voted"], } response = self.client.post(self.url, json.dumps(request_data), content_type="application/json") self.assertEqual(response.status_code, 200) response_data = json.loads(response.content) self.assertEqual(response_data, expected_response_data) self.assertEqual( urlparse(httpretty.last_request().path).path, "/api/v1/threads/test_thread/comments") self.assertEqual( httpretty.last_request().parsed_body, { "course_id": [unicode(self.course.id)], "body": ["Test body"], "user_id": [str(self.user.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 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": unicode(self.course.id), "user_id": str(self.author.id), "username": self.author.username, } merged_overrides.update(overrides) return make_minimal_cs_thread(merged_overrides)
def test_parent_id_nonexistent(self): self.register_get_comment_error_response("bad_parent", 404) context = get_context(self.course, self.request, make_minimal_cs_thread(), "bad_parent") serializer = CommentSerializer(data=self.minimal_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_error(self): self.register_get_user_response(self.user) cs_thread = make_minimal_cs_thread( {"id": "test_thread", "course_id": unicode(self.course.id), "user_id": str(self.user.id)} ) self.register_get_thread_response(cs_thread) request_data = {"title": ""} response = self.client.patch( # pylint: disable=no-member self.url, json.dumps(request_data), content_type="application/json" ) expected_response_data = {"field_errors": {"title": {"developer_message": "This field may not be blank."}}} self.assertEqual(response.status_code, 400) response_data = json.loads(response.content) self.assertEqual(response_data, expected_response_data)
def test_basic(self): self.register_get_user_response(self.user) cs_thread = make_minimal_cs_thread( { "id": self.thread_id, "course_id": unicode(self.course.id), "commentable_id": "test_topic", "username": self.user.username, "user_id": str(self.user.id), "title": "Test Title", "body": "Test body", "created_at": "2015-05-29T00:00:00Z", "updated_at": "2015-05-29T00:00:00Z", } ) expected_response_data = { "author": self.user.username, "author_label": None, "created_at": "2015-05-29T00:00:00Z", "updated_at": "2015-05-29T00:00:00Z", "raw_body": "Test body", "rendered_body": "<p>Test body</p>", "abuse_flagged": False, "voted": False, "vote_count": 0, "editable_fields": ["abuse_flagged", "following", "raw_body", "read", "title", "topic_id", "type", "voted"], "course_id": unicode(self.course.id), "topic_id": "test_topic", "group_id": None, "group_name": None, "title": "Test Title", "pinned": False, "closed": False, "following": False, "comment_count": 1, "unread_comment_count": 1, "comment_list_url": "http://testserver/api/discussion/v1/comments/?thread_id=test_thread", "endorsed_comment_list_url": None, "non_endorsed_comment_list_url": None, "read": False, "has_endorsed": False, "id": "test_thread", "type": "discussion", "response_count": 0, } self.register_get_thread_response(cs_thread) response = self.client.get(self.url) self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(response.content), expected_response_data) self.assertEqual(httpretty.last_request().method, "GET")
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 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": unicode(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): self.register_get_user_response(self.user) cs_thread = make_minimal_cs_thread({ "id": self.thread_id, "course_id": unicode(self.course.id), "commentable_id": "test_topic", "username": self.user.username, "user_id": str(self.user.id), "title": "Test Title", "body": "Test body", "created_at": "2015-05-29T00:00:00Z", "updated_at": "2015-05-29T00:00:00Z" }) expected_response_data = { "author": self.user.username, "author_label": None, "created_at": "2015-05-29T00:00:00Z", "updated_at": "2015-05-29T00:00:00Z", "raw_body": "Test body", "rendered_body": "<p>Test body</p>", "abuse_flagged": False, "voted": False, "vote_count": 0, "editable_fields": ["abuse_flagged", "following", "raw_body", "read", "title", "topic_id", "type", "voted"], "course_id": unicode(self.course.id), "topic_id": "test_topic", "group_id": None, "group_name": None, "title": "Test Title", "pinned": False, "closed": False, "following": False, "comment_count": 1, "unread_comment_count": 1, "comment_list_url": "http://testserver/api/discussion/v1/comments/?thread_id=test_thread", "endorsed_comment_list_url": None, "non_endorsed_comment_list_url": None, "read": False, "has_endorsed": False, "id": "test_thread", "type": "discussion", "response_count": 0, } self.register_get_thread_response(cs_thread) response = self.client.get(self.url) self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(response.content), expected_response_data) self.assertEqual(httpretty.last_request().method, "GET")
def test_order_direction(self, query): threads = [make_minimal_cs_thread()] self.register_get_user_response(self.user) self.register_get_threads_response(threads, page=1, num_pages=1) self.client.get(self.url, {"course_id": unicode(self.course.id), "order_direction": query}) self.assert_last_query_params( { "user_id": [unicode(self.user.id)], "course_id": [unicode(self.course.id)], "sort_key": ["activity"], "recursive": ["False"], "page": ["1"], "per_page": ["10"], "sort_order": [query], } )
def test_order_direction(self, query): threads = [make_minimal_cs_thread()] self.register_get_user_response(self.user) self.register_get_threads_response(threads, page=1, num_pages=1) self.client.get(self.url, { "course_id": unicode(self.course.id), "order_direction": query, }) self.assert_last_query_params({ "user_id": [unicode(self.user.id)], "course_id": [unicode(self.course.id)], "sort_key": ["activity"], "recursive": ["False"], "page": ["1"], "per_page": ["10"], "sort_order": [query], })
def register_thread(self, overrides=None): """ Create cs_thread with minimal fields and register response """ cs_thread = make_minimal_cs_thread({ "id": "test_thread", "course_id": unicode(self.course.id), "commentable_id": "original_topic", "username": self.user.username, "user_id": str(self.user.id), "thread_type": "discussion", "title": "Original Title", "body": "Original body", }) cs_thread.update(overrides or {}) self.register_get_thread_response(cs_thread) self.register_put_thread_response(cs_thread)
def test_basic(self): self.register_get_user_response(self.user) cs_thread = make_minimal_cs_thread( { "id": self.thread_id, "course_id": unicode(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) self.assertEqual(response.status_code, 204) self.assertEqual(response.content, "") self.assertEqual(urlparse(httpretty.last_request().path).path, "/api/v1/threads/{}".format(self.thread_id)) self.assertEqual(httpretty.last_request().method, "DELETE")
def setUp(self): super(CreateCommentTest, self).setUp() httpretty.reset() httpretty.enable() self.addCleanup(httpretty.disable) self.user = UserFactory.create() self.register_get_user_response(self.user) self.request = RequestFactory().get("/test_path") self.request.user = self.user self.course = CourseFactory.create() CourseEnrollmentFactory.create(user=self.user, course_id=self.course.id) self.register_get_thread_response( make_minimal_cs_thread( {"id": "test_thread", "course_id": unicode(self.course.id), "commentable_id": "test_topic"} ) ) self.minimal_data = {"thread_id": "test_thread", "raw_body": "Test body"}
def test_basic(self): thread = make_minimal_cs_thread({ "id": "test_thread", "course_id": unicode(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_basic(self): #from nose.tools import set_trace;set_trace() self.register_get_user_response(self.user) cs_thread = make_minimal_cs_thread({ "id": self.thread_id, "course_id": unicode(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) self.assertEqual(response.status_code, 204) self.assertEqual(response.content, "") self.assertEqual( urlparse(httpretty.last_request().path).path, "/api/v1/threads/{}".format(self.thread_id)) self.assertEqual(httpretty.last_request().method, "DELETE")
def test_basic(self): thread = make_minimal_cs_thread( { "id": "test_thread", "course_id": unicode(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_error(self): self.register_get_user_response(self.user) cs_thread = make_minimal_cs_thread({ "id": "test_thread", "course_id": unicode(self.course.id), "user_id": str(self.user.id), }) self.register_get_thread_response(cs_thread) request_data = {"title": ""} response = self.client.patch( # pylint: disable=no-member self.url, json.dumps(request_data), content_type="application/json" ) expected_response_data = { "field_errors": {"title": {"developer_message": "This field is required."}} } self.assertEqual(response.status_code, 400) response_data = json.loads(response.content) self.assertEqual(response_data, expected_response_data)
def test_view_query(self, query): threads = [make_minimal_cs_thread()] self.register_get_user_response(self.user) self.register_get_threads_response(threads, page=1, num_pages=1) self.client.get( self.url, { "course_id": unicode(self.course.id), "view": query, } ) self.assert_last_query_params({ "user_id": [unicode(self.user.id)], "course_id": [unicode(self.course.id)], "sort_key": ["date"], "sort_order": ["desc"], "recursive": ["False"], "page": ["1"], "per_page": ["10"], query: ["true"], })
def test_basic(self): self.register_get_user_response(self.user) cs_comment_child = self.make_comment_data("test_child_comment", self.comment_id, children=[]) cs_comment = self.make_comment_data(self.comment_id, None, [cs_comment_child]) cs_thread = make_minimal_cs_thread({ "id": self.thread_id, "course_id": unicode(self.course.id), "children": [cs_comment], }) self.register_get_thread_response(cs_thread) self.register_get_comment_response(cs_comment) expected_response_data = { "id": "test_child_comment", "parent_id": self.comment_id, "thread_id": self.thread_id, "author": self.user.username, "author_label": None, "raw_body": "Original body", "rendered_body": "<p>Original body</p>", "created_at": "2015-06-03T00:00:00Z", "updated_at": "2015-06-03T00:00:00Z", "children": [], "endorsed_at": None, "endorsed": False, "endorsed_by": None, "endorsed_by_label": None, "voted": False, "vote_count": 0, "abuse_flagged": False, "editable_fields": ["abuse_flagged", "raw_body", "voted"] } response = self.client.get(self.url) self.assertEqual(response.status_code, 200) self.assertEqual( json.loads(response.content)['results'][0], expected_response_data)
def setUp(self): super(CommentViewSetPartialUpdateTest, self).setUp() httpretty.reset() httpretty.enable() self.addCleanup(httpretty.disable) self.register_get_user_response(self.user) self.url = reverse("comment-detail", kwargs={"comment_id": "test_comment"}) cs_thread = make_minimal_cs_thread({"id": "test_thread", "course_id": unicode(self.course.id)}) self.register_get_thread_response(cs_thread) cs_comment = make_minimal_cs_comment( { "id": "test_comment", "course_id": cs_thread["course_id"], "thread_id": cs_thread["id"], "username": self.user.username, "user_id": str(self.user.id), "created_at": "2015-06-03T00:00:00Z", "updated_at": "2015-06-03T00:00:00Z", "body": "Original body", } ) self.register_get_comment_response(cs_comment) self.register_put_comment_response(cs_comment)
def test_basic(self): self.register_get_user_response(self.user) cs_thread = make_minimal_cs_thread({ "id": "test_thread", "course_id": unicode(self.course.id), "commentable_id": "original_topic", "username": self.user.username, "user_id": str(self.user.id), "created_at": "2015-05-29T00:00:00Z", "updated_at": "2015-05-29T00:00:00Z", "thread_type": "discussion", "title": "Original Title", "body": "Original body", }) self.register_get_thread_response(cs_thread) self.register_put_thread_response(cs_thread) request_data = {"raw_body": "Edited body"} expected_response_data = { "id": "test_thread", "course_id": unicode(self.course.id), "topic_id": "original_topic", "group_id": None, "group_name": None, "author": self.user.username, "author_label": None, "created_at": "2015-05-29T00:00:00Z", "updated_at": "2015-05-29T00:00:00Z", "type": "discussion", "title": "Original Title", "raw_body": "Edited body", "rendered_body": "<p>Edited body</p>", "pinned": False, "closed": False, "following": False, "abuse_flagged": False, "voted": False, "vote_count": 0, "comment_count": 0, "unread_comment_count": 0, "comment_list_url": "http://testserver/api/discussion/v1/comments/?thread_id=test_thread", "endorsed_comment_list_url": None, "non_endorsed_comment_list_url": None, "editable_fields": ["abuse_flagged", "following", "raw_body", "title", "topic_id", "type", "voted"], "read": False, "has_endorsed": False } response = self.client.patch( # pylint: disable=no-member self.url, json.dumps(request_data), content_type="application/json" ) self.assertEqual(response.status_code, 200) response_data = json.loads(response.content) self.assertEqual(response_data, expected_response_data) self.assertEqual( httpretty.last_request().parsed_body, { "course_id": [unicode(self.course.id)], "commentable_id": ["original_topic"], "thread_type": ["discussion"], "title": ["Original Title"], "body": ["Edited body"], "user_id": [str(self.user.id)], "anonymous": ["False"], "anonymous_to_peers": ["False"], "closed": ["False"], "pinned": ["False"], } )
def test_basic(self): self.register_get_user_response(self.user) cs_thread = make_minimal_cs_thread({ "id": "test_thread", "course_id": unicode(self.course.id), "commentable_id": "original_topic", "username": self.user.username, "user_id": str(self.user.id), "created_at": "2015-05-29T00:00:00Z", "updated_at": "2015-05-29T00:00:00Z", "thread_type": "discussion", "title": "Original Title", "body": "Original body", }) self.register_get_thread_response(cs_thread) self.register_put_thread_response(cs_thread) request_data = {"raw_body": "Edited body"} expected_response_data = { "id": "test_thread", "course_id": unicode(self.course.id), "topic_id": "original_topic", "group_id": None, "group_name": None, "author": self.user.username, "author_label": None, "created_at": "2015-05-29T00:00:00Z", "updated_at": "2015-05-29T00:00:00Z", "type": "discussion", "title": "Original Title", "raw_body": "Edited body", "rendered_body": "<p>Edited body</p>", "pinned": False, "closed": False, "following": False, "abuse_flagged": False, "voted": False, "vote_count": 0, "comment_count": 0, "unread_comment_count": 0, "comment_list_url": "http://testserver/api/discussion/v1/comments/?thread_id=test_thread", "endorsed_comment_list_url": None, "non_endorsed_comment_list_url": None, "editable_fields": [ "abuse_flagged", "following", "raw_body", "title", "topic_id", "type", "voted" ], "read": False, "has_endorsed": False, "response_count": 0, } response = self.client.patch( # pylint: disable=no-member self.url, json.dumps(request_data), content_type="application/json") self.assertEqual(response.status_code, 200) response_data = json.loads(response.content) self.assertEqual(response_data, expected_response_data) self.assertEqual( httpretty.last_request().parsed_body, { "course_id": [unicode(self.course.id)], "commentable_id": ["original_topic"], "thread_type": ["discussion"], "title": ["Original Title"], "body": ["Edited body"], "user_id": [str(self.user.id)], "anonymous": ["False"], "anonymous_to_peers": ["False"], "closed": ["False"], "pinned": ["False"], })