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_add_comment_view_with_existing_hash(self): node = DocumentNodeFactory() user = UserFactory(username="******", password="******") comment_text = "Here's a comment added through the comment view." post_data = { 'node': node.latest_hash(), 'commit': node.latest_hash(), 'project': node.project.slug, 'version': node.version.slug, 'document_page': node.page, 'text': comment_text } self.client.login(username="******", password="******") response = self.client.post('/api/v2/comments/', post_data) comment_from_orm = node.comments.filter(text=comment_text) self.assertTrue(comment_from_orm.exists()) self.assertEqual( comment_from_orm[0].node, node, "The comment exists, but lives in a different node! Not supposed to happen." )
def test_update_node_view(self): node = DocumentNodeFactory() # Our node has one snapshot. self.assertEqual(node.snapshots.count(), 1) new_hash = "CRAZYnewHASHtoUPDATEnode" commit = "COOLNEWGITCOMMITHASH" post_data = { 'old_hash': node.latest_hash(), 'new_hash': new_hash, 'commit': commit, 'project': node.project.slug, 'version': node.version.slug, 'page': node.page } request = self.request_factory.post('/_update_node/', post_data) response = update_node(request) response.render() self.assertEqual(response.data['current_hash'], new_hash) # We now have two snapshots. self.assertEqual(node.snapshots.count(), 2) # And the latest hash is the one we just set. self.assertEqual(node.latest_hash(), new_hash)
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_update_with_same_hash_does_not_create_new_snapshot(self): node = DocumentNodeFactory() hash = "SOMEHASH" commit = "SOMEGITCOMMIT" # We initially have just one snapshot. self.assertEqual(node.snapshots.count(), 1) # ...but when we update the hash, we have two. node.update_hash(hash, commit) self.assertEqual(node.snapshots.count(), 2) # If we update with the same exact hash and commit, it doesn't create a new snapshot. node.update_hash(hash, commit) self.assertEqual(node.snapshots.count(), 2)
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_add_comment_view_with_existing_hash(self): node = DocumentNodeFactory() user = UserFactory(username="******", password="******") comment_text = "Here's a comment added through the comment view." post_data = { 'node': node.latest_hash(), 'commit': node.latest_hash(), 'project': node.project.slug, 'version': node.version.slug, 'document_page': node.page, 'text': comment_text } self.client.login(username="******", password="******") response = self.client.post('/api/v2/comments/', post_data) comment_from_orm = node.comments.filter(text=comment_text) self.assertTrue(comment_from_orm.exists()) self.assertEqual(comment_from_orm[0].node, node, "The comment exists, but lives in a different node! Not supposed to happen.")
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_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)
def test_add_comment_view_without_existing_hash(self): comment_text = "Here's a comment added to a new hash." node = DocumentNodeFactory() UserFactory(username="******", password="******") number_of_nodes = DocumentNode.objects.count() post_data = { 'node': random.getrandbits(128), 'commit': random.getrandbits(128), 'project': node.project.slug, 'version': node.version.slug, 'document_page': node.page, 'text': comment_text } self.client.login(username="******", password="******") response = self.client.post('/api/v2/comments/', post_data) self.assertEqual(response.status_code, 201) self.assertEqual(response.data['text'], comment_text) self.assertEqual(DocumentNode.objects.count(), number_of_nodes + 1) # We created a new node.
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)