blob: b60130f36117a7c801172d10d0e3a8388cc5f30e [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Tests for prepareandsend.py"""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import mock
11import unittest
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020012from six.moves.urllib.parse import parse_qs
Copybara854996b2021-09-07 19:36:02 +000013
14from features import send_notifications
15from framework import urls
16from tracker import tracker_bizobj
17
18
19class 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ínezde942802022-07-15 14:06:55 +020033 params = {k: v[0] for k, v in parse_qs(encoded_params, True).items()}
Copybara854996b2021-09-07 19:36:02 +000034 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ínezf19ea432024-01-23 20:20:52 +010098 self.assertEqual(params[b'comment_text'], b'comment')
99 self.assertEqual(params[b'amendments'], b'')
Copybara854996b2021-09-07 19:36:02 +0000100
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ínezf19ea432024-01-23 20:20:52 +0100121 self.assertEqual(params[b'comment_text'], b'comment')
Copybara854996b2021-09-07 19:36:02 +0000122 self.assertEqual(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100123 params[b'amendments'].split(b'\n'),
124 [b' Status: New', b' Labels: -Removed Added'])
Copybara854996b2021-09-07 19:36:02 +0000125
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ínezf19ea432024-01-23 20:20:52 +0100136 self.assertEqual(params[b'project_id'], b'789')
Copybara854996b2021-09-07 19:36:02 +0000137 self.assertEqual(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100138 params[b'filter_rules'], b'if yellow make orange,if orange make blue')