Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style |
| 4 | # license that can be found in the LICENSE file or at |
| 5 | # https://developers.google.com/open-source/licenses/bsd |
| 6 | |
| 7 | """Tests for notify_helpers.py.""" |
| 8 | from __future__ import print_function |
| 9 | from __future__ import division |
| 10 | from __future__ import absolute_import |
| 11 | |
| 12 | import json |
| 13 | import mock |
| 14 | import unittest |
| 15 | import os |
| 16 | |
| 17 | from features import features_constants |
| 18 | from features import notify_helpers |
| 19 | from features import notify_reasons |
| 20 | from framework import emailfmt |
| 21 | from framework import framework_views |
| 22 | from framework import urls |
| 23 | from proto import user_pb2 |
| 24 | from services import service_manager |
| 25 | from testing import fake |
| 26 | |
| 27 | |
| 28 | REPLY_NOT_ALLOWED = notify_reasons.REPLY_NOT_ALLOWED |
| 29 | REPLY_MAY_COMMENT = notify_reasons.REPLY_MAY_COMMENT |
| 30 | REPLY_MAY_UPDATE = notify_reasons.REPLY_MAY_UPDATE |
| 31 | |
| 32 | |
| 33 | class TaskQueueingFunctionsTest(unittest.TestCase): |
| 34 | |
| 35 | @mock.patch('framework.cloud_tasks_helpers._get_client') |
| 36 | def testAddAllEmailTasks(self, get_client_mock): |
| 37 | notify_helpers.AddAllEmailTasks( |
| 38 | tasks=[{'to': 'user'}, {'to': 'user2'}]) |
| 39 | |
| 40 | self.assertEqual(get_client_mock().create_task.call_count, 2) |
| 41 | |
| 42 | queue_call_args = get_client_mock().queue_path.call_args_list |
| 43 | ((_app_id, _region, queue), _kwargs) = queue_call_args[0] |
| 44 | self.assertEqual(queue, features_constants.QUEUE_OUTBOUND_EMAIL) |
| 45 | ((_app_id, _region, queue), _kwargs) = queue_call_args[1] |
| 46 | self.assertEqual(queue, features_constants.QUEUE_OUTBOUND_EMAIL) |
| 47 | |
| 48 | task_call_args = get_client_mock().create_task.call_args_list |
| 49 | ((_parent, task), _kwargs) = task_call_args[0] |
| 50 | expected_task = { |
| 51 | 'app_engine_http_request': |
| 52 | { |
| 53 | 'relative_uri': urls.OUTBOUND_EMAIL_TASK + '.do', |
| 54 | 'body': json.dumps({ |
| 55 | 'to': 'user' |
| 56 | }).encode(), |
| 57 | 'headers': { |
| 58 | 'Content-type': 'application/json' |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | self.assertEqual(task, expected_task) |
| 63 | ((_parent, task), _kwargs) = task_call_args[1] |
| 64 | expected_task = { |
| 65 | 'app_engine_http_request': |
| 66 | { |
| 67 | 'relative_uri': urls.OUTBOUND_EMAIL_TASK + '.do', |
| 68 | 'body': json.dumps({ |
| 69 | 'to': 'user2' |
| 70 | }).encode(), |
| 71 | 'headers': { |
| 72 | 'Content-type': 'application/json' |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | self.assertEqual(task, expected_task) |
| 77 | |
| 78 | |
| 79 | class MergeLinkedAccountReasonsTest(unittest.TestCase): |
| 80 | |
| 81 | def setUp(self): |
| 82 | parent = user_pb2.User( |
| 83 | user_id=111, email='parent@example.org', |
| 84 | linked_child_ids=[222]) |
| 85 | child = user_pb2.User( |
| 86 | user_id=222, email='child@example.org', |
| 87 | linked_parent_id=111) |
| 88 | user_3 = user_pb2.User( |
| 89 | user_id=333, email='user4@example.org') |
| 90 | user_4 = user_pb2.User( |
| 91 | user_id=444, email='user4@example.org') |
| 92 | self.addr_perm_parent = notify_reasons.AddrPerm( |
| 93 | False, parent.email, parent, notify_reasons.REPLY_NOT_ALLOWED, |
| 94 | user_pb2.UserPrefs()) |
| 95 | self.addr_perm_child = notify_reasons.AddrPerm( |
| 96 | False, child.email, child, notify_reasons.REPLY_NOT_ALLOWED, |
| 97 | user_pb2.UserPrefs()) |
| 98 | self.addr_perm_3 = notify_reasons.AddrPerm( |
| 99 | False, user_3.email, user_3, notify_reasons.REPLY_NOT_ALLOWED, |
| 100 | user_pb2.UserPrefs()) |
| 101 | self.addr_perm_4 = notify_reasons.AddrPerm( |
| 102 | False, user_4.email, user_4, notify_reasons.REPLY_NOT_ALLOWED, |
| 103 | user_pb2.UserPrefs()) |
| 104 | self.addr_perm_5 = notify_reasons.AddrPerm( |
| 105 | False, 'alias@example.com', None, notify_reasons.REPLY_NOT_ALLOWED, |
| 106 | user_pb2.UserPrefs()) |
| 107 | |
| 108 | def testEmptyDict(self): |
| 109 | """Zero users to notify.""" |
| 110 | self.assertEqual( |
| 111 | {}, |
| 112 | notify_helpers._MergeLinkedAccountReasons({}, {})) |
| 113 | |
| 114 | def testNormal(self): |
| 115 | """No users are related.""" |
| 116 | addr_to_addrperm = { |
| 117 | self.addr_perm_parent.address: self.addr_perm_parent, |
| 118 | self.addr_perm_3.address: self.addr_perm_3, |
| 119 | self.addr_perm_4.address: self.addr_perm_4, |
| 120 | self.addr_perm_5.address: self.addr_perm_5, |
| 121 | } |
| 122 | addr_to_reasons = { |
| 123 | self.addr_perm_parent.address: [notify_reasons.REASON_CCD], |
| 124 | self.addr_perm_3.address: [notify_reasons.REASON_OWNER], |
| 125 | self.addr_perm_4.address: [notify_reasons.REASON_CCD], |
| 126 | self.addr_perm_5.address: [notify_reasons.REASON_CCD], |
| 127 | } |
| 128 | self.assertEqual( |
| 129 | {self.addr_perm_parent.address: [notify_reasons.REASON_CCD], |
| 130 | self.addr_perm_3.address: [notify_reasons.REASON_OWNER], |
| 131 | self.addr_perm_4.address: [notify_reasons.REASON_CCD], |
| 132 | self.addr_perm_5.address: [notify_reasons.REASON_CCD] |
| 133 | }, |
| 134 | notify_helpers._MergeLinkedAccountReasons( |
| 135 | addr_to_addrperm, addr_to_reasons)) |
| 136 | |
| 137 | def testMerged(self): |
| 138 | """A child is merged into parent notification.""" |
| 139 | addr_to_addrperm = { |
| 140 | self.addr_perm_parent.address: self.addr_perm_parent, |
| 141 | self.addr_perm_child.address: self.addr_perm_child, |
| 142 | } |
| 143 | addr_to_reasons = { |
| 144 | self.addr_perm_parent.address: [notify_reasons.REASON_OWNER], |
| 145 | self.addr_perm_child.address: [notify_reasons.REASON_CCD], |
| 146 | } |
| 147 | self.assertEqual( |
| 148 | {self.addr_perm_parent.address: |
| 149 | [notify_reasons.REASON_OWNER, |
| 150 | notify_reasons.REASON_LINKED_ACCOUNT] |
| 151 | }, |
| 152 | notify_helpers._MergeLinkedAccountReasons( |
| 153 | addr_to_addrperm, addr_to_reasons)) |
| 154 | |
| 155 | |
| 156 | class MakeBulletedEmailWorkItemsTest(unittest.TestCase): |
| 157 | |
| 158 | def setUp(self): |
| 159 | self.project = fake.Project(project_name='proj1') |
| 160 | self.commenter_view = framework_views.StuffUserView( |
| 161 | 111, 'test@example.com', True) |
| 162 | self.issue = fake.MakeTestIssue( |
| 163 | self.project.project_id, 1234, 'summary', 'New', 111) |
| 164 | self.detail_url = 'http://test-detail-url.com/id=1234' |
| 165 | |
| 166 | def testEmptyAddrs(self): |
| 167 | """Test the case where we found zero users to notify.""" |
| 168 | email_tasks = notify_helpers.MakeBulletedEmailWorkItems( |
| 169 | [], self.issue, 'link only body', 'non-member body', 'member body', |
| 170 | self.project, 'example.com', |
| 171 | self.commenter_view, self.detail_url) |
| 172 | self.assertEqual([], email_tasks) |
| 173 | email_tasks = notify_helpers.MakeBulletedEmailWorkItems( |
| 174 | [([], 'reason')], self.issue, 'link only body', 'non-member body', |
| 175 | 'member body', self.project, |
| 176 | 'example.com', self.commenter_view, self.detail_url) |
| 177 | self.assertEqual([], email_tasks) |
| 178 | |
| 179 | |
| 180 | class LinkOnlyLogicTest(unittest.TestCase): |
| 181 | |
| 182 | def setUp(self): |
| 183 | self.user_prefs = user_pb2.UserPrefs() |
| 184 | self.user = user_pb2.User() |
| 185 | self.issue = fake.MakeTestIssue( |
| 186 | 789, 1, 'summary one', 'New', 111) |
| 187 | self.rvg_issue = fake.MakeTestIssue( |
| 188 | 789, 2, 'summary two', 'New', 111, labels=['Restrict-View-Google']) |
| 189 | self.more_restricted_issue = fake.MakeTestIssue( |
| 190 | 789, 3, 'summary three', 'New', 111, labels=['Restrict-View-Core']) |
| 191 | self.both_restricted_issue = fake.MakeTestIssue( |
| 192 | 789, 4, 'summary four', 'New', 111, |
| 193 | labels=['Restrict-View-Google', 'Restrict-View-Core']) |
| 194 | self.addr_perm = notify_reasons.AddrPerm( |
| 195 | False, 'user@example.com', self.user, notify_reasons.REPLY_MAY_COMMENT, |
| 196 | self.user_prefs) |
| 197 | |
| 198 | def testGetNotifyRestrictedIssues_NoPrefsPassed(self): |
| 199 | """AlsoNotify and all-issues addresses have no UserPrefs. None is used.""" |
| 200 | actual = notify_helpers._GetNotifyRestrictedIssues( |
| 201 | None, 'user@example.com', self.user) |
| 202 | self.assertEqual('notify with link only', actual) |
| 203 | |
| 204 | self.user.last_visit_timestamp = 123456789 |
| 205 | actual = notify_helpers._GetNotifyRestrictedIssues( |
| 206 | None, 'user@example.com', self.user) |
| 207 | self.assertEqual('notify with details', actual) |
| 208 | |
| 209 | def testGetNotifyRestrictedIssues_PrefIsSet(self): |
| 210 | """When the notify_restricted_issues pref is set, we use it.""" |
| 211 | self.user_prefs.prefs.extend([ |
| 212 | user_pb2.UserPrefValue(name='x', value='y'), |
| 213 | user_pb2.UserPrefValue(name='notify_restricted_issues', value='z'), |
| 214 | ]) |
| 215 | actual = notify_helpers._GetNotifyRestrictedIssues( |
| 216 | self.user_prefs, 'user@example.com', self.user) |
| 217 | self.assertEqual('z', actual) |
| 218 | |
| 219 | def testGetNotifyRestrictedIssues_UserHasVisited(self): |
| 220 | """If user has ever visited, we know that they are not a mailing list.""" |
| 221 | self.user.last_visit_timestamp = 123456789 |
| 222 | actual = notify_helpers._GetNotifyRestrictedIssues( |
| 223 | self.user_prefs, 'user@example.com', self.user) |
| 224 | self.assertEqual('notify with details', actual) |
| 225 | |
| 226 | def testGetNotifyRestrictedIssues_GooglerNeverVisited(self): |
| 227 | """It could be a noogler or google mailing list.""" |
| 228 | actual = notify_helpers._GetNotifyRestrictedIssues( |
| 229 | self.user_prefs, 'user@google.com', self.user) |
| 230 | self.assertEqual('notify with details: Google', actual) |
| 231 | |
| 232 | def testGetNotifyRestrictedIssues_NonGooglerNeverVisited(self): |
| 233 | """It could be a new non-noogler or public mailing list.""" |
| 234 | actual = notify_helpers._GetNotifyRestrictedIssues( |
| 235 | self.user_prefs, 'user@example.com', self.user) |
| 236 | self.assertEqual('notify with link only', actual) |
| 237 | |
| 238 | # If email does not match any known user, user object will be None. |
| 239 | actual = notify_helpers._GetNotifyRestrictedIssues( |
| 240 | self.user_prefs, 'user@example.com', None) |
| 241 | self.assertEqual('notify with link only', actual) |
| 242 | |
| 243 | def testShouldUseLinkOnly_UnrestrictedIssue(self): |
| 244 | """Issue is not restricted, so go ahead and send comment details.""" |
| 245 | self.assertFalse(notify_helpers.ShouldUseLinkOnly( |
| 246 | self.addr_perm, self.issue)) |
| 247 | |
| 248 | def testShouldUseLinkOnly_AlwaysDetailed(self): |
| 249 | """Issue is not restricted, so go ahead and send comment details.""" |
| 250 | self.assertFalse( |
| 251 | notify_helpers.ShouldUseLinkOnly(self.addr_perm, self.issue, True)) |
| 252 | |
| 253 | @mock.patch('features.notify_helpers._GetNotifyRestrictedIssues') |
| 254 | def testShouldUseLinkOnly_NotifyWithDetails(self, fake_gnri): |
| 255 | """Issue is restricted, and user is allowed to get full comment details.""" |
| 256 | fake_gnri.return_value = notify_helpers.NOTIFY_WITH_DETAILS |
| 257 | self.assertFalse(notify_helpers.ShouldUseLinkOnly( |
| 258 | self.addr_perm, self.rvg_issue)) |
| 259 | self.assertFalse(notify_helpers.ShouldUseLinkOnly( |
| 260 | self.addr_perm, self.more_restricted_issue)) |
| 261 | self.assertFalse(notify_helpers.ShouldUseLinkOnly( |
| 262 | self.addr_perm, self.both_restricted_issue)) |
| 263 | |
| 264 | |
| 265 | class MakeEmailWorkItemTest(unittest.TestCase): |
| 266 | |
| 267 | def setUp(self): |
| 268 | self.project = fake.Project(project_name='proj1') |
| 269 | self.project.process_inbound_email = True |
| 270 | self.project2 = fake.Project(project_name='proj2') |
| 271 | self.project2.issue_notify_always_detailed = True |
| 272 | self.commenter_view = framework_views.StuffUserView( |
| 273 | 111, 'test@example.com', True) |
| 274 | self.expected_html_footer = ( |
| 275 | 'You received this message because:<br/> 1. reason<br/><br/>You may ' |
| 276 | 'adjust your notification preferences at:<br/><a href="https://' |
| 277 | 'example.com/hosting/settings">https://example.com/hosting/settings' |
| 278 | '</a>') |
| 279 | self.services = service_manager.Services( |
| 280 | user=fake.UserService()) |
| 281 | self.member = self.services.user.TestAddUser('member@example.com', 222) |
| 282 | self.issue = fake.MakeTestIssue( |
| 283 | self.project.project_id, 1234, 'summary', 'New', 111, |
| 284 | project_name='proj1') |
| 285 | self.detail_url = 'http://test-detail-url.com/id=1234' |
| 286 | |
| 287 | @mock.patch('features.notify_helpers.ShouldUseLinkOnly') |
| 288 | def testBodySelection_LinkOnly(self, mock_sulo): |
| 289 | """We send a link-only body when ShouldUseLinkOnly() is true.""" |
| 290 | mock_sulo.return_value = True |
| 291 | email_task = notify_helpers._MakeEmailWorkItem( |
| 292 | notify_reasons.AddrPerm( |
| 293 | True, 'a@a.com', self.member, REPLY_NOT_ALLOWED, |
| 294 | user_pb2.UserPrefs()), |
| 295 | ['reason'], self.issue, |
| 296 | 'body link-only', 'body mem', 'body mem', self.project, |
| 297 | 'example.com', self.commenter_view, self.detail_url) |
| 298 | self.assertIn('body link-only', email_task['body']) |
| 299 | |
| 300 | def testBodySelection_Member(self): |
| 301 | """We send members the email body that is indented for members.""" |
| 302 | email_task = notify_helpers._MakeEmailWorkItem( |
| 303 | notify_reasons.AddrPerm( |
| 304 | True, 'a@a.com', self.member, REPLY_NOT_ALLOWED, |
| 305 | user_pb2.UserPrefs()), |
| 306 | ['reason'], self.issue, |
| 307 | 'body link-only', 'body mem', 'body mem', self.project, |
| 308 | 'example.com', self.commenter_view, self.detail_url) |
| 309 | self.assertIn('body mem', email_task['body']) |
| 310 | |
| 311 | def testBodySelection_AlwaysDetailed(self): |
| 312 | """Always send full email when project configuration requires it.""" |
| 313 | email_task = notify_helpers._MakeEmailWorkItem( |
| 314 | notify_reasons.AddrPerm( |
| 315 | True, 'a@a.com', self.member, REPLY_NOT_ALLOWED, |
| 316 | user_pb2.UserPrefs()), ['reason'], self.issue, 'body link-only', |
| 317 | 'body mem', 'body mem', self.project2, 'example.com', |
| 318 | self.commenter_view, self.detail_url) |
| 319 | self.assertIn('body mem', email_task['body']) |
| 320 | |
| 321 | def testBodySelection_NonMember(self): |
| 322 | """We send non-members the email body that is indented for non-members.""" |
| 323 | email_task = notify_helpers._MakeEmailWorkItem( |
| 324 | notify_reasons.AddrPerm( |
| 325 | False, 'a@a.com', self.member, REPLY_NOT_ALLOWED, |
| 326 | user_pb2.UserPrefs()), |
| 327 | ['reason'], self.issue, |
| 328 | 'body link-only', 'body non', 'body mem', self.project, |
| 329 | 'example.com', self.commenter_view, self.detail_url) |
| 330 | |
| 331 | self.assertEqual('a@a.com', email_task['to']) |
| 332 | self.assertEqual('Issue 1234 in proj1: summary', email_task['subject']) |
| 333 | self.assertIn('body non', email_task['body']) |
| 334 | self.assertEqual( |
| 335 | emailfmt.FormatFromAddr(self.project, commenter_view=self.commenter_view, |
| 336 | can_reply_to=False), |
| 337 | email_task['from_addr']) |
| 338 | self.assertEqual(emailfmt.NoReplyAddress(), email_task['reply_to']) |
| 339 | |
| 340 | def testHtmlBody(self): |
| 341 | """"An html body is sent if a detail_url is specified.""" |
| 342 | email_task = notify_helpers._MakeEmailWorkItem( |
| 343 | notify_reasons.AddrPerm( |
| 344 | False, 'a@a.com', self.member, REPLY_NOT_ALLOWED, |
| 345 | user_pb2.UserPrefs()), |
| 346 | ['reason'], self.issue, |
| 347 | 'body link-only', 'body non', 'body mem', self.project, |
| 348 | 'example.com', self.commenter_view, self.detail_url) |
| 349 | |
| 350 | expected_html_body = ( |
| 351 | notify_helpers.HTML_BODY_WITH_GMAIL_ACTION_TEMPLATE % { |
| 352 | 'url': self.detail_url, |
| 353 | 'body': 'body non-- <br/>%s' % self.expected_html_footer}) |
| 354 | self.assertEqual(expected_html_body, email_task['html_body']) |
| 355 | |
| 356 | def testHtmlBody_WithUnicodeChars(self): |
| 357 | """"An html body is sent if a detail_url is specified.""" |
| 358 | unicode_content = '\xe2\x9d\xa4 â â' |
| 359 | email_task = notify_helpers._MakeEmailWorkItem( |
| 360 | notify_reasons.AddrPerm( |
| 361 | False, 'a@a.com', self.member, REPLY_NOT_ALLOWED, |
| 362 | user_pb2.UserPrefs()), |
| 363 | ['reason'], self.issue, |
| 364 | 'body link-only', unicode_content, 'unused body mem', |
| 365 | self.project, 'example.com', self.commenter_view, self.detail_url) |
| 366 | |
| 367 | expected_html_body = ( |
| 368 | notify_helpers.HTML_BODY_WITH_GMAIL_ACTION_TEMPLATE % { |
| 369 | 'url': self.detail_url, |
| 370 | 'body': '%s-- <br/>%s' % (unicode_content.decode('utf-8'), |
| 371 | self.expected_html_footer)}) |
| 372 | self.assertEqual(expected_html_body, email_task['html_body']) |
| 373 | |
| 374 | def testHtmlBody_WithLinks(self): |
| 375 | """"An html body is sent if a detail_url is specified.""" |
| 376 | email_task = notify_helpers._MakeEmailWorkItem( |
| 377 | notify_reasons.AddrPerm( |
| 378 | False, 'a@a.com', self.member, REPLY_NOT_ALLOWED, |
| 379 | user_pb2.UserPrefs()), |
| 380 | ['reason'], self.issue, |
| 381 | 'body link-only', 'test google.com test', 'unused body mem', |
| 382 | self.project, 'example.com', self.commenter_view, self.detail_url) |
| 383 | |
| 384 | expected_html_body = ( |
| 385 | notify_helpers.HTML_BODY_WITH_GMAIL_ACTION_TEMPLATE % { |
| 386 | 'url': self.detail_url, |
| 387 | 'body': ( |
| 388 | 'test <a href="http://google.com">google.com</a> test-- <br/>%s' % ( |
| 389 | self.expected_html_footer))}) |
| 390 | self.assertEqual(expected_html_body, email_task['html_body']) |
| 391 | |
| 392 | def testHtmlBody_LinkWithinTags(self): |
| 393 | """"An html body is sent with correct <a href>s.""" |
| 394 | email_task = notify_helpers._MakeEmailWorkItem( |
| 395 | notify_reasons.AddrPerm( |
| 396 | False, 'a@a.com', self.member, REPLY_NOT_ALLOWED, |
| 397 | user_pb2.UserPrefs()), |
| 398 | ['reason'], self.issue, |
| 399 | 'body link-only', 'a <http://google.com> z', 'unused body', |
| 400 | self.project, 'example.com', self.commenter_view, |
| 401 | self.detail_url) |
| 402 | |
| 403 | expected_html_body = ( |
| 404 | notify_helpers.HTML_BODY_WITH_GMAIL_ACTION_TEMPLATE % { |
| 405 | 'url': self.detail_url, |
| 406 | 'body': ( |
| 407 | 'a <<a href="http://google.com">http://google.com</a>> ' |
| 408 | 'z-- <br/>%s' % self.expected_html_footer)}) |
| 409 | self.assertEqual(expected_html_body, email_task['html_body']) |
| 410 | |
| 411 | def testHtmlBody_EmailWithinTags(self): |
| 412 | """"An html body is sent with correct <a href>s.""" |
| 413 | email_task = notify_helpers._MakeEmailWorkItem( |
| 414 | notify_reasons.AddrPerm( |
| 415 | False, 'a@a.com', self.member, REPLY_NOT_ALLOWED, |
| 416 | user_pb2.UserPrefs()), |
| 417 | ['reason'], self.issue, |
| 418 | 'body link-only', 'a <tt@chromium.org> <aa@chromium.org> z', |
| 419 | 'unused body mem', self.project, 'example.com', self.commenter_view, |
| 420 | self.detail_url) |
| 421 | |
| 422 | expected_html_body = ( |
| 423 | notify_helpers.HTML_BODY_WITH_GMAIL_ACTION_TEMPLATE % { |
| 424 | 'url': self.detail_url, |
| 425 | 'body': ( |
| 426 | 'a <<a href="mailto:tt@chromium.org">tt@chromium.org</a>>' |
| 427 | ' <<a href="mailto:aa@chromium.org">aa@chromium.org</a>> ' |
| 428 | 'z-- <br/>%s' % self.expected_html_footer)}) |
| 429 | self.assertEqual(expected_html_body, email_task['html_body']) |
| 430 | |
| 431 | def testHtmlBody_WithEscapedHtml(self): |
| 432 | """"An html body is sent with html content escaped.""" |
| 433 | body_with_html_content = ( |
| 434 | '<a href="http://www.google.com">test</a> \'something\'') |
| 435 | email_task = notify_helpers._MakeEmailWorkItem( |
| 436 | notify_reasons.AddrPerm( |
| 437 | False, 'a@a.com', self.member, REPLY_NOT_ALLOWED, |
| 438 | user_pb2.UserPrefs()), |
| 439 | ['reason'], self.issue, |
| 440 | 'body link-only', body_with_html_content, 'unused body mem', |
| 441 | self.project, 'example.com', self.commenter_view, self.detail_url) |
| 442 | |
| 443 | escaped_body_with_html_content = ( |
| 444 | '<a href="http://www.google.com">test</a> ' |
| 445 | ''something'') |
| 446 | notify_helpers._MakeNotificationFooter( |
| 447 | ['reason'], REPLY_NOT_ALLOWED, 'example.com') |
| 448 | expected_html_body = ( |
| 449 | notify_helpers.HTML_BODY_WITH_GMAIL_ACTION_TEMPLATE % { |
| 450 | 'url': self.detail_url, |
| 451 | 'body': '%s-- <br/>%s' % (escaped_body_with_html_content, |
| 452 | self.expected_html_footer)}) |
| 453 | self.assertEqual(expected_html_body, email_task['html_body']) |
| 454 | |
| 455 | def doTestAddHTMLTags(self, body, expected): |
| 456 | actual = notify_helpers._AddHTMLTags(body) |
| 457 | self.assertEqual(expected, actual) |
| 458 | |
| 459 | def testAddHTMLTags_Email(self): |
| 460 | """An email address produces <a href="mailto:...">...</a>.""" |
| 461 | self.doTestAddHTMLTags( |
| 462 | 'test test@example.com.', |
| 463 | ('test <a href="mailto:test@example.com">' |
| 464 | 'test@example.com</a>.')) |
| 465 | |
| 466 | def testAddHTMLTags_EmailInQuotes(self): |
| 467 | """Quoted "test@example.com" produces "<a href="...">...</a>".""" |
| 468 | self.doTestAddHTMLTags( |
| 469 | 'test "test@example.com".', |
| 470 | ('test "<a href="mailto:test@example.com">' |
| 471 | 'test@example.com</a>".')) |
| 472 | |
| 473 | def testAddHTMLTags_EmailInAngles(self): |
| 474 | """Bracketed <test@example.com> produces <<a href="...">...</a>>.""" |
| 475 | self.doTestAddHTMLTags( |
| 476 | 'test <test@example.com>.', |
| 477 | ('test <<a href="mailto:test@example.com">' |
| 478 | 'test@example.com</a>>.')) |
| 479 | |
| 480 | def testAddHTMLTags_Website(self): |
| 481 | """A website URL produces <a href="http:...">...</a>.""" |
| 482 | self.doTestAddHTMLTags( |
| 483 | 'test http://www.example.com.', |
| 484 | ('test <a href="http://www.example.com">' |
| 485 | 'http://www.example.com</a>.')) |
| 486 | |
| 487 | def testAddHTMLTags_WebsiteInQuotes(self): |
| 488 | """A link in quotes gets the quotes escaped.""" |
| 489 | self.doTestAddHTMLTags( |
| 490 | 'test "http://www.example.com".', |
| 491 | ('test "<a href="http://www.example.com">' |
| 492 | 'http://www.example.com</a>".')) |
| 493 | |
| 494 | def testAddHTMLTags_WebsiteInAngles(self): |
| 495 | """Bracketed <www.example.com> produces <<a href="...">...</a>>.""" |
| 496 | self.doTestAddHTMLTags( |
| 497 | 'test <http://www.example.com>.', |
| 498 | ('test <<a href="http://www.example.com">' |
| 499 | 'http://www.example.com</a>>.')) |
| 500 | |
| 501 | def testReplyInvitation(self): |
| 502 | """We include a footer about replying that is appropriate for that user.""" |
| 503 | email_task = notify_helpers._MakeEmailWorkItem( |
| 504 | notify_reasons.AddrPerm( |
| 505 | True, 'a@a.com', self.member, REPLY_NOT_ALLOWED, |
| 506 | user_pb2.UserPrefs()), |
| 507 | ['reason'], self.issue, |
| 508 | 'body link-only', 'body non', 'body mem', self.project, |
| 509 | 'example.com', self.commenter_view, self.detail_url) |
| 510 | self.assertEqual(emailfmt.NoReplyAddress(), email_task['reply_to']) |
| 511 | self.assertNotIn('Reply to this email', email_task['body']) |
| 512 | |
| 513 | email_task = notify_helpers._MakeEmailWorkItem( |
| 514 | notify_reasons.AddrPerm( |
| 515 | True, 'a@a.com', self.member, REPLY_MAY_COMMENT, |
| 516 | user_pb2.UserPrefs()), |
| 517 | ['reason'], self.issue, |
| 518 | 'body link-only', 'body non', 'body mem', self.project, |
| 519 | 'example.com', self.commenter_view, self.detail_url) |
| 520 | self.assertEqual( |
| 521 | '%s@%s' % (self.project.project_name, emailfmt.MailDomain()), |
| 522 | email_task['reply_to']) |
| 523 | self.assertIn('Reply to this email to add a comment', email_task['body']) |
| 524 | self.assertNotIn('make changes', email_task['body']) |
| 525 | |
| 526 | email_task = notify_helpers._MakeEmailWorkItem( |
| 527 | notify_reasons.AddrPerm( |
| 528 | True, 'a@a.com', self.member, REPLY_MAY_UPDATE, |
| 529 | user_pb2.UserPrefs()), |
| 530 | ['reason'], self.issue, |
| 531 | 'body link-only', 'body non', 'body mem', self.project, |
| 532 | 'example.com', self.commenter_view, self.detail_url) |
| 533 | self.assertEqual( |
| 534 | '%s@%s' % (self.project.project_name, emailfmt.MailDomain()), |
| 535 | email_task['reply_to']) |
| 536 | self.assertIn('Reply to this email to add a comment', email_task['body']) |
| 537 | self.assertIn('make updates', email_task['body']) |
| 538 | |
| 539 | def testInboundEmailDisabled(self): |
| 540 | """We don't invite replies if they are disabled for this project.""" |
| 541 | self.project.process_inbound_email = False |
| 542 | email_task = notify_helpers._MakeEmailWorkItem( |
| 543 | notify_reasons.AddrPerm( |
| 544 | True, 'a@a.com', self.member, REPLY_MAY_UPDATE, |
| 545 | user_pb2.UserPrefs()), |
| 546 | ['reason'], self.issue, |
| 547 | 'body link-only', 'body non', 'body mem', |
| 548 | self.project, 'example.com', self.commenter_view, self.detail_url) |
| 549 | self.assertEqual(emailfmt.NoReplyAddress(), email_task['reply_to']) |
| 550 | |
| 551 | def testReasons(self): |
| 552 | """The footer lists reasons why that email was sent to that user.""" |
| 553 | email_task = notify_helpers._MakeEmailWorkItem( |
| 554 | notify_reasons.AddrPerm( |
| 555 | True, 'a@a.com', self.member, REPLY_MAY_UPDATE, |
| 556 | user_pb2.UserPrefs()), |
| 557 | ['Funny', 'Caring', 'Near'], self.issue, |
| 558 | 'body link-only', 'body non', 'body mem', |
| 559 | self.project, 'example.com', self.commenter_view, self.detail_url) |
| 560 | self.assertIn('because:', email_task['body']) |
| 561 | self.assertIn('1. Funny', email_task['body']) |
| 562 | self.assertIn('2. Caring', email_task['body']) |
| 563 | self.assertIn('3. Near', email_task['body']) |
| 564 | |
| 565 | email_task = notify_helpers._MakeEmailWorkItem( |
| 566 | notify_reasons.AddrPerm( |
| 567 | True, 'a@a.com', self.member, REPLY_MAY_UPDATE, |
| 568 | user_pb2.UserPrefs()), |
| 569 | [], self.issue, |
| 570 | 'body link-only', 'body non', 'body mem', |
| 571 | self.project, 'example.com', self.commenter_view, self.detail_url) |
| 572 | self.assertNotIn('because', email_task['body']) |
| 573 | |
| 574 | |
| 575 | class MakeNotificationFooterTest(unittest.TestCase): |
| 576 | |
| 577 | def testMakeNotificationFooter_NoReason(self): |
| 578 | footer = notify_helpers._MakeNotificationFooter( |
| 579 | [], REPLY_NOT_ALLOWED, 'example.com') |
| 580 | self.assertEqual('', footer) |
| 581 | |
| 582 | def testMakeNotificationFooter_WithReason(self): |
| 583 | footer = notify_helpers._MakeNotificationFooter( |
| 584 | ['REASON'], REPLY_NOT_ALLOWED, 'example.com') |
| 585 | self.assertIn('REASON', footer) |
| 586 | self.assertIn('https://example.com/hosting/settings', footer) |
| 587 | |
| 588 | footer = notify_helpers._MakeNotificationFooter( |
| 589 | ['REASON'], REPLY_NOT_ALLOWED, 'example.com') |
| 590 | self.assertIn('REASON', footer) |
| 591 | self.assertIn('https://example.com/hosting/settings', footer) |
| 592 | |
| 593 | def testMakeNotificationFooter_ManyReasons(self): |
| 594 | footer = notify_helpers._MakeNotificationFooter( |
| 595 | ['Funny', 'Caring', 'Warmblooded'], REPLY_NOT_ALLOWED, |
| 596 | 'example.com') |
| 597 | self.assertIn('Funny', footer) |
| 598 | self.assertIn('Caring', footer) |
| 599 | self.assertIn('Warmblooded', footer) |
| 600 | |
| 601 | def testMakeNotificationFooter_WithReplyInstructions(self): |
| 602 | footer = notify_helpers._MakeNotificationFooter( |
| 603 | ['REASON'], REPLY_NOT_ALLOWED, 'example.com') |
| 604 | self.assertNotIn('Reply', footer) |
| 605 | self.assertIn('https://example.com/hosting/settings', footer) |
| 606 | |
| 607 | footer = notify_helpers._MakeNotificationFooter( |
| 608 | ['REASON'], REPLY_MAY_COMMENT, 'example.com') |
| 609 | self.assertIn('add a comment', footer) |
| 610 | self.assertNotIn('make updates', footer) |
| 611 | self.assertIn('https://example.com/hosting/settings', footer) |
| 612 | |
| 613 | footer = notify_helpers._MakeNotificationFooter( |
| 614 | ['REASON'], REPLY_MAY_UPDATE, 'example.com') |
| 615 | self.assertIn('add a comment', footer) |
| 616 | self.assertIn('make updates', footer) |
| 617 | self.assertIn('https://example.com/hosting/settings', footer) |