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