|
2 | 2 |
|
3 | 3 | import pytest
|
4 | 4 | from django.contrib.auth.models import AnonymousUser
|
| 5 | +from django.utils.functional import SimpleLazyObject |
5 | 6 |
|
6 | 7 | from sentry.notifications.class_manager import NotificationClassNotSetException, manager, register
|
7 | 8 | from sentry.notifications.utils.tasks import _send_notification, async_send_notification
|
8 | 9 | from sentry.testutils.cases import TestCase
|
9 | 10 | from sentry.testutils.helpers.notifications import AnotherDummyNotification, DummyNotification
|
| 11 | +from sentry.users.services.user.serial import serialize_generic_user |
10 | 12 |
|
11 | 13 |
|
12 | 14 | class NotificationTaskTests(TestCase):
|
@@ -83,6 +85,32 @@ def test_call_task_with_anonymous_user(self, mock_delay):
|
83 | 85 | ],
|
84 | 86 | )
|
85 | 87 |
|
| 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 | + |
86 | 114 | @patch(
|
87 | 115 | "sentry.testutils.helpers.notifications.AnotherDummyNotification",
|
88 | 116 | )
|
|
0 commit comments