-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathtest_openapi_security_schemes.py
136 lines (105 loc) · 3.73 KB
/
test_openapi_security_schemes.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
from __future__ import annotations
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.event_handler.openapi.models import (
APIKey,
APIKeyIn,
HTTPBearer,
MutualTLS,
OAuth2,
OAuthFlowImplicit,
OAuthFlows,
OpenIdConnect,
)
def test_openapi_security_scheme_api_key():
app = APIGatewayRestResolver()
@app.get("/")
def handler():
raise NotImplementedError()
schema = app.get_openapi_schema(
security_schemes={
"apiKey": APIKey(name="X-API-KEY", description="API Key", in_=APIKeyIn.header),
},
)
security_schemes = schema.components.securitySchemes
assert security_schemes is not None
assert "apiKey" in security_schemes
api_key_scheme = security_schemes["apiKey"]
assert api_key_scheme.type_.value == "apiKey"
assert api_key_scheme.name == "X-API-KEY"
assert api_key_scheme.description == "API Key"
assert api_key_scheme.in_.value == "header"
def test_openapi_security_scheme_http():
app = APIGatewayRestResolver()
@app.get("/")
def handler():
raise NotImplementedError()
schema = app.get_openapi_schema(
security_schemes={
"bearerAuth": HTTPBearer(
description="JWT Token",
bearerFormat="JWT",
),
},
)
security_schemes = schema.components.securitySchemes
assert security_schemes is not None
assert "bearerAuth" in security_schemes
http_scheme = security_schemes["bearerAuth"]
assert http_scheme.type_.value == "http"
assert http_scheme.scheme == "bearer"
assert http_scheme.bearerFormat == "JWT"
def test_openapi_security_scheme_oauth2():
app = APIGatewayRestResolver()
@app.get("/")
def handler():
raise NotImplementedError()
schema = app.get_openapi_schema(
security_schemes={
"oauth2": OAuth2(
flows=OAuthFlows(
implicit=OAuthFlowImplicit(
authorizationUrl="https://example.com/oauth2/authorize",
),
),
),
},
)
security_schemes = schema.components.securitySchemes
assert security_schemes is not None
assert "oauth2" in security_schemes
oauth2_scheme = security_schemes["oauth2"]
assert oauth2_scheme.type_.value == "oauth2"
assert oauth2_scheme.flows.implicit.authorizationUrl == "https://example.com/oauth2/authorize"
def test_openapi_security_scheme_open_id_connect():
app = APIGatewayRestResolver()
@app.get("/")
def handler():
raise NotImplementedError()
schema = app.get_openapi_schema(
security_schemes={
"openIdConnect": OpenIdConnect(
openIdConnectUrl="https://example.com/oauth2/authorize",
),
},
)
security_schemes = schema.components.securitySchemes
assert security_schemes is not None
assert "openIdConnect" in security_schemes
open_id_connect_scheme = security_schemes["openIdConnect"]
assert open_id_connect_scheme.type_.value == "openIdConnect"
assert open_id_connect_scheme.openIdConnectUrl == "https://example.com/oauth2/authorize"
def test_openapi_security_scheme_mtls():
app = APIGatewayRestResolver()
@app.get("/")
def handler():
raise NotImplementedError()
schema = app.get_openapi_schema(
security_schemes={
"mutualTLS": MutualTLS(description="mTLS Authentication"),
},
)
security_schemes = schema.components.securitySchemes
assert security_schemes is not None
assert "mutualTLS" in security_schemes
mtls_scheme = security_schemes["mutualTLS"]
assert mtls_scheme.description == "mTLS Authentication"