/
tests.py
181 lines (128 loc) · 6 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
import unittest
from werkzeug.test import EnvironBuilder
from flask import Flask, request
from flask.ext.jsonweb import JsonWeb, JsonWebBadRequest, \
JsonWebRequest, json_view, encode, decode, schema
def get_request(obj):
builder = EnvironBuilder("/", data=encode.dumper(obj))
builder.headers["content-type"] = "application/json"
return JsonWebRequest(builder.get_environ())
class TestJsonWebRequest(unittest.TestCase):
def setUp(self):
@encode.to_object()
@decode.from_object()
class Person(object):
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
self.person_cls = Person
def tearDown(self):
decode._default_object_handlers.clear()
def test_json_attribute(self):
request = get_request(self.person_cls("bob", "smith"))
person = request.json
self.assertIsInstance(person, self.person_cls)
def test_validation_error(self):
v = schema.validators
class PersonSchema(schema.ObjectSchema):
first_name = v.String()
last_name = v.String()
schema.bind_schema("Person", PersonSchema)
request = get_request(self.person_cls(1, "smith"))
with self.assertRaises(JsonWebBadRequest) as c:
person = request.json
error = c.exception
self.assertIn("Error validating object", str(error))
self.assertEqual("Expected str got int instead.", str(error.extra["fields"]["first_name"]))
def test_json_decode_error(self):
request = get_request({"__type__": "foo"})
with self.assertRaises(JsonWebBadRequest) as c:
obj = request.json
error = c.exception.get_body(request.environ)
self.assertIn("Cannot decode object foo. No such object.", error)
class TestJsonWeb(unittest.TestCase):
def setUp(self):
@encode.to_object()
@decode.from_object()
class Person(object):
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
self.person_cls = Person
app = Flask("test")
#app.testing = True
JsonWeb(app)
@app.route("/person", methods=["POST"])
@json_view(expects=Person)
def test_view():
request.json
return "ok"
@app.route("/person", methods=["GET"])
@json_view()
def get_person():
return Person("Bob", "Smith")
@app.route("/error", methods=["GET"])
@json_view()
def error_view():
raise TypeError("Boom!")
self.test_client = app.test_client()
def tearDown(self):
decode._default_object_handlers.clear()
def post(self, url, data):
return self.test_client.post(url, data=encode.dumper(data),
content_type="application/json")
def test_view_gets_decoded_instance(self):
person = self.person_cls("Bob", "Smith")
with self.test_client as c:
res = c.post("/person", data=encode.dumper(person), content_type="application/json")
self.assertIsInstance(request._get_current_object(), JsonWebRequest)
self.assertIsInstance(request.json, self.person_cls)
self.assertEqual(res.status_code, 200)
def test_validation_error_response_when_type_not_expected(self):
"""
Test that a validation error response is returned if request.json
returns an object that is not an instance of the class supplied to
JsonWeb.expects.
"""
res = self.post("/person", [1,2,3])
self.assertEqual(res.status_code, 400)
self.assertIn("Expected Person got list instead.", res.data)
def test_validation_error_response(self):
"""
Test that a validation error response is returned when
object validation fails.
"""
v = schema.validators
class PersonSchema(schema.ObjectSchema):
first_name = v.String()
last_name = v.String()
schema.bind_schema("Person", PersonSchema)
res = self.post("/person", self.person_cls(1, "smith"))
self.assertEqual(res.status_code, 400)
self.assertIn("Expected str got int instead.", res.data)
def test_jsonweb_error_response(self):
res = self.post("/person", {"__type__": "Foo", "value": 42})
self.assertEqual(res.status_code, 400)
self.assertIn("Cannot decode object Foo. No such object.", res.data)
def test_non_jsonweb_error_response(self):
res = self.test_client.get("/error")
self.assertEqual(res.status_code, 500)
self.assertIn("Unhandled Exception.", res.data)
def test_wrong_content_type_returns_error(self):
res = self.test_client.post("/person",
data=encode.dumper(self.person_cls(1, "smith")))
self.assertEqual(res.status_code, 400)
def test_decorated_view_makes_json_response(self):
res = self.test_client.get("/person")
self.assertEqual(res.status_code, 200)
self.assertEqual(res.content_type, "application/json")
self.assertIn('"__type__": "Person"', res.data)
self.assertIn('"first_name": "Bob"', res.data)
self.assertIn('"last_name": "Smith"', res.data)
def suite():
s = unittest.TestSuite()
s.addTest(unittest.makeSuite(TestJsonWebRequest))
s.addTest(unittest.makeSuite(TestJsonWeb))
return s
if __name__ == "__main__":
unittest.main(defaultTest="suite")