Debian Codegraph @

test/tornado/test_tornado.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 import base64
2 import json
3 import os
4 import tempfile
5
6 import pytest
7 import six
8
9 from sparkpost.tornado import SparkPost, SparkPostAPIException
10 from tornado import ioloop
11 from .utils import AsyncClientMock
12
13 responses = AsyncClientMock()
14
15
16 @responses.activate
17 def test_success_send():
18 responses.add(
19 responses.POST,
20 'https://api.sparkpost.com/api/v1/transmissions',
21 status=200,
22 content_type='application/json',
23 body='{"results": "yay"}'
24 )
25 sp = SparkPost('fake-key')
26 results = ioloop.IOLoop().run_sync(sp.transmission.send)
27 assert results == 'yay'
28
29
30 @responses.activate
31 def test_success_send_with_attachments():
32 try:
33 # Let's compare unicode for Python 2 / 3 compatibility
34 test_content = six.u("Hello \nWorld\n")
35 (_, temp_file_path) = tempfile.mkstemp()
36 with open(temp_file_path, "w") as temp_file:
37 temp_file.write(test_content)
38
39 responses.add(
40 responses.POST,
41 'https://api.sparkpost.com/api/v1/transmissions',
42 status=200,
43 content_type='application/json',
44 body='{"results": "yay"}'
45 )
46 sp = SparkPost('fake-key')
47
48 attachment = {
49 "name": "test.txt",
50 "type": "text/plain",
51 "filename": temp_file_path
52 }
53
54 def send():
55 return sp.transmission.send(attachments=[attachment])
56 results = ioloop.IOLoop().run_sync(send)
57
58 request_params = json.loads(responses.calls[0].request.body)
59 content = base64.b64decode(
60 request_params["content"]["attachments"][0]["data"])
61 # Let's compare unicode for Python 2 / 3 compatibility
62 assert test_content == content.decode("ascii")
63
64 assert results == 'yay'
65
66 attachment = {
67 "name": "test.txt",
68 "type": "text/plain",
69 "data": base64.b64encode(
70 test_content.encode("ascii")).decode("ascii")
71 }
72
73 def send():
74 return sp.transmission.send(attachments=[attachment])
75 results = ioloop.IOLoop().run_sync(send)
76
77 request_params = json.loads(responses.calls[1].request.body)
78 content = base64.b64decode(
79 request_params["content"]["attachments"][0]["data"])
80 # Let's compare unicode for Python 2 / 3 compatibility
81 assert test_content == content.decode("ascii")
82
83 assert results == 'yay'
84 finally:
85 os.unlink(temp_file_path)
86
87
88 @responses.activate
89 def test_fail_send():
90 responses.add(
91 responses.POST,
92 'https://api.sparkpost.com/api/v1/transmissions',
93 status=500,
94 content_type='application/json',
95 body="""
96 {"errors": [{"message": "You failed", "description": "More Info"}]}
97 """
98 )
99 with pytest.raises(SparkPostAPIException):
100 sp = SparkPost('fake-key')
101 ioloop.IOLoop().run_sync(sp.transmission.send)
102
103
104 @responses.activate
105 def test_success_get():
106 responses.add(
107 responses.GET,
108 'https://api.sparkpost.com/api/v1/transmissions/foobar',
109 status=200,
110 content_type='application/json',
111 body='{"results": {"transmission": {}}}'
112 )
113 sp = SparkPost('fake-key')
114
115 def send():
116 return sp.transmission.get('foobar')
117 results = ioloop.IOLoop().run_sync(send, timeout=3)
118 assert results == {}
119
120
121 @responses.activate
122 def test_fail_get():
123 responses.add(
124 responses.GET,
125 'https://api.sparkpost.com/api/v1/transmissions/foobar',
126 status=404,
127 content_type='application/json',
128 body="""
129 {"errors": [{"message": "cant find", "description": "where you go"}]}
130 """
131 )
132 with pytest.raises(SparkPostAPIException):
133 sp = SparkPost('fake-key')
134
135 def send():
136 return sp.transmission.get('foobar')
137 ioloop.IOLoop().run_sync(send, timeout=3)
138
139
140 @responses.activate
141 def test_nocontent_get():
142 responses.add(
143 responses.GET,
144 'https://api.sparkpost.com/api/v1/transmissions',
145 status=204,
146 content_type='application/json',
147 body=''
148 )
149 sp = SparkPost('fake-key')
150 response = ioloop.IOLoop().run_sync(sp.transmission.list)
151 assert response is True
152
153
154 @responses.activate
155 def test_brokenjson_get():
156 responses.add(
157 responses.GET,
158 'https://api.sparkpost.com/api/v1/transmissions',
159 status=200,
160 content_type='application/json',
161 body='{"results":'
162 )
163 with pytest.raises(SparkPostAPIException):
164 sp = SparkPost('fake-key')
165 ioloop.IOLoop().run_sync(sp.transmission.list)
166
167
168 @responses.activate
169 def test_noresults_get():
170 responses.add(
171 responses.GET,
172 'https://api.sparkpost.com/api/v1/transmissions',
173 status=200,
174 content_type='application/json',
175 body='{"ok": false}'
176 )
177 sp = SparkPost('fake-key')
178 response = ioloop.IOLoop().run_sync(sp.transmission.list)
179 assert response == {"ok": False}
180
181
182 @responses.activate
183 def test_success_list():
184 responses.add(
185 responses.GET,
186 'https://api.sparkpost.com/api/v1/transmissions',
187 status=200,
188 content_type='application/json',
189 body='{"results": []}'
190 )
191 sp = SparkPost('fake-key')
192 response = ioloop.IOLoop().run_sync(sp.transmission.list)
193 assert response == []