/
tests.py
258 lines (213 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# -*- coding: utf-8 -*-
"""
tests
~~~~~
Flask Exceptional extension unit testing.
:copyright: (c) 2011 by Jonathan Zempel.
:license: BSD, see LICENSE for more details.
"""
from __future__ import with_statement
from flask import abort, Flask, g, json
from flask.ext.exceptional import Exceptional
from functools import wraps
from os import environ
from sys import exc_info
from werkzeug.debug.tbtools import Traceback
import unittest
class ExceptionalTestCase(unittest.TestCase):
"""Exceptional extension test cases.
"""
debug_url = environ.get("EXCEPTIONAL_DEBUG_URL")
@staticmethod
def create_application():
"""Create a test Flask application.
"""
ret_val = Flask(__name__)
ret_val.testing = True
ret_val.config["EXCEPTIONAL_API_KEY"] = "key"
ret_val.config["EXCEPTIONAL_DEBUG_URL"] = environ.get(
"EXCEPTIONAL_DEBUG_URL", "http://posttestserver.com/post.php")
ret_val.config["PROPAGATE_EXCEPTIONS"] = False
@ret_val.route("/error")
def error():
"""Do something that raises an exception.
"""
1 / 0
@ret_val.route("/http/<int:code>")
def http(code):
"""Raises an HTTP exception.
"""
abort(code)
return ret_val
def setUp(self):
"""Set up each test.
"""
self.app = self.create_application()
self.exceptional = Exceptional(self.app)
def test_01_exception(self):
"""Test mandatory data requirements for the Exceptional API.
See http://docs.exceptional.io/api/publish for details.
"""
with self.app.test_client() as client:
client.get("/error")
data = json.loads(g.exceptional)
exception = data["exception"]
assert "backtrace" in exception
assert "exception_class" in exception
assert "message" in exception
assert "occurred_at" in exception
environment = data["application_environment"]
assert environment[
"application_root_directory"] == self.app.root_path
assert "env" in environment
def test_02_http_exception(self):
"""Test logging an HTTP exception.
"""
with self.app.test_client() as client:
client.get("/http/404")
data = json.loads(g.exceptional)
exception = data["exception"]
assert "404" in exception["message"]
def test_03_post_form(self):
"""Test POSTing form data.
"""
data = {"foo": "bar", "baz": "qux"}
with self.app.test_client() as client:
client.post("/error", data=data)
data = json.loads(g.exceptional)
request = data["request"]
parameters = request["parameters"]
assert parameters["foo"] == "bar"
assert parameters["baz"] == "qux"
def test_04_post_file(self):
"""Test POSTing file data.
"""
resource = self.app.open_resource("README")
data = {"file": resource}
with self.app.test_client() as client:
client.post("/error", data=data)
data = json.loads(g.exceptional)
request = data["request"]
parameters = request["parameters"]
assert "file" in parameters
def test_05_filter_header(self):
"""Test header data filtering.
"""
self.app.config["EXCEPTIONAL_HEADER_FILTER"] = ["Host"]
Exceptional(self.app)
with self.app.test_client() as client:
client.get("/error")
data = json.loads(g.exceptional)
request = data["request"]
headers = request["headers"]
assert headers["Host"] == "[FILTERED]"
def test_06_filter_parameter(self):
"""Test parameter data filtering.
"""
data = {"foo": "bar", "baz": "qux"}
self.app.config["EXCEPTIONAL_PARAMETER_FILTER"] = ["baz"]
Exceptional(self.app)
with self.app.test_client() as client:
client.post("/error", data=data)
data = json.loads(g.exceptional)
request = data["request"]
parameters = request["parameters"]
assert parameters["baz"] == "[FILTERED]"
def test_07_unexceptional(self):
"""Test disabled Exceptional logging.
"""
self.app = self.create_application()
del self.app.config["EXCEPTIONAL_API_KEY"]
Exceptional(self.app)
with self.app.test_client() as client:
client.get("/error")
assert hasattr(g, "exceptional") is False
def test_08_http_unexceptional(self):
"""Test non-logged HTTP error code.
"""
with self.app.test_client() as client:
client.get("/http/500")
assert hasattr(g, "exceptional") is False
def test_09_debug(self):
"""Test exception in debug mode.
"""
self.app = self.create_application()
self.app.debug = True
exceptional = Exceptional(self.app)
self.app.config["EXCEPTIONAL_ENVIRONMENT_FILTER"].append("os.*")
self.app.config["PROPAGATE_EXCEPTIONS"] = None
assert exceptional.url == self.app.config["EXCEPTIONAL_DEBUG_URL"]
with self.app.test_client() as client:
self.assertRaises(ZeroDivisionError, client.get, "/error")
json.loads(g.exceptional)
print "See %s for HTTP request details." % exceptional.url
def test_10_publish(self):
"""Test direct exception publishing.
"""
self.app = self.create_application()
try:
raise ValueError
except ValueError:
type, exception, traceback = exc_info()
traceback = Traceback(type, exception, traceback)
data = json.loads(Exceptional.publish(self.app.config, traceback))
exception = data["exception"]
assert exception["exception_class"] == ValueError.__name__
def test_11_utf8_decode(self):
"""Test sending an invalid UTF-8 byte sequence through Exceptional.
"""
self.app.config["INVALID_UTF-8"] = "\xf0"
Exceptional(self.app)
with self.app.test_client() as client:
client.get("/error")
data = json.loads(g.exceptional)
application_environment = data["application_environment"]
environment = application_environment["env"]
assert environment["INVALID_UTF-8"] == u"\ufffd"
def test_12_json(self):
"""Test JSON request handling.
"""
self.app = self.create_application()
Exceptional(self.app)
data = json.dumps({"foo": {"bar": "baz"}})
with self.app.test_client() as client:
client.post("/error", content_type="application/json", data=data)
data = json.loads(g.exceptional)
request = data["request"]
parameters = request["parameters"]
assert "bar" in parameters["foo"]
def test_13_invalid_json(self):
"""Test invalid JSON request handling.
"""
data = '{"foo": {"bar": invalid}}'
with self.app.test_client() as client:
client.post("/error", content_type="application/json", data=data)
data = json.loads(g.exceptional)
request = data["request"]
parameters = request["parameters"]
assert "INVALID_JSON" in parameters
def test_14_context(self):
"""Test exception context data.
"""
def exception_handler(app):
handle_exception = app.handle_exception
@wraps(handle_exception)
def ret_val(exception):
Exceptional.context(foo="bar")
return handle_exception(exception)
return ret_val
handle_exception = self.app.handle_exception
self.app.handle_exception = exception_handler(self.app)
with self.app.test_client() as client:
client.get("/error")
data = json.loads(g.exceptional)
context = data["context"]
assert context["foo"] == "bar"
self.app.handle_exception = handle_exception
with self.app.test_client() as client:
client.get("/error")
data = json.loads(g.exceptional)
context = data["context"]
assert context is None
if __name__ == "__main__":
unittest.main()