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