-
Notifications
You must be signed in to change notification settings - Fork 62
/
test_addresses.py
243 lines (183 loc) · 7.78 KB
/
test_addresses.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
# encoding: utf-8
"""Test the TurboMail Message class."""
from __future__ import unicode_literals
import pytest
from marrow.mailer.address import Address, AddressList, AutoConverter
from marrow.util.compat import bytes, unicode
class TestAddress(object):
def test_punycode(self):
addr = Address('Foo', 'foo@exámple.test')
assert bytes(addr) == b'Foo <[email protected]>'
def test_bytestring(self):
addr = Address('Foo <foo@exámple.test>'.encode('utf-8'))
assert bytes(addr) == b'Foo <[email protected]>'
def test_address_from_addresslist(self):
email = '[email protected]'
addr = Address(AddressList([Address(email)]))
assert unicode(addr) == email
def test_address_from_addresslist_limit_0(self):
with pytest.raises(ValueError):
Address(AddressList())
def test_address_from_addresslist_limit_2(self):
email = '[email protected]'
with pytest.raises(ValueError):
Address(AddressList([Address(email), Address(email)]))
def test_initialization_with_tuple(self):
name = 'Foo'
emailaddress = '[email protected]'
address = Address((name, emailaddress))
assert '%s <%s>' % (name, emailaddress) == unicode(address)
def test_initialization_with_string(self):
emailaddress = '[email protected]'
address = Address(emailaddress)
assert unicode(address) == emailaddress
def test_initialization_with_named_string(self):
emailaddress = 'My Name <[email protected]>'
address = Address(emailaddress)
assert unicode(address) == emailaddress
def test_invalid_initialization(self):
with pytest.raises(TypeError):
Address(123)
def test_compare_address(self):
addr1 = Address('[email protected]')
addr2 = Address(' [email protected] ')
assert addr1 == addr2
def test_compare_unicode(self):
addr = Address('[email protected]')
assert addr == '[email protected]'
def test_compare_bytestring(self):
addr = Address('[email protected]')
assert addr == b'[email protected]'
def test_compare_tuple(self):
addr = Address('foo', '[email protected]')
assert addr == ('foo', '[email protected]')
def test_compare_othertype(self):
addr = Address('[email protected]')
with pytest.raises(NotImplementedError):
addr != 123
def test_len(self):
addr = Address('[email protected]')
assert len(addr), len('[email protected]')
def test_repr(self):
addr = Address('[email protected]')
assert repr(addr) == 'Address("[email protected]")'
def test_validation_truncates_at_second_at_character(self):
# This is basically due to Python's parseaddr behavior.
assert 'bad@user' == Address('bad@[email protected]')
def test_validation_rejects_addresses_without_at(self):
# TODO: This may be actually a valid input - some mail systems allow to
# use only the local part which will be qualified by the MTA
with pytest.raises(ValueError):
Address('baduser.example.com')
def test_validation_accepts_uncommon_local_parts(self):
Address('[email protected]')
# This address caused 100% CPU load for 50s in Python's (2.5.2) re
# module on Fedora Linux 10 (AMD x2 4200).
Address('[email protected]')
Address('customer/[email protected]')
Address('[email protected] ')
Address('!def!xyz%[email protected] ')
Address('[email protected]')
Address('!$&*-=^`|~#%\'+/?_{}@example.com')
def test_revalidation(self):
addr = Address('[email protected]')
assert addr.valid == True
# TODO: Later
# def test_validation_accepts_quoted_local_parts(self):
# Address('"Fred Bloggs"@example.com ')
# Address('"Joe\\Blow"@example.com ')
# Address('"Abc@def"@example.com ')
# Address('"Abc\@def"@example.com')
def test_validation_accepts_multilevel_domains(self):
Address('[email protected]')
Address('[email protected]')
Address('[email protected]')
def test_validation_accepts_domain_without_tld(self):
assert Address('user@company') == 'user@company'
def test_validation_rejects_local_parts_starting_or_ending_with_dot(self):
with pytest.raises(ValueError):
Address('[email protected]')
with pytest.raises(ValueError):
Address('[email protected]')
def test_validation_rejects_double_dot(self):
with pytest.raises(ValueError):
Address('[email protected]')
# TODO: Later
# def test_validation_rejects_special_characters_if_not_quoted(self):
# for char in '()[]\;:,<>':
# localpart = 'foo%sbar' % char
# self.assertRaises(ValueError, Address, '%[email protected]' % localpart)
# Address("%s"@example.com' % localpart)
# TODO: Later
# def test_validation_accepts_ip_address_literals(self):
# Address('jsmith@[192.168.2.1]')
class TestAddressList(object):
"""Test the AddressList helper class."""
addresses = AutoConverter('_addresses', AddressList)
@classmethod
def setup_class(cls):
cls._addresses = AddressList()
def setup_method(self, method):
self.addresses = AddressList()
def teardown_method(self, method):
del self.addresses
def test_assignment(self):
self.addresses = []
self.addresses = ['[email protected]']
assert self.addresses == ['[email protected]']
def test_assign_single_address(self):
address = '[email protected]'
self.addresses = address
assert [address] == self.addresses
assert unicode(self.addresses) == address
def test_assign_list_of_addresses(self):
addresses = ['[email protected]', '[email protected]']
self.addresses = addresses
assert ', '.join(addresses) == unicode(self.addresses)
assert addresses == self.addresses
def test_assign_list_of_named_addresses(self):
addresses = [('Test User 1', '[email protected]'), ('Test User 2', '[email protected]')]
self.addresses = addresses
string_addresses = [unicode(Address(*value)) for value in addresses]
assert ', '.join(string_addresses) == unicode(self.addresses)
assert string_addresses == self.addresses
def test_assign_item(self):
self.addresses.append('[email protected]')
assert self.addresses[0] == '[email protected]'
self.addresses[0] = '[email protected]'
assert isinstance(self.addresses[0], Address)
assert self.addresses[0] == '[email protected]'
def test_assign_slice(self):
self.addresses[:] = ('[email protected]', '[email protected]')
assert isinstance(self.addresses[0], Address)
assert isinstance(self.addresses[1], Address)
def test_init_accepts_string_list(self):
addresses = '[email protected], [email protected]'
self.addresses = addresses
assert addresses == unicode(self.addresses)
def test_init_accepts_tuple(self):
addresses = AddressList(('foo', '[email protected]'))
assert addresses == [('foo', '[email protected]')]
def test_bytes(self):
self.addresses = [('User1', 'foo@exámple.test'), ('User2', 'foo@exámple.test')]
assert bytes(self.addresses) == b'User1 <[email protected]>, User2 <[email protected]>'
def test_repr(self):
assert repr(self.addresses) == 'AddressList()'
self.addresses = ['[email protected]', '[email protected]']
assert repr(self.addresses) == 'AddressList("[email protected], [email protected]")'
def test_invalid_init(self):
with pytest.raises(ValueError):
AddressList(2)
def test_addresses(self):
self.addresses = [('Test User 1', '[email protected]'), ('Test User 2', '[email protected]')]
assert self.addresses.addresses == AddressList('[email protected], [email protected]')
def test_validation_strips_multiline_addresses(self):
self.addresses = '[email protected]'
evil_input = '\n'.join(evil_lines)
self.addresses.append(evil_input)
assert self.addresses == ['[email protected]', evil_lines[0]]
def test_return_addresses_as_strings(self):
self.addresses = 'foo@exámple.test'
encoded_address = '[email protected]'
assert self.addresses.string_addresses == [encoded_address]