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 the issueoriginal module.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import mock |
| 11 | import unittest |
| 12 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 13 | from framework import exceptions |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 14 | from framework import permissions |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 15 | from mrproto import tracker_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 16 | from services import service_manager |
| 17 | from testing import fake |
| 18 | from testing import testing_helpers |
| 19 | from tracker import issueoriginal |
| 20 | |
| 21 | |
| 22 | STRIPPED_MSG = 'Are you sure that it is plugged in?\n' |
| 23 | ORIG_MSG = ('Are you sure that it is plugged in?\n' |
| 24 | '\n' |
| 25 | '> Issue 1 entered by user foo:\n' |
| 26 | '> http://blah blah\n' |
| 27 | '> The screen is just dark when I press power on\n') |
| 28 | XXX_GOOD_UNICODE_MSG = u'Thanks,\n\342\230\206*username*'.encode('utf-8') |
| 29 | GOOD_UNICODE_MSG = u'Thanks,\n XXX *username*' |
| 30 | XXX_BAD_UNICODE_MSG = ORIG_MSG + ('\xff' * 1000) |
| 31 | BAD_UNICODE_MSG = ORIG_MSG + 'XXX' |
| 32 | GMAIL_CRUFT_MSG = ORIG_MSG # XXX .replace(' ', ' \xa0 ') |
| 33 | |
| 34 | |
| 35 | class IssueOriginalTest(unittest.TestCase): |
| 36 | |
| 37 | def setUp(self): |
| 38 | self.services = service_manager.Services( |
| 39 | project=fake.ProjectService(), |
| 40 | config=fake.ConfigService(), |
| 41 | issue=fake.IssueService(), |
| 42 | user=fake.UserService()) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 43 | self.servlet = issueoriginal.IssueOriginal(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 44 | |
| 45 | self.proj = self.services.project.TestAddProject('proj', project_id=789) |
| 46 | summary = 'System wont boot' |
| 47 | status = 'New' |
| 48 | cnxn = 'fake connection' |
| 49 | self.services.user.TestAddUser('commenter@example.com', 222) |
| 50 | |
| 51 | created_issue_1 = fake.MakeTestIssue( |
| 52 | 789, 1, summary, status, 111, reporter_id=111) |
| 53 | self.services.issue.TestAddIssue(created_issue_1) |
| 54 | self.local_id_1 = created_issue_1.local_id |
| 55 | comment_0 = tracker_pb2.IssueComment( |
| 56 | issue_id=created_issue_1.issue_id, |
| 57 | user_id=222, |
| 58 | project_id=789, |
| 59 | content=STRIPPED_MSG, |
| 60 | inbound_message=ORIG_MSG) |
| 61 | self.services.issue.InsertComment(cnxn, comment_0) |
| 62 | comment_1 = tracker_pb2.IssueComment( |
| 63 | issue_id=created_issue_1.issue_id, |
| 64 | user_id=222, |
| 65 | project_id=789, |
| 66 | content=STRIPPED_MSG, |
| 67 | inbound_message=BAD_UNICODE_MSG) |
| 68 | self.services.issue.InsertComment(cnxn, comment_1) |
| 69 | comment_2 = tracker_pb2.IssueComment( |
| 70 | issue_id=created_issue_1.issue_id, |
| 71 | user_id=222, |
| 72 | project_id=789, |
| 73 | content=STRIPPED_MSG, |
| 74 | inbound_message=GMAIL_CRUFT_MSG) |
| 75 | self.services.issue.InsertComment(cnxn, comment_2) |
| 76 | comment_3 = tracker_pb2.IssueComment( |
| 77 | issue_id=created_issue_1.issue_id, |
| 78 | user_id=222, |
| 79 | project_id=789, |
| 80 | content=STRIPPED_MSG, |
| 81 | inbound_message=GOOD_UNICODE_MSG) |
| 82 | self.services.issue.InsertComment(cnxn, comment_3) |
| 83 | self.issue_1 = self.services.issue.GetIssueByLocalID( |
| 84 | cnxn, 789, self.local_id_1) |
| 85 | self.comments = [comment_0, comment_1, comment_2, comment_3] |
| 86 | |
| 87 | @mock.patch('framework.permissions.GetPermissions') |
| 88 | def testAssertBasePermission(self, mock_getpermissions): |
| 89 | """Permit users who can view issue, view inbound message and delete.""" |
| 90 | _request, mr = testing_helpers.GetRequestObjects( |
| 91 | path='/p/proj/issues/original?id=1&seq=1', |
| 92 | project=self.proj) |
| 93 | |
| 94 | # Allow the user to view the issue itself. |
| 95 | mock_getpermissions.return_value = ( |
| 96 | permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET) |
| 97 | |
| 98 | # Someone without VIEW permission cannot view the inbound email. |
| 99 | mr.perms = permissions.EMPTY_PERMISSIONSET |
| 100 | self.assertRaises(permissions.PermissionException, |
| 101 | self.servlet.AssertBasePermission, mr) |
| 102 | |
| 103 | # Contributors don't have VIEW_INBOUND_MESSAGES. |
| 104 | mr.perms = permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET |
| 105 | self.assertRaises(permissions.PermissionException, |
| 106 | self.servlet.AssertBasePermission, mr) |
| 107 | |
| 108 | # Committers do have VIEW_INBOUND_MESSAGES. |
| 109 | mr.perms = permissions.COMMITTER_ACTIVE_PERMISSIONSET |
| 110 | self.servlet.AssertBasePermission(mr) |
| 111 | |
| 112 | # But, a committer cannot use that if they cannot view the issue. |
| 113 | self.issue_1.labels.append('Restrict-View-Foo') |
| 114 | mr.perms = permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET |
| 115 | self.assertRaises(permissions.PermissionException, |
| 116 | self.servlet.AssertBasePermission, mr) |
| 117 | |
| 118 | # Project owners have VIEW_INBOUND_MESSAGES and bypass restrictions. |
| 119 | mock_getpermissions.return_value = ( |
| 120 | permissions.OWNER_ACTIVE_PERMISSIONSET) |
| 121 | mr.perms = permissions.OWNER_ACTIVE_PERMISSIONSET |
| 122 | self.servlet.AssertBasePermission(mr) |
| 123 | |
| 124 | def testGatherPageData_Normal(self): |
| 125 | _request, mr = testing_helpers.GetRequestObjects( |
| 126 | path='/p/proj/issues/original?id=1&seq=1', |
| 127 | project=self.proj) |
| 128 | page_data = self.servlet.GatherPageData(mr) |
| 129 | self.assertEqual(1, page_data['local_id']) |
| 130 | self.assertEqual(1, page_data['seq']) |
| 131 | self.assertFalse(page_data['is_binary']) |
| 132 | self.assertEqual(ORIG_MSG, page_data['message_body']) |
| 133 | |
| 134 | def testGatherPageData_GoodUnicode(self): |
| 135 | _request, mr = testing_helpers.GetRequestObjects( |
| 136 | path='/p/proj/issues/original?id=1&seq=4', |
| 137 | project=self.proj) |
| 138 | page_data = self.servlet.GatherPageData(mr) |
| 139 | self.assertEqual(1, page_data['local_id']) |
| 140 | self.assertEqual(4, page_data['seq']) |
| 141 | self.assertEqual(GOOD_UNICODE_MSG, page_data['message_body']) |
| 142 | self.assertFalse(page_data['is_binary']) |
| 143 | |
| 144 | def testGatherPageData_BadUnicode(self): |
| 145 | _request, mr = testing_helpers.GetRequestObjects( |
| 146 | path='/p/proj/issues/original?id=1&seq=2', |
| 147 | project=self.proj) |
| 148 | page_data = self.servlet.GatherPageData(mr) |
| 149 | self.assertEqual(1, page_data['local_id']) |
| 150 | self.assertEqual(2, page_data['seq']) |
| 151 | # xxx: should be true if cruft was there. |
| 152 | # self.assertTrue(page_data['is_binary']) |
| 153 | |
| 154 | def testGatherPageData_GmailCruft(self): |
| 155 | _request, mr = testing_helpers.GetRequestObjects( |
| 156 | path='/p/proj/issues/original?id=1&seq=3', |
| 157 | project=self.proj) |
| 158 | page_data = self.servlet.GatherPageData(mr) |
| 159 | self.assertEqual(1, page_data['local_id']) |
| 160 | self.assertEqual(3, page_data['seq']) |
| 161 | self.assertFalse(page_data['is_binary']) |
| 162 | self.assertEqual(ORIG_MSG, page_data['message_body']) |
| 163 | |
| 164 | def testGatherPageData_404(self): |
| 165 | _request, mr = testing_helpers.GetRequestObjects( |
| 166 | path='/p/proj/issues/original', |
| 167 | project=self.proj) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 168 | with self.assertRaises(Exception) as cm: |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 169 | self.servlet.GatherPageData(mr) |
| 170 | self.assertEqual(404, cm.exception.code) |
| 171 | |
| 172 | _request, mr = testing_helpers.GetRequestObjects( |
| 173 | path='/p/proj/issues/original?id=1&seq=999', |
| 174 | project=self.proj) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 175 | with self.assertRaises(Exception) as cm: |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 176 | self.servlet.GatherPageData(mr) |
| 177 | self.assertEqual(404, cm.exception.code) |
| 178 | |
| 179 | _request, mr = testing_helpers.GetRequestObjects( |
| 180 | path='/p/proj/issues/original?id=999&seq=1', |
| 181 | project=self.proj) |
| 182 | with self.assertRaises(exceptions.NoSuchIssueException) as cm: |
| 183 | self.servlet.GatherPageData(mr) |
| 184 | |
| 185 | def testGetIssueAndComment_Normal(self): |
| 186 | _request, mr = testing_helpers.GetRequestObjects( |
| 187 | path='/p/proj/issues/original?id=1&seq=1', |
| 188 | project=self.proj) |
| 189 | issue, comment = self.servlet._GetIssueAndComment(mr) |
| 190 | self.assertEqual(self.issue_1, issue) |
| 191 | self.assertEqual(self.comments[1].content, comment.content) |
| 192 | |
| 193 | def testGetIssueAndComment_NoSuchComment(self): |
| 194 | _request, mr = testing_helpers.GetRequestObjects( |
| 195 | path='/p/proj/issues/original?id=1&seq=99', |
| 196 | project=self.proj) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 197 | with self.assertRaises(Exception) as cm: |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 198 | self.servlet._GetIssueAndComment(mr) |
| 199 | self.assertEqual(404, cm.exception.code) |
| 200 | |
| 201 | def testGetIssueAndComment_Malformed(self): |
| 202 | _request, mr = testing_helpers.GetRequestObjects( |
| 203 | path='/p/proj/issues/original', |
| 204 | project=self.proj) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 205 | with self.assertRaises(Exception) as cm: |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 206 | self.servlet._GetIssueAndComment(mr) |
| 207 | self.assertEqual(404, cm.exception.code) |
| 208 | |
| 209 | _request, mr = testing_helpers.GetRequestObjects( |
| 210 | path='/p/proj/issues/original?id=1', |
| 211 | project=self.proj) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 212 | with self.assertRaises(Exception) as cm: |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 213 | self.servlet._GetIssueAndComment(mr) |
| 214 | self.assertEqual(404, cm.exception.code) |
| 215 | |
| 216 | _request, mr = testing_helpers.GetRequestObjects( |
| 217 | path='/p/proj/issues/original?seq=1', |
| 218 | project=self.proj) |
| 219 | with self.assertRaises(exceptions.NoSuchIssueException) as cm: |
| 220 | self.servlet._GetIssueAndComment(mr) |