-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
1270 lines (1022 loc) · 32.9 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import os
import re
import unittest
from unittest.mock import Mock, patch
from extract_raw_content import constants, html, text, utils
from mail_parser import serialize_mail
STANDARD_REPLIES = "mails/standard_replies"
RE_WHITESPACE = re.compile(r"\s")
RE_DOUBLE_WHITESPACE = re.compile(r"\s")
def get_email_as_bytes(name):
with open(
os.path.join(
os.path.dirname(__file__),
"mails",
name,
),
"rb",
) as f:
return f.read()
class TestMain(unittest.TestCase):
def test_disposition_notification(self):
mail = get_email_as_bytes("disposition-notification.eml")
body = serialize_mail(mail)
body_map = {k: v for k, v in body}
manifest = json.loads(body_map["manifest"][1].read().decode("utf-8"))
self.assertTrue(
manifest["headers"]["auto_reply_type"], "disposition-notification"
)
def test_vacation_reply(self):
mail = get_email_as_bytes("vacation-reply.eml")
body = serialize_mail(mail)
body_map = {k: v for k, v in body}
manifest = json.loads(body_map["manifest"][1].read().decode("utf-8"))
self.assertTrue(manifest["headers"]["auto_reply_type"], "vacation-reply")
def test_html_only(self):
mail = get_email_as_bytes("html_only.eml")
body = serialize_mail(mail)
body_map = {k: v for k, v in body}
manifest = json.loads(body_map["manifest"][1].read().decode("utf-8"))
self.assertTrue(manifest["text"]["content"])
self.assertTrue(manifest["text"]["html_content"])
def test_get_delimiter(self):
self.assertEqual("\r\n", text.get_delimiter("abc\r\n123"))
self.assertEqual("\n", text.get_delimiter("abc\n123"))
self.assertEqual("\n", text.get_delimiter("abc"))
def test_html_to_text(self):
html = """<body>
<p>Hello world!</p>
<br>
<ul>
<li>One!</li>
<li>Two</li>
</ul>
<p>
Haha
</p>
</body>"""
text = utils.html_to_text(html)
self.assertEqual("Hello world! \n\n * One! \n * Two \nHaha", text)
self.assertEqual("привет!", utils.html_to_text("<b>привет!</b>"))
html = "<body><br/><br/>Hi</body>"
self.assertEqual("Hi", utils.html_to_text(html))
html = """Hi
<style type="text/css">
div, p, li {
font: 13px 'Lucida Grande', Arial, sans-serif;
}
</style>
<style type="text/css">
h1 {
font: 13px 'Lucida Grande', Arial, sans-serif;
}
</style>"""
self.assertEqual("Hi", utils.html_to_text(html))
html = """<div>
<!-- COMMENT 1 -->
<span>TEXT 1</span>
<p>TEXT 2 <!-- COMMENT 2 --></p>
</div>"""
self.assertEqual("TEXT 1 \nTEXT 2", utils.html_to_text(html))
def test_comment_no_parent(self):
s = "<!-- COMMENT 1 --> no comment"
d = html.html_document_fromstring(s)
self.assertEqual("no comment", utils.html_tree_to_text(d))
@patch.object(utils, "html_fromstring", Mock(return_value=None))
def test_bad_html_to_text(self):
bad_html = "one<br>two<br>three"
self.assertEqual(None, utils.html_to_text(bad_html))
def test_quotation_splitter_inside_blockquote(self):
msg_body = """Reply
<blockquote>
<div>
On 11-Apr-2011, at 6:54 PM, Bob <[email protected]> wrote:
</div>
<div>
Test
</div>
</blockquote>"""
self.assertEqual(
"<html><head></head><body>Reply</body></html>",
RE_WHITESPACE.sub("", html.extract_from_html(msg_body)),
)
def test_quotation_splitter_outside_blockquote(self):
msg_body = """Reply
<div>
On 11-Apr-2011, at 6:54 PM, Bob <[email protected]> wrote:
</div>
<blockquote>
<div>
Test
</div>
</blockquote>
"""
self.assertEqual(
"<html><head></head><body>Reply</body></html>",
RE_WHITESPACE.sub("", html.extract_from_html(msg_body)),
)
def test_regular_blockquote(self):
msg_body = """Reply
<blockquote>Regular</blockquote>
<div>
On 11-Apr-2011, at 6:54 PM, Bob <[email protected]> wrote:
</div>
<blockquote>
<div>
<blockquote>Nested</blockquote>
</div>
</blockquote>
"""
self.assertEqual(
"<html><head></head><body>Reply"
+ "<blockquote>Regular</blockquote></body></html>",
RE_WHITESPACE.sub("", html.extract_from_html(msg_body)),
)
def test_no_blockquote(self):
msg_body = """
<html>
<body>
Reply
<div>
On 11-Apr-2011, at 6:54 PM, Bob <[email protected]> wrote:
</div>
<div>
Test
</div>
</body>
</html>
"""
reply = """
<html>
<head></head>
<body>
Reply
</body></html>"""
self.assertEqual(
RE_WHITESPACE.sub("", reply),
RE_WHITESPACE.sub("", html.extract_from_html(msg_body)),
)
def test_empty_body(self):
self.assertEqual("", html.extract_from_html(""))
def test_validate_output_html(self):
msg_body = """Reply
<div>
On 11-Apr-2011, at 6:54 PM, Bob <[email protected]> wrote:
<blockquote>
<div>
Test
</div>
</blockquote>
</div>
<div/>
"""
out = html.extract_from_html(msg_body)
self.assertTrue(
"<html>" in out and "</html>" in out,
"Invalid HTML - <html>/</html> tag not present",
)
self.assertTrue(
"<div/>" not in out, "Invalid HTML output - <div/> element is not valid"
)
def test_gmail_quote(self):
msg_body = """Reply
<div class="gmail_quote">
<div class="gmail_quote">
On 11-Apr-2011, at 6:54 PM, Bob <[email protected]> wrote:
<div>
Test
</div>
</div>
</div>"""
self.assertEqual(
"<html><head></head><body>Reply</body></html>",
RE_WHITESPACE.sub("", html.extract_from_html(msg_body)),
)
def test_gmail_quote_compact(self):
msg_body = (
"Reply"
'<div class="gmail_quote">'
'<div class="gmail_quote">'
+ "On 11-Apr-2011, at 6:54 PM, Bob <[email protected]> wrote:"
"<div>Test</div>"
"</div>"
"</div>"
)
self.assertEqual(
"<html><head></head><body>Reply</body></html>",
RE_WHITESPACE.sub("", html.extract_from_html(msg_body)),
)
def test_gmail_quote_blockquote(self):
msg_body = """Message
<blockquote class="gmail_quote">
<div class="gmail_default">
My name is William Shakespeare.
<br/>
</div>
</blockquote>"""
self.assertEqual(
RE_WHITESPACE.sub("", msg_body),
RE_WHITESPACE.sub("", html.extract_from_html(msg_body)),
)
def test_blockquote_disclaimer(self):
msg_body = """
<html>
<body>
<div>
<div>
message
</div>
<blockquote>
Quote
</blockquote>
</div>
<div>
disclaimer
</div>
</body>
</html>
"""
stripped_html = """
<html>
<head></head>
<body>
<div>
<div>
message
</div>
</div>
<div>
disclaimer
</div>
</body>
</html>
"""
self.assertEqual(
RE_WHITESPACE.sub("", stripped_html),
RE_WHITESPACE.sub("", html.extract_from_html(msg_body)),
)
def test_date_block(self):
msg_body = """
<div>
message<br>
<div>
<hr>
Date: Fri, 23 Mar 2012 12:35:31 -0600<br>
To: <a href="mailto:[email protected]">[email protected]</a><br>
From: <a href="mailto:[email protected]">[email protected]</a><br>
Subject: You Have New Mail From Mary!<br><br>
text
</div>
</div>
"""
self.assertEqual(
"<html><head></head><body><div>message<br></div></body></html>",
RE_WHITESPACE.sub("", html.extract_from_html(msg_body)),
)
def test_from_block(self):
msg_body = """<div>
message<br>
<div>
<hr>
From: <a href="mailto:[email protected]">[email protected]</a><br>
Date: Fri, 23 Mar 2012 12:35:31 -0600<br>
To: <a href="mailto:[email protected]">[email protected]</a><br>
Subject: You Have New Mail From Mary!<br><br>
text
</div></div>
"""
self.assertEqual(
"<html><head></head><body><div>message<br></div></body></html>",
RE_WHITESPACE.sub("", html.extract_from_html(msg_body)),
)
def test_reply_shares_div_with_from_block(self):
msg_body = """
<body>
<div>
Blah<br><br>
<hr>Date: Tue, 22 May 2012 18:29:16 -0600<br>
To: [email protected]<br>
From: [email protected]<br>
Subject: You Have New Mail From x!<br><br>
</div>
</body>"""
self.assertEqual(
"<html><head></head><body><div>Blah<br><br></div></body></html>",
RE_WHITESPACE.sub("", html.extract_from_html(msg_body)),
)
def test_reply_quotations_share_block(self):
stripped_html = text.extract_from_plain(
get_email_as_bytes("reply-quotations-share-block.eml").decode("utf-8")
)
self.assertTrue(stripped_html)
self.assertTrue("From" not in stripped_html)
def test_OLK_SRC_BODY_SECTION_stripped(self):
self.assertEqual(
"<html><head></head><body><div>Reply</div></body></html>",
RE_WHITESPACE.sub(
"",
html.extract_from_html(get_email_as_bytes("OLK_SRC_BODY_SECTION.html")),
),
)
def test_reply_separated_by_hr(self):
self.assertEqual(
"<html><head></head><body><div>Hi<div>there</div></div></body></html>",
RE_WHITESPACE.sub(
"",
html.extract_from_html(
get_email_as_bytes("reply-separated-by-hr.html")
),
),
)
def test_from_block_and_quotations_in_separate_divs(self):
msg_body = """
Reply
<div>
<hr/>
<div>
<font>
<b>From: [email protected]</b>
<b>Date: Thu, 24 Mar 2016 08:07:12 -0700</b>
</font>
</div>
<div>
Quoted message
</div>
</div>
"""
self.assertEqual(
"<html><head></head><body>Reply<div><hr></div></body></html>",
RE_WHITESPACE.sub("", html.extract_from_html(msg_body)),
)
def extract_reply_and_check(self, filename):
kwargs = {}
kwargs["encoding"] = "utf8"
with open(filename, **kwargs) as f:
msg_body = f.read()
reply = html.extract_from_html(msg_body)
plain_reply = utils.html_to_text(reply)
self.assertEqual(
RE_WHITESPACE.sub("", "Hi. I am fine.\n\nThanks,\nAlex"),
RE_WHITESPACE.sub("", plain_reply),
)
def test_CRLF(self):
"""CR is not converted to ' '"""
symbol = " "
extracted = html.extract_from_html("<html>\r\n</html>")
self.assertFalse(symbol in extracted)
self.assertEqual("<html></html>", RE_WHITESPACE.sub("", extracted))
msg_body = """My
reply
<blockquote>
<div>
On 11-Apr-2011, at 6:54 PM, Bob <[email protected]> wrote:
</div>
<div>
Test
</div>
</blockquote>"""
msg_body = msg_body.replace("\n", "\r\n")
extracted = html.extract_from_html(msg_body)
self.assertFalse(symbol in extracted)
# Keep new lines otherwise "My reply" becomes one word - "Myreply"
self.assertEqual(
"<html><head></head><body>My\nreply\n</body></html>", extracted
)
def test_gmail_forwarded_msg(self):
msg_body = (
'<div dir="ltr"><br>'
+ '<div class="gmail_quote">---------- Forwarded message ----------<br>'
+ 'From: <b class="gmail_sendername">Bob</b> <span dir="ltr">'
+ '<<a href="mailto:[email protected]">[email protected]</a>>'
+ "</span><br>Date: Fri, Feb 11, 2010 at 5:59 PM<br>"
+ "Subject: Bob WFH today<br>To: Mary <"
+ '<a href="mailto:[email protected]">'
+ '[email protected]</a>><br><br><br><div dir="ltr">eom</div>'
+ "</div><br></div>"
)
extracted = html.extract_from_html(msg_body)
self.assertEqual(
RE_WHITESPACE.sub("", msg_body), RE_WHITESPACE.sub("", extracted)
)
def test_readable_html_empty(self):
msg_body = """
<blockquote>
Reply
<div>
On 11-Apr-2011, at 6:54 PM, Bob <[email protected]> wrote:
</div>
<div>
Test
</div>
</blockquote>"""
self.assertEqual(
RE_WHITESPACE.sub("", msg_body),
RE_WHITESPACE.sub("", html.extract_from_html(msg_body)),
)
@patch.object(html, "html_document_fromstring", Mock(return_value=None))
def test_bad_html(self):
bad_html = "<html></html>"
self.assertEqual(bad_html, html.extract_from_html(bad_html))
def test_gmail_reply(self):
self.extract_reply_and_check("mails/html_replies/gmail.html")
def test_mail_ru_reply(self):
self.extract_reply_and_check("mails/html_replies/mail_ru.html")
def test_hotmail_reply(self):
self.extract_reply_and_check("mails/html_replies/hotmail.html")
def test_ms_outlook_2003_reply(self):
self.extract_reply_and_check("mails/html_replies/ms_outlook_2003.html")
def test_ms_outlook_2007_reply(self):
self.extract_reply_and_check("mails/html_replies/ms_outlook_2007.html")
def test_ms_outlook_2010_reply(self):
self.extract_reply_and_check("mails/html_replies/ms_outlook_2010.html")
def test_thunderbird_reply(self):
self.extract_reply_and_check("mails/html_replies/thunderbird.html")
def test_windows_mail_reply(self):
self.extract_reply_and_check("mails/html_replies/windows_mail.html")
def test_yandex_ru_reply(self):
self.extract_reply_and_check("mails/html_replies/yandex_ru.html")
@patch.object(constants, "MAX_LINES_COUNT", 1)
def test_too_many_lines(self):
msg_body = """Test reply
Hi
-----Original Message-----
Test"""
self.assertEqual("Test reply", text.extract_from_plain(msg_body))
def test_pattern_on_date_somebody_wrote(self):
msg_body = """Test reply
On 11-Apr-2011, at 6:54 PM, Roman Tkachenko <[email protected]> wrote:
>
> Test
>
> Roman"""
self.assertEqual("Test reply", text.extract_from_plain(msg_body))
def test_pattern_on_date_polymail(self):
msg_body = """Test reply
On Tue, Apr 11, 2017 at 10:07 PM John Smith
<
mailto:John Smith <[email protected]>
> wrote:
Test quoted data
"""
self.assertEqual("Test reply", text.extract_from_plain(msg_body))
def test_pattern_sent_from_samsung_smb_wrote(self):
msg_body = """Test reply
Sent from Samsung MobileName <[email protected]> wrote:
>
> Test
>
> Roman"""
self.assertEqual("Test reply", text.extract_from_plain(msg_body))
def test_pattern_on_date_wrote_somebody(self):
self.assertEqual(
"Lorem",
text.extract_from_plain(
"""Lorem
Op 13-02-2014 3:18 schreef Julius Caesar <[email protected]>:
Veniam laborum mlkshk kale chips authentic.
Normcore mumblecore laboris, fanny pack readymade eu blog chia pop-up
freegan enim master cleanse.
"""
),
)
def test_pattern_on_date_somebody_wrote_date_with_slashes(self):
msg_body = """Test reply
On 04/19/2011 07:10 AM, Roman Tkachenko wrote:
>
> Test.
>
> Roman"""
self.assertEqual("Test reply", text.extract_from_plain(msg_body))
def test_date_time_email_splitter(self):
msg_body = """Test reply
2014-10-17 11:28 GMT+03:00 Postmaster <
> First from site
>
"""
self.assertEqual("Test reply", text.extract_from_plain(msg_body))
def test_pattern_on_date_somebody_wrote_allows_space_in_front(self):
msg_body = """Thanks Thanmai
On Mar 8, 2012 9:59 AM, "Example.com" <
[email protected]> wrote:
>**
> Blah-blah-blah"""
self.assertEqual("Thanks Thanmai", text.extract_from_plain(msg_body))
def test_pattern_on_date_somebody_sent(self):
msg_body = """Test reply
On 11-Apr-2011, at 6:54 PM, Roman Tkachenko <[email protected]> sent:
>
> Test
>
> Roman"""
self.assertEqual("Test reply", text.extract_from_plain(msg_body))
def test_appointment(self):
msg_body = """Response
10/19/2017 @ 9:30 am for physical therapy
Bla
1517 4th Avenue Ste 300
London CA 19129, 555-421-6780
John Doe, FCLS
Mailgun Inc
555-941-0697
From: [email protected] [mailto:[email protected]]
Sent: Wednesday, October 18, 2017 2:05 PM
To: John Doer - SIU <[email protected]>
Subject: RE: Claim # 5551188-1
Text"""
expected = """Response
10/19/2017 @ 9:30 am for physical therapy
Bla
1517 4th Avenue Ste 300
London CA 19129, 555-421-6780
John Doe, FCLS
Mailgun Inc
555-941-0697"""
self.assertEqual(expected, text.extract_from_plain(msg_body))
def test_line_starts_with_on(self):
msg_body = """Blah-blah-blah
On blah-blah-blah"""
self.assertEqual(msg_body, text.extract_from_plain(msg_body))
def test_reply_and_quotation_splitter_share_line(self):
# reply lines and 'On <date> <person> wrote:' splitter pattern
# are on the same line
msg_body = """reply On Wed, Apr 4, 2012 at 3:59 PM, [email protected] wrote:
> Hi"""
self.assertEqual("reply", text.extract_from_plain(msg_body))
# test pattern '--- On <date> <person> wrote:' with reply text on
# the same line
msg_body = """reply--- On Wed, Apr 4, 2012 at 3:59 PM, [email protected] wrote:
> Hi"""
self.assertEqual("reply", text.extract_from_plain(msg_body))
# test pattern '--- On <date> <person> wrote:' with reply text containing
# '-' symbol
msg_body = """reply
bla-bla - bla--- On Wed, Apr 4, 2012 at 3:59 PM, [email protected] wrote:
> Hi"""
reply = """reply
bla-bla - bla"""
self.assertEqual(reply, text.extract_from_plain(msg_body))
def test_android_wrote(self):
msg_body = """Test reply
---- John Smith wrote ----
> quoted
> text
"""
self.assertEqual("Test reply", text.extract_from_plain(msg_body))
def test_reply_wraps_quotations(self):
msg_body = """Test reply
On 04/19/2011 07:10 AM, Roman Tkachenko wrote:
>
> Test
Regards, Roman"""
reply = """Test reply
Regards, Roman"""
self.assertEqual(reply, text.extract_from_plain(msg_body))
def test_reply_wraps_nested_quotations(self):
msg_body = """Test reply
On 04/19/2011 07:10 AM, Roman Tkachenko wrote:
>Test test
>On 04/19/2011 07:10 AM, Roman Tkachenko wrote:
>
>>
>> Test.
>>
>> Roman
Regards, Roman"""
reply = """Test reply
Regards, Roman"""
self.assertEqual(reply, text.extract_from_plain(msg_body))
def test_quotation_separator_takes_2_lines(self):
msg_body = """Test reply
On Fri, May 6, 2011 at 6:03 PM, Roman Tkachenko from Hacker News
<[email protected]> wrote:
> Test.
>
> Roman
Regards, Roman"""
reply = """Test reply
Regards, Roman"""
self.assertEqual(reply, text.extract_from_plain(msg_body))
def test_quotation_separator_takes_3_lines(self):
msg_body = """Test reply
On Nov 30, 2011, at 12:47 PM, Somebody <
wrote:
Test message
"""
self.assertEqual("Test reply", text.extract_from_plain(msg_body))
def test_short_quotation(self):
msg_body = """Hi
On 04/19/2011 07:10 AM, Roman Tkachenko wrote:
> Hello"""
self.assertEqual("Hi", text.extract_from_plain(msg_body))
def test_with_indent(self):
msg_body = """
YOLO salvia cillum kogi typewriter mumblecore cardigan skateboard Austin.
------On 12/29/1987 17:32 PM, Julius Caesar wrote-----
Brunch mumblecore pug Marfa tofu, irure taxidermy hoodie readymade pariatur.
"""
self.assertEqual(
"YOLO salvia cillum kogi typewriter mumblecore cardigan skateboard Austin.",
text.extract_from_plain(msg_body),
)
def test_short_quotation_with_newline(self):
msg_body = """Btw blah blah...
On Tue, Jan 27, 2015 at 12:42 PM -0800, "Company" <[email protected]> wrote:
Hi Mark,
Blah blah?
Thanks,Christine
On Jan 27, 2015, at 11:55 AM, Mark XXX <[email protected]> wrote:
Lorem ipsum?
Mark
Sent from Acompli"""
self.assertEqual("Btw blah blah...", text.extract_from_plain(msg_body))
def test_pattern_date_email_with_unicode(self):
msg_body = """Replying ok
2011/4/7 Nathan \xd0\xb8ova <[email protected]>
> Cool beans, scro"""
self.assertEqual("Replying ok", text.extract_from_plain(msg_body))
def test_english_from_block(self):
self.assertEqual(
"Allo! Follow up MIME!",
text.extract_from_plain(
"""Allo! Follow up MIME!
From: [email protected]
Sent: March-19-11 5:42 PM
To: Somebody
Subject: The manager has commented on your Loop
Blah-blah-blah
"""
),
)
def test_german_from_block(self):
self.assertEqual(
"Allo! Follow up MIME!",
text.extract_from_plain(
"""Allo! Follow up MIME!
Von: [email protected]
Gesendet: Dienstag, 25. November 2014 14:59
An: Somebody
Betreff: The manager has commented on your Loop
Blah-blah-blah
"""
),
)
def test_french_multiline_from_block(self):
self.assertEqual(
"Lorem ipsum",
text.extract_from_plain(
"""Lorem ipsum
De : Brendan xxx [mailto:[email protected]]
Envoyé : vendredi 23 janvier 2015 16:39
À : Camille XXX
Objet : Follow Up
Blah-blah-blah
"""
),
)
def test_french_from_block(self):
self.assertEqual(
"Lorem ipsum",
text.extract_from_plain(
"""Lorem ipsum
Le 23 janv. 2015 à 22:03, Brendan xxx
<[email protected]<mailto:[email protected]>> a écrit:
Bonjour!"""
),
)
def test_polish_from_block(self):
self.assertEqual(
"Lorem ipsum",
text.extract_from_plain(
"""Lorem ipsum
W dniu 28 stycznia 2015 01:53 użytkownik Zoe xxx <[email protected]>
napisał:
Blah!
"""
),
)
def test_danish_from_block(self):
self.assertEqual(
"Allo! Follow up MIME!",
text.extract_from_plain(
"""Allo! Follow up MIME!
Fra: [email protected]
Sendt: 19. march 2011 12:10
Til: Somebody
Emne: The manager has commented on your Loop
Blah-blah-blah
"""
),
)
def test_swedish_from_block(self):
self.assertEqual(
"Allo! Follow up MIME!",
text.extract_from_plain(
"""Allo! Follow up MIME!
Från: Anno Sportel [mailto:[email protected]]
Skickat: den 26 augusti 2015 14:45
Till: Isacson Leiff
Ämne: RE: Week 36
Blah-blah-blah
"""
),
)
def test_swedish_from_line(self):
self.assertEqual(
"Lorem",
text.extract_from_plain(
"""Lorem
Den 14 september, 2015 02:23:18, Valentino Rudy ([email protected]) skrev:
Veniam laborum mlkshk kale chips authentic.
Normcore mumblecore laboris, fanny pack
readymade eu blog chia pop-up freegan enim master cleanse.
"""
),
)
def test_norwegian_from_line(self):
self.assertEqual(
"Lorem",
text.extract_from_plain(
"""Lorem
På 14 september 2015 på 02:23:18, Valentino Rudy ([email protected]) skrev:
Veniam laborum mlkshk kale chips authentic.
Normcore mumblecore laboris, fanny pack
readymade eu blog chia pop-up freegan enim master cleanse.
"""
),
)
def test_dutch_from_block(self):
self.assertEqual(
"Gluten-free culpa lo-fi et nesciunt nostrud.",
text.extract_from_plain(
"""Gluten-free culpa lo-fi et nesciunt nostrud.
Op 17-feb.-2015, om 13:18 heeft Julius Caesar
<[email protected]> het volgende geschreven:
Small batch beard laboris tempor, non listicle hella Tumblr heirloom.
"""
),
)
def test_vietnamese_from_block(self):
self.assertEqual(
"Hello",
text.extract_from_plain(
"""Hello
Vào 14:24 8 tháng 6, 2017, Hùng Nguyễn <[email protected]> đã viết:
> Xin chào
"""
),
)
def test_quotation_marker_false_positive(self):
msg_body = """Visit us now for assistance...
>>> >>> http://www.domain.com <<<
Visit our site by clicking the link above"""
self.assertEqual(msg_body, text.extract_from_plain(msg_body))
def test_link_closed_with_quotation_marker_on_new_line(self):
msg_body = """8.45am-1pm
From: [email protected]
Date: Wed, 16 May 2012 00:15:02 -0600
<http://email.example.com/c/dHJhY2tpbmdfY29kZT1mMDdjYzBmNzM1ZjYzMGIxNT
> <[email protected] <mailto:[email protected]> >
Requester: """
self.assertEqual("8.45am-1pm", text.extract_from_plain(msg_body))
def test_link_breaks_quotation_markers_sequence(self):
# link starts and ends on the same line
msg_body = """Blah
On Thursday, October 25, 2012 at 3:03 PM, life is short. on Bob wrote:
>
> Post a response by replying to this email
>
(http://example.com/c/YzOTYzMmE) >
> life is short. (http://example.com/c/YzMmE)
>
"""
self.assertEqual("Blah", text.extract_from_plain(msg_body))
# link starts after some text on one line and ends on another
msg_body = """Blah
On Monday, 24 September, 2012 at 3:46 PM, bob wrote: