def test_get_comments_view(self): number_of_comments = DocumentComment.objects.count() # (from the canopy) response = self.client.get('/api/v2/comments/') self.assertEqual(number_of_comments, response.data['count']) # moooore comments. DocumentCommentFactory.create_batch(50) response = self.client.get('/api/v2/comments/') self.assertEqual(number_of_comments + 50, response.data['count'])
def test_get_comments_view(self): number_of_comments = DocumentComment.objects.count( ) # (from the canopy) response = self.client.get('/api/v2/comments/') self.assertEqual(number_of_comments, response.data['count']) # moooore comments. DocumentCommentFactory.create_batch(50) response = self.client.get('/api/v2/comments/') self.assertEqual(number_of_comments + 50, response.data['count'])
def test_add_comment_view_with_changed_hash(self): first_hash = "THEoriginalHASH" second_hash = 'ANEWCRAZYHASH' comment_text = "This comment will follow its node despite hash changing." # Create a comment on a node whose latest hash is the first one. node = DocumentNodeFactory(hash=first_hash) comnent = DocumentCommentFactory(node=node, text=comment_text) # Now change the node's hash. node.update_hash(second_hash, 'ANEWCRAZYCOMMIT') node_from_orm = DocumentNode.objects.from_hash( version_slug=node.version.slug, page=node.page, node_hash=node.latest_hash(), project_slug=node.project.slug) # It's the same node. self.assertEqual(node, node_from_orm) # Get all the comments with the second hash. query_params = { 'node': second_hash, 'document_page': node.page, 'project': node.project.slug, 'version': node.version.slug, } response = self.client.get('/api/v2/comments/', query_params) self.assertEqual(response.data['results'][0]['text'], comment_text)
def test_get_metadata_view(self): node = DocumentNodeFactory() get_data = { 'project': node.project.slug, 'version': node.version.slug, 'page': node.page } request = self.request_factory.get('/_get_metadata/', get_data) response = get_metadata(request) response.render() number_of_comments = response.data[node.latest_hash()] # There haven't been any comments yet. self.assertEqual(number_of_comments, 0) # Now we'll make one. comment = DocumentCommentFactory(node=node, text="Our first comment!") second_request = self.request_factory.get('/_get_metadata/', get_data) second_response = get_metadata(request) second_response.render() number_of_comments = second_response.data[node.latest_hash()] # And sure enough - one comment. self.assertEqual(number_of_comments, 1)
def test_moderate_comment_by_approving(self): user = UserFactory(username="******", password="******") project = ProjectFactory() project.users.add(user) node = DocumentNodeFactory(project=project) comment = DocumentCommentFactory(node=node) post_data = { 'decision': 1, } self.assertFalse(comment.has_been_approved_since_most_recent_node_change()) self.client.login(username="******", password="******") response = self.client.put('/api/v2/comments/%s/moderate/' % comment.id, post_data) self.assertEqual(response.data['decision'], 1) self.assertTrue(comment.has_been_approved_since_most_recent_node_change())
def test_moderate_comment_by_approving(self): user = UserFactory(username="******", password="******") project = ProjectFactory() project.users.add(user) node = DocumentNodeFactory(project=project) comment = DocumentCommentFactory(node=node) post_data = { 'decision': 1, } self.assertFalse( comment.has_been_approved_since_most_recent_node_change()) self.client.login(username="******", password="******") response = self.client.put( '/api/v2/comments/%s/moderate/' % comment.id, post_data) self.assertEqual(response.data['decision'], 1) self.assertTrue( comment.has_been_approved_since_most_recent_node_change())
def test_stranger_cannot_moderate_comments(self): node = DocumentNodeFactory() user = UserFactory() comment = DocumentCommentFactory(node=node) post_data = { 'decision': 1, } response = self.client.put( '/api/v2/comments/%s/moderate/' % comment.id, post_data) self.assertEqual(response.status_code, 403)
def test_node_can_be_sought_From_new_hash(self): first_hash = "THEoriginalHASH" second_hash = 'ANEWCRAZYHASH' node = DocumentNodeFactory(hash=first_hash) comnent = DocumentCommentFactory() node.update_hash(second_hash, 'ANEWCRAZYCOMMIT') node_from_orm = DocumentNode.objects.from_hash( node.version.slug, node.page, node.latest_hash(), project_slug=node.project.slug) self.assertEqual(node, node_from_orm) node.update_hash(first_hash, 'AthirdCommit') node_from_orm2 = DocumentNode.objects.from_hash( node.version.slug, node.page, first_hash, node.project.slug) self.assertEqual(node, node_from_orm2)