-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathtest_payload.py
More file actions
377 lines (299 loc) · 11.2 KB
/
Copy pathtest_payload.py
File metadata and controls
377 lines (299 loc) · 11.2 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import flask_restx as restx
class PayloadTest(object):
def assert_errors(self, client, url, data, *errors):
out = client.post_json(url, data, status=400)
assert "message" in out
assert "errors" in out
for error in errors:
assert error in out["errors"]
def test_validation_false_on_constructor(self, app, client):
api = restx.Api(app, validate=False)
fields = api.model(
"Person",
{
"name": restx.fields.String(required=True),
"age": restx.fields.Integer,
"birthdate": restx.fields.DateTime,
},
)
@api.route("/validation/")
class ValidationOff(restx.Resource):
@api.expect(fields)
def post(self):
return {}
data = client.post_json("/validation/", {})
assert data == {}
def test_validation_false_on_constructor_with_override(self, app, client):
api = restx.Api(app, validate=False)
fields = api.model(
"Person",
{
"name": restx.fields.String(required=True),
"age": restx.fields.Integer,
"birthdate": restx.fields.DateTime,
},
)
@api.route("/validation/")
class ValidationOn(restx.Resource):
@api.expect(fields, validate=True)
def post(self):
return {}
self.assert_errors(client, "/validation/", {}, "name")
def test_validation_true_on_constructor(self, app, client):
api = restx.Api(app, validate=True)
fields = api.model(
"Person",
{
"name": restx.fields.String(required=True),
"age": restx.fields.Integer,
"birthdate": restx.fields.DateTime,
},
)
@api.route("/validation/")
class ValidationOff(restx.Resource):
@api.expect(fields)
def post(self):
return {}
self.assert_errors(client, "/validation/", {}, "name")
def test_validation_true_on_constructor_with_override(self, app, client):
api = restx.Api(app, validate=True)
fields = api.model(
"Person",
{
"name": restx.fields.String(required=True),
"age": restx.fields.Integer,
"birthdate": restx.fields.DateTime,
},
)
@api.route("/validation/")
class ValidationOff(restx.Resource):
@api.expect(fields, validate=False)
def post(self):
return {}
data = client.post_json("/validation/", {})
assert data == {}
def _setup_api_format_checker_tests(self, app, format_checker=None):
class IPAddress(restx.fields.Raw):
__schema_type__ = "string"
__schema_format__ = "ipv4"
api = restx.Api(app, format_checker=format_checker)
model = api.model("MyModel", {"ip": IPAddress(required=True)})
@api.route("/format_checker/")
class TestResource(restx.Resource):
@api.expect(model, validate=True)
def post(self):
return {}
def test_format_checker_none_on_constructor(self, app, client):
self._setup_api_format_checker_tests(app)
out = client.post_json("/format_checker/", {"ip": "192.168.1"})
assert out == {}
def test_format_checker_object_on_constructor(self, app, client):
from jsonschema import FormatChecker
self._setup_api_format_checker_tests(app, format_checker=FormatChecker())
out = client.post_json("/format_checker/", {"ip": "192.168.1"}, status=400)
assert "ipv4" in out["errors"]["ip"]
def test_validation_false_in_config(self, app, client):
app.config["RESTX_VALIDATE"] = False
api = restx.Api(app)
fields = api.model(
"Person",
{
"name": restx.fields.String(required=True),
"age": restx.fields.Integer,
"birthdate": restx.fields.DateTime,
},
)
@api.route("/validation/")
class ValidationOff(restx.Resource):
@api.expect(fields)
def post(self):
return {}
out = client.post_json("/validation/", {})
# assert response.status_code == 200
# out = json.loads(response.data.decode('utf8'))
assert out == {}
def test_validation_in_config(self, app, client):
app.config["RESTX_VALIDATE"] = True
api = restx.Api(app)
fields = api.model(
"Person",
{
"name": restx.fields.String(required=True),
"age": restx.fields.Integer,
"birthdate": restx.fields.DateTime,
},
)
@api.route("/validation/")
class ValidationOn(restx.Resource):
@api.expect(fields)
def post(self):
return {}
self.assert_errors(client, "/validation/", {}, "name")
def test_api_payload(self, app, client):
api = restx.Api(app, validate=True)
fields = api.model(
"Person",
{
"name": restx.fields.String(required=True),
"age": restx.fields.Integer,
"birthdate": restx.fields.DateTime,
},
)
@api.route("/validation/")
class Payload(restx.Resource):
payload = None
@api.expect(fields)
def post(self):
Payload.payload = api.payload
return {}
data = {
"name": "John Doe",
"age": 15,
}
client.post_json("/validation/", data)
assert Payload.payload == data
def test_validation_with_inheritance(self, app, client):
"""It should perform validation with inheritance (allOf/$ref)"""
api = restx.Api(app, validate=True)
fields = api.model(
"Parent",
{
"name": restx.fields.String(required=True),
},
)
child_fields = api.inherit(
"Child",
fields,
{
"age": restx.fields.Integer,
},
)
@api.route("/validation/")
class Inheritance(restx.Resource):
@api.expect(child_fields)
def post(self):
return {}
client.post_json(
"/validation/",
{
"name": "John Doe",
"age": 15,
},
)
self.assert_errors(
client,
"/validation/",
{
"age": "15",
},
"name",
"age",
)
def test_validation_on_list(self, app, client):
"""It should perform validation on lists"""
api = restx.Api(app, validate=True)
person = api.model(
"Person",
{
"name": restx.fields.String(required=True),
"age": restx.fields.Integer(required=True),
},
)
family = api.model(
"Family",
{
"name": restx.fields.String(required=True),
"members": restx.fields.List(restx.fields.Nested(person)),
},
)
@api.route("/validation/")
class List(restx.Resource):
@api.expect(family)
def post(self):
return {}
self.assert_errors(
client,
"/validation/",
{"name": "Doe", "members": [{"name": "Jonn"}, {"age": 42}]},
"members.0.age",
"members.1.name",
)
def _setup_expect_validation_single_resource_tests(self, app):
# Setup a minimal Api with endpoint that expects in input payload
# a single object of a resource
api = restx.Api(app, validate=True)
user = api.model("User", {"username": restx.fields.String()})
@api.route("/validation/")
class Users(restx.Resource):
@api.expect(user)
def post(self):
return {}
def _setup_expect_validation_collection_resource_tests(self, app):
# Setup a minimal Api with endpoint that expects in input payload
# one or more objects of a resource
api = restx.Api(app, validate=True)
user = api.model("User", {"username": restx.fields.String()})
@api.route("/validation/")
class Users(restx.Resource):
@api.expect([user])
def post(self):
return {}
def test_expect_validation_single_resource_success(self, app, client):
self._setup_expect_validation_single_resource_tests(app)
# Input payload is a valid JSON object
out = client.post_json("/validation/", {"username": "alice"})
assert {} == out
def test_expect_validation_single_resource_error(self, app, client):
self._setup_expect_validation_single_resource_tests(app)
# Input payload is an invalid JSON object
self.assert_errors(client, "/validation/", {"username": 123}, "username")
# Input payload is a JSON array (expected JSON object)
self.assert_errors(client, "/validation/", [{"username": 123}], "")
def test_expect_validation_collection_resource_success(self, app, client):
self._setup_expect_validation_collection_resource_tests(app)
# Input payload is a valid JSON object
out = client.post_json("/validation/", {"username": "alice"})
assert {} == out
# Input payload is a JSON array with valid JSON objects
out = client.post_json(
"/validation/", [{"username": "alice"}, {"username": "bob"}]
)
assert {} == out
def test_expect_validation_collection_resource_error(self, app, client):
self._setup_expect_validation_collection_resource_tests(app)
# Input payload is an invalid JSON object
self.assert_errors(client, "/validation/", {"username": 123}, "username")
# Input payload is a JSON array but with an invalid JSON object
self.assert_errors(
client,
"/validation/",
[{"username": "alice"}, {"username": 123}],
"username",
)
def test_validation_with_propagate(self, app, client):
app.config["PROPAGATE_EXCEPTIONS"] = True
api = restx.Api(app, validate=True)
fields = api.model(
"Person",
{
"name": restx.fields.String(required=True),
"age": restx.fields.Integer,
"birthdate": restx.fields.DateTime,
},
)
@api.route("/validation/")
class ValidationOff(restx.Resource):
@api.expect(fields)
def post(self):
return {}
self.assert_errors(client, "/validation/", {}, "name")
def test_empty_payload(self, app, client):
api = restx.Api(app, validate=True)
@api.route("/empty/")
class Payload(restx.Resource):
def post(self):
return {}
response = client.post(
"/empty/", data="", headers={"content-type": "application/json"}
)
assert response.status_code == 200