Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 84d035a

Browse files
committedJul 8, 2024·
Polishing the PR with best practicies - Tests
1 parent 3fceac7 commit 84d035a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
 

‎tests/functional/event_handler/_pydantic/test_openapi_extensions.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,28 @@
1212

1313

1414
def test_openapi_extension_root_level():
15+
# GIVEN an APIGatewayRestResolver instance
1516
app = APIGatewayRestResolver()
1617

1718
cors_config = {
1819
"maxAge": 0,
1920
"allowCredentials": False,
2021
}
2122

23+
# WHEN we get the OpenAPI JSON schema with CORS extension in the Root Level
2224
schema = json.loads(
2325
app.get_openapi_json_schema(
2426
openapi_extensions={"x-amazon-apigateway-cors": cors_config},
2527
),
2628
)
2729

30+
# THEN the OpenAPI schema must contain the "x-amazon-apigateway-cors" extension
2831
assert "x-amazon-apigateway-cors" in schema
2932
assert schema["x-amazon-apigateway-cors"] == cors_config
3033

3134

3235
def test_openapi_extension_server_level():
36+
# GIVEN an APIGatewayRestResolver instance
3337
app = APIGatewayRestResolver()
3438

3539
endpoint_config = {
@@ -42,6 +46,7 @@ def test_openapi_extension_server_level():
4246
"description": "Example website",
4347
}
4448

49+
# WHEN we get the OpenAPI JSON schema with a server-level openapi extension
4550
schema = json.loads(
4651
app.get_openapi_json_schema(
4752
title="Hello API",
@@ -57,11 +62,13 @@ def test_openapi_extension_server_level():
5762
),
5863
)
5964

65+
# THEN the OpenAPI schema must contain the "x-amazon-apigateway-endpoint-configuration" at the server level
6066
assert "x-amazon-apigateway-endpoint-configuration" in schema["servers"][0]
6167
assert schema["servers"][0]["x-amazon-apigateway-endpoint-configuration"] == endpoint_config
6268

6369

6470
def test_openapi_extension_security_scheme_level_with_api_key():
71+
# GIVEN an APIGatewayRestResolver instance
6572
app = APIGatewayRestResolver()
6673

6774
authorizer_config = {
@@ -76,6 +83,7 @@ def test_openapi_extension_security_scheme_level_with_api_key():
7683
"in_": APIKeyIn.header,
7784
}
7885

86+
# WHEN we get the OpenAPI JSON schema with a security scheme-level extension for a custom auth
7987
schema = json.loads(
8088
app.get_openapi_json_schema(
8189
security_schemes={
@@ -90,12 +98,14 @@ def test_openapi_extension_security_scheme_level_with_api_key():
9098
),
9199
)
92100

101+
# THEN the OpenAPI schema must contain the "x-amazon-apigateway-authtype" extension at the security scheme level
93102
assert "x-amazon-apigateway-authtype" in schema["components"]["securitySchemes"]["apiKey"]
94103
assert schema["components"]["securitySchemes"]["apiKey"]["x-amazon-apigateway-authtype"] == "custom"
95104
assert schema["components"]["securitySchemes"]["apiKey"]["x-amazon-apigateway-authorizer"] == authorizer_config
96105

97106

98107
def test_openapi_extension_security_scheme_level_with_oauth2():
108+
# GIVEN an APIGatewayRestResolver instance
99109
app = APIGatewayRestResolver()
100110

101111
authorizer_config = {
@@ -115,6 +125,7 @@ def test_openapi_extension_security_scheme_level_with_oauth2():
115125
),
116126
}
117127

128+
# WHEN we get the OpenAPI JSON schema with a security scheme-level extension for a custom auth
118129
schema = json.loads(
119130
app.get_openapi_json_schema(
120131
security_schemes={
@@ -128,19 +139,24 @@ def test_openapi_extension_security_scheme_level_with_oauth2():
128139
),
129140
)
130141

142+
# THEN the OpenAPI schema must contain the "x-amazon-apigateway-authorizer" extension at the security scheme level
131143
assert "x-amazon-apigateway-authorizer" in schema["components"]["securitySchemes"]["oauth2"]
132144
assert schema["components"]["securitySchemes"]["oauth2"]["x-amazon-apigateway-authorizer"] == authorizer_config
133145

134146

135147
def test_openapi_extension_operation_level(openapi_extension_integration_detail):
148+
# GIVEN an APIGatewayRestResolver instance
136149
app = APIGatewayRestResolver()
137150

151+
# WHEN we define an integration extension at operation level
152+
# AND get the schema
138153
@app.get("/test", openapi_extensions={"x-amazon-apigateway-integration": openapi_extension_integration_detail})
139154
def lambda_handler():
140155
pass
141156

142157
schema = json.loads(app.get_openapi_json_schema())
143158

159+
# THEN the OpenAPI schema must contain the "x-amazon-apigateway-integration" extension at the operation level
144160
assert "x-amazon-apigateway-integration" in schema["paths"]["/test"]["get"]
145161
assert schema["paths"]["/test"]["get"]["x-amazon-apigateway-integration"] == openapi_extension_integration_detail
146162

@@ -149,8 +165,11 @@ def test_openapi_extension_operation_level_multiple_paths(
149165
openapi_extension_integration_detail,
150166
openapi_extension_validator_detail,
151167
):
168+
# GIVEN an APIGatewayRestResolver instance
152169
app = APIGatewayRestResolver()
153170

171+
# WHEN we define multiple routes with integration extension at operation level
172+
# AND get the schema
154173
@app.get("/test", openapi_extensions={"x-amazon-apigateway-integration": openapi_extension_integration_detail})
155174
def lambda_handler_get():
156175
pass
@@ -161,6 +180,7 @@ def lambda_handler_post():
161180

162181
schema = json.loads(app.get_openapi_json_schema())
163182

183+
# THEN each route must contain only your extension
164184
assert "x-amazon-apigateway-integration" in schema["paths"]["/test"]["get"]
165185
assert schema["paths"]["/test"]["get"]["x-amazon-apigateway-integration"] == openapi_extension_integration_detail
166186

@@ -172,9 +192,12 @@ def lambda_handler_post():
172192

173193

174194
def test_openapi_extension_operation_level_with_router(openapi_extension_integration_detail):
195+
# GIVEN an APIGatewayRestResolver and Router instance
175196
app = APIGatewayRestResolver()
176197
router = Router()
177198

199+
# WHEN we define an integration extension at operation level using Router
200+
# AND get the schema
178201
@router.get("/test", openapi_extensions={"x-amazon-apigateway-integration": openapi_extension_integration_detail})
179202
def lambda_handler():
180203
pass
@@ -183,6 +206,7 @@ def lambda_handler():
183206

184207
schema = json.loads(app.get_openapi_json_schema())
185208

209+
# THEN the OpenAPI schema must contain the "x-amazon-apigateway-integration" extension at the operation level
186210
assert "x-amazon-apigateway-integration" in schema["paths"]["/test"]["get"]
187211
assert schema["paths"]["/test"]["get"]["x-amazon-apigateway-integration"] == openapi_extension_integration_detail
188212

@@ -191,9 +215,12 @@ def test_openapi_extension_operation_level_multiple_paths_with_router(
191215
openapi_extension_integration_detail,
192216
openapi_extension_validator_detail,
193217
):
218+
# GIVEN an APIGatewayRestResolver and Router instance
194219
app = APIGatewayRestResolver()
195220
router = Router()
196221

222+
# WHEN we define multiple routes using extensions at operation level using Router
223+
# AND get the schema
197224
@router.get("/test", openapi_extensions={"x-amazon-apigateway-integration": openapi_extension_integration_detail})
198225
def lambda_handler_get():
199226
pass
@@ -209,6 +236,7 @@ def lambda_handler_post():
209236

210237
schema = json.loads(app.get_openapi_json_schema())
211238

239+
# THEN each route must contain only your extension
212240
assert "x-amazon-apigateway-integration" in schema["paths"]["/test"]["get"]
213241
assert schema["paths"]["/test"]["get"]["x-amazon-apigateway-integration"] == openapi_extension_integration_detail
214242

0 commit comments

Comments
 (0)
Please sign in to comment.