-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_sender.py
executable file
·247 lines (193 loc) · 8.72 KB
/
test_sender.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_sender
~~~~~~~~~~~
Run tests for Sender.
:copyright: (c) 2016 by Shipeng Feng.
:license: BSD, see LICENSE for more details.
"""
import sys
import unittest
from sender import Mail, Message, Attachment
from sender import SenderError
class BaseTestCase(unittest.TestCase):
"""Baseclass for all the tests that sender uses. We use this
BaseTestCase for code style consistency.
"""
def setup(self):
pass
def teardown(self):
pass
def setUp(self):
self.setup()
def tearDown(self):
unittest.TestCase.tearDown(self)
self.teardown()
def assert_equal(self, first, second):
return self.assertEqual(first, second)
def assert_true(self, expr, msg=None):
self.assertTrue(expr, msg)
def assert_false(self, expr, msg=None):
self.assertFalse(expr, msg)
def assert_raises(self, exception, callable=None, *args, **kwargs):
self.assertRaises(exception, callable, *args, **kwargs)
def assert_in(self, first, second):
self.assertIn(first, second)
def assert_not_in(self, first, second):
self.assertNotIn(first, second)
def assert_isinstance(self, obj, cls):
self.assertIsInstance(obj, cls)
if sys.version_info[:2] == (2, 6):
def assertIn(self, x, y):
assert x in y, "%r not found in %r" % (x, y)
def assertNotIn(self, x, y):
assert x not in y, "%r unexpectedly in %r" % (x, y)
def assertIsInstance(self, x, y):
assert isinstance(x, y), "not isinstance(%r, %r)" % (x, y)
class MailTestCase(BaseTestCase):
def test_global_fromaddr(self):
pass
class MessageTestCase(BaseTestCase):
def test_subject(self):
msg = Message('test')
self.assert_equal(msg.subject, 'test')
msg = Message('test', fromaddr='[email protected]', to='[email protected]')
self.assert_in(msg.subject, str(msg))
def test_to(self):
msg = Message(fromaddr='[email protected]', to='[email protected]')
self.assert_equal(msg.to, set(['[email protected]']))
self.assert_in('[email protected]', str(msg))
msg = Message(to=['[email protected]', '[email protected]'])
self.assert_equal(msg.to, set(['[email protected]',
'[email protected]']))
def test_fromaddr(self):
msg = Message(fromaddr='[email protected]', to='[email protected]')
self.assert_equal(msg.fromaddr, '[email protected]')
self.assert_in('[email protected]', str(msg))
msg = Message()
msg.fromaddr = ('From', '[email protected]')
self.assert_in('<[email protected]>', str(msg))
def test_cc(self):
msg = Message(fromaddr='[email protected]', to='[email protected]',
cc='[email protected]')
self.assert_in('[email protected]', str(msg))
def test_bcc(self):
msg = Message(fromaddr='[email protected]', to='[email protected]',
bcc='[email protected]')
self.assert_not_in('[email protected]', str(msg))
def test_reply_to(self):
msg = Message(fromaddr='[email protected]', to='[email protected]',
reply_to='[email protected]')
self.assert_equal(msg.reply_to, '[email protected]')
self.assert_in('[email protected]', str(msg))
def test_process_address(self):
msg = Message(fromaddr=('From\r\n', 'from\r\[email protected]'),
to='to\[email protected]', reply_to='reply-to\[email protected]')
self.assert_in('<[email protected]>', str(msg))
self.assert_in('[email protected]', str(msg))
self.assert_in('[email protected]', str(msg))
def test_charset(self):
msg = Message()
self.assert_equal(msg.charset, 'utf-8')
msg = Message(charset='ascii')
self.assert_equal(msg.charset, 'ascii')
def test_extra_headers(self):
msg = Message(fromaddr='[email protected]', to='[email protected]',
extra_headers={'Extra-Header-Test': 'Test'})
self.assert_in('Extra-Header-Test: Test', str(msg))
def test_mail_and_rcpt_options(self):
msg = Message()
self.assert_equal(msg.mail_options, [])
self.assert_equal(msg.rcpt_options, [])
msg = Message(mail_options=['BODY=8BITMIME'])
self.assert_equal(msg.mail_options, ['BODY=8BITMIME'])
msg = Message(rcpt_options=['NOTIFY=OK'])
self.assert_equal(msg.rcpt_options, ['NOTIFY=OK'])
def test_to_addrs(self):
msg = Message(to='[email protected]')
self.assert_equal(msg.to_addrs, set(['[email protected]']))
msg = Message(to='[email protected]', cc='[email protected]',
bcc=['[email protected]', '[email protected]'])
expected_to_addrs = set(['[email protected]', '[email protected]',
self.assert_equal(msg.to_addrs, expected_to_addrs)
msg = Message(to='[email protected]', cc='[email protected]')
self.assert_equal(msg.to_addrs, set(['[email protected]']))
def test_validate(self):
msg = Message(fromaddr='[email protected]')
self.assert_raises(SenderError, msg.validate)
msg = Message(to='[email protected]')
self.assert_raises(SenderError, msg.validate)
msg = Message(subject='subject\r', fromaddr='[email protected]',
to='[email protected]')
self.assert_raises(SenderError, msg.validate)
msg = Message(subject='subject\n', fromaddr='[email protected]',
to='[email protected]')
self.assert_raises(SenderError, msg.validate)
def test_attach(self):
msg = Message()
att = Attachment()
atts = [Attachment() for i in range(3)]
msg.attach(att)
self.assert_equal(msg.attachments, [att])
msg.attach(atts)
self.assert_equal(msg.attachments, [att] + atts)
def test_attach_attachment(self):
msg = Message()
msg.attach_attachment('test.txt', 'text/plain', 'this is test')
self.assert_equal(msg.attachments[0].filename, 'test.txt')
self.assert_equal(msg.attachments[0].content_type, 'text/plain')
self.assert_equal(msg.attachments[0].data, 'this is test')
def test_plain_text(self):
plain_text = 'Hello!\nIt works.'
msg = Message(fromaddr='[email protected]', to='[email protected]',
body=plain_text)
self.assert_equal(msg.body, plain_text)
self.assert_in('Content-Type: text/plain', str(msg))
def test_plain_text_with_attachments(self):
msg = Message(fromaddr='[email protected]', to='[email protected]',
subject='hello', body='hello world')
msg.attach_attachment(content_type='text/plain', data=b'this is test')
self.assert_in('Content-Type: multipart/mixed', str(msg))
def test_html(self):
html_text = '<b>Hello</b><br/>It works.'
msg = Message(fromaddr='[email protected]', to='[email protected]',
html=html_text)
self.assert_equal(msg.html, html_text)
self.assert_in('Content-Type: multipart/alternative', str(msg))
def test_message_id(self):
msg = Message(fromaddr='[email protected]', to='[email protected]')
self.assert_in('Message-ID: %s' % msg.message_id, str(msg))
def test_attachment_ascii_filename(self):
msg = Message(fromaddr='[email protected]', to='[email protected]')
msg.attach_attachment('my test doc.txt', 'text/plain', b'this is test')
self.assert_in('Content-Disposition: attachment; filename='
'"my test doc.txt"', str(msg))
def test_attachment_unicode_filename(self):
msg = Message(fromaddr='[email protected]', to='[email protected]')
# Chinese filename :)
msg.attach_attachment(u'我的测试文档.txt', 'text/plain',
'this is test')
self.assert_in('UTF8\'\'%E6%88%91%E7%9A%84%E6%B5%8B%E8%AF'
'%95%E6%96%87%E6%A1%A3.txt', str(msg))
class AttachmentTestCase(BaseTestCase):
def test_disposition(self):
attach = Attachment()
self.assert_equal(attach.disposition, 'attachment')
def test_headers(self):
attach = Attachment()
self.assert_equal(attach.headers, {})
class SenderTestCase(BaseTestCase):
pass
def suite():
"""A testsuite that has all the sender tests.
"""
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(MailTestCase))
suite.addTest(unittest.makeSuite(MessageTestCase))
suite.addTest(unittest.makeSuite(AttachmentTestCase))
suite.addTest(unittest.makeSuite(SenderTestCase))
return suite
if __name__ == "__main__":
unittest.main(__name__, defaultTest='suite')