blob: f6a2447604d1e4f763b5f17e1ea4e1cd6eba42b3 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Tests for issueattachmenttext."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020010import mock
Copybara854996b2021-09-07 19:36:02 +000011import unittest
Copybara854996b2021-09-07 19:36:02 +000012
Copybara854996b2021-09-07 19:36:02 +000013import ezt
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020014from google.appengine.ext import testbed
15from google.cloud import storage
Copybara854996b2021-09-07 19:36:02 +000016
Copybara854996b2021-09-07 19:36:02 +000017from framework import permissions
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010018from mrproto import tracker_pb2
Copybara854996b2021-09-07 19:36:02 +000019from services import service_manager
20from testing import fake
21from testing import testing_helpers
22from tracker import issueattachmenttext
23
24
25class 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ínezf19ea432024-01-23 20:20:52 +010038 self.servlet = issueattachmenttext.AttachmentText(services=services)
Copybara854996b2021-09-07 19:36:02 +000039
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ínezde942802022-07-15 14:06:55 +020081
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ínezf19ea432024-01-23 20:20:52 +010087 self.blob.download_as_bytes = mock.MagicMock(return_value=b'')
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020088 mock.patch.object(storage, 'Client', return_value=self.client).start()
Copybara854996b2021-09-07 19:36:02 +000089
90 def tearDown(self):
91 self.testbed.deactivate()
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020092 mock.patch.stopall()
Copybara854996b2021-09-07 19:36:02 +000093
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ínezf19ea432024-01-23 20:20:52 +0100141 with self.assertRaises(Exception) as cm:
Copybara854996b2021-09-07 19:36:02 +0000142 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ínezf19ea432024-01-23 20:20:52 +0100152 with self.assertRaises(Exception) as cm:
Copybara854996b2021-09-07 19:36:02 +0000153 self.servlet.GatherPageData(mr)
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100154 self.assertEqual(404, cm.exception.code)
Copybara854996b2021-09-07 19:36:02 +0000155
156 def testGatherPageData_Normal(self):
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200157 self.blob.download_as_bytes = mock.MagicMock(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100158 return_value=b'/app_default_bucket/pid/attachments/abcdefg')
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200159
Copybara854996b2021-09-07 19:36:02 +0000160 _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ínezf19ea432024-01-23 20:20:52 +0100175 self.assertEqual(
176 '/app_default_bucket/pid/attachments/abcdefg', file_lines[0].line)
Copybara854996b2021-09-07 19:36:02 +0000177
178 self.assertEqual(None, page_data['code_reviews'])
179
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200180 @mock.patch('framework.filecontent.DecodeFileContents')
Copybara854996b2021-09-07 19:36:02 +0000181 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'])