Skip to content

Commit 1e2f45f

Browse files
authored
fix(taskworker) Fix async notification parameter serialization (#91720)
RpcUser objects can contain frozenset() which needs to be downcast for JSON encoding.
1 parent 4fcdb00 commit 1e2f45f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/sentry/notifications/utils/tasks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def serialize_lazy_object_user(arg: SimpleLazyObject, key: str | None = None) ->
3131
for k, v in raw_data.items():
3232
if isinstance(v, datetime):
3333
v = v.isoformat()
34+
if isinstance(v, frozenset):
35+
v = list(v)
3436

3537
parsed_data[k] = v
3638

tests/sentry/notifications/utils/test_tasks.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import pytest
44
from django.contrib.auth.models import AnonymousUser
5+
from django.utils.functional import SimpleLazyObject
56

67
from sentry.notifications.class_manager import NotificationClassNotSetException, manager, register
78
from sentry.notifications.utils.tasks import _send_notification, async_send_notification
89
from sentry.testutils.cases import TestCase
910
from sentry.testutils.helpers.notifications import AnotherDummyNotification, DummyNotification
11+
from sentry.users.services.user.serial import serialize_generic_user
1012

1113

1214
class NotificationTaskTests(TestCase):
@@ -83,6 +85,32 @@ def test_call_task_with_anonymous_user(self, mock_delay):
8385
],
8486
)
8587

88+
@patch("sentry.notifications.utils.tasks._send_notification.delay")
89+
def test_call_task_with_lazy_object_user(self, mock_delay):
90+
register()(AnotherDummyNotification)
91+
92+
lazyuser = SimpleLazyObject(lambda: serialize_generic_user(self.user))
93+
async_send_notification(AnotherDummyNotification, user=lazyuser)
94+
rpc_user = serialize_generic_user(self.user)
95+
assert rpc_user
96+
expected_data = rpc_user.dict()
97+
expected_data["emails"] = list(expected_data["emails"])
98+
expected_data["useremails"] = list(expected_data["useremails"])
99+
expected_data["roles"] = list(expected_data["roles"])
100+
expected_data["permissions"] = list(expected_data["permissions"])
101+
expected_data["last_active"] = expected_data["last_active"].isoformat()
102+
103+
mock_delay.assert_called_with(
104+
"AnotherDummyNotification",
105+
[
106+
{
107+
"type": "lazyobjectrpcuser",
108+
"data": expected_data,
109+
"key": "user",
110+
},
111+
],
112+
)
113+
86114
@patch(
87115
"sentry.testutils.helpers.notifications.AnotherDummyNotification",
88116
)

0 commit comments

Comments
 (0)