2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
|
|
|
|
2017-01-30 21:18:41 +01:00
|
|
|
from zerver.lib.test_classes import WebhookTestCase
|
|
|
|
|
2024-02-15 12:25:57 +01:00
|
|
|
EXPECTED_TOPIC = "Message from Slack"
|
|
|
|
EXPECTED_MESSAGE = "**slack_user**: test"
|
|
|
|
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2017-01-30 21:18:41 +01:00
|
|
|
class SlackWebhookTests(WebhookTestCase):
|
2024-05-04 22:02:50 +02:00
|
|
|
CHANNEL_NAME = "slack"
|
2017-01-30 21:18:41 +01:00
|
|
|
URL_TEMPLATE = "/api/v1/external/slack?stream={stream}&api_key={api_key}"
|
2021-06-26 09:18:33 +02:00
|
|
|
WEBHOOK_DIR_NAME = "slack"
|
2017-01-30 21:18:41 +01:00
|
|
|
|
2024-02-15 12:25:57 +01:00
|
|
|
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")
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name = "channel: general"
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"message_info",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2024-02-15 12:25:57 +01:00
|
|
|
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:
|
2024-05-04 22:02:50 +02:00
|
|
|
self.CHANNEL_NAME = "general"
|
2024-02-15 12:25:57 +01:00
|
|
|
self.url = self.build_webhook_url(channels_map_to_topics="0")
|
|
|
|
self.check_webhook(
|
|
|
|
"message_info",
|
|
|
|
EXPECTED_TOPIC,
|
|
|
|
EXPECTED_MESSAGE,
|
2020-08-23 15:49:24 +02:00
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2017-01-30 21:18:41 +01:00
|
|
|
|
2024-02-15 12:25:57 +01:00
|
|
|
def test_slack_channels_map_to_topics_false_and_user_specified_topic(self) -> None:
|
2024-05-04 22:02:50 +02:00
|
|
|
self.CHANNEL_NAME = "general"
|
2024-02-15 12:25:57 +01:00
|
|
|
self.url = self.build_webhook_url(topic="test", channels_map_to_topics="0")
|
|
|
|
expected_topic_name = "test"
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"message_info",
|
2024-01-17 15:53:30 +01:00
|
|
|
expected_topic_name,
|
2024-02-15 12:25:57 +01:00
|
|
|
EXPECTED_MESSAGE,
|
2020-08-23 15:49:24 +02:00
|
|
|
content_type="application/x-www-form-urlencoded",
|
|
|
|
)
|
2017-01-30 21:18:41 +01:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_missing_data_user_name(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
payload = self.get_body("message_info_missing_user_name")
|
2017-01-30 21:18:41 +01:00
|
|
|
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")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_missing_data_channel_name(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
payload = self.get_body("message_info_missing_channel_name")
|
2017-01-30 21:18:41 +01:00
|
|
|
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")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_missing_data_text(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
payload = self.get_body("message_info_missing_text")
|
2017-01-30 21:18:41 +01:00
|
|
|
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")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_invalid_channels_map_to_topics(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
payload = self.get_body("message_info")
|
2024-02-15 12:25:57 +01:00
|
|
|
url = self.build_webhook_url(channels_map_to_topics="abc")
|
2017-01-30 21:18:41 +01:00
|
|
|
result = self.client_post(url, payload, content_type="application/x-www-form-urlencoded")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assert_json_error(result, "Error: channels_map_to_topics parameter other than 0 or 1")
|
2017-01-30 21:18:41 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-11-09 08:59:34 +01:00
|
|
|
def get_body(self, fixture_name: str) -> str:
|
2018-04-20 03:57:21 +02:00
|
|
|
return self.webhook_fixture_data("slack", fixture_name, file_type="txt")
|