|
| 1 | +import pytest |
| 2 | + |
| 3 | +from aws_lambda_powertools.event_handler import APIGatewayRestResolver |
| 4 | +from aws_lambda_powertools.event_handler.openapi.models import APIKey, APIKeyIn |
| 5 | + |
| 6 | + |
| 7 | +def test_openapi_top_level_security(): |
| 8 | + app = APIGatewayRestResolver() |
| 9 | + |
| 10 | + @app.get("/") |
| 11 | + def handler(): |
| 12 | + raise NotImplementedError() |
| 13 | + |
| 14 | + schema = app.get_openapi_schema( |
| 15 | + security_schemes={ |
| 16 | + "apiKey": APIKey(name="X-API-KEY", description="API Key", in_=APIKeyIn.header), |
| 17 | + }, |
| 18 | + security=[{"apiKey": []}], |
| 19 | + ) |
| 20 | + |
| 21 | + security = schema.security |
| 22 | + assert security is not None |
| 23 | + |
| 24 | + assert len(security) == 1 |
| 25 | + assert security[0] == {"apiKey": []} |
| 26 | + |
| 27 | + |
| 28 | +def test_openapi_top_level_security_missing(): |
| 29 | + app = APIGatewayRestResolver() |
| 30 | + |
| 31 | + @app.get("/") |
| 32 | + def handler(): |
| 33 | + raise NotImplementedError() |
| 34 | + |
| 35 | + with pytest.raises(ValueError): |
| 36 | + app.get_openapi_schema( |
| 37 | + security=[{"apiKey": []}], |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def test_openapi_operation_security(): |
| 42 | + app = APIGatewayRestResolver() |
| 43 | + |
| 44 | + @app.get("/", security=[{"apiKey": []}]) |
| 45 | + def handler(): |
| 46 | + raise NotImplementedError() |
| 47 | + |
| 48 | + schema = app.get_openapi_schema( |
| 49 | + security_schemes={ |
| 50 | + "apiKey": APIKey(name="X-API-KEY", description="API Key", in_=APIKeyIn.header), |
| 51 | + }, |
| 52 | + ) |
| 53 | + |
| 54 | + security = schema.security |
| 55 | + assert security is None |
| 56 | + |
| 57 | + operation = schema.paths["/"].get |
| 58 | + security = operation.security |
| 59 | + assert security is not None |
| 60 | + |
| 61 | + assert len(security) == 1 |
| 62 | + assert security[0] == {"apiKey": []} |
0 commit comments