Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style |
| 3 | # license that can be found in the LICENSE file or at |
| 4 | # https://developers.google.com/open-source/licenses/bsd |
| 5 | |
| 6 | """Tests for issueattachmenttext.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import logging |
| 12 | import unittest |
| 13 | from mock import patch |
| 14 | |
| 15 | from google.appengine.ext import testbed |
| 16 | |
| 17 | from third_party import cloudstorage |
| 18 | import ezt |
| 19 | |
| 20 | import webapp2 |
| 21 | |
| 22 | from framework import filecontent |
| 23 | from framework import permissions |
| 24 | from proto import tracker_pb2 |
| 25 | from services import service_manager |
| 26 | from testing import fake |
| 27 | from testing import testing_helpers |
| 28 | from tracker import issueattachmenttext |
| 29 | |
| 30 | |
| 31 | class IssueAttachmentTextTest(unittest.TestCase): |
| 32 | |
| 33 | def setUp(self): |
| 34 | self.testbed = testbed.Testbed() |
| 35 | self.testbed.activate() |
| 36 | self.testbed.init_app_identity_stub() |
| 37 | |
| 38 | services = service_manager.Services( |
| 39 | project=fake.ProjectService(), |
| 40 | config=fake.ConfigService(), |
| 41 | issue=fake.IssueService(), |
| 42 | user=fake.UserService()) |
| 43 | self.project = services.project.TestAddProject('proj') |
| 44 | self.servlet = issueattachmenttext.AttachmentText( |
| 45 | 'req', 'res', services=services) |
| 46 | |
| 47 | services.user.TestAddUser('commenter@example.com', 111) |
| 48 | |
| 49 | self.issue = tracker_pb2.Issue() |
| 50 | self.issue.local_id = 1 |
| 51 | self.issue.issue_id = 1 |
| 52 | self.issue.summary = 'sum' |
| 53 | self.issue.project_name = 'proj' |
| 54 | self.issue.project_id = self.project.project_id |
| 55 | services.issue.TestAddIssue(self.issue) |
| 56 | |
| 57 | self.comment0 = tracker_pb2.IssueComment() |
| 58 | self.comment0.content = 'this is the description' |
| 59 | self.comment0.user_id = 111 |
| 60 | self.comment1 = tracker_pb2.IssueComment() |
| 61 | self.comment1.content = 'this is a comment' |
| 62 | self.comment1.user_id = 111 |
| 63 | |
| 64 | self.attach0 = tracker_pb2.Attachment( |
| 65 | attachment_id=4567, filename='b.txt', mimetype='text/plain', |
| 66 | gcs_object_id='/pid/attachments/abcd') |
| 67 | self.comment0.attachments.append(self.attach0) |
| 68 | |
| 69 | self.attach1 = tracker_pb2.Attachment( |
| 70 | attachment_id=1234, filename='a.txt', mimetype='text/plain', |
| 71 | gcs_object_id='/pid/attachments/abcdefg') |
| 72 | self.comment0.attachments.append(self.attach1) |
| 73 | |
| 74 | self.bin_attach = tracker_pb2.Attachment( |
| 75 | attachment_id=2468, mimetype='application/octets', |
| 76 | gcs_object_id='/pid/attachments/\0\0\0\0\0\1\2\3') |
| 77 | self.comment1.attachments.append(self.bin_attach) |
| 78 | |
| 79 | self.comment0.project_id = self.project.project_id |
| 80 | services.issue.TestAddComment(self.comment0, self.issue.local_id) |
| 81 | self.comment1.project_id = self.project.project_id |
| 82 | services.issue.TestAddComment(self.comment1, self.issue.local_id) |
| 83 | services.issue.TestAddAttachment( |
| 84 | self.attach0, self.comment0.id, self.issue.issue_id) |
| 85 | services.issue.TestAddAttachment( |
| 86 | self.attach1, self.comment1.id, self.issue.issue_id) |
| 87 | # TODO(jrobbins): add tests for binary content |
| 88 | self._old_gcs_open = cloudstorage.open |
| 89 | cloudstorage.open = fake.gcs_open |
| 90 | |
| 91 | def tearDown(self): |
| 92 | self.testbed.deactivate() |
| 93 | cloudstorage.open = self._old_gcs_open |
| 94 | |
| 95 | def testGatherPageData_CommentDeleted(self): |
| 96 | """If the attachment's comment was deleted, give a 403.""" |
| 97 | _request, mr = testing_helpers.GetRequestObjects( |
| 98 | project=self.project, |
| 99 | path='/a/d.com/p/proj/issues/attachmentText?aid=1234', |
| 100 | perms=permissions.READ_ONLY_PERMISSIONSET) |
| 101 | self.servlet.GatherPageData(mr) # OK |
| 102 | self.comment1.deleted_by = 111 |
| 103 | self.assertRaises( # 403 |
| 104 | permissions.PermissionException, |
| 105 | self.servlet.GatherPageData, mr) |
| 106 | |
| 107 | def testGatherPageData_IssueNotViewable(self): |
| 108 | """If the attachment's issue is not viewable, give a 403.""" |
| 109 | _request, mr = testing_helpers.GetRequestObjects( |
| 110 | project=self.project, |
| 111 | path='/p/proj/issues/attachment?aid=1234', |
| 112 | perms=permissions.EMPTY_PERMISSIONSET) # No VIEW |
| 113 | self.assertRaises( |
| 114 | permissions.PermissionException, |
| 115 | self.servlet.GatherPageData, mr) |
| 116 | |
| 117 | def testGatherPageData_IssueDeleted(self): |
| 118 | _request, mr = testing_helpers.GetRequestObjects( |
| 119 | project=self.project, |
| 120 | path='/p/proj/issues/attachment?aid=1234', |
| 121 | perms=permissions.READ_ONLY_PERMISSIONSET) |
| 122 | self.issue.deleted = True |
| 123 | self.assertRaises( # Issue was deleted |
| 124 | permissions.PermissionException, |
| 125 | self.servlet.GatherPageData, mr) |
| 126 | |
| 127 | def testGatherPageData_IssueRestricted(self): |
| 128 | _request, mr = testing_helpers.GetRequestObjects( |
| 129 | project=self.project, |
| 130 | path='/p/proj/issues/attachment?aid=1234', |
| 131 | perms=permissions.READ_ONLY_PERMISSIONSET) |
| 132 | self.issue.labels.append('Restrict-View-Nobody') |
| 133 | self.assertRaises( # Issue is restricted |
| 134 | permissions.PermissionException, |
| 135 | self.servlet.GatherPageData, mr) |
| 136 | |
| 137 | def testGatherPageData_NoSuchAttachment(self): |
| 138 | _request, mr = testing_helpers.GetRequestObjects( |
| 139 | project=self.project, |
| 140 | path='/p/proj/issues/attachmentText?aid=9999', |
| 141 | perms=permissions.READ_ONLY_PERMISSIONSET) |
| 142 | with self.assertRaises(webapp2.HTTPException) as cm: |
| 143 | self.servlet.GatherPageData(mr) |
| 144 | self.assertEqual(404, cm.exception.code) |
| 145 | |
| 146 | def testGatherPageData_AttachmentDeleted(self): |
| 147 | """If the attachment was deleted, give a 404.""" |
| 148 | _request, mr = testing_helpers.GetRequestObjects( |
| 149 | project=self.project, |
| 150 | path='/p/proj/issues/attachmentText?aid=1234', |
| 151 | perms=permissions.READ_ONLY_PERMISSIONSET) |
| 152 | self.attach1.deleted = True |
| 153 | with self.assertRaises(webapp2.HTTPException) as cm: |
| 154 | self.servlet.GatherPageData(mr) |
| 155 | self.assertEqual(404, cm.exception.code) |
| 156 | |
| 157 | def testGatherPageData_Normal(self): |
| 158 | _request, mr = testing_helpers.GetRequestObjects( |
| 159 | project=self.project, |
| 160 | path='/p/proj/issues/attachmentText?id=1&aid=1234', |
| 161 | perms=permissions.READ_ONLY_PERMISSIONSET) |
| 162 | page_data = self.servlet.GatherPageData(mr) |
| 163 | self.assertEqual(1, page_data['local_id']) |
| 164 | self.assertEqual('a.txt', page_data['filename']) |
| 165 | self.assertEqual('43 bytes', page_data['filesize']) |
| 166 | self.assertEqual(ezt.boolean(False), page_data['should_prettify']) |
| 167 | self.assertEqual(ezt.boolean(False), page_data['is_binary']) |
| 168 | self.assertEqual(ezt.boolean(False), page_data['too_large']) |
| 169 | |
| 170 | file_lines = page_data['file_lines'] |
| 171 | self.assertEqual(1, len(file_lines)) |
| 172 | self.assertEqual(1, file_lines[0].num) |
| 173 | self.assertEqual('/app_default_bucket/pid/attachments/abcdefg', |
| 174 | file_lines[0].line) |
| 175 | |
| 176 | self.assertEqual(None, page_data['code_reviews']) |
| 177 | |
| 178 | @patch('framework.filecontent.DecodeFileContents') |
| 179 | def testGatherPageData_HugeFile(self, mock_DecodeFileContents): |
| 180 | _request, mr = testing_helpers.GetRequestObjects( |
| 181 | project=self.project, |
| 182 | path='/p/proj/issues/attachmentText?id=1&aid=1234', |
| 183 | perms=permissions.READ_ONLY_PERMISSIONSET) |
| 184 | mock_DecodeFileContents.return_value = ( |
| 185 | 'too large text', False, True) |
| 186 | |
| 187 | page_data = self.servlet.GatherPageData(mr) |
| 188 | |
| 189 | self.assertEqual(ezt.boolean(False), page_data['should_prettify']) |
| 190 | self.assertEqual(ezt.boolean(False), page_data['is_binary']) |
| 191 | self.assertEqual(ezt.boolean(True), page_data['too_large']) |