Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,15 @@ def resolve(
>>> return events.resolve(event, context)
"""

self._setup_context(event, context)

if self.current_event.info.operation == "PUBLISH":
response = self._publish_events(payload=self.current_event.events)
else:
response = self._subscribe_events()

self.clear_context()
try:
self._setup_context(event, context)

if self.current_event.info.operation == "PUBLISH":
response = self._publish_events(payload=self.current_event.events)
else:
response = self._subscribe_events()
finally:
self.clear_context()

return response

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import copy

import pytest

from aws_lambda_powertools.event_handler import AppSyncEventsResolver
Expand Down Expand Up @@ -100,3 +102,75 @@ def handle_events(payload):
# THEN the exception must propagate to the caller
with pytest.raises(UnauthorizedException):
app.resolve(mock_event, lambda_context)


def test_publish_clears_context_when_unauthorized_raised(lambda_context, mock_event):
# GIVEN an AppSyncEventsResolver that sets context on one event and denies the next
publish_event = copy.deepcopy(mock_event)
publish_event["info"]["operation"] = "PUBLISH"
publish_event["info"]["channel"]["path"] = "/default/test"
publish_event["events"] = [
{"id": "123", "payload": {"data": "test data"}},
{"id": "456", "payload": {"data": "more test data"}},
]

app = AppSyncEventsResolver()
calls = []

@app.on_publish(path="/default/*")
def handle_events(payload):
calls.append(payload)
if len(calls) == 1:
app.append_context(user_id="12345")
return {"processed": True}
if len(calls) == 2:
raise UnauthorizedException
return dict(app.context)

# WHEN the failing batch invocation runs
with pytest.raises(UnauthorizedException):
app.resolve(publish_event, lambda_context)

# THEN context is cleared and not leaked into the next invocation
assert app.context == {}

next_event = copy.deepcopy(mock_event)
next_event["info"]["operation"] = "PUBLISH"
next_event["info"]["channel"]["path"] = "/default/test"
next_event["events"] = [{"id": "789", "payload": {"data": "next invocation"}}]
next_result = app.resolve(next_event, lambda_context)
assert next_result == {"events": [{"id": "789", "payload": {}}]}


def test_subscribe_clears_context_when_unauthorized_raised(lambda_context, mock_event):
# GIVEN a subscribe event where the handler appends context before denying access
subscribe_event = copy.deepcopy(mock_event)
subscribe_event["info"]["operation"] = "SUBSCRIBE"
subscribe_event["info"]["channel"]["path"] = "/default/test"

app = AppSyncEventsResolver()

@app.on_subscribe(path="/default/*")
def handle_subscribe():
app.append_context(user_id="12345")
raise UnauthorizedException

# WHEN resolving raises UnauthorizedException
with pytest.raises(UnauthorizedException):
app.resolve(subscribe_event, lambda_context)

# THEN context must still be cleared
assert app.context == {}


def test_resolve_clears_context_when_event_setup_raises(lambda_context):
# GIVEN an AppSyncEventsResolver with pre-populated context
app = AppSyncEventsResolver()
app.append_context(user_id="12345")

# WHEN resolving an invalid event fails during event setup / inspection
with pytest.raises((AttributeError, KeyError, TypeError, ValueError)):
app.resolve({}, lambda_context)

# THEN pre-populated context must still be cleared
assert app.context == {}