2020-02-12 23:48:55 +01:00
|
|
|
from zerver.lib.test_classes import WebhookTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class SlackIncomingHookTests(WebhookTestCase):
|
2021-02-12 08:20:45 +01:00
|
|
|
STREAM_NAME = "slack_incoming"
|
2020-02-12 23:48:55 +01:00
|
|
|
URL_TEMPLATE = "/api/v1/external/slack_incoming?&api_key={api_key}&stream={stream}"
|
2021-02-12 08:20:45 +01:00
|
|
|
FIXTURE_DIR_NAME = "slack_incoming"
|
2020-02-12 23:48:55 +01:00
|
|
|
|
|
|
|
def test_message(self) -> None:
|
|
|
|
expected_topic = "(no topic)"
|
|
|
|
expected_message = """
|
|
|
|
Hello, world.
|
|
|
|
""".strip()
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
2021-02-12 08:19:30 +01:00
|
|
|
"text",
|
|
|
|
expected_topic,
|
|
|
|
expected_message,
|
2020-02-12 23:48:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_message_as_www_urlencoded(self) -> None:
|
|
|
|
expected_topic = "devops"
|
|
|
|
expected_message = """
|
|
|
|
:zap: chris has started deploying project tag v0.0.2rc10 to staging
|
|
|
|
""".strip()
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
|
|
|
"urlencoded_text",
|
2020-02-12 23:48:55 +01:00
|
|
|
expected_topic,
|
|
|
|
expected_message,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
content_type="application/x-www-form-urlencoded",
|
2020-02-12 23:48:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_message_with_actions(self) -> None:
|
|
|
|
expected_topic = "C1H9RESGL"
|
|
|
|
expected_message = """
|
2021-06-24 16:44:49 +02:00
|
|
|
Danny Torrence left the following *review* for your property:
|
2020-02-12 23:48:55 +01:00
|
|
|
|
|
|
|
[Overlook Hotel](https://google.com) \n :star: \n Doors had too many axe holes, guest in room 237 was far too rowdy, whole place felt stuck in the 1920s.
|
|
|
|
[Haunted hotel image](https://is5-ssl.mzstatic.com/image/thumb/Purple3/v4/d3/72/5c/d3725c8f-c642-5d69-1904-aa36e4297885/source/256x256bb.jpg)
|
|
|
|
""".strip()
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
2021-02-12 08:19:30 +01:00
|
|
|
"actions",
|
|
|
|
expected_topic,
|
|
|
|
expected_message,
|
2020-02-12 23:48:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_message_with_blocks(self) -> None:
|
|
|
|
expected_topic = "(no topic)"
|
|
|
|
expected_message = """
|
|
|
|
Danny Torrence left the following review for your property:
|
|
|
|
|
|
|
|
[Overlook Hotel](https://example.com) \n :star: \n Doors had too many axe holes, guest in room 237 was far too rowdy, whole place felt stuck in the 1920s.
|
|
|
|
[Haunted hotel image](https://is5-ssl.mzstatic.com/image/thumb/Purple3/v4/d3/72/5c/d3725c8f-c642-5d69-1904-aa36e4297885/source/256x256bb.jpg)
|
|
|
|
""".strip()
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
2021-02-12 08:19:30 +01:00
|
|
|
"blocks",
|
|
|
|
expected_topic,
|
|
|
|
expected_message,
|
2020-02-12 23:48:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_message_with_attachment(self) -> None:
|
|
|
|
expected_topic = "prometheus-alerts"
|
|
|
|
expected_message = """
|
|
|
|
[[FIRING:2] InstanceDown for api-server (env="prod", severity="critical")](https://alertmanager.local//#/alerts?receiver=default)
|
|
|
|
:chart_with_upwards_trend: **[Graph](http://generator.local/1)** :notebook: **[Runbook](https://runbook.local/1)**
|
|
|
|
|
|
|
|
**Alert details**:
|
|
|
|
**Alert:** api-server down - `critical`
|
|
|
|
**Description:** api-server at 1.2.3.4:8080 couldn't be scraped **Details:**
|
|
|
|
• **alertname:** `InstanceDown`
|
|
|
|
• **env:** `prod`
|
|
|
|
• **instance:** `1.2.3.4:8080`
|
|
|
|
• **job:** `api-server`
|
|
|
|
• **severity:** `critical`
|
|
|
|
|
|
|
|
**Alert:** api-server down - `critical`
|
|
|
|
**Description:** api-server at 1.2.3.4:8081 couldn't be scraped **Details:**
|
|
|
|
• **alertname:** `InstanceDown`
|
|
|
|
• **env:** `prod`
|
|
|
|
• **instance:** `1.2.3.4:8081`
|
|
|
|
• **job:** `api-server`
|
|
|
|
• **severity:** `critical`
|
|
|
|
""".strip()
|
|
|
|
|
2020-08-23 15:49:24 +02:00
|
|
|
self.check_webhook(
|
2021-02-12 08:19:30 +01:00
|
|
|
"attachment",
|
|
|
|
expected_topic,
|
|
|
|
expected_message,
|
2020-02-12 23:48:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def get_body(self, fixture_name: str) -> str:
|
|
|
|
if "urlencoded" in fixture_name:
|
|
|
|
file_type = "txt"
|
|
|
|
else:
|
|
|
|
file_type = "json"
|
|
|
|
return self.webhook_fixture_data("slack_incoming", fixture_name, file_type=file_type)
|