-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathtest_openapi_config.py
89 lines (66 loc) · 2.59 KB
/
test_openapi_config.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
from __future__ import annotations
import json
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
def test_export_openapi_schema_with_custom_configuration():
# GIVEN an API Gateway resolver with OpenAPI validation enabled
app = APIGatewayRestResolver(enable_validation=True)
# GIVEN custom OpenAPI configuration
openapi_title = "My API"
openapi_myapi_version = "1.1.1-alpha"
app.configure_openapi(title=openapi_title, version=openapi_myapi_version)
# WHEN we have a simple handler
@app.get("/")
def handler():
pass
# WHEN we get the schema
schema = app.get_openapi_schema()
# THEN the schema should contain our custom configuration
assert schema.info.title == openapi_title
assert schema.info.version == openapi_myapi_version
def test_prioritize_direct_parameters_over_stored_configuration():
# GIVEN
stored_config = {
"title": "Stored API Title",
"version": "1.0.0",
}
direct_params = {
"title": "Direct API Title",
"version": "2.0.0",
}
# GIVEN an API Gateway resolver with OpenAPI validation enabled
app = APIGatewayRestResolver(enable_validation=True)
app.configure_openapi(**stored_config)
# WHEN we have a simple handler
@app.get("/")
def handler():
pass
# WHEN we get the schema with direct params
schema = app.get_openapi_schema(**direct_params)
# THEN direct parameters must override stored configuration
assert schema.info.title == direct_params["title"]
assert schema.info.version == direct_params["version"]
def test_export_openapi_schema_with_custom_configuration_and_json_export():
# GIVEN an API Gateway resolver with OpenAPI validation enabled
app = APIGatewayRestResolver(enable_validation=True)
# GIVEN custom OpenAPI configuration
openapi_title = "My API"
openapi_myapi_version = "1.1.1-alpha"
openapi_version = "3.1.2"
openapi_description = "My descrition"
app.configure_openapi(
title=openapi_title,
version=openapi_myapi_version,
openapi_version=openapi_version,
description=openapi_description,
)
# WHEN we have a simple handler
@app.get("/")
def handler():
pass
# WHEN we get the schema
schema = json.loads(app.get_openapi_json_schema())
# THEN the schema should contain our custom configuration
assert schema["info"]["title"] == openapi_title
assert schema["info"]["version"] == openapi_myapi_version
assert schema["openapi"] == openapi_version
assert schema["info"]["description"] == openapi_description