Skip to content

Commit a6bbdcc

Browse files
Adding e2e tests
1 parent 291872f commit a6bbdcc

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from pydantic import BaseModel
2+
3+
from aws_lambda_powertools.event_handler import APIGatewayRestResolver, Response
4+
from aws_lambda_powertools.event_handler.middlewares import NextMiddleware
5+
from aws_lambda_powertools.utilities.typing import LambdaContext
6+
7+
8+
def middleware_auth(app: APIGatewayRestResolver, next_middleware: NextMiddleware):
9+
# Return early response
10+
return Response(status_code=202, content_type="application/json", body="{}")
11+
12+
13+
app = APIGatewayRestResolver(enable_validation=True)
14+
app.use(middlewares=[middleware_auth])
15+
16+
17+
class MyModel(BaseModel):
18+
name: str
19+
20+
21+
@app.get("/data_validation_middleware")
22+
def get_data_validation_and_middleware() -> MyModel:
23+
return MyModel(name="powertools")
24+
25+
26+
def lambda_handler(event, context: LambdaContext):
27+
return app.resolve(event, context)

tests/e2e/event_handler/infrastructure.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def create_resources(self):
2323
functions["ApiGatewayRestHandler"],
2424
functions["OpenapiHandler"],
2525
functions["OpenapiHandlerWithPep563"],
26+
functions["DataValidationAndMiddleware"],
2627
],
2728
)
2829
self._create_api_gateway_http(function=functions["ApiGatewayHttpHandler"])
@@ -101,6 +102,9 @@ def _create_api_gateway_rest(self, function: list[Function]):
101102
openapi_schema = apigw.root.add_resource("openapi_schema_with_pep563")
102103
openapi_schema.add_method("GET", apigwv1.LambdaIntegration(function[2], proxy=True))
103104

105+
openapi_schema = apigw.root.add_resource("data_validation_middleware")
106+
openapi_schema.add_method("GET", apigwv1.LambdaIntegration(function[3], proxy=True))
107+
104108
CfnOutput(self.stack, "APIGatewayRestUrl", value=apigw.url)
105109

106110
def _create_lambda_function_url(self, function: Function):

tests/e2e/event_handler/test_openapi.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,18 @@ def test_get_openapi_schema_with_pep563(apigw_rest_endpoint):
4444
assert "Powertools e2e API" in response.text
4545
assert "x-amazon-apigateway-gateway-responses" in response.text
4646
assert response.status_code == 200
47+
48+
49+
def test_get_openapi_validation_and_middleware(apigw_rest_endpoint):
50+
# GIVEN
51+
url = f"{apigw_rest_endpoint}data_validation_middleware"
52+
53+
# WHEN
54+
response = data_fetcher.get_http_response(
55+
Request(
56+
method="GET",
57+
url=url,
58+
),
59+
)
60+
61+
assert response.status_code == 202

0 commit comments

Comments
 (0)