forked from themartorana/python-postmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
214 lines (174 loc) · 8.35 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
import sys
import unittest
from email.mime.image import MIMEImage
from io import BytesIO
if sys.version_info[0] < 3:
from StringIO import StringIO
from urllib2 import HTTPError
else:
from io import StringIO
from urllib.error import HTTPError
import mock
from postmark import (
PMBatchMail, PMMail, PMMailInactiveRecipientException,
PMMailUnprocessableEntityException, PMMailServerErrorException,
PMMailMissingValueException, PMBounceManager
)
from django.conf import settings
class PMMailTests(unittest.TestCase):
def test_406_error_inactive_recipient(self):
json_payload = BytesIO()
json_payload.write(b'{"Message": "", "ErrorCode": 406}')
json_payload.seek(0)
message = PMMail(sender='[email protected]', to='[email protected]',
subject='Subject', text_body='Body', api_key='test')
with mock.patch('postmark.core.urlopen', side_effect=HTTPError('',
422, '', {}, json_payload)):
self.assertRaises(PMMailInactiveRecipientException, message.send)
def test_422_error_unprocessable_entity(self):
json_payload = BytesIO()
json_payload.write(b'{"Message": "", "ErrorCode": 422}')
json_payload.seek(0)
message = PMMail(sender='[email protected]', to='[email protected]',
subject='Subject', text_body='Body', api_key='test')
with mock.patch('postmark.core.urlopen', side_effect=HTTPError('',
422, '', {}, json_payload)):
self.assertRaises(PMMailUnprocessableEntityException, message.send)
def test_500_error_server_error(self):
message = PMMail(sender='[email protected]', to='[email protected]',
subject='Subject', text_body='Body', api_key='test')
with mock.patch('postmark.core.urlopen', side_effect=HTTPError('',
500, '', {}, None)):
self.assertRaises(PMMailServerErrorException, message.send)
def assert_missing_value_exception(self, message_func, error_message):
with self.assertRaises(PMMailMissingValueException) as cm:
message_func()
self.assertEqual(error_message, cm.exception.parameter)
def test_send(self):
# Confirm send() still works as before use_template was added
message = PMMail(sender='[email protected]', to='[email protected]',
subject='Subject', text_body='Body', api_key='test')
with mock.patch('postmark.core.urlopen', side_effect=HTTPError('',
200, '', {}, None)):
message.send()
def test_missing_subject(self):
# No subject should raise exception when using send()
message = PMMail(sender='[email protected]', to='[email protected]',
text_body='Body', api_key='test')
self.assert_missing_value_exception(
message.send,
'Cannot send an e-mail without a subject'
)
def test_check_values_bad_template_data(self):
# Try sending with template ID only
message = PMMail(api_key='test', sender='[email protected]', to='[email protected]', template_id=1)
self.assert_missing_value_exception(
message.send,
'Cannot send a template e-mail without a both template_id and template_model set'
)
def test_send_with_template(self):
# Both template_id and template_model are set, so send should work.
message = PMMail(api_key='test', sender='[email protected]', to='[email protected]',
template_id=1, template_model={'junk': 'more junk'})
with mock.patch('postmark.core.urlopen', side_effect=HTTPError('',
200, '', {}, None)):
message.send()
def test_inline_attachments(self):
image = MIMEImage(b'image_file', 'png', name='image.png')
image_with_id = MIMEImage(b'inline_image_file', 'png', name='image_with_id.png')
image_with_id.add_header('Content-ID', '<[email protected]>')
inline_image = MIMEImage(b'inline_image_file', 'png', name='inline_image.png')
inline_image.add_header('Content-ID', '<[email protected]>')
inline_image.add_header('Content-Disposition', 'inline', filename='inline_image.png')
expected = [
{'Name': 'TextFile', 'Content': 'content', 'ContentType': 'text/plain'},
{'Name': 'InlineImage', 'Content': 'image_content', 'ContentType': 'image/png', 'ContentID': 'cid:[email protected]'},
{'Name': 'image.png', 'Content': 'aW1hZ2VfZmlsZQ==', 'ContentType': 'image/png'},
{'Name': 'image_with_id.png', 'Content': 'aW5saW5lX2ltYWdlX2ZpbGU=', 'ContentType': 'image/png', 'ContentID': '[email protected]'},
{'Name': 'inline_image.png', 'Content': 'aW5saW5lX2ltYWdlX2ZpbGU=', 'ContentType': 'image/png', 'ContentID': 'cid:[email protected]'},
]
json_message = PMMail(
sender='[email protected]', to='[email protected]', subject='Subject', text_body='Body', api_key='test',
attachments=[
('TextFile', 'content', 'text/plain'),
('InlineImage', 'image_content', 'image/png', 'cid:[email protected]'),
image,
image_with_id,
inline_image,
]
).to_json_message()
assert len(json_message['Attachments']) == len(expected)
for orig, attachment in zip(expected, json_message['Attachments']):
for k, v in orig.items():
assert orig[k] == attachment[k].rstrip()
class PMBatchMailTests(unittest.TestCase):
def test_406_error_inactive_recipient(self):
messages = [
PMMail(
sender='[email protected]', to='[email protected]',
subject='Subject', text_body='Body', api_key='test'
),
PMMail(
sender='[email protected]', to='[email protected]',
subject='Subject', text_body='Body', api_key='test'
),
]
json_payload = BytesIO()
json_payload.write(b'{"Message": "", "ErrorCode": 406}')
json_payload.seek(0)
batch = PMBatchMail(messages=messages, api_key='test')
with mock.patch('postmark.core.urlopen', side_effect=HTTPError('',
422, '', {}, json_payload)):
self.assertRaises(PMMailInactiveRecipientException, batch.send)
def test_422_error_unprocessable_entity(self):
messages = [
PMMail(
sender='[email protected]', to='[email protected]',
subject='Subject', text_body='Body', api_key='test'
),
PMMail(
sender='[email protected]', to='[email protected]',
subject='Subject', text_body='Body', api_key='test'
),
]
json_payload = BytesIO()
json_payload.write(b'{"Message": "", "ErrorCode": 422}')
json_payload.seek(0)
batch = PMBatchMail(messages=messages, api_key='test')
with mock.patch('postmark.core.urlopen', side_effect=HTTPError('',
422, '', {}, json_payload)):
self.assertRaises(PMMailUnprocessableEntityException, batch.send)
def test_500_error_server_error(self):
messages = [
PMMail(
sender='[email protected]', to='[email protected]',
subject='Subject', text_body='Body', api_key='test'
),
PMMail(
sender='[email protected]', to='[email protected]',
subject='Subject', text_body='Body', api_key='test'
),
]
batch = PMBatchMail(messages=messages, api_key='test')
with mock.patch('postmark.core.urlopen', side_effect=HTTPError('',
500, '', {}, None)):
self.assertRaises(PMMailServerErrorException, batch.send)
class PMBounceManagerTests(unittest.TestCase):
def test_activate(self):
bounce = PMBounceManager(api_key='test')
with mock.patch('postmark.core.HTTPConnection.getresponse') as mock_response:
mock_response.return_value = StringIO('{"test": "test"}')
self.assertEquals(bounce.activate(1), {'test': 'test'})
if __name__ == '__main__':
if not settings.configured:
settings.configure(
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
}
},
INSTALLED_APPS=[
],
MIDDLEWARE_CLASSES=[],
)
unittest.main()