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