forked from themartorana/python-postmark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
163 lines (132 loc) · 6.17 KB
/
Copy pathtests.py
File metadata and controls
163 lines (132 loc) · 6.17 KB
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
import sys
import unittest
from email.mime.image import MIMEImage
from io import BytesIO
if sys.version_info[0] < 3:
from urllib2 import HTTPError
else:
from urllib.error import HTTPError
import mock
from postmark import (
PMBatchMail, PMMail, PMMailInactiveRecipientException,
PMMailUnprocessableEntityException, PMMailServerErrorException
)
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)
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)
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):
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 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')
inline_image = MIMEImage(b'inline_image_file', 'png', name='inline_image.png')
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'),
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(
subject='Subject', text_body='Body', api_key='test'
),
PMMail(
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(
subject='Subject', text_body='Body', api_key='test'
),
PMMail(
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(
subject='Subject', text_body='Body', api_key='test'
),
PMMail(
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)
if __name__ == '__main__':
if not settings.configured:
settings.configure(
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
}
},
INSTALLED_APPS=[
],
MIDDLEWARE_CLASSES=[],
)
unittest.main()