|
| 1 | +import pytest |
| 2 | + |
| 3 | +from aws_lambda_powertools.event_handler import AppSyncEventsResolver |
| 4 | +from aws_lambda_powertools.event_handler.events_appsync.exceptions import UnauthorizedException |
| 5 | +from tests.functional.utils import load_event |
| 6 | + |
| 7 | + |
| 8 | +class LambdaContext: |
| 9 | + def __init__(self): |
| 10 | + self.function_name = "test-func" |
| 11 | + self.memory_limit_in_mb = 128 |
| 12 | + self.invoked_function_arn = "arn:aws:lambda:eu-west-1:809313241234:function:test-func" |
| 13 | + self.aws_request_id = "52fdfc07-2182-154f-163f-5f0f9a621d72" |
| 14 | + |
| 15 | + def get_remaining_time_in_millis(self) -> int: |
| 16 | + return 1000 |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture(scope="module") |
| 20 | +def lambda_context() -> LambdaContext: |
| 21 | + return LambdaContext() |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="module") |
| 25 | +def mock_event(): |
| 26 | + return load_event("appSyncEventsEvent.json") |
| 27 | + |
| 28 | + |
| 29 | +def test_publish_events_throw_unauthorized_exception_without_aggregate(lambda_context, mock_event): |
| 30 | + """UnauthorizedException raised in a per-event resolver must not be swallowed. |
| 31 | +
|
| 32 | + When aggregate=False, each event is handled independently and any other exception is |
| 33 | + turned into a per-event error payload. Authorization failures are the exception: they |
| 34 | + must fail the whole request, otherwise a denied caller receives a successful response. |
| 35 | + """ |
| 36 | + # GIVEN a publish event with a single event |
| 37 | + mock_event["info"]["operation"] = "PUBLISH" |
| 38 | + mock_event["info"]["channel"]["path"] = "/default/test" |
| 39 | + mock_event["events"] = [ |
| 40 | + {"id": "123", "payload": {"data": "test data"}}, |
| 41 | + ] |
| 42 | + |
| 43 | + # GIVEN an AppSyncEventsResolver with a per-event resolver that denies access |
| 44 | + app = AppSyncEventsResolver() |
| 45 | + |
| 46 | + @app.on_publish(path="/default/*") |
| 47 | + def handle_events(payload): |
| 48 | + raise UnauthorizedException |
| 49 | + |
| 50 | + # WHEN we resolve the event |
| 51 | + # THEN the exception must propagate to the caller |
| 52 | + with pytest.raises(UnauthorizedException): |
| 53 | + app.resolve(mock_event, lambda_context) |
| 54 | + |
| 55 | + |
| 56 | +def test_async_publish_events_throw_unauthorized_exception_without_aggregate(lambda_context, mock_event): |
| 57 | + """UnauthorizedException raised in an async per-event resolver must not be swallowed.""" |
| 58 | + # GIVEN a publish event with a single event |
| 59 | + mock_event["info"]["operation"] = "PUBLISH" |
| 60 | + mock_event["info"]["channel"]["path"] = "/default/test" |
| 61 | + mock_event["events"] = [ |
| 62 | + {"id": "123", "payload": {"data": "test data"}}, |
| 63 | + ] |
| 64 | + |
| 65 | + # GIVEN an AppSyncEventsResolver with an async per-event resolver that denies access |
| 66 | + app = AppSyncEventsResolver() |
| 67 | + |
| 68 | + @app.async_on_publish(path="/default/*") |
| 69 | + async def handle_events(payload): |
| 70 | + raise UnauthorizedException |
| 71 | + |
| 72 | + # WHEN we resolve the event |
| 73 | + # THEN the exception must propagate to the caller |
| 74 | + with pytest.raises(UnauthorizedException): |
| 75 | + app.resolve(mock_event, lambda_context) |
| 76 | + |
| 77 | + |
| 78 | +def test_publish_events_throw_unauthorized_exception_with_multiple_events(lambda_context, mock_event): |
| 79 | + """A single denied event must fail the whole batch, even when other events succeed.""" |
| 80 | + # GIVEN a publish event with two events |
| 81 | + mock_event["info"]["operation"] = "PUBLISH" |
| 82 | + mock_event["info"]["channel"]["path"] = "/default/test" |
| 83 | + mock_event["events"] = [ |
| 84 | + {"id": "123", "payload": {"data": "test data"}}, |
| 85 | + {"id": "456", "payload": {"data": "more test data"}}, |
| 86 | + ] |
| 87 | + |
| 88 | + # GIVEN an AppSyncEventsResolver that allows the first event and denies the second one |
| 89 | + app = AppSyncEventsResolver() |
| 90 | + calls = [] |
| 91 | + |
| 92 | + @app.on_publish(path="/default/*") |
| 93 | + def handle_events(payload): |
| 94 | + calls.append(payload) |
| 95 | + if len(calls) == 2: |
| 96 | + raise UnauthorizedException |
| 97 | + return {"processed": True} |
| 98 | + |
| 99 | + # WHEN we resolve the event |
| 100 | + # THEN the exception must propagate to the caller |
| 101 | + with pytest.raises(UnauthorizedException): |
| 102 | + app.resolve(mock_event, lambda_context) |
0 commit comments