Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame^] | 1 | # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed 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 | """Unittests for monorail.features.commitlogcommands.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import mock |
| 12 | import unittest |
| 13 | |
| 14 | from features import commitlogcommands |
| 15 | from features import send_notifications |
| 16 | from framework import monorailcontext |
| 17 | from proto import tracker_pb2 |
| 18 | from services import service_manager |
| 19 | from testing import fake |
| 20 | from testing import testing_helpers |
| 21 | |
| 22 | |
| 23 | class InboundEmailTest(unittest.TestCase): |
| 24 | |
| 25 | def setUp(self): |
| 26 | self.cnxn = 'fake cnxn' |
| 27 | self.services = service_manager.Services( |
| 28 | issue=fake.IssueService(), |
| 29 | user=fake.UserService(), |
| 30 | usergroup=fake.UserGroupService(), |
| 31 | project=fake.ProjectService(), |
| 32 | config=fake.ConfigService()) |
| 33 | |
| 34 | self.member = self.services.user.TestAddUser('member@example.com', 111) |
| 35 | self.outsider = self.services.user.TestAddUser('outsider@example.com', 222) |
| 36 | self.project = self.services.project.TestAddProject( |
| 37 | 'proj', project_id=987, process_inbound_email=True, |
| 38 | committer_ids=[self.member.user_id]) |
| 39 | self.issue = tracker_pb2.Issue() |
| 40 | self.issue.issue_id = 98701 |
| 41 | self.issue.project_id = 987 |
| 42 | self.issue.local_id = 1 |
| 43 | self.issue.owner_id = 0 |
| 44 | self.issue.summary = 'summary' |
| 45 | self.issue.status = 'Assigned' |
| 46 | self.services.issue.TestAddIssue(self.issue) |
| 47 | |
| 48 | self.uia = commitlogcommands.UpdateIssueAction(self.issue.local_id) |
| 49 | |
| 50 | def testParse_NoCommandLines(self): |
| 51 | commands_found = self.uia.Parse(self.cnxn, self.project.project_name, 111, |
| 52 | ['line 1'], self.services, |
| 53 | hostport='testing-app.appspot.com', strip_quoted_lines=True) |
| 54 | self.assertEqual(False, commands_found) |
| 55 | self.assertEqual('line 1', self.uia.description) |
| 56 | self.assertEqual('line 1', self.uia.inbound_message) |
| 57 | |
| 58 | def testParse_StripQuotedLines(self): |
| 59 | commands_found = self.uia.Parse(self.cnxn, self.project.project_name, 111, |
| 60 | ['summary:something', '> line 1', 'line 2'], self.services, |
| 61 | hostport='testing-app.appspot.com', strip_quoted_lines=True) |
| 62 | self.assertEqual(True, commands_found) |
| 63 | self.assertEqual('line 2', self.uia.description) |
| 64 | self.assertEqual( |
| 65 | 'summary:something\n> line 1\nline 2', self.uia.inbound_message) |
| 66 | |
| 67 | def testParse_NoStripQuotedLines(self): |
| 68 | commands_found = self.uia.Parse(self.cnxn, self.project.project_name, 111, |
| 69 | ['summary:something', '> line 1', 'line 2'], self.services, |
| 70 | hostport='testing-app.appspot.com') |
| 71 | self.assertEqual(True, commands_found) |
| 72 | self.assertEqual('> line 1\nline 2', self.uia.description) |
| 73 | self.assertIsNone(self.uia.inbound_message) |
| 74 | |
| 75 | def setupAndCallRun(self, mc, commenter_id, mock_pasicn): |
| 76 | self.uia.Parse(self.cnxn, self.project.project_name, 111, |
| 77 | ['summary:something', 'status:New', '> line 1', '> line 2'], |
| 78 | self.services, hostport='testing-app.appspot.com') |
| 79 | self.uia.Run(mc, self.services) |
| 80 | |
| 81 | mock_pasicn.assert_called_once_with( |
| 82 | self.issue.issue_id, 'testing-app.appspot.com', commenter_id, |
| 83 | old_owner_id=self.issue.owner_id, comment_id=1, send_email=True) |
| 84 | |
| 85 | @mock.patch( |
| 86 | 'features.send_notifications.PrepareAndSendIssueChangeNotification') |
| 87 | def testRun_AllowEdit(self, mock_pasicn): |
| 88 | mc = monorailcontext.MonorailContext( |
| 89 | self.services, cnxn=self.cnxn, requester='member@example.com') |
| 90 | mc.LookupLoggedInUserPerms(self.project) |
| 91 | |
| 92 | self.setupAndCallRun(mc, 111, mock_pasicn) |
| 93 | |
| 94 | self.assertEqual('> line 1\n> line 2', self.uia.description) |
| 95 | # Assert that amendments were made to the issue. |
| 96 | self.assertEqual('something', self.issue.summary) |
| 97 | self.assertEqual('New', self.issue.status) |
| 98 | |
| 99 | @mock.patch( |
| 100 | 'features.send_notifications.PrepareAndSendIssueChangeNotification') |
| 101 | def testRun_NoAllowEdit(self, mock_pasicn): |
| 102 | mc = monorailcontext.MonorailContext( |
| 103 | self.services, cnxn=self.cnxn, requester='outsider@example.com') |
| 104 | mc.LookupLoggedInUserPerms(self.project) |
| 105 | |
| 106 | self.setupAndCallRun(mc, 222, mock_pasicn) |
| 107 | |
| 108 | self.assertEqual('> line 1\n> line 2', self.uia.description) |
| 109 | # Assert that amendments were *not* made to the issue. |
| 110 | self.assertEqual('summary', self.issue.summary) |
| 111 | self.assertEqual('Assigned', self.issue.status) |