Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

webhooks: Return message ID from check_send_webhook_message.

george0fTheJungle22ffe2fdf026
diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py
index ba00671f79d..000018891fd 100644
--- a/zerver/lib/test_classes.py
+++ b/zerver/lib/test_classes.py
@@ -2485,7 +2485,7 @@ class WebhookTestCase(ZulipTestCase):
if all_event_types is None:
return # nocoverage
- def side_effect(*args: Any, **kwargs: Any) -> None:
+ def side_effect(*args: Any, **kwargs: Any) -> int | None:
complete_event_type = (
kwargs.get("complete_event_type")
if len(args) < 5
@@ -2505,7 +2505,7 @@ self-documenting the supported event types for this integration.
You can fix this by adding "{complete_event_type}" to ALL_EVENT_TYPES for this webhook.
""".strip()
)
- check_send_webhook_message(*args, **kwargs)
+ return check_send_webhook_message(*args, **kwargs)
self.patch = mock.patch(
f"zerver.webhooks.{self.WEBHOOK_DIR_NAME}.view.check_send_webhook_message",
diff --git a/zerver/lib/webhooks/common.py b/zerver/lib/webhooks/common.py
index aa528f3e43f..5cc22d0b317 100644
--- a/zerver/lib/webhooks/common.py
+++ b/zerver/lib/webhooks/common.py
@@ -150,7 +150,7 @@ def check_send_webhook_message(
exclude_events: Json[list[str]] | None = None,
unquote_url_parameters: bool = False,
no_previews: bool = False,
-) -> None:
+) -> int | None:
if complete_event_type is not None and (
# Here, we implement Zulip's generic support for filtering
# events sent by the third-party service.
@@ -171,13 +171,13 @@ def check_send_webhook_message(
and any(fnmatch.fnmatch(complete_event_type, pattern) for pattern in exclude_events)
)
):
- return
+ return None
client = RequestNotes.get_notes(request).client
assert client is not None
if stream is None:
assert user_profile.bot_owner is not None
- check_send_private_message(
+ return check_send_private_message(
user_profile, client, user_profile.bot_owner, body, no_previews=no_previews
)
else:
@@ -195,11 +195,11 @@ def check_send_webhook_message(
try:
if stream.isdecimal():
- check_send_stream_message_by_id(
+ return check_send_stream_message_by_id(
user_profile, client, int(stream), topic, body, no_previews=no_previews
)
else:
- check_send_stream_message(
+ return check_send_stream_message(
user_profile, client, stream, topic, body, no_previews=no_previews
)
except StreamDoesNotExistError:
@@ -207,7 +207,7 @@ def check_send_webhook_message(
# notifying that the webhook bot just tried to send a message to a
# non-existent stream, so we don't need to re-raise it since it
# clutters up webhook-errors.log
- pass
+ return None
def standardize_headers(input_headers: None | dict[str, Any]) -> dict[str, str]:
diff --git a/zerver/tests/test_webhooks_common.py b/zerver/tests/test_webhooks_common.py
index a9bc8a69245..48e28d1c49d 100644
--- a/zerver/tests/test_webhooks_common.py
+++ b/zerver/tests/test_webhooks_common.py
@@ -12,6 +12,7 @@ from typing_extensions import override
from zerver.actions.streams import do_rename_stream
from zerver.decorator import webhook_view
from zerver.lib.exceptions import InvalidJSONError, JsonableError
+from zerver.lib.request import RequestNotes
from zerver.lib.send_email import FromAddress
from zerver.lib.test_classes import WebhookTestCase, ZulipTestCase
from zerver.lib.test_helpers import HostRequestMock
@@ -19,12 +20,13 @@ from zerver.lib.webhooks.common import (
INVALID_JSON_MESSAGE,
MISSING_EVENT_HEADER_MESSAGE,
MissingHTTPEventHeaderError,
+ check_send_webhook_message,
get_fixture_http_headers,
standardize_headers,
validate_extract_webhook_http_header,
validate_webhook_signature,
)
-from zerver.models import UserProfile
+from zerver.models import Client, Message, UserProfile
from zerver.models.realms import get_realm
from zerver.models.users import get_user
@@ -176,6 +178,29 @@ class WebhooksCommonTestCase(ZulipTestCase):
):
validate_webhook_signature(request, payload, signature)
+ def test_check_send_webhook_message_returns_id(self) -> None:
+ webhook_bot = get_user("[email protected]", get_realm("zulip"))
+ stream = self.make_stream("test_stream")
+ self.subscribe(webhook_bot, stream.name)
+
+ request = HostRequestMock()
+ request.user = webhook_bot
+ client = Client.objects.get_or_create(name="TestClient")[0]
+ RequestNotes.get_notes(request).client = client
+
+ message_id = check_send_webhook_message(
+ request,
+ webhook_bot,
+ "Test topic",
+ "Test message content",
+ stream=stream.name,
+ )
+
+ self.assertIsInstance(message_id, int)
+ assert message_id is not None
+ msg = Message.objects.get(id=message_id)
+ self.assertEqual(msg.topic_name(), "Test topic")
+
class WebhookURLConfigurationTestCase(WebhookTestCase):
CHANNEL_NAME = "helloworld"