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