-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathtest_openapi_validation_middleware.py
332 lines (243 loc) · 9.86 KB
/
test_openapi_validation_middleware.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
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
import json
from dataclasses import dataclass
from enum import Enum
from pathlib import PurePath
from typing import List, Tuple
from pydantic import BaseModel
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.event_handler.openapi.params import Body
from aws_lambda_powertools.shared.types import Annotated
from tests.functional.utils import load_event
LOAD_GW_EVENT = load_event("apiGatewayProxyEvent.json")
def test_validate_scalars():
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)
# WHEN a handler is defined with a scalar parameter
@app.get("/users/<user_id>")
def handler(user_id: int):
print(user_id)
# sending a number
LOAD_GW_EVENT["path"] = "/users/123"
# THEN the handler should be invoked and return 200
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
# sending a string
LOAD_GW_EVENT["path"] = "/users/abc"
# THEN the handler should be invoked and return 422
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 422
assert any(text in result["body"] for text in ["type_error.integer", "int_parsing"])
def test_validate_scalars_with_default():
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)
# WHEN a handler is defined with a default scalar parameter
@app.get("/users/<user_id>")
def handler(user_id: int = 123):
print(user_id)
# sending a number
LOAD_GW_EVENT["path"] = "/users/123"
# THEN the handler should be invoked and return 200
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
# sending a string
LOAD_GW_EVENT["path"] = "/users/abc"
# THEN the handler should be invoked and return 422
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 422
assert any(text in result["body"] for text in ["type_error.integer", "int_parsing"])
def test_validate_scalars_with_default_and_optional():
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)
# WHEN a handler is defined with a default scalar parameter
@app.get("/users/<user_id>")
def handler(user_id: int = 123, include_extra: bool = False):
print(user_id)
# sending a number
LOAD_GW_EVENT["path"] = "/users/123"
# THEN the handler should be invoked and return 200
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
# sending a string
LOAD_GW_EVENT["path"] = "/users/abc"
# THEN the handler should be invoked and return 422
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 422
assert any(text in result["body"] for text in ["type_error.integer", "int_parsing"])
def test_validate_return_type():
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)
# WHEN a handler is defined with a return type
@app.get("/")
def handler() -> int:
return 123
LOAD_GW_EVENT["path"] = "/"
# THEN the handler should be invoked and return 200
# THEN the body must be 123
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
assert result["body"] == "123"
def test_validate_return_list():
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)
# WHEN a handler is defined with a return type
@app.get("/")
def handler() -> List[int]:
return [123, 234]
LOAD_GW_EVENT["path"] = "/"
# THEN the handler should be invoked and return 200
# THEN the body must be [123, 234]
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
assert json.loads(result["body"]) == [123, 234]
def test_validate_return_tuple():
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)
sample_tuple = (1, 2, 3)
# WHEN a handler is defined with a return type as Tuple
@app.get("/")
def handler() -> Tuple:
return sample_tuple
LOAD_GW_EVENT["path"] = "/"
# THEN the handler should be invoked and return 200
# THEN the body must be a tuple
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
assert json.loads(result["body"]) == [1, 2, 3]
def test_validate_return_purepath():
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)
sample_path = PurePath(__file__)
# WHEN a handler is defined with a return type as string
# WHEN return value is a PurePath
@app.get("/")
def handler() -> str:
return sample_path.as_posix()
LOAD_GW_EVENT["path"] = "/"
# THEN the handler should be invoked and return 200
# THEN the body must be a string
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
assert result["body"] == sample_path.as_posix()
def test_validate_return_enum():
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)
class Model(Enum):
name = "powertools"
# WHEN a handler is defined with a return type as Enum
@app.get("/")
def handler() -> Model:
return Model.name.value
LOAD_GW_EVENT["path"] = "/"
# THEN the handler should be invoked and return 200
# THEN the body must be a string
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
assert result["body"] == "powertools"
def test_validate_return_dataclass():
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)
@dataclass
class Model:
name: str
age: int
# WHEN a handler is defined with a return type as dataclass
@app.get("/")
def handler() -> Model:
return Model(name="John", age=30)
LOAD_GW_EVENT["path"] = "/"
# THEN the handler should be invoked and return 200
# THEN the body must be a JSON object
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
assert json.loads(result["body"]) == {"name": "John", "age": 30}
def test_validate_return_model():
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)
class Model(BaseModel):
name: str
age: int
# WHEN a handler is defined with a return type as Pydantic model
@app.get("/")
def handler() -> Model:
return Model(name="John", age=30)
LOAD_GW_EVENT["path"] = "/"
# THEN the handler should be invoked and return 200
# THEN the body must be a JSON object
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
assert json.loads(result["body"]) == {"name": "John", "age": 30}
def test_validate_invalid_return_model():
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)
class Model(BaseModel):
name: str
age: int
# WHEN a handler is defined with a return type as Pydantic model
@app.get("/")
def handler() -> Model:
return {"name": "John"} # type: ignore
LOAD_GW_EVENT["path"] = "/"
# THEN the handler should be invoked and return 422
# THEN the body must be a dict
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 422
assert "missing" in result["body"]
def test_validate_body_param():
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)
class Model(BaseModel):
name: str
age: int
# WHEN a handler is defined with a body parameter
@app.post("/")
def handler(user: Model) -> Model:
return user
LOAD_GW_EVENT["httpMethod"] = "POST"
LOAD_GW_EVENT["path"] = "/"
LOAD_GW_EVENT["body"] = json.dumps({"name": "John", "age": 30})
# THEN the handler should be invoked and return 200
# THEN the body must be a JSON object
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
assert json.loads(result["body"]) == {"name": "John", "age": 30}
def test_validate_body_param_with_invalid_date():
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)
class Model(BaseModel):
name: str
age: int
# WHEN a handler is defined with a body parameter
@app.post("/")
def handler(user: Model) -> Model:
return user
LOAD_GW_EVENT["httpMethod"] = "POST"
LOAD_GW_EVENT["path"] = "/"
LOAD_GW_EVENT["body"] = "{" # invalid JSON
# THEN the handler should be invoked and return 422
# THEN the body must have the "json_invalid" error message
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 422
assert "json_invalid" in result["body"]
def test_validate_embed_body_param():
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)
class Model(BaseModel):
name: str
age: int
# WHEN a handler is defined with a body parameter
@app.post("/")
def handler(user: Annotated[Model, Body(embed=True)]) -> Model:
return user
LOAD_GW_EVENT["httpMethod"] = "POST"
LOAD_GW_EVENT["path"] = "/"
LOAD_GW_EVENT["body"] = json.dumps({"name": "John", "age": 30})
# THEN the handler should be invoked and return 422
# THEN the body must be a dict
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 422
assert "missing" in result["body"]
# THEN the handler should be invoked and return 200
# THEN the body must be a dict
LOAD_GW_EVENT["body"] = json.dumps({"user": {"name": "John", "age": 30}})
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200