1
1
import pytest
2
2
3
3
from aws_lambda_powertools .event_handler import APIGatewayRestResolver
4
+ from aws_lambda_powertools .event_handler .api_gateway import Router
4
5
from aws_lambda_powertools .event_handler .openapi .models import APIKey , APIKeyIn
5
6
6
7
7
8
def test_openapi_top_level_security ():
9
+ # GIVEN an APIGatewayRestResolver instance
8
10
app = APIGatewayRestResolver ()
9
11
10
12
@app .get ("/" )
11
13
def handler ():
12
14
raise NotImplementedError ()
13
15
16
+ # WHEN the get_openapi_schema method is called with a security scheme
14
17
schema = app .get_openapi_schema (
15
18
security_schemes = {
16
19
"apiKey" : APIKey (name = "X-API-KEY" , description = "API Key" , in_ = APIKeyIn .header ),
17
20
},
18
21
security = [{"apiKey" : []}],
19
22
)
20
23
24
+ # THEN the resulting schema should have security defined at the top level
21
25
security = schema .security
22
26
assert security is not None
23
27
@@ -26,31 +30,67 @@ def handler():
26
30
27
31
28
32
def test_openapi_top_level_security_missing ():
33
+ # GIVEN an APIGatewayRestResolver instance
29
34
app = APIGatewayRestResolver ()
30
35
31
36
@app .get ("/" )
32
37
def handler ():
33
38
raise NotImplementedError ()
34
39
40
+ # WHEN the get_openapi_schema method is called with security defined without security schemes
41
+ # THEN a ValueError should be raised
35
42
with pytest .raises (ValueError ):
36
43
app .get_openapi_schema (
37
44
security = [{"apiKey" : []}],
38
45
)
39
46
40
47
41
48
def test_openapi_operation_security ():
49
+ # GIVEN an APIGatewayRestResolver instance
42
50
app = APIGatewayRestResolver ()
43
51
44
52
@app .get ("/" , security = [{"apiKey" : []}])
45
53
def handler ():
46
54
raise NotImplementedError ()
47
55
56
+ # WHEN the get_openapi_schema method is called with security defined at the operation level
48
57
schema = app .get_openapi_schema (
49
58
security_schemes = {
50
59
"apiKey" : APIKey (name = "X-API-KEY" , description = "API Key" , in_ = APIKeyIn .header ),
51
60
},
52
61
)
53
62
63
+ # THEN the resulting schema should have security defined at the operation level, not the top level
64
+ security = schema .security
65
+ assert security is None
66
+
67
+ operation = schema .paths ["/" ].get
68
+ security = operation .security
69
+ assert security is not None
70
+
71
+ assert len (security ) == 1
72
+ assert security [0 ] == {"apiKey" : []}
73
+
74
+
75
+ def test_openapi_operation_security_with_router ():
76
+ # GIVEN an APIGatewayRestResolver instance with a Router
77
+ app = APIGatewayRestResolver ()
78
+ router = Router ()
79
+
80
+ @router .get ("/" , security = [{"apiKey" : []}])
81
+ def handler ():
82
+ raise NotImplementedError ()
83
+
84
+ app .include_router (router )
85
+
86
+ # WHEN the get_openapi_schema method is called with security defined at the operation level in the Router
87
+ schema = app .get_openapi_schema (
88
+ security_schemes = {
89
+ "apiKey" : APIKey (name = "X-API-KEY" , description = "API Key" , in_ = APIKeyIn .header ),
90
+ },
91
+ )
92
+
93
+ # THEN the resulting schema should have security defined at the operation level
54
94
security = schema .security
55
95
assert security is None
56
96
0 commit comments