forked from turner-townsend/flask-pydantic-spec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_plugin_flask.py
266 lines (216 loc) · 7.36 KB
/
test_plugin_flask.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
259
260
261
262
263
264
265
266
from datetime import datetime
from io import BytesIO
from random import randint
import gzip
import pytest
import json
from flask import Flask, jsonify, request
from werkzeug.datastructures import FileStorage
from flask_pydantic_spec.types import Response, MultipartFormRequest
from flask_pydantic_spec import FlaskPydanticSpec
from .common import (
Query,
Resp,
JSON,
Headers,
Cookies,
DemoModel,
QueryParams,
Users,
FileName,
)
def before_handler(req, resp, err, _):
if err:
resp.headers["X-Error"] = "Validation Error"
def after_handler(req, resp, err, _):
resp.headers["X-Validation"] = "Pass"
def api_after_handler(req, resp, err, _):
resp.headers["X-API"] = "OK"
api = FlaskPydanticSpec(
"flask", before=before_handler, after=after_handler, title="Test API"
)
app = Flask(__name__)
@app.route("/ping")
@api.validate(headers=Headers, tags=["test", "health"])
def ping():
"""summary
description"""
return jsonify(msg="pong")
@app.route("/api/user", methods=["GET"])
@api.validate(
query=QueryParams,
resp=Response(HTTP_200=Users, HTTP_401=None),
)
def get_users():
allowed_names = ["james", "annabel", "bethany"]
query_params = request.context.query
return jsonify(
{
"data": [
{"name": name}
for name in sorted(
set(allowed_names).intersection(set(query_params.name))
)
]
}
)
@app.route("/api/user/<name>", methods=["POST"])
@api.validate(
query=Query,
body=JSON,
cookies=Cookies,
resp=Response(HTTP_200=Resp, HTTP_401=None),
tags=["api", "test"],
after=api_after_handler,
)
def user_score(name):
score = [randint(0, request.context.body.limit) for _ in range(5)]
score.sort(
reverse=request.context.query.order if request.context.query.order else False
)
assert request.context.cookies.pub == "abcdefg"
assert request.cookies["pub"] == "abcdefg"
return jsonify(name=request.context.body.name, score=score)
@app.route("/api/group/<name>", methods=["GET"])
@api.validate(
resp=Response(HTTP_200=Resp, HTTP_401=None, validate=False), tags=["api", "test"]
)
def group_score(name):
score = ["a", "b", "c", "d", "e"]
return jsonify(name=name, score=score)
@app.route("/api/file", methods=["POST"])
@api.validate(
body=MultipartFormRequest(model=FileName), resp=Response(HTTP_200=DemoModel)
)
def upload_file():
files = request.files
body = request.context.body
assert body is not None
assert files is not None
return jsonify(uid=1, limit=2, name=body.file_name)
api.register(app)
@pytest.fixture(params=[422, 400])
def client(request):
api.config.VALIDATION_ERROR_CODE = request.param
with app.test_client() as client:
yield client
@pytest.mark.parametrize("client", [422], indirect=True)
def test_flask_validate(client):
resp = client.get("/ping")
assert resp.status_code == 422
assert resp.headers.get("X-Error") == "Validation Error"
resp = client.get("/ping", headers={"lang": "en-US"})
assert resp.json == {"msg": "pong"}
assert resp.headers.get("X-Error") is None
assert resp.headers.get("X-Validation") == "Pass"
resp = client.post("api/user/flask")
assert resp.status_code == 422
assert resp.headers.get("X-Error") == "Validation Error"
client.set_cookie("flask", "pub", "abcdefg")
resp = client.post(
"/api/user/flask?order=1",
data=json.dumps(dict(name="flask", limit=10)),
content_type="application/json",
)
assert resp.status_code == 200, resp.json
assert resp.headers.get("X-Validation") is None
assert resp.headers.get("X-API") == "OK"
assert resp.json["name"] == "flask"
assert resp.json["score"] == sorted(resp.json["score"], reverse=True)
resp = client.post(
"/api/user/flask?order=0",
data=json.dumps(dict(name="flask", limit=10)),
content_type="application/json",
)
assert resp.json["score"] == sorted(resp.json["score"], reverse=False)
resp = client.post(
"/api/user/flask",
data=json.dumps(dict(name="flask", limit=10)),
content_type="application/json",
)
assert resp.json["score"] == sorted(resp.json["score"], reverse=False)
@pytest.mark.parametrize("client", [422], indirect=True)
def test_sending_file(client):
file = FileStorage(BytesIO(b"abcde"), filename="test.jpg", name="test.jpg")
resp = client.post(
"/api/file",
data={
"file": file,
"file_name": "another_test.jpg",
"data": json.dumps(
{"type": "foo", "created_at": str(datetime.now().date())}
),
},
content_type="multipart/form-data",
)
assert resp.status_code == 200
assert resp.json["name"] == "another_test.jpg"
@pytest.mark.parametrize("client", [422], indirect=True)
def test_query_params(client):
resp = client.get("api/user?name=james&name=bethany&name=claire")
assert resp.status_code == 200
assert len(resp.json["data"]) == 2
assert resp.json["data"] == [
{
"name": "bethany",
},
{
"name": "james",
},
]
@pytest.mark.parametrize("client", [200], indirect=True)
def test_flask_skip_validation(client):
resp = client.get("api/group/test")
assert resp.status_code == 200
assert resp.json["name"] == "test"
assert resp.json["score"] == ["a", "b", "c", "d", "e"]
@pytest.mark.parametrize("client", [422], indirect=True)
def test_flask_doc(client):
resp = client.get("/apidoc/openapi.json")
assert resp.json == api.spec
resp = client.get("/apidoc/redoc")
assert resp.status_code == 200
assert b"spec-url='/apidoc/openapi.json'" in resp.data
assert b"<title>Test API</title>" in resp.data
resp = client.get("/apidoc/swagger")
assert resp.status_code == 200
@pytest.mark.parametrize("client", [400], indirect=True)
def test_flask_validate_with_alternative_code(client):
resp = client.get("/ping")
assert resp.status_code == 400
assert resp.headers.get("X-Error") == "Validation Error"
resp = client.post("api/user/flask")
assert resp.status_code == 400
assert resp.headers.get("X-Error") == "Validation Error"
@pytest.mark.parametrize("client", [400], indirect=True)
def test_flask_post_gzip(client):
body = dict(name="flask", limit=10)
compressed = gzip.compress(bytes(json.dumps(body), encoding="utf-8"))
client.set_cookie("flask", "pub", "abcdefg")
resp = client.post(
"/api/user/flask?order=0",
data=compressed,
headers={
"content-type": "application/json",
"content-encoding": "gzip",
},
)
assert resp.status_code == 200
assert resp.json["name"] == "flask"
@pytest.mark.parametrize("client", [400], indirect=True)
def test_flask_post_gzip_failure(client):
body = dict(name="flask")
compressed = gzip.compress(bytes(json.dumps(body), encoding="utf-8"))
client.set_cookie("flask", "pub", "abcdefg")
resp = client.post(
"/api/user/flask?order=0",
data=compressed,
headers={
"content-type": "application/json",
"content-encoding": "gzip",
},
)
assert resp.status_code == 400
assert resp.json == [
{"loc": ["limit"], "msg": "field required", "type": "value_error.missing"}
]