/
tests.py
952 lines (708 loc) · 31.2 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
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
# -*- coding: utf-8 -*-
# BBB Python 2 vs 3 compat
from __future__ import unicode_literals
import unittest
import os
from io import StringIO
from pyramid import testing
here = os.path.dirname(os.path.abspath(__file__))
class TestAttachment(unittest.TestCase):
def test_data_from_string(self):
from pyramid_mailer.message import Attachment
a = Attachment(data="foo")
self.assert_(a.data == "foo")
def test_data_from_file_obj(self):
from pyramid_mailer.message import Attachment
a = Attachment(data=StringIO("foo"))
self.assert_(a.data == "foo")
class TestMessage(unittest.TestCase):
def test_initialize(self):
from pyramid_mailer.message import Message
msg = Message(subject="subject",
sender="support@mysite.com",
recipients=["to@example.com"])
self.assert_(msg.subject == "subject")
self.assert_(msg.sender == "support@mysite.com")
self.assert_(msg.recipients == ["to@example.com"])
def test_recipients_properly_initialized(self):
from pyramid_mailer.message import Message
msg = Message(subject="subject")
self.assert_(msg.recipients == [])
msg2 = Message(subject="subject")
msg2.add_recipient("somebody@here.com")
self.assert_(len(msg.recipients) == 0)
msg3 = Message(subject="subject")
msg3.add_recipient("somebody@here.com")
self.assert_(len(msg.recipients) == 0)
def test_add_recipient(self):
from pyramid_mailer.message import Message
msg = Message("testing")
msg.add_recipient("to@example.com")
self.assert_(msg.recipients == ["to@example.com"])
def test_add_cc(self):
from pyramid_mailer.message import Message
msg = Message("testing")
msg.add_cc("to@example.com")
self.assert_(msg.cc == ["to@example.com"])
def test_add_bcc(self):
from pyramid_mailer.message import Message
msg = Message("testing")
msg.add_bcc("to@example.com")
self.assert_(msg.bcc == ["to@example.com"])
def test_send_without_sender(self):
from pyramid_mailer.message import Message
from pyramid_mailer.mailer import Mailer
from pyramid_mailer.exceptions import InvalidMessage
msg = Message(subject="testing",
recipients=["to@example.com"],
body="testing")
mailer = Mailer()
self.assertRaises(InvalidMessage, mailer.send, msg)
def test_send_without_recipients(self):
from pyramid_mailer.message import Message
from pyramid_mailer.mailer import Mailer
from pyramid_mailer.exceptions import InvalidMessage
mailer = Mailer()
msg = Message(subject="testing",
recipients=[],
body="testing")
self.assertRaises(InvalidMessage, mailer.send, msg)
def test_send_without_body(self):
from pyramid_mailer.message import Message
from pyramid_mailer.mailer import Mailer
from pyramid_mailer.exceptions import InvalidMessage
msg = Message(subject="testing",
sender="sender@example.com",
recipients=["to@example.com"])
mailer = Mailer()
self.assertRaises(InvalidMessage, mailer.send, msg)
msg.html = "<b>test</b>"
mailer.send(msg)
def test_cc(self):
from pyramid_mailer.message import Message
msg = Message(subject="testing",
sender="sender@example.com",
recipients=["to@example.com"],
body="testing",
cc=["tosomeoneelse@example.com"])
response = msg.get_response()
self.assert_("Cc: tosomeoneelse@example.com" in str(response))
def test_attach(self):
from pyramid_mailer.message import Message
from pyramid_mailer.message import Attachment
msg = Message(subject="testing",
recipients=["to@example.com"],
body="testing")
msg.attach(Attachment(data="this is a test",
content_type="text/plain"))
a = msg.attachments[0]
self.assert_(a.filename is None)
self.assert_(a.disposition == 'attachment')
self.assert_(a.content_type == "text/plain")
self.assert_(a.data == "this is a test")
response = msg.get_response()
self.assert_(len(response.attachments) == 1)
def test_bad_header_subject(self):
from pyramid_mailer.message import Message
from pyramid_mailer.mailer import Mailer
from pyramid_mailer.exceptions import BadHeaders
msg = Message(subject="testing\n\r",
sender="from@example.com",
body="testing",
recipients=["to@example.com"])
mailer = Mailer()
self.assertRaises(BadHeaders, mailer.send, msg)
def test_bad_header_sender(self):
from pyramid_mailer.message import Message
from pyramid_mailer.mailer import Mailer
from pyramid_mailer.exceptions import BadHeaders
mailer = Mailer()
msg = Message(subject="testing",
sender="from@example.com\n\r",
recipients=["to@example.com"],
body="testing")
self.assertRaises(BadHeaders, mailer.send, msg)
def test_bad_header_recipient(self):
from pyramid_mailer.message import Message
from pyramid_mailer.mailer import Mailer
from pyramid_mailer.exceptions import BadHeaders
mailer = Mailer()
msg = Message(subject="testing",
sender="from@example.com",
recipients=[
"to@example.com",
"to\r\n@example.com"],
body="testing")
self.assertRaises(BadHeaders, mailer.send, msg)
def test_send_to(self):
from pyramid_mailer.message import Message
msg = Message(subject="testing",
sender="from@example.com",
recipients=[
"to@example.com"],
cc=['somebodyelse@example.com',
'to@example.com'],
bcc=['anotherperson@example.com'],
body="testing")
self.assert_(msg.send_to == set(["to@example.com",
"somebodyelse@example.com",
"anotherperson@example.com"]))
def test_is_bad_headers_if_no_bad_headers(self):
from pyramid_mailer.message import Message
msg = Message(subject="testing",
sender="from@example.com",
body="testing",
recipients=["to@example.com"])
self.assert_(not(msg.is_bad_headers()))
def test_is_bad_headers_if_subject_empty(self):
from pyramid_mailer.message import Message
msg = Message(sender="from@example.com",
body="testing",
recipients=["to@example.com"])
self.assert_(not(msg.is_bad_headers()))
def test_is_bad_headers_if_bad_headers(self):
from pyramid_mailer.message import Message
msg = Message(subject="testing\n\r",
sender="from@\nexample.com",
body="testing",
recipients=["to@example.com"])
self.assert_(msg.is_bad_headers())
class TestMailer(unittest.TestCase):
def test_dummy_send_immediately(self):
from pyramid_mailer.mailer import DummyMailer
from pyramid_mailer.message import Message
mailer = DummyMailer()
msg = Message(subject="testing",
sender="sender@example.com",
recipients=["tester@example.com"],
body="test")
mailer.send_immediately(msg)
self.assert_(len(mailer.outbox)) == 1
def test_dummy_send_immediately_and_fail_silently(self):
from pyramid_mailer.mailer import DummyMailer
from pyramid_mailer.message import Message
mailer = DummyMailer()
msg = Message(subject="testing",
sender="sender@example.com",
recipients=["tester@example.com"],
body="test")
mailer.send_immediately(msg, True)
self.assert_(len(mailer.outbox)) == 1
def test_dummy_send(self):
from pyramid_mailer.mailer import DummyMailer
from pyramid_mailer.message import Message
mailer = DummyMailer()
msg = Message(subject="testing",
sender="sender@example.com",
recipients=["tester@example.com"],
body="test")
mailer.send(msg)
self.assert_(len(mailer.outbox)) == 1
def test_dummy_send_to_queue(self):
from pyramid_mailer.mailer import DummyMailer
from pyramid_mailer.message import Message
mailer = DummyMailer()
msg = Message(subject="testing",
sender="sender@example.com",
recipients=["tester@example.com"],
body="test")
mailer.send_to_queue(msg)
self.assert_(len(mailer.queue)) == 1
def test_send_immediately(self):
import socket
from pyramid_mailer.mailer import Mailer
from pyramid_mailer.message import Message
mailer = Mailer(host='localhost', port='28322')
msg = Message(subject="testing",
sender="sender@example.com",
recipients=["tester@example.com"],
body="test")
self.assertRaises(socket.error,
mailer.send_immediately,
msg)
def test_send_immediately_and_fail_silently(self):
from pyramid_mailer.mailer import Mailer
from pyramid_mailer.message import Message
mailer = Mailer()
msg = Message(subject="testing",
sender="sender@example.com",
recipients=["tester@example.com"],
body="test")
mailer.send_immediately(msg, True)
def test_send(self):
from pyramid_mailer.mailer import Mailer
from pyramid_mailer.message import Message
mailer = Mailer()
msg = Message(subject="testing",
sender="sender@example.com",
recipients=["tester@example.com"],
body="test")
mailer.send(msg)
def test_send_to_queue_unconfigured(self):
from pyramid_mailer.mailer import Mailer
from pyramid_mailer.message import Message
msg = Message(subject="testing",
sender="sender@example.com",
recipients=["tester@example.com"],
body="test")
mailer = Mailer()
self.assertRaises(RuntimeError, mailer.send_to_queue, msg)
def test_send_to_queue(self):
import os
import tempfile
from pyramid_mailer.mailer import Mailer
from pyramid_mailer.message import Message
test_queue = os.path.join(tempfile.gettempdir(), 'test_queue')
for dir in ('cur', 'new', 'tmp'):
try:
os.makedirs(os.path.join(test_queue, dir))
except OSError:
pass
mailer = Mailer(queue_path=test_queue)
msg = Message(subject="testing",
sender="sender@example.com",
recipients=["tester@example.com"],
body="test")
mailer.send_to_queue(msg)
def test_use_ssl_mailer(self):
try:
from smtplib import SMTP_SSL
ssl_enabled = True
except ImportError: # pragma: no cover
from smtplib import SMTP
ssl_enabled = False
from pyramid_mailer.mailer import Mailer
mailer = Mailer(ssl=True)
if ssl_enabled:
self.assert_(mailer.direct_delivery.mailer.smtp == SMTP_SSL)
from ssl import SSLError
try:
self.assert_(mailer.direct_delivery.mailer.smtp_factory())
except (IOError, SSLError):
pass
else: # pragma: no cover
self.assert_(mailer.direct_delivery.mailer.smtp == SMTP)
import socket
try:
self.assert_(mailer.direct_delivery.mailer.smtp_factory())
except socket.error as e:
# smtp mailer might fail to resolve hostname
self.assert_(e.args[0] == 61)
def test_from_settings_factory(self):
try:
from smtplib import SMTP_SSL
ssl_enabled = True
except ImportError: # pragma: no cover
from smtplib import SMTP
ssl_enabled = False
from pyramid_mailer import mailer_factory_from_settings
settings = {'mymail.host' : 'my.server.com',
'mymail.port' : 123,
'mymail.username' : 'tester',
'mymail.password' : 'test',
'mymail.tls' : True,
'mymail.ssl' : True,
'mymail.keyfile' : 'ssl.key',
'mymail.certfile' : 'ssl.crt',
'mymail.queue_path' : '/tmp',
'mymail.debug' : 1}
mailer = mailer_factory_from_settings(settings, prefix='mymail.')
self.assert_(mailer.direct_delivery.mailer.hostname=='my.server.com')
self.assert_(mailer.direct_delivery.mailer.port==123)
self.assert_(mailer.direct_delivery.mailer.username=='tester')
self.assert_(mailer.direct_delivery.mailer.password=='test')
self.assert_(mailer.direct_delivery.mailer.force_tls==True)
if ssl_enabled:
self.assert_(mailer.direct_delivery.mailer.smtp == SMTP_SSL)
else: # pragma: no cover
self.assert_(mailer.direct_delivery.mailer.smtp == SMTP)
self.assert_(mailer.direct_delivery.mailer.keyfile == 'ssl.key')
self.assert_(mailer.direct_delivery.mailer.certfile == 'ssl.crt')
self.assert_(mailer.queue_delivery.queuePath == '/tmp')
self.assert_(mailer.direct_delivery.mailer.debug_smtp == 1)
def test_from_settings(self):
try:
from smtplib import SMTP_SSL
ssl_enabled = True
except ImportError: # pragma: no cover
from smtplib import SMTP
ssl_enabled = False
from pyramid_mailer.mailer import Mailer
settings = {'mymail.host' : 'my.server.com',
'mymail.port' : 123,
'mymail.username' : 'tester',
'mymail.password' : 'test',
'mymail.tls' : True,
'mymail.ssl' : True,
'mymail.keyfile' : 'ssl.key',
'mymail.certfile' : 'ssl.crt',
'mymail.queue_path' : '/tmp',
'mymail.debug' : 1}
mailer = Mailer.from_settings(settings, prefix='mymail.')
self.assert_(mailer.direct_delivery.mailer.hostname=='my.server.com')
self.assert_(mailer.direct_delivery.mailer.port==123)
self.assert_(mailer.direct_delivery.mailer.username=='tester')
self.assert_(mailer.direct_delivery.mailer.password=='test')
self.assert_(mailer.direct_delivery.mailer.force_tls==True)
if ssl_enabled:
self.assert_(mailer.direct_delivery.mailer.smtp == SMTP_SSL)
else: # pragma: no cover
self.assert_(mailer.direct_delivery.mailer.smtp == SMTP)
self.assert_(mailer.direct_delivery.mailer.keyfile == 'ssl.key')
self.assert_(mailer.direct_delivery.mailer.certfile == 'ssl.crt')
self.assert_(mailer.queue_delivery.queuePath == '/tmp')
self.assert_(mailer.direct_delivery.mailer.debug_smtp == 1)
class TestGetMailer(unittest.TestCase):
def _get_mailer(self, arg):
from pyramid_mailer import get_mailer
return get_mailer(arg)
def test_arg_is_registry(self):
mailer = object()
registry = DummyRegistry(mailer)
result = self._get_mailer(registry)
self.assertEqual(result, mailer)
def test_arg_is_request(self):
class Dummy(object):
pass
mailer = object()
registry = DummyRegistry(mailer)
request = Dummy()
request.registry = registry
result = self._get_mailer(request)
self.assertEqual(result, mailer)
class Test_includeme(unittest.TestCase):
def _do_includeme(self, config):
from pyramid_mailer import includeme
includeme(config)
def test_with_default_prefix(self):
from pyramid_mailer.interfaces import IMailer
registry = DummyRegistry()
settings = {'mail.default_sender':'sender'}
config = DummyConfig(registry, settings)
self._do_includeme(config)
self.assertEqual(registry.registered[IMailer].default_sender, 'sender')
def test_with_specified_prefix(self):
from pyramid_mailer.interfaces import IMailer
registry = DummyRegistry()
settings = {'pyramid_mailer.prefix':'foo.',
'foo.default_sender':'sender'}
config = DummyConfig(registry, settings)
self._do_includeme(config)
self.assertEqual(registry.registered[IMailer].default_sender, 'sender')
class TestIncludemeTesting(unittest.TestCase):
def test_includeme(self):
from pyramid_mailer.interfaces import IMailer
from pyramid_mailer.mailer import DummyMailer
from pyramid_mailer.testing import includeme
registry = DummyRegistry()
config = DummyConfig(registry, {})
includeme(config)
self.assertEqual(registry.registered[IMailer].__class__, DummyMailer)
class TestFunctional(unittest.TestCase):
def setUp(self):
self.config = testing.setUp()
def tearDown(self):
testing.tearDown()
def test_get_mailer_realthing(self):
from pyramid_mailer import get_mailer
from pyramid_mailer.mailer import Mailer
self.config.include('pyramid_mailer')
request = testing.DummyRequest()
mailer = get_mailer(request)
self.assertEqual(mailer.__class__, Mailer)
def test_get_mailer_dummy(self):
from pyramid_mailer import get_mailer
from pyramid_mailer.testing import DummyMailer
self.config.include('pyramid_mailer.testing')
request = testing.DummyRequest()
mailer = get_mailer(request)
self.assertEqual(mailer.__class__, DummyMailer)
class DummyConfig(object):
def __init__(self, registry, settings):
self.registry = registry
self.registry.settings = settings
class DummyRegistry(object):
def __init__(self, result=None):
self.result = result
self.registered = {}
def getUtility(self, iface):
return self.result
def registerUtility(self, impl, iface):
self.registered[iface] = impl
class TestEncodingError(unittest.TestCase):
def _makeOne(self):
from pyramid_mailer.response import EncodingError
return EncodingError()
def test_it(self):
inst = self._makeOne()
self.assertTrue(isinstance(inst, Exception))
class Test_normalize_header(unittest.TestCase):
def _callFUT(self, header):
from pyramid_mailer.response import normalize_header
return normalize_header(header)
def test_it(self):
result = self._callFUT('content-type')
self.assertEqual(result, 'Content-Type')
class TestMailBase(unittest.TestCase):
def _makeOne(self, items=()):
from pyramid_mailer.response import MailBase
return MailBase(items)
def test___getitem__hit(self):
base = self._makeOne([('Content-Type', 'text/html')])
self.assertEqual(base['content-type'], 'text/html')
def test___getitem__miss(self):
base = self._makeOne([('Content-Type', 'text/html')])
self.assertEqual(base['Wrong'], None)
def test___iter__(self):
base = self._makeOne([('Content-Type', 'text/html')])
self.assertEqual(list(base), ['Content-Type'])
def test___contains__(self):
base = self._makeOne([('Content-Type', 'text/html')])
self.assertTrue('content-type' in base)
def test___delitem__(self):
base = self._makeOne([('Content-Type', 'text/html')])
del base['content-type']
self.assertFalse(base.headers)
def test___nonzero__false(self):
base = self._makeOne()
self.assertFalse(base)
def test___nonzero__true_body(self):
base = self._makeOne()
base.body = 'body'
self.assertTrue(base)
def test___nonzero__true_headers(self):
base = self._makeOne([('Content-Type', 'text/html')])
self.assertTrue(base)
def test___nonzero__true_parts(self):
base = self._makeOne()
base.parts = [True]
self.assertTrue(base)
def test_keys(self):
base = self._makeOne([('Content-Type', 'text/html'),
('Content-Disposition', 'inline')])
self.assertEqual(base.keys(), ['Content-Disposition', 'Content-Type'])
def test_attach_file(self):
base = self._makeOne()
base.attach_file('filename', 'data', 'ctype', 'inline')
self.assertEqual(len(base.parts), 1)
part = base.parts[0]
self.assertEqual(part.content_encoding['Content-Type'],
('ctype', {'name':'filename'}))
self.assertEqual(part.content_encoding['Content-Disposition'],
('inline', {'filename':'filename'}))
self.assertEqual(part.body, 'data')
def test_attach_text(self):
base = self._makeOne()
base.attach_text('data', 'ctype')
self.assertEqual(len(base.parts), 1)
part = base.parts[0]
self.assertEqual(part.content_encoding['Content-Type'], ('ctype', {}))
self.assertEqual(part.body, 'data')
def test_walk(self):
base1 = self._makeOne()
base2 = self._makeOne()
base3 = self._makeOne()
base1.parts = [base2]
base2.parts = [base3]
self.assertEqual(list(base1.walk()), [base2, base3])
class TestMailResponse(unittest.TestCase):
def _makeOne(self, **kw):
from pyramid_mailer.response import MailResponse
return MailResponse(**kw)
def test_ctor(self):
response = self._makeOne(To='To', From='From', Subject='Subject',
Body='Body', Html='Html')
self.assertEqual(response.Body, 'Body')
self.assertEqual(response.Html, 'Html')
self.assertEqual(response.base.headers['To'], 'To')
self.assertEqual(response.base.headers['From'], 'From')
self.assertEqual(response.base.headers['Subject'], 'Subject')
self.assertEqual(response.multipart, 'Html')
self.assertEqual(response.attachments, [])
def test___contains__(self):
response = self._makeOne(To='To')
self.assertTrue('To' in response)
def test___getitem__(self):
response = self._makeOne(To='To')
self.assertEqual(response['To'], 'To')
def test___setitem__(self):
response = self._makeOne(To='To')
response['To'] = 'To2'
self.assertEqual(response.base['To'], 'To2')
def test___delitem__(self):
response = self._makeOne(To='To')
del response['To']
self.assertFalse('To' in response.base)
def test_attach(self):
import os
this = os.path.abspath(__file__)
response = self._makeOne()
response.attach(filename=this, content_type='content_type',
data='data', disposition='disposition')
self.assertEqual(len(response.attachments), 1)
attachment = response.attachments[0]
self.assertEqual(attachment['filename'], this)
self.assertEqual(attachment['content_type'], 'content_type')
self.assertEqual(attachment['data'], 'data')
self.assertEqual(attachment['disposition'], 'disposition')
def test_attach_no_content_type(self):
import os
this = os.path.abspath(__file__)
response = self._makeOne()
response.attach(filename=this, data='data', disposition='disposition')
self.assertEqual(len(response.attachments), 1)
attachment = response.attachments[0]
self.assertEqual(attachment['filename'], this)
self.assertTrue('python' in attachment['content_type'])
self.assertEqual(attachment['data'], 'data')
self.assertEqual(attachment['disposition'], 'disposition')
def test_attach_part(self):
response = self._makeOne()
response.attach_part('part')
self.assertEqual(len(response.attachments), 1)
attachment = response.attachments[0]
self.assertEqual(attachment['filename'], None)
self.assertEqual(attachment['content_type'], None)
self.assertEqual(attachment['data'], None)
self.assertEqual(attachment['disposition'], None)
self.assertEqual(attachment['part'], 'part')
def test_attach_all_parts(self):
response = self._makeOne()
request = DummyMailRequest()
response.attach_all_parts(request)
self.assertEqual(len(response.attachments), 1)
attachment = response.attachments[0]
self.assertEqual(attachment['part'], request)
self.assertEqual(response.base.content_encoding, {})
def test_clear(self):
response = self._makeOne()
response.attachments = [True]
response.base.parts = [True]
response.clear()
self.assertEqual(response.attachments, [])
self.assertEqual(response.base.parts, [])
self.assertEqual(response.multipart, False)
def test_update(self):
response = self._makeOne()
response.update({'a':'1'})
self.assertEqual(response.base['a'], '1')
def test___str__(self):
response = self._makeOne(To='To', From='From', Subject='Subject',
Body='Body', Html='Html')
s = str(response)
self.assertTrue('Content-Type' in s)
def test_to_message(self):
from pyramid_mailer.response import MIMEPart
response = self._makeOne(To='To', From='From', Subject='Subject',
Body='Body', Html='Html')
message = response.to_message()
self.assertEqual(message.__class__, MIMEPart)
def test_to_message_multiple_to_recipients(self):
response = self._makeOne(
To=['chrism@plope.com', 'billg@microsoft.com'],
From='From', Subject='Subject',
Body='Body', Html='Html')
message = response.to_message()
self.assertEqual(str(message['To']),
'chrism@plope.com, billg@microsoft.com')
def test_to_message_multipart(self):
from pyramid_mailer.response import MIMEPart
response = self._makeOne(To='To', From='From', Subject='Subject',
Body='Body', Html='Html')
import os
this = os.path.abspath(__file__)
response = self._makeOne()
response.attach(filename=this, content_type='text/html',
data='data', disposition='disposition')
message = response.to_message()
self.assertEqual(message.__class__, MIMEPart)
def test_to_message_with_parts(self):
from pyramid_mailer.response import MIMEPart
part = DummyPart()
response = self._makeOne(To='To', From='From', Subject='Subject')
response.multipart = True
response.attachments = [{'part':part}]
message = response.to_message()
self.assertEqual(message.__class__, MIMEPart)
def test_to_message_with_parts2(self):
this = os.path.join(here, 'tests.py')
from pyramid_mailer.response import MIMEPart
response = self._makeOne(To='To', From='From', Subject='Subject')
response.multipart = True
response.attachments = [{'filename':this, 'content_type':'text/python'}]
message = response.to_message()
self.assertEqual(message.__class__, MIMEPart)
def test_to_message_with_parts3(self):
from pyramid_mailer.response import MIMEPart
response = self._makeOne(To='To', From='From', Subject='Subject')
response.multipart = True
response.attachments = [{'data':'data', 'content_type':'text/html'}]
message = response.to_message()
self.assertEqual(message.__class__, MIMEPart)
def test_to_message_with_parts4(self):
from pyramid_mailer.response import MIMEPart
response = self._makeOne(To='To', From='From', Subject='Subject')
response.multipart = True
response.base.content_encoding['Content-Type'] = ('text/html', None)
response.attachments = [{'data':'data', 'content_type':'text/html'}]
message = response.to_message()
self.assertEqual(message.__class__, MIMEPart)
def test_to_message_with_parts5(self):
from pyramid_mailer.response import MIMEPart
response = self._makeOne(To='To', From='From', Subject='Subject')
response.multipart = True
data = os.urandom(100)
response.attachments = [{'data': data,
'content_type':'application/octet-stream'}]
message = response.to_message()
self.assertEqual(message.__class__, MIMEPart)
def test_all_parts(self):
response = self._makeOne()
self.assertEqual(response.all_parts(), [])
def test_keys(self):
response = self._makeOne()
self.assertEqual(response.keys(), ['From', 'Subject', 'To'])
class Test_to_message(unittest.TestCase):
def _callFUT(self, mail):
from pyramid_mailer.response import to_message
return to_message(mail)
def _makeBase(self, items=()):
from pyramid_mailer.response import MailBase
return MailBase(items)
def test_no_ctype(self):
from pyramid_mailer.response import MIMEPart
mail = self._makeBase()
result = self._callFUT(mail)
self.assertEqual(result.__class__, MIMEPart)
def test_no_ctype_no_parts(self):
from pyramid_mailer.response import MIMEPart
mail = self._makeBase()
mail.parts = []
result = self._callFUT(mail)
self.assertEqual(result.__class__, MIMEPart)
class TestMIMEPart(unittest.TestCase):
def _makeOne(self, type, **params):
from pyramid_mailer.response import MIMEPart
return MIMEPart(type, **params)
def test___repr__(self):
part = self._makeOne('text/html')
result = repr(part)
self.assertEqual(
result,
"<MIMEPart 'html/text': 'text/html', None, multipart=False>")
class Dummy(object):
pass
class DummyMailRequest(object):
def __init__(self):
self.base = Dummy()
self.base.content_encoding = {}
def all_parts(self):
return [self]
class DummyPart(object):
def __init__(self):
self.content_encoding = {'Content-Type':('text/html', {}),
'Content-Disposition':('inline', {})}
self.parts = []
self.body = 'body'
def keys(self):
return []