Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: appengine/monorail/framework/test/emailfmt_test.py

Issue 1868553004: Open Source Monorail (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is govered 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 monorail.framework.emailfmt."""
7
8 import unittest
9
10 import settings
11 from framework import emailfmt
12 from framework import framework_views
13 from proto import project_pb2
14 from testing import testing_helpers
15
16 from google.appengine.api import apiproxy_stub_map
17
18
19 class EmailFmtTest(unittest.TestCase):
20
21 @unittest.skipIf('memcache' not in
22 apiproxy_stub_map.apiproxy._APIProxyStubMap__stub_map,
23 'memcache api proxy will not be found')
24 def testValidateReferencesHeader(self):
25 project = project_pb2.Project()
26 project.project_name = 'open-open'
27 subject = 'slipped disk'
28 expected = emailfmt.MakeMessageID(
29 'jrobbins@gmail.com', subject,
30 '%s@%s' % (project.project_name, emailfmt.MailDomain()))
31 self.assertTrue(
32 emailfmt.ValidateReferencesHeader(
33 expected, project, 'jrobbins@gmail.com', subject))
34
35 self.assertFalse(
36 emailfmt.ValidateReferencesHeader(
37 expected, project, 'jrobbins@gmail.com', 'something else'))
38
39 self.assertFalse(
40 emailfmt.ValidateReferencesHeader(
41 expected, project, 'someoneelse@gmail.com', subject))
42
43 project.project_name = 'other-project'
44 self.assertFalse(
45 emailfmt.ValidateReferencesHeader(
46 expected, project, 'jrobbins@gmail.com', subject))
47
48 def testParseEmailMessage(self):
49 msg = testing_helpers.MakeMessage(testing_helpers.HEADER_LINES, 'awesome!')
50
51 (from_addr, to_addrs, cc_addrs, references, subject,
52 body) = emailfmt.ParseEmailMessage(msg)
53
54 self.assertEqual('user@example.com', from_addr)
55 self.assertEqual(['proj@monorail.example.com'], to_addrs)
56 self.assertEqual(['ningerso@chromium.org'], cc_addrs)
57 # Expected msg-id was generated from a previous known-good test run.
58 self.assertEqual(['<0=969704940193871313=13442892928193434663='
59 'proj@monorail.example.com>'],
60 references)
61 self.assertEqual('Issue 123 in proj: broken link', subject)
62 self.assertEqual('awesome!', body)
63
64 references_header = ('References', '<1234@foo.com> <5678@bar.com>')
65 msg = testing_helpers.MakeMessage(
66 testing_helpers.HEADER_LINES + [references_header], 'awesome!')
67 (from_addr, to_addrs, cc_addrs, references, subject,
68 body) = emailfmt.ParseEmailMessage(msg)
69 self.assertItemsEqual(
70 ['<5678@bar.com>',
71 '<0=969704940193871313=13442892928193434663='
72 'proj@monorail.example.com>',
73 '<1234@foo.com>'],
74 references)
75
76 def testParseEmailMessage_Bulk(self):
77 for precedence in ['Bulk', 'Junk']:
78 msg = testing_helpers.MakeMessage(
79 testing_helpers.HEADER_LINES + [('Precedence', precedence)],
80 'I am on vacation!')
81
82 (from_addr, to_addrs, cc_addrs, in_reply_to, subject,
83 body) = emailfmt.ParseEmailMessage(msg)
84
85 self.assertEqual('', from_addr)
86 self.assertEqual([], to_addrs)
87 self.assertEqual([], cc_addrs)
88 self.assertEqual('', in_reply_to)
89 self.assertEqual('', subject)
90 self.assertEqual('', body)
91
92 def testExtractAddrs(self):
93 header_val = ''
94 self.assertEqual(
95 [], emailfmt._ExtractAddrs(header_val))
96
97 header_val = 'J. Robbins <a@b.com>, c@d.com,\n Nick "Name" Dude <e@f.com>'
98 self.assertEqual(
99 ['a@b.com', 'c@d.com', 'e@f.com'],
100 emailfmt._ExtractAddrs(header_val))
101
102 header_val = ('hot: J. O\'Robbins <a@b.com>; '
103 'cool: "friendly" <e.g-h@i-j.k-L.com>')
104 self.assertEqual(
105 ['a@b.com', 'e.g-h@i-j.k-L.com'],
106 emailfmt._ExtractAddrs(header_val))
107
108 def CheckIdentifiedValues(
109 self, project_addr, subject, expected_project_name, expected_local_id):
110 """Testing helper function to check 3 results against expected values."""
111 project_name, local_id = emailfmt.IdentifyProjectAndIssue(
112 project_addr, subject)
113 self.assertEqual(expected_project_name, project_name)
114 self.assertEqual(expected_local_id, local_id)
115
116 def testIdentifyProjectAndIssues(self):
117 self.CheckIdentifiedValues(
118 'proj@monorail.example.com',
119 'Issue 123 in proj: the dogs wont eat the dogfood',
120 'proj', 123)
121
122 self.CheckIdentifiedValues(
123 'Proj@MonoRail.Example.Com',
124 'Issue 123 in proj: the dogs wont eat the dogfood',
125 'proj', 123)
126
127 self.CheckIdentifiedValues(
128 'proj-4-u@test-example3.com',
129 'Issue 123 in proj-4-u: this one goes to: 11',
130 'proj-4-u', 123)
131
132 self.CheckIdentifiedValues(
133 'night@monorail.example.com',
134 'Issue 451 in day: something is fishy',
135 None, 451)
136
137 self.CheckIdentifiedValues(
138 'no_reply@chromium.org',
139 'Issue 234 in project foo: ignore this one',
140 None, None)
141
142 def testStripSubjectPrefixes(self):
143 self.assertEqual(
144 '',
145 emailfmt._StripSubjectPrefixes(''))
146
147 self.assertEqual(
148 'this is it',
149 emailfmt._StripSubjectPrefixes('this is it'))
150
151 self.assertEqual(
152 'this is it',
153 emailfmt._StripSubjectPrefixes('re: this is it'))
154
155 self.assertEqual(
156 'this is it',
157 emailfmt._StripSubjectPrefixes('Re: Fwd: aw:this is it'))
158
159 self.assertEqual(
160 'This - . IS it',
161 emailfmt._StripSubjectPrefixes('This - . IS it'))
162
163
164 class MailDomainTest(unittest.TestCase):
165
166 def testTrivialCases(self):
167 self.assertEqual(
168 'testbed-test.appspotmail.com',
169 emailfmt.MailDomain())
170
171
172 class NoReplyAddressTest(unittest.TestCase):
173
174 def testNoCommenter(self):
175 self.assertEqual(
176 'no_reply@testbed-test.appspotmail.com',
177 emailfmt.NoReplyAddress())
178
179 def testWithCommenter(self):
180 commenter_view = framework_views.UserView(111L, 'user@example.com', True)
181 self.assertEqual(
182 'user@example.com via Monorail <no_reply@testbed-test.appspotmail.com>',
183 emailfmt.NoReplyAddress(
184 commenter_view=commenter_view, reveal_addr=True))
185
186 def testObscuredCommenter(self):
187 commenter_view = framework_views.UserView(111L, 'user@example.com', True)
188 self.assertEqual(
189 'u...@example.com via Monorail <no_reply@testbed-test.appspotmail.com>',
190 emailfmt.NoReplyAddress(
191 commenter_view=commenter_view, reveal_addr=False))
192
193
194 class FormatFromAddrTest(unittest.TestCase):
195
196 def setUp(self):
197 self.project = project_pb2.Project(project_name='proj')
198
199 def testNoCommenter(self):
200 self.assertEqual(settings.send_email_as,
201 emailfmt.FormatFromAddr(self.project))
202
203 def testNoCommenterWithNoReply(self):
204 self.assertEqual(settings.send_noreply_email_as,
205 emailfmt.FormatFromAddr(self.project, can_reply_to=False))
206
207 def testWithCommenter(self):
208 commenter_view = framework_views.UserView(111L, 'user@example.com', True)
209 self.assertEqual(
210 'user@example.com via Monorail <%s>' % settings.send_email_as,
211 emailfmt.FormatFromAddr(
212 self.project, commenter_view=commenter_view, reveal_addr=True))
213
214 def testObscuredCommenter(self):
215 commenter_view = framework_views.UserView(111L, 'user@example.com', True)
216 self.assertEqual(
217 'u...@example.com via Monorail <%s>' % settings.send_email_as,
218 emailfmt.FormatFromAddr(
219 self.project, commenter_view=commenter_view, reveal_addr=False))
220
221 def testServiceAccountCommenter(self):
222 johndoe_bot = '123456789@developer.gserviceaccount.com'
223 commenter_view = framework_views.UserView(111L, johndoe_bot, True)
224 self.assertEqual(
225 ('johndoe@example.com via Monorail <%s>' % settings.send_email_as),
226 emailfmt.FormatFromAddr(
227 self.project, commenter_view=commenter_view, reveal_addr=False))
228
229
230 class NormalizeHeaderWhitespaceTest(unittest.TestCase):
231
232 def testTrivialCases(self):
233 self.assertEqual(
234 '',
235 emailfmt.NormalizeHeader(''))
236
237 self.assertEqual(
238 '',
239 emailfmt.NormalizeHeader(' \t\n'))
240
241 self.assertEqual(
242 'a',
243 emailfmt.NormalizeHeader('a'))
244
245 self.assertEqual(
246 'a b',
247 emailfmt.NormalizeHeader(' a b '))
248
249 def testLongSummary(self):
250 big_string = 'x' * 500
251 self.assertEqual(
252 big_string[:emailfmt.MAX_HEADER_CHARS_CONSIDERED],
253 emailfmt.NormalizeHeader(big_string))
254
255 big_string = 'x y ' * 500
256 self.assertEqual(
257 big_string[:emailfmt.MAX_HEADER_CHARS_CONSIDERED],
258 emailfmt.NormalizeHeader(big_string))
259
260 big_string = 'x ' * 100
261 self.assertEqual(
262 'x ' * 99 + 'x',
263 emailfmt.NormalizeHeader(big_string))
264
265 def testNormalCase(self):
266 self.assertEqual(
267 '[a] b: c d',
268 emailfmt.NormalizeHeader('[a] b:\tc\n\td'))
269
270
271 class MakeMessageIDTest(unittest.TestCase):
272
273 @unittest.skipIf('memcache' not in
274 apiproxy_stub_map.apiproxy._APIProxyStubMap__stub_map,
275 'memcache api proxy will not be found')
276 def testMakeMessageIDTest(self):
277 message_id = emailfmt.MakeMessageID(
278 'to@to.com', 'subject', 'from@from.com')
279 self.assertTrue(message_id.startswith('<0='))
280 self.assertEqual('testbed-test.appspotmail.com>',
281 message_id.split('@')[-1])
282
283 settings.mail_domain = None
284 message_id = emailfmt.MakeMessageID(
285 'to@to.com', 'subject', 'from@from.com')
286 self.assertTrue(message_id.startswith('<0='))
287 self.assertEqual('testbed-test.appspotmail.com>',
288 message_id.split('@')[-1])
289
290 message_id = emailfmt.MakeMessageID(
291 'to@to.com', 'subject', 'from@from.com')
292 self.assertTrue(message_id.startswith('<0='))
293 self.assertEqual('testbed-test.appspotmail.com>',
294 message_id.split('@')[-1])
295
296 message_id_ws_1 = emailfmt.MakeMessageID(
297 'to@to.com',
298 'this is a very long subject that is sure to be wordwrapped by gmail',
299 'from@from.com')
300 message_id_ws_2 = emailfmt.MakeMessageID(
301 'to@to.com',
302 'this is a very long subject that \n\tis sure to be '
303 'wordwrapped \t\tby gmail',
304 'from@from.com')
305 self.assertEqual(message_id_ws_1, message_id_ws_2)
306
307
308 class GetReferencesTest(unittest.TestCase):
309
310 def testNotPartOfThread(self):
311 refs = emailfmt.GetReferences(
312 'a@a.com', 'hi', None, emailfmt.NoReplyAddress())
313 self.assertEqual(0, len(refs))
314
315 @unittest.skipIf('memcache' not in
316 apiproxy_stub_map.apiproxy._APIProxyStubMap__stub_map,
317 'memcache api proxy will not be found')
318 def testAnywhereInThread(self):
319 refs = emailfmt.GetReferences(
320 'a@a.com', 'hi', 0, emailfmt.NoReplyAddress())
321 self.assertTrue(len(refs))
322 self.assertTrue(refs.startswith('<0='))
323
324
325 class StripQuotedTextTest(unittest.TestCase):
326
327 def CheckExpected(self, expected_output, test_input):
328 actual_output = emailfmt.StripQuotedText(test_input)
329 self.assertEqual(expected_output, actual_output)
330
331 def testAllNewText(self):
332 self.CheckExpected('', '')
333 self.CheckExpected('', '\n')
334 self.CheckExpected('', '\n\n')
335 self.CheckExpected('new', 'new')
336 self.CheckExpected('new', '\nnew\n')
337 self.CheckExpected('new\ntext', '\nnew\ntext\n')
338 self.CheckExpected('new\n\ntext', '\nnew\n\ntext\n')
339
340 def testQuotedLines(self):
341 self.CheckExpected(
342 ('new\n'
343 'text'),
344 ('new\n'
345 'text\n'
346 '\n'
347 '> something you said\n'
348 '> that took two lines'))
349
350 self.CheckExpected(
351 ('new\n'
352 'text'),
353 ('new\n'
354 'text\n'
355 '\n'
356 '> something you said\n'
357 '> that took two lines'))
358
359 self.CheckExpected(
360 ('new\n'
361 'text'),
362 ('> something you said\n'
363 '> that took two lines\n'
364 'new\n'
365 'text\n'
366 '\n'))
367
368 self.CheckExpected(
369 ('newtext'),
370 ('> something you said\n'
371 '> that took two lines\n'
372 'newtext'))
373
374 self.CheckExpected(
375 ('new\n'
376 'text'),
377 ('new\n'
378 'text\n'
379 '\n'
380 '> something you said\n'
381 '> > in response to some other junk'))
382
383 self.CheckExpected(
384 ('new\n'
385 '\n'
386 'text'),
387 ('new\n'
388 '\n'
389 '> something you said\n'
390 '> > in response to some other junk\n'
391 '\n'
392 'text\n'))
393
394 self.CheckExpected(
395 ('new\n'
396 '\n'
397 'text'),
398 ('new\n'
399 'On Mon, Jan 1, 2023, So-and-so <so@and-so.com> Wrote:\n'
400 '> something you said\n'
401 '> > in response to some other junk\n'
402 '\n'
403 'text\n'))
404
405 self.CheckExpected(
406 ('new\n'
407 '\n'
408 'text'),
409 ('new\n'
410 'On Mon, Jan 1, 2023, So-and-so <so@and-so.com> Wrote:\n'
411 '\n'
412 '> something you said\n'
413 '> > in response to some other junk\n'
414 '\n'
415 'text\n'))
416
417 self.CheckExpected(
418 ('new\n'
419 '\n'
420 'text'),
421 ('new\n'
422 'On Mon, Jan 1, 2023, user@example.com via Monorail\n'
423 '<monorail@chromium.com> Wrote:\n'
424 '\n'
425 '> something you said\n'
426 '> > in response to some other junk\n'
427 '\n'
428 'text\n'))
429
430 self.CheckExpected(
431 ('new\n'
432 '\n'
433 'text'),
434 ('new\n'
435 'On Jan 14, 2016 6:19 AM, "user@example.com via Monorail" <\n'
436 'monorail@chromium.com> Wrote:\n'
437 '\n'
438 '> something you said\n'
439 '> > in response to some other junk\n'
440 '\n'
441 'text\n'))
442
443 self.CheckExpected(
444 ('new\n'
445 '\n'
446 'text'),
447 ('new\n'
448 'On Jan 14, 2016 6:19 AM, "user@example.com via Monorail" <\n'
449 'monorail@monorail-prod.appspotmail.com> wrote:\n'
450 '\n'
451 '> something you said\n'
452 '> > in response to some other junk\n'
453 '\n'
454 'text\n'))
455
456 self.CheckExpected(
457 ('new\n'
458 '\n'
459 'text'),
460 ('new\n'
461 'On Mon, Jan 1, 2023, So-and-so so@and-so.com wrote:\n'
462 '\n'
463 '> something you said\n'
464 '> > in response to some other junk\n'
465 '\n'
466 'text\n'))
467
468 self.CheckExpected(
469 ('new\n'
470 '\n'
471 'text'),
472 ('new\n'
473 'On Wed, Sep 8, 2010 at 6:56 PM, So =AND= <so@gmail.com>wrote:\n'
474 '\n'
475 '> something you said\n'
476 '> > in response to some other junk\n'
477 '\n'
478 'text\n'))
479
480 self.CheckExpected(
481 ('new\n'
482 '\n'
483 'text'),
484 ('new\n'
485 'On Mon, Jan 1, 2023, So-and-so <so@and-so.com> Wrote:\n'
486 '\n'
487 '> something you said\n'
488 '> > in response to some other junk\n'
489 '\n'
490 'text\n'))
491
492 self.CheckExpected(
493 ('new\n'
494 '\n'
495 'text'),
496 ('new\n'
497 'project-name@testbed-test.appspotmail.com wrote:\n'
498 '\n'
499 '> something you said\n'
500 '> > in response to some other junk\n'
501 '\n'
502 'text\n'))
503
504 self.CheckExpected(
505 ('new\n'
506 '\n'
507 'text'),
508 ('new\n'
509 'project-name@testbed-test.appspotmail.com a \xc3\xa9crit :\n'
510 '\n'
511 '> something you said\n'
512 '> > in response to some other junk\n'
513 '\n'
514 'text\n'))
515
516 self.CheckExpected(
517 ('new\n'
518 '\n'
519 'text'),
520 ('new\n'
521 'project.domain.com@testbed-test.appspotmail.com a \xc3\xa9crit :\n'
522 '\n'
523 '> something you said\n'
524 '> > in response to some other junk\n'
525 '\n'
526 'text\n'))
527
528 self.CheckExpected(
529 ('new\n'
530 '\n'
531 'text'),
532 ('new\n'
533 '2023/01/4 <so@and-so.com>\n'
534 '\n'
535 '> something you said\n'
536 '> > in response to some other junk\n'
537 '\n'
538 'text\n'))
539
540 self.CheckExpected(
541 ('new\n'
542 '\n'
543 'text'),
544 ('new\n'
545 '2023/01/4 <so-and@so.com>\n'
546 '\n'
547 '> something you said\n'
548 '> > in response to some other junk\n'
549 '\n'
550 'text\n'))
551
552 def testBoundaryLines(self):
553
554 self.CheckExpected(
555 ('new'),
556 ('new\n'
557 '---- forwarded message ======\n'
558 '\n'
559 'something you said\n'
560 '> in response to some other junk\n'
561 '\n'
562 'text\n'))
563
564 self.CheckExpected(
565 ('new'),
566 ('new\n'
567 '-----Original Message-----\n'
568 '\n'
569 'something you said\n'
570 '> in response to some other junk\n'
571 '\n'
572 'text\n'))
573
574 self.CheckExpected(
575 ('new'),
576 ('new\n'
577 '\n'
578 'Updates:\n'
579 '\tStatus: Fixed\n'
580 '\n'
581 'notification text\n'))
582
583 self.CheckExpected(
584 ('new'),
585 ('new\n'
586 '\n'
587 'Comment #1 on issue 9 by username: Is there ...'
588 'notification text\n'))
589
590 def testSignatures(self):
591
592 self.CheckExpected(
593 ('new\n'
594 'text'),
595 ('new\n'
596 'text\n'
597 '-- \n'
598 'Name\n'
599 'phone\n'
600 'funny quote, or legal disclaimers\n'))
601
602 self.CheckExpected(
603 ('new\n'
604 'text'),
605 ('new\n'
606 'text\n'
607 '--\n'
608 'Name\n'
609 'phone\n'
610 'funny quote, or legal disclaimers\n'))
611
612 self.CheckExpected(
613 ('new\n'
614 'text'),
615 ('new\n'
616 'text\n'
617 '--\n'
618 'Name\n'
619 'ginormous signature\n'
620 'phone\n'
621 'address\n'
622 'address\n'
623 'address\n'
624 'homepage\n'
625 'social network A\n'
626 'social network B\n'
627 'social network C\n'
628 'funny quote\n'
629 '4 lines about why email should be short\n'
630 'legal disclaimers\n'))
631
632 self.CheckExpected(
633 ('new\n'
634 'text'),
635 ('new\n'
636 'text\n'
637 '_______________\n'
638 'Name\n'
639 'phone\n'
640 'funny quote, or legal disclaimers\n'))
641
642 self.CheckExpected(
643 ('new\n'
644 'text'),
645 ('new\n'
646 'text\n'
647 '\n'
648 'Thanks,\n'
649 'Name\n'
650 '\n'
651 '_______________\n'
652 'Name\n'
653 'phone\n'
654 'funny quote, or legal disclaimers\n'))
655
656 self.CheckExpected(
657 ('new\n'
658 'text'),
659 ('new\n'
660 'text\n'
661 '\n'
662 'Thanks,\n'
663 'Name'))
664
665 self.CheckExpected(
666 ('new\n'
667 'text'),
668 ('new\n'
669 'text\n'
670 '\n'
671 'Cheers,\n'
672 'Name'))
673
674 self.CheckExpected(
675 ('new\n'
676 'text'),
677 ('new\n'
678 'text\n'
679 '\n'
680 'Regards\n'
681 'Name'))
682
683 self.CheckExpected(
684 ('new\n'
685 'text'),
686 ('new\n'
687 'text\n'
688 '\n'
689 'best regards'))
690
691 self.CheckExpected(
692 ('new\n'
693 'text'),
694 ('new\n'
695 'text\n'
696 '\n'
697 'THX'))
698
699 self.CheckExpected(
700 ('new\n'
701 'text'),
702 ('new\n'
703 'text\n'
704 '\n'
705 'Thank you,\n'
706 'Name'))
707
708 self.CheckExpected(
709 ('new\n'
710 'text'),
711 ('new\n'
712 'text\n'
713 '\n'
714 'Sent from my iPhone'))
715
716 self.CheckExpected(
717 ('new\n'
718 'text'),
719 ('new\n'
720 'text\n'
721 '\n'
722 'Sent from my iPod'))
723
724
725 if __name__ == '__main__':
726 unittest.main()
OLDNEW
« no previous file with comments | « appengine/monorail/framework/test/captcha_test.py ('k') | appengine/monorail/framework/test/filecontent_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698