Skip to content

Commit 4ee6ae7

Browse files
refactor(test): make CORS test consistent with expected behavior (#4882)
* fix: cors is opt out if enabled * test: opt-out instead opt-in for cors endpoints * test: catch some UserWarnings --------- Co-authored-by: Leandro Damascena <[email protected]>
1 parent 2d738d0 commit 4ee6ae7

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,22 @@ class CORSConfig:
122122
Examples
123123
--------
124124
125-
Simple cors example using the default permissive cors, not this should only be used during early prototyping
125+
Simple CORS example using the default permissive CORS, note that this should only be used during early prototyping.
126126
127127
```python
128-
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
128+
from aws_lambda_powertools.event_handler.api_gateway import (
129+
APIGatewayRestResolver, CORSConfig
130+
)
129131
130-
app = APIGatewayRestResolver()
132+
app = APIGatewayRestResolver(cors=CORSConfig())
131133
132-
@app.get("/my/path", cors=True)
134+
@app.get("/my/path")
133135
def with_cors():
134136
return {"message": "Foo"}
135137
```
136138
137139
Using a custom CORSConfig where `with_cors` used the custom provided CORSConfig and `without_cors`
138-
do not include any cors headers.
140+
do not include any CORS headers.
139141
140142
```python
141143
from aws_lambda_powertools.event_handler.api_gateway import (

tests/functional/event_handler/required_dependencies/test_api_gateway.py

+24-17
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,15 @@ def handler(event, context):
323323

324324

325325
def test_cors():
326-
# GIVEN a function with cors=True
326+
# GIVEN a function
327327
# AND http method set to GET
328328
app = ApiGatewayResolver(cors=CORSConfig("https://aws.amazon.com", allow_credentials=True))
329329

330-
@app.get("/my/path", cors=True)
330+
@app.get("/my/path")
331331
def with_cors() -> Response:
332332
return Response(200, content_types.TEXT_HTML, "test")
333333

334-
@app.get("/without-cors")
334+
@app.get("/without-cors", cors=False)
335335
def without_cors() -> Response:
336336
return Response(200, content_types.TEXT_HTML, "test")
337337

@@ -350,17 +350,17 @@ def handler(event, context):
350350
assert headers["Access-Control-Allow-Headers"] == [",".join(sorted(CORSConfig._REQUIRED_HEADERS))]
351351

352352
# THEN for routes without cors flag return no cors headers
353-
mock_event = {"path": "/my/request", "httpMethod": "GET"}
353+
mock_event = {"path": "/without-cors", "httpMethod": "GET"}
354354
result = handler(mock_event, None)
355355
assert "Access-Control-Allow-Origin" not in result["multiValueHeaders"]
356356

357357

358358
def test_cors_no_request_origin():
359-
# GIVEN a function with cors=True
359+
# GIVEN a function
360360
# AND http method set to GET
361-
app = ApiGatewayResolver()
361+
app = ApiGatewayResolver(cors=CORSConfig())
362362

363-
@app.get("/my/path", cors=True)
363+
@app.get("/my/path")
364364
def with_cors() -> Response:
365365
return Response(200, content_types.TEXT_HTML, "test")
366366

@@ -381,7 +381,7 @@ def handler(event, context):
381381

382382

383383
def test_cors_allow_all_request_origins():
384-
# GIVEN a function with cors=True
384+
# GIVEN a function
385385
# AND http method set to GET
386386
app = ApiGatewayResolver(
387387
cors=CORSConfig(
@@ -390,11 +390,11 @@ def test_cors_allow_all_request_origins():
390390
),
391391
)
392392

393-
@app.get("/my/path", cors=True)
393+
@app.get("/my/path")
394394
def with_cors() -> Response:
395395
return Response(200, content_types.TEXT_HTML, "test")
396396

397-
@app.get("/without-cors")
397+
@app.get("/without-cors", cors=False)
398398
def without_cors() -> Response:
399399
return Response(200, content_types.TEXT_HTML, "test")
400400

@@ -413,7 +413,7 @@ def handler(event, context):
413413
assert headers["Access-Control-Allow-Headers"] == [",".join(sorted(CORSConfig._REQUIRED_HEADERS))]
414414

415415
# THEN for routes without cors flag return no cors headers
416-
mock_event = {"path": "/my/request", "httpMethod": "GET"}
416+
mock_event = {"path": "/without-cors", "httpMethod": "GET"}
417417
result = handler(mock_event, None)
418418
assert "Access-Control-Allow-Origin" not in result["multiValueHeaders"]
419419

@@ -811,7 +811,7 @@ def test_custom_preflight_response():
811811
# AND the request matches this custom preflight route
812812
app = ApiGatewayResolver(cors=CORSConfig())
813813

814-
@app.route(method="OPTIONS", rule="/some-call", cors=True)
814+
@app.route(method="OPTIONS", rule="/some-call")
815815
def custom_preflight():
816816
return Response(
817817
status_code=200,
@@ -820,7 +820,7 @@ def custom_preflight():
820820
headers={"Access-Control-Allow-Methods": ["CUSTOM"]},
821821
)
822822

823-
@app.route(method="CUSTOM", rule="/some-call", cors=True)
823+
@app.route(method="CUSTOM", rule="/some-call")
824824
def custom_method(): ...
825825

826826
# AND the request includes an origin
@@ -903,7 +903,7 @@ def internal_server_error():
903903
assert result["body"] == json_dump(expected)
904904

905905
# GIVEN an ServiceError with a custom status code
906-
@app.get(rule="/service-error", cors=True)
906+
@app.get(rule="/service-error")
907907
def service_error():
908908
raise ServiceError(502, "Something went wrong!")
909909

@@ -964,7 +964,8 @@ def raises_error():
964964
def test_powertools_dev_sets_debug_mode(monkeypatch):
965965
# GIVEN a debug mode environment variable is set
966966
monkeypatch.setenv(constants.POWERTOOLS_DEV_ENV, "true")
967-
app = ApiGatewayResolver()
967+
with pytest.warns(UserWarning, match="POWERTOOLS_DEV environment variable is enabled."):
968+
app = ApiGatewayResolver()
968969

969970
# WHEN calling app._debug
970971
# THEN the debug mode is enabled
@@ -1428,7 +1429,8 @@ def get_func():
14281429
def get_func_another_duplicate():
14291430
raise RuntimeError()
14301431

1431-
app.include_router(router)
1432+
with pytest.warns(UserWarning, match="A route like this was already registered"):
1433+
app.include_router(router)
14321434

14331435
# WHEN calling the handler
14341436
result = app(LOAD_GW_EVENT, None)
@@ -1707,7 +1709,12 @@ def my_path():
17071709
@event_source(data_class=APIGatewayProxyEventV2)
17081710
def handler(event: APIGatewayProxyEventV2, context):
17091711
assert isinstance(event, APIGatewayProxyEventV2)
1710-
return app.resolve(event, context)
1712+
1713+
with pytest.warns(
1714+
UserWarning,
1715+
match="You don't need to serialize event to Event Source Data Class when using Event Handler",
1716+
):
1717+
return app.resolve(event, context)
17111718

17121719
# THEN
17131720
result = handler(load_event("apiGatewayProxyV2Event.json"), None)

0 commit comments

Comments
 (0)