forked from sendgrid/sendgrid-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
142 lines (121 loc) · 4.72 KB
/
__init__.py
File metadata and controls
142 lines (121 loc) · 4.72 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
import os
import unittest2 as unittest
import json
import sys
try:
from StringIO import StringIO
except ImportError: # Python 3
from io import StringIO
from sendgrid import SendGridClient, Mail
from sendgrid.exceptions import SendGridClientError, SendGridServerError
from sendgrid.sendgrid import HTTPError
SG_USER, SG_PWD = os.getenv('SG_USER'), os.getenv('SG_PWD')
class TestSendGrid(unittest.TestCase):
def setUp(self):
self.sg = SendGridClient(SG_USER, SG_PWD)
@unittest.skipUnless(sys.version_info < (3, 0), 'only for python2')
def test_unicode_recipients(self):
recipients = [unicode('test@test.com'), unicode('guy@man.com')]
m = Mail(to=recipients,
subject='testing',
html='awesome',
from_email='from@test.com')
mock = {'to[]': ['test@test.com', 'guy@man.com']}
result = self.sg._build_body(m)
self.assertEqual(result['to[]'], mock['to[]'])
def test_send(self):
m = Mail()
m.add_to('John, Doe <john@email.com>')
m.set_subject('test')
m.set_html('WIN')
m.set_text('WIN')
m.set_from('doe@email.com')
m.add_substitution('subKey', 'subValue')
m.add_section('testSection', 'sectionValue')
m.add_category('testCategory')
m.add_unique_arg('testUnique', 'uniqueValue')
m.add_filter('testFilter', 'filter', 'filterValue')
m.add_attachment_stream('testFile', 'fileValue')
url = self.sg._build_body(m)
url.pop('api_key', None)
url.pop('api_user', None)
url.pop('date', None)
test_url = json.loads('''
{
"to[]": ["john@email.com"],
"toname[]": ["John Doe"],
"html": "WIN",
"text": "WIN",
"subject": "test",
"files[testFile]": "fileValue",
"from": "doe@email.com"
}
''')
test_url['x-smtpapi'] = json.dumps(json.loads('''
{
"to" : ["John, Doe <john@email.com>"],
"sub": {
"subKey": ["subValue"]
},
"section": {
"testSection":"sectionValue"
},
"category": ["testCategory"],
"unique_args": {
"testUnique":"uniqueValue"
},
"filters": {
"testFilter": {
"settings": {
"filter": "filterValue"
}
}
}
}
'''))
self.assertEqual(url, test_url)
@unittest.skipUnless(sys.version_info < (3, 0), 'only for python2')
def test__build_body_unicode(self):
"""test _build_body() handles encoded unicode outside ascii range"""
from_email = '\xd0\x9d\xd0\xb8\xd0\xba\xd0\xb0@email.com'
from_name = '\xd0\x9a\xd0\xbb\xd0\xb0\xd0\xb2\xd0\xb4\xd0\xb8\xd1\x8f'
subject = '\xd0\x9d\xd0\xb0\xd0\xb4\xd0\xb5\xd0\xb6\xd0\xb4\xd0\xb0'
text = '\xd0\x9d\xd0\xb0\xd0\xb4\xd0\xb5\xd0\xb6\xd0\xb4\xd0\xb0'
html = '\xd0\x9d\xd0\xb0\xd0\xb4\xd0\xb5\xd0\xb6\xd0\xb4\xd0\xb0'
m = Mail()
m.add_to('John, Doe <john@email.com>')
m.set_subject(subject)
m.set_html(html)
m.set_text(text)
m.set_from("%s <%s>" % (from_name, from_email))
url = self.sg._build_body(m)
self.assertEqual(from_email, url['from'])
self.assertEqual(from_name, url['fromname'])
self.assertEqual(subject, url['subject'])
self.assertEqual(text, url['text'])
self.assertEqual(html, url['html'])
def test_drop_to_header(self):
m = Mail()
m.add_to('John, Doe <john@email.com>')
m.set_from('doe@email.com')
m.set_subject('test')
m.set_text('test')
m.add_bcc('John, Doe <john@email.com>')
url = self.sg._build_body(m)
print url
class SendGridClientUnderTest(SendGridClient):
def _make_request(self, message):
raise self.error
class TestSendGridErrorHandling(unittest.TestCase):
def setUp(self):
self.sg = SendGridClientUnderTest(SG_USER, SG_PWD, raise_errors=True)
def test_client_raises_clinet_error_in_case_of_4xx(self):
self.sg.error = HTTPError('url', 403, 'msg', {}, StringIO('body'))
with self.assertRaises(SendGridClientError):
self.sg.send(Mail())
def test_client_raises_clinet_error_in_case_of_5xx(self):
self.sg.error = HTTPError('url', 503, 'msg', {}, StringIO('body'))
with self.assertRaises(SendGridServerError):
self.sg.send(Mail())
if __name__ == '__main__':
unittest.main()