Files
zulip/zerver/webhooks/slack/tests.py
Lauryn Menard 3a03e14938 integrations: Update Slack webhook to use check_send_webhook_message.
Due to the channel_map_to_topics URL parameter in the Slack webhook,
it was not migrated to use the check_send_webhook_message.

By using check_send_webhook_message, any topic parameter in the
webhook URL will be prioritized over mapping Slack channels to
topics, e.g. when channel_map_to_topics is true. This is because
the default behaviour for incoming webhooks is to send a default
topic as a parameter to check_send_webhook_message in case there
is no topic specified in the URL.

In contrast, we can override the stream passed in the URL when
channel_map_to_topics is false by passing the Slack channel name
to check_send_webhook_message. The default behaviour for incoming
webhooks is to send a direct message if there is no specified
stream in the URL, so a default stream is not generally passed
to check_send_webhook_message.

Fixes #27601.
2024-02-25 16:47:34 -08:00

100 lines
3.9 KiB
Python

from typing_extensions import override
from zerver.lib.test_classes import WebhookTestCase
EXPECTED_TOPIC = "Message from Slack"
EXPECTED_MESSAGE = "**slack_user**: test"
class SlackWebhookTests(WebhookTestCase):
STREAM_NAME = "slack"
URL_TEMPLATE = "/api/v1/external/slack?stream={stream}&api_key={api_key}"
WEBHOOK_DIR_NAME = "slack"
def test_slack_only_stream_parameter(self) -> None:
self.check_webhook(
"message_info",
EXPECTED_TOPIC,
EXPECTED_MESSAGE,
content_type="application/x-www-form-urlencoded",
)
def test_slack_with_user_specified_topic(self) -> None:
self.url = self.build_webhook_url(topic="test")
expected_topic_name = "test"
self.check_webhook(
"message_info",
expected_topic_name,
EXPECTED_MESSAGE,
content_type="application/x-www-form-urlencoded",
)
def test_slack_channels_map_to_topics_true(self) -> None:
self.url = self.build_webhook_url(channels_map_to_topics="1")
expected_topic_name = "channel: general"
self.check_webhook(
"message_info",
expected_topic_name,
EXPECTED_MESSAGE,
content_type="application/x-www-form-urlencoded",
)
def test_slack_channels_map_to_topics_true_and_user_specified_topic(self) -> None:
self.url = self.build_webhook_url(topic="test", channels_map_to_topics="1")
expected_topic_name = "test"
self.check_webhook(
"message_info",
expected_topic_name,
EXPECTED_MESSAGE,
content_type="application/x-www-form-urlencoded",
)
def test_slack_channels_map_to_topics_false(self) -> None:
self.STREAM_NAME = "general"
self.url = self.build_webhook_url(channels_map_to_topics="0")
self.check_webhook(
"message_info",
EXPECTED_TOPIC,
EXPECTED_MESSAGE,
content_type="application/x-www-form-urlencoded",
)
def test_slack_channels_map_to_topics_false_and_user_specified_topic(self) -> None:
self.STREAM_NAME = "general"
self.url = self.build_webhook_url(topic="test", channels_map_to_topics="0")
expected_topic_name = "test"
self.check_webhook(
"message_info",
expected_topic_name,
EXPECTED_MESSAGE,
content_type="application/x-www-form-urlencoded",
)
def test_missing_data_user_name(self) -> None:
payload = self.get_body("message_info_missing_user_name")
url = self.build_webhook_url()
result = self.client_post(url, payload, content_type="application/x-www-form-urlencoded")
self.assert_json_error(result, "Missing 'user_name' argument")
def test_missing_data_channel_name(self) -> None:
payload = self.get_body("message_info_missing_channel_name")
url = self.build_webhook_url()
result = self.client_post(url, payload, content_type="application/x-www-form-urlencoded")
self.assert_json_error(result, "Missing 'channel_name' argument")
def test_missing_data_text(self) -> None:
payload = self.get_body("message_info_missing_text")
url = self.build_webhook_url()
result = self.client_post(url, payload, content_type="application/x-www-form-urlencoded")
self.assert_json_error(result, "Missing 'text' argument")
def test_invalid_channels_map_to_topics(self) -> None:
payload = self.get_body("message_info")
url = self.build_webhook_url(channels_map_to_topics="abc")
result = self.client_post(url, payload, content_type="application/x-www-form-urlencoded")
self.assert_json_error(result, "Error: channels_map_to_topics parameter other than 0 or 1")
@override
def get_body(self, fixture_name: str) -> str:
return self.webhook_fixture_data("slack", fixture_name, file_type="txt")