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