2017-01-30 21:18:41 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from zerver.lib.test_classes import WebhookTestCase
|
|
|
|
|
|
|
|
class SlackWebhookTests(WebhookTestCase):
|
|
|
|
STREAM_NAME = 'slack'
|
|
|
|
URL_TEMPLATE = "/api/v1/external/slack?stream={stream}&api_key={api_key}"
|
|
|
|
FIXTURE_DIR_NAME = 'slack'
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_slack_channel_to_topic(self) -> None:
|
2017-01-30 21:18:41 +01:00
|
|
|
|
2018-11-09 20:33:58 +01:00
|
|
|
expected_topic = u"channel: general"
|
2017-01-30 21:18:41 +01:00
|
|
|
expected_message = u"**slack_user**: `test\n`"
|
2018-11-09 20:33:58 +01:00
|
|
|
self.send_and_test_stream_message('message_info', expected_topic, expected_message,
|
2017-01-30 21:18:41 +01:00
|
|
|
content_type="application/x-www-form-urlencoded")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_slack_channel_to_stream(self) -> None:
|
2017-01-30 21:18:41 +01:00
|
|
|
|
|
|
|
self.STREAM_NAME = 'general'
|
|
|
|
self.url = "{}{}".format(self.url, "&channels_map_to_topics=0")
|
2018-11-09 20:33:58 +01:00
|
|
|
expected_topic = u"Message from Slack"
|
2017-01-30 21:18:41 +01:00
|
|
|
expected_message = u"**slack_user**: `test\n`"
|
2018-11-09 20:33:58 +01:00
|
|
|
self.send_and_test_stream_message('message_info', expected_topic, expected_message,
|
2017-01-30 21:18:41 +01:00
|
|
|
content_type="application/x-www-form-urlencoded")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_missing_data_user_name(self) -> None:
|
2017-01-30 21:18:41 +01:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_missing_data_channel_name(self) -> None:
|
2017-01-30 21:18:41 +01:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_missing_data_text(self) -> None:
|
2017-01-30 21:18:41 +01:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def test_invalid_channels_map_to_topics(self) -> None:
|
2017-01-30 21:18:41 +01:00
|
|
|
|
|
|
|
payload = self.get_body('message_info')
|
|
|
|
url = "{}{}".format(self.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')
|
|
|
|
|
2017-11-09 08:59:34 +01:00
|
|
|
def get_body(self, fixture_name: str) -> str:
|
2017-01-30 21:18:41 +01:00
|
|
|
|
2018-04-20 03:57:21 +02:00
|
|
|
return self.webhook_fixture_data("slack", fixture_name, file_type="txt")
|