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 the issueoriginal module.""" |
| 7 |
| 8 import unittest |
| 9 |
| 10 import webapp2 |
| 11 |
| 12 from framework import framework_helpers |
| 13 from framework import permissions |
| 14 from services import service_manager |
| 15 from testing import fake |
| 16 from testing import testing_helpers |
| 17 from tracker import issueoriginal |
| 18 |
| 19 |
| 20 STRIPPED_MSG = 'Are you sure that it is plugged in?\n' |
| 21 ORIG_MSG = ('Are you sure that it is plugged in?\n' |
| 22 '\n' |
| 23 '> Issue 1 entered by user foo:\n' |
| 24 '> http://blah blah\n' |
| 25 '> The screen is just dark when I press power on\n') |
| 26 XXX_GOOD_UNICODE_MSG = u'Thanks,\n\342\230\206*username*'.encode('utf-8') |
| 27 GOOD_UNICODE_MSG = u'Thanks,\n XXX *username*' |
| 28 XXX_BAD_UNICODE_MSG = ORIG_MSG + ('\xff' * 1000) |
| 29 BAD_UNICODE_MSG = ORIG_MSG + 'XXX' |
| 30 GMAIL_CRUFT_MSG = ORIG_MSG # XXX .replace(' ', ' \xa0 ') |
| 31 |
| 32 |
| 33 class IssueOriginalTest(unittest.TestCase): |
| 34 |
| 35 def setUp(self): |
| 36 self.services = service_manager.Services( |
| 37 project=fake.ProjectService(), |
| 38 config=fake.ConfigService(), |
| 39 issue=fake.IssueService(), |
| 40 user=fake.UserService()) |
| 41 self.servlet = issueoriginal.IssueOriginal( |
| 42 'req', 'res', services=self.services) |
| 43 |
| 44 self.proj = self.services.project.TestAddProject('proj', project_id=789) |
| 45 summary = 'System wont boot' |
| 46 status = 'New' |
| 47 cnxn = 'fake connection' |
| 48 self.local_id_1 = self.services.issue.CreateIssue( |
| 49 cnxn, self.services, |
| 50 789, summary, status, 111L, [], [], [], [], 111L, |
| 51 'The screen is just dark when I press power on') |
| 52 _amendments, comment_0 = self.services.issue.ApplyIssueComment( |
| 53 cnxn, self.services, 222L, 789, 1, |
| 54 summary, status, 222L, [], [], [], [], [], [], [], [], 0, |
| 55 comment=STRIPPED_MSG, inbound_message=ORIG_MSG) |
| 56 _amendments, comment_1 = self.services.issue.ApplyIssueComment( |
| 57 cnxn, self.services, 222L, 789, 1, |
| 58 summary, status, 222L, [], [], [], [], [], [], [], [], None, |
| 59 comment=STRIPPED_MSG, inbound_message=BAD_UNICODE_MSG) |
| 60 _amendments, comment_2 = self.services.issue.ApplyIssueComment( |
| 61 cnxn, self.services, 222L, 789, 1, |
| 62 summary, status, 222L, [], [], [], [], [], [], [], [], 0, |
| 63 comment=STRIPPED_MSG, inbound_message=GMAIL_CRUFT_MSG) |
| 64 _amendments, comment_3 = self.services.issue.ApplyIssueComment( |
| 65 cnxn, self.services, 222L, 789, 1, |
| 66 summary, status, 222L, [], [], [], [], [], [], [], [], 0, |
| 67 comment=STRIPPED_MSG, inbound_message=GOOD_UNICODE_MSG) |
| 68 self.issue_1 = self.services.issue.GetIssueByLocalID( |
| 69 cnxn, 789, self.local_id_1) |
| 70 self.comments = [comment_0, comment_1, comment_2, comment_3] |
| 71 |
| 72 def testAssertBasePermission(self): |
| 73 """Permit users who can view issue, view inbound message and delete.""" |
| 74 _request, mr = testing_helpers.GetRequestObjects( |
| 75 path='/p/proj/issues/original?id=1&seq=1', |
| 76 project=self.proj) |
| 77 mr.perms = permissions.EMPTY_PERMISSIONSET |
| 78 self.assertRaises(permissions.PermissionException, |
| 79 self.servlet.AssertBasePermission, mr) |
| 80 mr.perms = permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET |
| 81 self.assertRaises(permissions.PermissionException, |
| 82 self.servlet.AssertBasePermission, mr) |
| 83 mr.perms = permissions.OWNER_ACTIVE_PERMISSIONSET |
| 84 self.servlet.AssertBasePermission(mr) |
| 85 |
| 86 def testGatherPageData_Normal(self): |
| 87 _request, mr = testing_helpers.GetRequestObjects( |
| 88 path='/p/proj/issues/original?id=1&seq=1', |
| 89 project=self.proj) |
| 90 page_data = self.servlet.GatherPageData(mr) |
| 91 self.assertEqual(1, page_data['local_id']) |
| 92 self.assertEqual(1, page_data['seq']) |
| 93 self.assertFalse(page_data['is_binary']) |
| 94 self.assertEqual(ORIG_MSG, page_data['message_body']) |
| 95 |
| 96 def testGatherPageData_GoodUnicode(self): |
| 97 _request, mr = testing_helpers.GetRequestObjects( |
| 98 path='/p/proj/issues/original?id=1&seq=4', |
| 99 project=self.proj) |
| 100 page_data = self.servlet.GatherPageData(mr) |
| 101 self.assertEqual(1, page_data['local_id']) |
| 102 self.assertEqual(4, page_data['seq']) |
| 103 self.assertEqual(GOOD_UNICODE_MSG, page_data['message_body']) |
| 104 self.assertFalse(page_data['is_binary']) |
| 105 |
| 106 def testGatherPageData_BadUnicode(self): |
| 107 _request, mr = testing_helpers.GetRequestObjects( |
| 108 path='/p/proj/issues/original?id=1&seq=2', |
| 109 project=self.proj) |
| 110 page_data = self.servlet.GatherPageData(mr) |
| 111 self.assertEqual(1, page_data['local_id']) |
| 112 self.assertEqual(2, page_data['seq']) |
| 113 # xxx: should be true if cruft was there. |
| 114 # self.assertTrue(page_data['is_binary']) |
| 115 |
| 116 def testGatherPageData_GmailCruft(self): |
| 117 _request, mr = testing_helpers.GetRequestObjects( |
| 118 path='/p/proj/issues/original?id=1&seq=3', |
| 119 project=self.proj) |
| 120 page_data = self.servlet.GatherPageData(mr) |
| 121 self.assertEqual(1, page_data['local_id']) |
| 122 self.assertEqual(3, page_data['seq']) |
| 123 self.assertFalse(page_data['is_binary']) |
| 124 self.assertEqual(ORIG_MSG, page_data['message_body']) |
| 125 |
| 126 def testGatherPageData_404(self): |
| 127 _request, mr = testing_helpers.GetRequestObjects( |
| 128 path='/p/proj/issues/original', |
| 129 project=self.proj) |
| 130 try: |
| 131 self.servlet.GatherPageData(mr) |
| 132 self.fail() |
| 133 except webapp2.HTTPException as e: |
| 134 self.assertEquals(404, e.code) |
| 135 |
| 136 _request, mr = testing_helpers.GetRequestObjects( |
| 137 path='/p/proj/issues/original?id=1&seq=999', |
| 138 project=self.proj) |
| 139 try: |
| 140 self.servlet.GatherPageData(mr) |
| 141 self.fail() |
| 142 except webapp2.HTTPException as e: |
| 143 self.assertEquals(404, e.code) |
| 144 |
| 145 _request, mr = testing_helpers.GetRequestObjects( |
| 146 path='/p/proj/issues/original?id=999&seq=1', |
| 147 project=self.proj) |
| 148 try: |
| 149 self.servlet.GatherPageData(mr) |
| 150 self.fail() |
| 151 except webapp2.HTTPException as e: |
| 152 self.assertEquals(404, e.code) |
| 153 |
| 154 def testGetIssueAndComment_Normal(self): |
| 155 _request, mr = testing_helpers.GetRequestObjects( |
| 156 path='/p/proj/issues/original?id=1&seq=1', |
| 157 project=self.proj) |
| 158 issue, comment = self.servlet._GetIssueAndComment(mr) |
| 159 self.assertEqual(self.issue_1, issue) |
| 160 self.assertEqual(self.comments[1].content, comment.content) |
| 161 |
| 162 def testGetIssueAndComment_NoSuchComment(self): |
| 163 _request, mr = testing_helpers.GetRequestObjects( |
| 164 path='/p/proj/issues/original?id=1&seq=99', |
| 165 project=self.proj) |
| 166 try: |
| 167 self.servlet._GetIssueAndComment(mr) |
| 168 self.fail() |
| 169 except webapp2.HTTPException as e: |
| 170 self.assertEquals(404, e.code) |
| 171 |
| 172 def testGetIssueAndComment_Malformed(self): |
| 173 _request, mr = testing_helpers.GetRequestObjects( |
| 174 path='/p/proj/issues/original', |
| 175 project=self.proj) |
| 176 try: |
| 177 self.servlet._GetIssueAndComment(mr) |
| 178 self.fail() |
| 179 except webapp2.HTTPException as e: |
| 180 self.assertEquals(404, e.code) |
| 181 |
| 182 _request, mr = testing_helpers.GetRequestObjects( |
| 183 path='/p/proj/issues/original?id=1', |
| 184 project=self.proj) |
| 185 try: |
| 186 self.servlet._GetIssueAndComment(mr) |
| 187 self.fail() |
| 188 except webapp2.HTTPException as e: |
| 189 self.assertEquals(404, e.code) |
| 190 |
| 191 _request, mr = testing_helpers.GetRequestObjects( |
| 192 path='/p/proj/issues/original?seq=1', |
| 193 project=self.proj) |
| 194 try: |
| 195 self.servlet._GetIssueAndComment(mr) |
| 196 self.fail() |
| 197 except webapp2.HTTPException as e: |
| 198 self.assertEquals(404, e.code) |
| 199 |
| 200 _request, mr = testing_helpers.GetRequestObjects( |
| 201 path='/p/proj/issues/original?id=abc', |
| 202 project=self.proj) |
| 203 try: |
| 204 self.servlet._GetIssueAndComment(mr) |
| 205 self.fail() |
| 206 except webapp2.HTTPException as e: |
| 207 self.assertEquals(404, e.code) |
| 208 |
| 209 _request, mr = testing_helpers.GetRequestObjects( |
| 210 path='/p/proj/issues/original?seq=abc', |
| 211 project=self.proj) |
| 212 try: |
| 213 self.servlet._GetIssueAndComment(mr) |
| 214 self.fail() |
| 215 except webapp2.HTTPException as e: |
| 216 self.assertEquals(404, e.code) |
| 217 |
| 218 |
| 219 if __name__ == '__main__': |
| 220 unittest.main() |
OLD | NEW |