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