forked from pallets-eco/flask-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
622 lines (515 loc) · 24.7 KB
/
tests.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
# -*- coding: utf-8 -*-
from __future__ import with_statement
import base64
import email
import unittest
import time
import re
import mock
from email.header import Header
from email import charset
from flask import Flask
from flask_mail import Mail, Message, BadHeaderError, sanitize_address
from speaklater import make_lazy_string
class TestCase(unittest.TestCase):
TESTING = True
MAIL_DEFAULT_SENDER = "support@mysite.com"
def setUp(self):
self.app = Flask(__name__)
self.app.config.from_object(self)
self.assertTrue(self.app.testing)
self.mail = Mail(self.app)
self.ctx = self.app.test_request_context()
self.ctx.push()
def tearDown(self):
self.ctx.pop()
def assertIn(self, member, container, msg=None):
if hasattr(unittest.TestCase, 'assertIn'):
return unittest.TestCase.assertIn(self, member, container, msg)
return self.assertTrue(member in container)
def assertNotIn(self, member, container, msg=None):
if hasattr(unittest.TestCase, 'assertNotIn'):
return unittest.TestCase.assertNotIn(self, member, container, msg)
return self.assertFalse(member in container)
def assertIsNone(self, obj, msg=None):
if hasattr(unittest.TestCase, 'assertIsNone'):
return unittest.TestCase.assertIsNone(self, obj, msg)
return self.assertTrue(obj is None)
def assertIsNotNone(self, obj, msg=None):
if hasattr(unittest.TestCase, 'assertIsNotNone'):
return unittest.TestCase.assertIsNotNone(self, obj, msg)
return self.assertTrue(obj is not None)
class TestInitialization(TestCase):
def test_init_mail(self):
mail = self.mail.init_mail(
self.app.config,
self.app.debug,
self.app.testing
)
self.assertEquals(self.mail.state.__dict__, mail.__dict__)
class TestMessage(TestCase):
def test_initialize(self):
msg = Message(subject="subject",
recipients=["to@example.com"])
self.assertEqual(msg.sender, self.app.extensions['mail'].default_sender)
self.assertEqual(msg.recipients, ["to@example.com"])
def test_recipients_properly_initialized(self):
msg = Message(subject="subject")
self.assertEqual(msg.recipients, [])
msg2 = Message(subject="subject")
msg2.add_recipient("somebody@here.com")
self.assertEqual(len(msg2.recipients), 1)
def test_esmtp_options_properly_initialized(self):
msg = Message(subject="subject")
self.assertEqual(msg.mail_options, [])
self.assertEqual(msg.rcpt_options, [])
msg = Message(subject="subject", mail_options=['BODY=8BITMIME'])
self.assertEqual(msg.mail_options, ['BODY=8BITMIME'])
msg2 = Message(subject="subject", rcpt_options=['NOTIFY=SUCCESS'])
self.assertEqual(msg2.rcpt_options, ['NOTIFY=SUCCESS'])
def test_sendto_properly_set(self):
msg = Message(subject="subject", recipients=["somebody@here.com"],
cc=["cc@example.com"], bcc=["bcc@example.com"])
self.assertEqual(len(msg.send_to), 3)
msg.add_recipient("cc@example.com")
self.assertEqual(len(msg.send_to), 3)
def test_add_recipient(self):
msg = Message("testing")
msg.add_recipient("to@example.com")
self.assertEqual(msg.recipients, ["to@example.com"])
def test_sender_as_tuple(self):
msg = Message(subject="testing",
sender=("tester", "tester@example.com"))
self.assertEqual('tester <tester@example.com>', msg.sender)
def test_default_sender_as_tuple(self):
self.app.extensions['mail'].default_sender = ('tester', 'tester@example.com')
msg = Message(subject="testing")
self.assertEqual('tester <tester@example.com>', msg.sender)
def test_reply_to(self):
msg = Message(subject="testing",
recipients=["to@example.com"],
sender="spammer <spammer@example.com>",
reply_to="somebody <somebody@example.com>",
body="testing")
response = msg.as_string()
h = Header("Reply-To: %s" % sanitize_address('somebody <somebody@example.com>'))
self.assertIn(h.encode(), str(response))
def test_send_without_sender(self):
self.app.extensions['mail'].default_sender = None
msg = Message(subject="testing", recipients=["to@example.com"], body="testing")
self.assertRaises(AssertionError, self.mail.send, msg)
def test_send_without_recipients(self):
msg = Message(subject="testing",
recipients=[],
body="testing")
self.assertRaises(AssertionError, self.mail.send, msg)
def test_bcc(self):
msg = Message(sender="from@example.com",
subject="testing",
recipients=["to@example.com"],
body="testing",
bcc=["tosomeoneelse@example.com"])
response = msg.as_string()
self.assertNotIn("tosomeoneelse@example.com", str(response))
def test_cc(self):
msg = Message(sender="from@example.com",
subject="testing",
recipients=["to@example.com"],
body="testing",
cc=["tosomeoneelse@example.com"])
response = msg.as_string()
self.assertIn("Cc: tosomeoneelse@example.com", str(response))
def test_attach(self):
msg = Message(subject="testing",
recipients=["to@example.com"],
body="testing")
msg.attach(data=b"this is a test",
content_type="text/plain")
a = msg.attachments[0]
self.assertIsNone(a.filename)
self.assertEqual(a.disposition, 'attachment')
self.assertEqual(a.content_type, "text/plain")
self.assertEqual(a.data, b"this is a test")
def test_bad_header_subject(self):
msg = Message(subject="testing\r\n",
sender="from@example.com",
body="testing",
recipients=["to@example.com"])
self.assertRaises(BadHeaderError, self.mail.send, msg)
def test_multiline_subject(self):
msg = Message(subject="testing\r\n testing\r\n testing \r\n \ttesting",
sender="from@example.com",
body="testing",
recipients=["to@example.com"])
self.mail.send(msg)
response = msg.as_string()
self.assertIn("From: from@example.com", str(response))
self.assertIn("testing\r\n testing\r\n testing \r\n \ttesting", str(response))
def test_bad_multiline_subject(self):
msg = Message(subject="testing\r\n testing\r\n ",
sender="from@example.com",
body="testing",
recipients=["to@example.com"])
self.assertRaises(BadHeaderError, self.mail.send, msg)
msg = Message(subject="testing\r\n testing\r\n\t",
sender="from@example.com",
body="testing",
recipients=["to@example.com"])
self.assertRaises(BadHeaderError, self.mail.send, msg)
msg = Message(subject="testing\r\n testing\r\n\n",
sender="from@example.com",
body="testing",
recipients=["to@example.com"])
self.assertRaises(BadHeaderError, self.mail.send, msg)
def test_bad_header_sender(self):
msg = Message(subject="testing",
sender="from@example.com\r\n",
recipients=["to@example.com"],
body="testing")
self.assertIn('From: from@example.com', msg.as_string())
def test_bad_header_reply_to(self):
msg = Message(subject="testing",
sender="from@example.com",
reply_to="evil@example.com\r",
recipients=["to@example.com"],
body="testing")
self.assertIn('From: from@example.com', msg.as_string())
self.assertIn('To: to@example.com', msg.as_string())
self.assertIn('Reply-To: evil@example.com', msg.as_string())
def test_bad_header_recipient(self):
msg = Message(subject="testing",
sender="from@example.com",
recipients=[
"to@example.com",
"to\r\n@example.com"],
body="testing")
self.assertIn('To: to@example.com', msg.as_string())
def test_emails_are_sanitized(self):
msg = Message(subject="testing",
sender="sender\r\n@example.com",
reply_to="reply_to\r\n@example.com",
recipients=["recipient\r\n@example.com"])
self.assertIn('sender@example.com', msg.as_string())
self.assertIn('reply_to@example.com', msg.as_string())
self.assertIn('recipient@example.com', msg.as_string())
def test_plain_message(self):
plain_text = "Hello Joe,\nHow are you?"
msg = Message(sender="from@example.com",
subject="subject",
recipients=["to@example.com"],
body=plain_text)
self.assertEqual(plain_text, msg.body)
self.assertIn('Content-Type: text/plain', msg.as_string())
def test_message_str(self):
msg = Message(sender="from@example.com",
subject="subject",
recipients=["to@example.com"],
body="some plain text")
self.assertEqual(msg.as_string(), str(msg))
def test_plain_message_with_attachments(self):
msg = Message(sender="from@example.com",
subject="subject",
recipients=["to@example.com"],
body="hello")
msg.attach(data=b"this is a test",
content_type="text/plain")
self.assertIn('Content-Type: multipart/mixed', msg.as_string())
def test_plain_message_with_ascii_attachment(self):
msg = Message(subject="subject",
recipients=["to@example.com"],
body="hello")
msg.attach(data=b"this is a test",
content_type="text/plain",
filename='test doc.txt')
self.assertIn('Content-Disposition: attachment;filename=test doc.txt', msg.as_string())
def test_plain_message_with_unicode_attachment(self):
msg = Message(subject="subject",
recipients=["to@example.com"],
body="hello")
msg.attach(data=b"this is a test",
content_type="text/plain",
filename=u'ünicöde ←→ ✓.txt')
parsed = email.message_from_string(msg.as_string())
self.assertIn(re.sub(r'\s+', ' ', parsed.get_payload()[1].get('Content-Disposition')), [
'attachment; filename*="UTF8\'\'%C3%BCnic%C3%B6de%20%E2%86%90%E2%86%92%20%E2%9C%93.txt"',
'attachment; filename*=UTF8\'\'%C3%BCnic%C3%B6de%20%E2%86%90%E2%86%92%20%E2%9C%93.txt'
])
def test_html_message(self):
html_text = "<p>Hello World</p>"
msg = Message(sender="from@example.com",
subject="subject",
recipients=["to@example.com"],
html=html_text)
self.assertEqual(html_text, msg.html)
self.assertIn('Content-Type: multipart/alternative', msg.as_string())
def test_html_message_with_attachments(self):
html_text = "<p>Hello World</p>"
plain_text = 'Hello World'
msg = Message(sender="from@example.com",
subject="subject",
recipients=["to@example.com"],
body=plain_text,
html=html_text)
msg.attach(data=b"this is a test",
content_type="text/plain")
self.assertEqual(html_text, msg.html)
self.assertIn('Content-Type: multipart/alternative', msg.as_string())
parsed = email.message_from_string(msg.as_string())
self.assertEqual(len(parsed.get_payload()), 2)
body, attachment = parsed.get_payload()
self.assertEqual(len(body.get_payload()), 2)
plain, html = body.get_payload()
self.assertEqual(plain.get_payload(), plain_text)
self.assertEqual(html.get_payload(), html_text)
self.assertEqual(base64.b64decode(attachment.get_payload()), b'this is a test')
def test_date_header(self):
before = time.time()
msg = Message(sender="from@example.com",
subject="subject",
recipients=["to@example.com"],
body="hello",
date=time.time())
after = time.time()
self.assertTrue(before <= msg.date <= after)
dateFormatted = email.utils.formatdate(msg.date, localtime=True)
self.assertIn('Date: ' + dateFormatted, msg.as_string())
def test_msgid_header(self):
msg = Message(sender="from@example.com",
subject="subject",
recipients=["to@example.com"],
body="hello")
# see RFC 5322 section 3.6.4. for the exact format specification
r = re.compile(r"<\S+@\S+>").match(msg.msgId)
self.assertIsNotNone(r)
self.assertIn('Message-ID: ' + msg.msgId, msg.as_string())
def test_unicode_sender_tuple(self):
msg = Message(subject="subject",
sender=(u"ÄÜÖ → ✓", 'from@example.com>'),
recipients=["to@example.com"])
self.assertIn('From: =?utf-8?b?w4TDnMOWIOKGkiDinJM=?= <from@example.com>', msg.as_string())
def test_unicode_sender(self):
msg = Message(subject="subject",
sender=u'ÄÜÖ → ✓ <from@example.com>>',
recipients=["to@example.com"])
self.assertIn('From: =?utf-8?b?w4TDnMOWIOKGkiDinJM=?= <from@example.com>', msg.as_string())
def test_unicode_headers(self):
msg = Message(subject="subject",
sender=u'ÄÜÖ → ✓ <from@example.com>',
recipients=[u"Ä <t1@example.com>", u"Ü <t2@example.com>"],
cc=[u"Ö <cc@example.com>"])
response = msg.as_string()
a1 = sanitize_address(u"Ä <t1@example.com>")
a2 = sanitize_address(u"Ü <t2@example.com>")
h1_a = Header("To: %s, %s" % (a1, a2))
h1_b = Header("To: %s, %s" % (a2, a1))
h2 = Header("From: %s" % sanitize_address(u"ÄÜÖ → ✓ <from@example.com>"))
h3 = Header("Cc: %s" % sanitize_address(u"Ö <cc@example.com>"))
# Ugly, but there's no guaranteed order of the recipieints in the header
try:
self.assertIn(h1_a.encode(), response)
except AssertionError:
self.assertIn(h1_b.encode(), response)
self.assertIn(h2.encode(), response)
self.assertIn(h3.encode(), response)
def test_unicode_subject(self):
msg = Message(subject=make_lazy_string(lambda a: a, u"sübject"),
sender='from@example.com',
recipients=["to@example.com"])
self.assertIn('=?utf-8?q?s=C3=BCbject?=', msg.as_string())
def test_extra_headers(self):
msg = Message(sender="from@example.com",
subject="subject",
recipients=["to@example.com"],
body="hello",
extra_headers={'X-Extra-Header': 'Yes'})
self.assertIn('X-Extra-Header: Yes', msg.as_string())
def test_message_charset(self):
msg = Message(sender="from@example.com",
subject="subject",
recipients=["foo@bar.com"],
charset='us-ascii')
# ascii body
msg.body = "normal ascii text"
self.assertIn('Content-Type: text/plain; charset="us-ascii"', msg.as_string())
# ascii html
msg = Message(sender="from@example.com",
subject="subject",
recipients=["foo@bar.com"],
charset='us-ascii')
msg.body = None
msg.html = "<html><h1>hello</h1></html>"
self.assertIn('Content-Type: text/html; charset="us-ascii"', msg.as_string())
# unicode body
msg = Message(sender="from@example.com",
subject="subject",
recipients=["foo@bar.com"])
msg.body = u"ünicöde ←→ ✓"
self.assertIn('Content-Type: text/plain; charset="utf-8"', msg.as_string())
# unicode body and unicode html
msg = Message(sender="from@example.com",
subject="subject",
recipients=["foo@bar.com"])
msg.html = u"ünicöde ←→ ✓"
self.assertIn('Content-Type: text/plain; charset="utf-8"', msg.as_string())
self.assertIn('Content-Type: text/html; charset="utf-8"', msg.as_string())
# unicode body and attachments
msg = Message(sender="from@example.com",
subject="subject",
recipients=["foo@bar.com"])
msg.html = None
msg.attach(data=b"foobar", content_type='text/csv')
self.assertIn('Content-Type: text/plain; charset="utf-8"', msg.as_string())
# unicode sender as tuple
msg = Message(sender=(u"送信者", "from@example.com"),
subject=u"表題",
recipients=["foo@bar.com"],
reply_to=u"返信先 <somebody@example.com>",
charset='shift_jis') # japanese
msg.body = u'内容'
self.assertIn('From: =?iso-2022-jp?', msg.as_string())
self.assertNotIn('From: =?utf-8?', msg.as_string())
self.assertIn('Subject: =?iso-2022-jp?', msg.as_string())
self.assertNotIn('Subject: =?utf-8?', msg.as_string())
self.assertIn('Reply-To: =?iso-2022-jp?', msg.as_string())
self.assertNotIn('Reply-To: =?utf-8?', msg.as_string())
self.assertIn('Content-Type: text/plain; charset="iso-2022-jp"', msg.as_string())
# unicode subject sjis
msg = Message(sender="from@example.com",
subject=u"表題",
recipients=["foo@bar.com"],
charset='shift_jis') # japanese
msg.body = u'内容'
self.assertIn('Subject: =?iso-2022-jp?', msg.as_string())
self.assertIn('Content-Type: text/plain; charset="iso-2022-jp"', msg.as_string())
# unicode subject utf-8
msg = Message(sender="from@example.com",
subject="subject",
recipients=["foo@bar.com"],
charset='utf-8')
msg.body = u'内容'
self.assertIn('Subject: subject', msg.as_string())
self.assertIn('Content-Type: text/plain; charset="utf-8"', msg.as_string())
# ascii subject
msg = Message(sender="from@example.com",
subject="subject",
recipients=["foo@bar.com"],
charset='us-ascii')
msg.body = "normal ascii text"
self.assertNotIn('Subject: =?us-ascii?', msg.as_string())
self.assertIn('Content-Type: text/plain; charset="us-ascii"', msg.as_string())
# default charset
msg = Message(sender="from@example.com",
subject="subject",
recipients=["foo@bar.com"])
msg.body = "normal ascii text"
self.assertNotIn('Subject: =?', msg.as_string())
self.assertIn('Content-Type: text/plain; charset="utf-8"', msg.as_string())
def test_empty_subject_header(self):
msg = Message(sender="from@example.com",
recipients=["foo@bar.com"])
msg.body = "normal ascii text"
self.mail.send(msg)
self.assertNotIn('Subject:', msg.as_string())
class TestMail(TestCase):
def test_send(self):
with self.mail.record_messages() as outbox:
msg = Message(subject="testing",
recipients=["tester@example.com"],
body="test")
self.mail.send(msg)
self.assertIsNotNone(msg.date)
self.assertEqual(len(outbox), 1)
sent_msg = outbox[0]
self.assertEqual(msg.sender, self.app.extensions['mail'].default_sender)
def test_send_message(self):
with self.mail.record_messages() as outbox:
self.mail.send_message(subject="testing",
recipients=["tester@example.com"],
body="test")
self.assertEqual(len(outbox), 1)
msg = outbox[0]
self.assertEqual(msg.subject, "testing")
self.assertEqual(msg.recipients, ["tester@example.com"])
self.assertEqual(msg.body, "test")
self.assertEqual(msg.sender, self.app.extensions['mail'].default_sender)
class TestConnection(TestCase):
def test_send_message(self):
with self.mail.record_messages() as outbox:
with self.mail.connect() as conn:
conn.send_message(subject="testing",
recipients=["to@example.com"],
body="testing")
self.assertEqual(len(outbox), 1)
sent_msg = outbox[0]
self.assertEqual(sent_msg.sender, self.app.extensions['mail'].default_sender)
def test_send_single(self):
with self.mail.record_messages() as outbox:
with self.mail.connect() as conn:
msg = Message(subject="testing",
recipients=["to@example.com"],
body="testing")
conn.send(msg)
self.assertEqual(len(outbox), 1)
sent_msg = outbox[0]
self.assertEqual(sent_msg.subject, "testing")
self.assertEqual(sent_msg.recipients, ["to@example.com"])
self.assertEqual(sent_msg.body, "testing")
self.assertEqual(sent_msg.sender, self.app.extensions['mail'].default_sender)
def test_send_many(self):
with self.mail.record_messages() as outbox:
with self.mail.connect() as conn:
for i in range(100):
msg = Message(subject="testing",
recipients=["to@example.com"],
body="testing")
conn.send(msg)
self.assertEqual(len(outbox), 100)
sent_msg = outbox[0]
self.assertEqual(sent_msg.sender, self.app.extensions['mail'].default_sender)
def test_send_without_sender(self):
self.app.extensions['mail'].default_sender = None
msg = Message(subject="testing", recipients=["to@example.com"], body="testing")
with self.mail.connect() as conn:
self.assertRaises(AssertionError, conn.send, msg)
def test_send_without_recipients(self):
msg = Message(subject="testing",
recipients=[],
body="testing")
with self.mail.connect() as conn:
self.assertRaises(AssertionError, conn.send, msg)
def test_bad_header_subject(self):
msg = Message(subject="testing\n\r",
body="testing",
recipients=["to@example.com"])
with self.mail.connect() as conn:
self.assertRaises(BadHeaderError, conn.send, msg)
def test_sendmail_with_ascii_recipient(self):
with self.mail.connect() as conn:
with mock.patch.object(conn, 'host') as host:
msg = Message(subject="testing",
sender="from@example.com",
recipients=["to@example.com"],
body="testing")
conn.send(msg)
host.sendmail.assert_called_once_with(
"from@example.com",
["to@example.com"],
msg.as_string(),
msg.mail_options,
msg.rcpt_options
)
def test_sendmail_with_non_ascii_recipient(self):
with self.mail.connect() as conn:
with mock.patch.object(conn, 'host') as host:
msg = Message(subject="testing",
sender="from@example.com",
recipients=[u'ÄÜÖ → ✓ <to@example.com>'],
body="testing")
conn.send(msg)
host.sendmail.assert_called_once_with(
"from@example.com",
["=?utf-8?b?w4TDnMOWIOKGkiDinJM=?= <to@example.com>"],
msg.as_string(),
msg.mail_options,
msg.rcpt_options
)