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 | """Tests for prepareandsend.py""" |
| 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 | import urlparse |
| 14 | |
| 15 | from features import send_notifications |
| 16 | from framework import urls |
| 17 | from tracker import tracker_bizobj |
| 18 | |
| 19 | |
| 20 | class SendNotificationTest(unittest.TestCase): |
| 21 | |
| 22 | def _get_filtered_task_call_args(self, create_task_mock, relative_uri): |
| 23 | return [ |
| 24 | (args, _kwargs) |
| 25 | for (args, _kwargs) in create_task_mock.call_args_list |
| 26 | if args[0]['app_engine_http_request']['relative_uri'].startswith( |
| 27 | relative_uri) |
| 28 | ] |
| 29 | |
| 30 | def _get_create_task_path_and_params(self, call): |
| 31 | (args, _kwargs) = call |
| 32 | path = args[0]['app_engine_http_request']['relative_uri'] |
| 33 | encoded_params = args[0]['app_engine_http_request']['body'] |
| 34 | params = { |
| 35 | k: v[0] for k, v in urlparse.parse_qs(encoded_params, True).items() |
| 36 | } |
| 37 | return path, params |
| 38 | |
| 39 | @mock.patch('framework.cloud_tasks_helpers.create_task') |
| 40 | def testPrepareAndSendIssueChangeNotification(self, create_task_mock): |
| 41 | send_notifications.PrepareAndSendIssueChangeNotification( |
| 42 | issue_id=78901, |
| 43 | hostport='testbed-test.appspotmail.com', |
| 44 | commenter_id=1, |
| 45 | old_owner_id=2, |
| 46 | send_email=True) |
| 47 | |
| 48 | call_args_list = self._get_filtered_task_call_args( |
| 49 | create_task_mock, urls.NOTIFY_ISSUE_CHANGE_TASK + '.do') |
| 50 | self.assertEqual(1, len(call_args_list)) |
| 51 | |
| 52 | @mock.patch('framework.cloud_tasks_helpers.create_task') |
| 53 | def testPrepareAndSendIssueBlockingNotification(self, create_task_mock): |
| 54 | send_notifications.PrepareAndSendIssueBlockingNotification( |
| 55 | issue_id=78901, |
| 56 | hostport='testbed-test.appspotmail.com', |
| 57 | delta_blocker_iids=[], |
| 58 | commenter_id=1, |
| 59 | send_email=True) |
| 60 | |
| 61 | call_args_list = self._get_filtered_task_call_args( |
| 62 | create_task_mock, urls.NOTIFY_BLOCKING_CHANGE_TASK + '.do') |
| 63 | self.assertEqual(0, len(call_args_list)) |
| 64 | |
| 65 | send_notifications.PrepareAndSendIssueBlockingNotification( |
| 66 | issue_id=78901, |
| 67 | hostport='testbed-test.appspotmail.com', |
| 68 | delta_blocker_iids=[2], |
| 69 | commenter_id=1, |
| 70 | send_email=True) |
| 71 | |
| 72 | call_args_list = self._get_filtered_task_call_args( |
| 73 | create_task_mock, urls.NOTIFY_BLOCKING_CHANGE_TASK + '.do') |
| 74 | self.assertEqual(1, len(call_args_list)) |
| 75 | |
| 76 | @mock.patch('framework.cloud_tasks_helpers.create_task') |
| 77 | def testPrepareAndSendApprovalChangeNotification(self, create_task_mock): |
| 78 | send_notifications.PrepareAndSendApprovalChangeNotification( |
| 79 | 78901, 3, 'testbed-test.appspotmail.com', 55) |
| 80 | |
| 81 | call_args_list = self._get_filtered_task_call_args( |
| 82 | create_task_mock, urls.NOTIFY_APPROVAL_CHANGE_TASK + '.do') |
| 83 | self.assertEqual(1, len(call_args_list)) |
| 84 | |
| 85 | @mock.patch('framework.cloud_tasks_helpers.create_task') |
| 86 | def testSendIssueBulkChangeNotification_CommentOnly(self, create_task_mock): |
| 87 | send_notifications.SendIssueBulkChangeNotification( |
| 88 | issue_ids=[78901], |
| 89 | hostport='testbed-test.appspotmail.com', |
| 90 | old_owner_ids=[2], |
| 91 | comment_text='comment', |
| 92 | commenter_id=1, |
| 93 | amendments=[], |
| 94 | send_email=True, |
| 95 | users_by_id=2) |
| 96 | |
| 97 | call_args_list = self._get_filtered_task_call_args( |
| 98 | create_task_mock, urls.NOTIFY_BULK_CHANGE_TASK + '.do') |
| 99 | self.assertEqual(1, len(call_args_list)) |
| 100 | _path, params = self._get_create_task_path_and_params(call_args_list[0]) |
| 101 | self.assertEqual(params['comment_text'], 'comment') |
| 102 | self.assertEqual(params['amendments'], '') |
| 103 | |
| 104 | @mock.patch('framework.cloud_tasks_helpers.create_task') |
| 105 | def testSendIssueBulkChangeNotification_Normal(self, create_task_mock): |
| 106 | send_notifications.SendIssueBulkChangeNotification( |
| 107 | issue_ids=[78901], |
| 108 | hostport='testbed-test.appspotmail.com', |
| 109 | old_owner_ids=[2], |
| 110 | comment_text='comment', |
| 111 | commenter_id=1, |
| 112 | amendments=[ |
| 113 | tracker_bizobj.MakeStatusAmendment('New', 'Old'), |
| 114 | tracker_bizobj.MakeLabelsAmendment(['Added'], ['Removed']), |
| 115 | tracker_bizobj.MakeStatusAmendment('New', 'Old'), |
| 116 | ], |
| 117 | send_email=True, |
| 118 | users_by_id=2) |
| 119 | |
| 120 | call_args_list = self._get_filtered_task_call_args( |
| 121 | create_task_mock, urls.NOTIFY_BULK_CHANGE_TASK + '.do') |
| 122 | self.assertEqual(1, len(call_args_list)) |
| 123 | _path, params = self._get_create_task_path_and_params(call_args_list[0]) |
| 124 | self.assertEqual(params['comment_text'], 'comment') |
| 125 | self.assertEqual( |
| 126 | params['amendments'].split('\n'), |
| 127 | [' Status: New', ' Labels: -Removed Added']) |
| 128 | |
| 129 | @mock.patch('framework.cloud_tasks_helpers.create_task') |
| 130 | def testPrepareAndSendDeletedFilterRulesNotifications(self, create_task_mock): |
| 131 | filter_rule_strs = ['if yellow make orange', 'if orange make blue'] |
| 132 | send_notifications.PrepareAndSendDeletedFilterRulesNotification( |
| 133 | 789, 'testbed-test.appspotmail.com', filter_rule_strs) |
| 134 | |
| 135 | call_args_list = self._get_filtered_task_call_args( |
| 136 | create_task_mock, urls.NOTIFY_RULES_DELETED_TASK + '.do') |
| 137 | self.assertEqual(1, len(call_args_list)) |
| 138 | _path, params = self._get_create_task_path_and_params(call_args_list[0]) |
| 139 | self.assertEqual(params['project_id'], '789') |
| 140 | self.assertEqual( |
| 141 | params['filter_rules'], 'if yellow make orange,if orange make blue') |