Skip to content

Commit 8a5bcf5

Browse files
Adding subscribe test
1 parent 5f012ab commit 8a5bcf5

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed

aws_lambda_powertools/event_handler/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
Response,
1212
)
1313
from aws_lambda_powertools.event_handler.appsync import AppSyncResolver
14-
from aws_lambda_powertools.event_handler.appsync_events import AppSyncEventsResolver
1514
from aws_lambda_powertools.event_handler.bedrock_agent import BedrockAgentResolver
15+
from aws_lambda_powertools.event_handler.events_appsync.appsync_events import AppSyncEventsResolver
1616
from aws_lambda_powertools.event_handler.lambda_function_url import (
1717
LambdaFunctionUrlResolver,
1818
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from aws_lambda_powertools.event_handler.events_appsync.appsync_events import AppSyncEventsResolver
2+
3+
__all__ = [
4+
"AppSyncEventsResolver",
5+
]

aws_lambda_powertools/event_handler/appsync_events.py renamed to aws_lambda_powertools/event_handler/events_appsync/appsync_events.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from aws_lambda_powertools.event_handler.events_appsync.router import Router
99
from aws_lambda_powertools.utilities.data_classes.appsync_resolver_events_event import AppSyncResolverEventsEvent
1010
from aws_lambda_powertools.warnings import PowertoolsUserWarning
11+
from aws_lambda_powertools.event_handler.events_appsync.exceptions import UnauthorizedException
1112

1213
if TYPE_CHECKING:
1314
from collections.abc import Callable
@@ -153,6 +154,8 @@ def _subscribe_events(self) -> Any:
153154
if resolver:
154155
try:
155156
return resolver["func"]()
157+
except UnauthorizedException:
158+
raise
156159
except Exception as error:
157160
return {"error": self._format_error_response(error)}
158161

@@ -239,6 +242,8 @@ def _process_publish_event_sync_resolver(
239242
)
240243

241244
return {"events": response}
245+
except UnauthorizedException:
246+
raise
242247
except Exception as error:
243248
return {"error": self._format_error_response(error)}
244249

@@ -291,6 +296,8 @@ async def _call_publish_event_async_resolver(
291296
)
292297

293298
return {"events": response}
299+
except UnauthorizedException:
300+
raise
294301
except Exception as error:
295302
return {"error": self._format_error_response(error)}
296303

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from __future__ import annotations
2+
3+
4+
class UnauthorizedException(Exception):
5+
"""
6+
Error to be thrown to communicate the subscription is unauthorized.
7+
8+
When this error is raised, the client will receive a 40x error code
9+
and the subscription will be closed.
10+
11+
Attributes:
12+
message (str): The error message describing the unauthorized access.
13+
"""
14+
15+
def __init__(self, message: str | None = None, *args, **kwargs):
16+
"""
17+
Initialize the UnauthorizedException.
18+
19+
Args:
20+
message (str): A descriptive error message.
21+
*args: Variable positional arguments.
22+
**kwargs: Variable keyword arguments.
23+
"""
24+
super().__init__(message, *args, **kwargs)
25+
self.name = "UnauthorizedException"

tests/functional/event_handler/required_dependencies/appsync/test_appsync_events_resolvers.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from aws_lambda_powertools.event_handler import AppSyncEventsResolver
7+
from aws_lambda_powertools.event_handler.events_appsync.exceptions import UnauthorizedException
78
from aws_lambda_powertools.event_handler.events_appsync.router import Router
89
from aws_lambda_powertools.warnings import PowertoolsUserWarning
910
from tests.functional.utils import load_event
@@ -1571,3 +1572,40 @@ def test_handler():
15711572

15721573
# THEN we should get an error response
15731574
assert not result
1575+
1576+
def test_publish_events_throw_unauthorized_exception(lambda_context, mock_event):
1577+
"""Test handling events with an empty payload."""
1578+
# GIVEN a sample publish event with empty events
1579+
mock_event["info"]["operation"] = "PUBLISH"
1580+
mock_event["info"]["channel"]["path"] = "/default/test"
1581+
mock_event["events"] = [
1582+
{"id": "123", "payload": {"data": "test data"}},
1583+
]
1584+
1585+
# GIVEN an AppSyncEventsResolver
1586+
app = AppSyncEventsResolver()
1587+
1588+
@app.on_publish(path="/default/*", aggregate=True)
1589+
def handle_events(payload):
1590+
raise UnauthorizedException
1591+
1592+
# WHEN we resolve the event with unauthorized route
1593+
with pytest.raises(UnauthorizedException):
1594+
app.resolve(mock_event, lambda_context)
1595+
1596+
def test_subscribe_events_throw_unauthorized_exception(lambda_context, mock_event):
1597+
"""Test handling events with an empty payload."""
1598+
# GIVEN a sample publish event with empty events
1599+
mock_event["info"]["operation"] = "SUBSCRIBE"
1600+
mock_event["info"]["channel"]["path"] = "/default/test"
1601+
1602+
# GIVEN an AppSyncEventsResolver
1603+
app = AppSyncEventsResolver()
1604+
1605+
@app.on_subscribe(path="/default/*")
1606+
def handle_events():
1607+
raise UnauthorizedException
1608+
1609+
# WHEN we resolve the event with unauthorized route
1610+
with pytest.raises(UnauthorizedException):
1611+
app.resolve(mock_event, lambda_context)

0 commit comments

Comments
 (0)