Debian Codegraph @

test/tornado/test_tornado.py

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