2020-06-11 00:54:34 +02:00
|
|
|
import os
|
2024-07-12 02:30:17 +02:00
|
|
|
from typing import Any
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2018-08-01 00:18:04 +02:00
|
|
|
from zerver.data_import.slack_message_conversion import (
|
2018-01-15 13:29:33 +01:00
|
|
|
convert_to_zulip_markdown,
|
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
|
|
|
get_user_full_name,
|
2018-01-15 13:29:33 +01:00
|
|
|
)
|
|
|
|
from zerver.lib import mdiff
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2018-01-15 13:29:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SlackMessageConversion(ZulipTestCase):
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-02-12 08:19:30 +01:00
|
|
|
def assertEqual(self, first: Any, second: Any, msg: str = "") -> None:
|
2018-05-10 19:00:29 +02:00
|
|
|
if isinstance(first, str) and isinstance(second, str):
|
2018-01-15 13:29:33 +01:00
|
|
|
if first != second:
|
2021-02-12 08:19:30 +01:00
|
|
|
raise AssertionError(
|
|
|
|
"Actual and expected outputs do not match; showing diff.\n"
|
|
|
|
+ mdiff.diff_strings(first, second)
|
|
|
|
+ msg
|
|
|
|
)
|
2018-01-15 13:29:33 +01:00
|
|
|
else:
|
|
|
|
super().assertEqual(first, second)
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def load_slack_message_conversion_tests(self) -> dict[Any, Any]:
|
2018-01-15 13:29:33 +01:00
|
|
|
test_fixtures = {}
|
2021-02-12 08:19:30 +01:00
|
|
|
with open(
|
2021-02-12 08:20:45 +01:00
|
|
|
os.path.join(os.path.dirname(__file__), "fixtures/slack_message_conversion.json"), "rb"
|
2021-02-12 08:19:30 +01:00
|
|
|
) as f:
|
2020-08-07 01:09:47 +02:00
|
|
|
data = orjson.loads(f.read())
|
2021-02-12 08:20:45 +01:00
|
|
|
for test in data["regular_tests"]:
|
|
|
|
test_fixtures[test["name"]] = test
|
2018-01-15 13:29:33 +01:00
|
|
|
|
|
|
|
return test_fixtures
|
|
|
|
|
|
|
|
def test_message_conversion_fixtures(self) -> None:
|
|
|
|
format_tests = self.load_slack_message_conversion_tests()
|
2021-02-12 08:20:45 +01:00
|
|
|
valid_keys = {"name", "input", "conversion_output"}
|
2018-01-15 13:29:33 +01:00
|
|
|
|
|
|
|
for name, test in format_tests.items():
|
|
|
|
# Check that there aren't any unexpected keys as those are often typos
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(set(test.keys()) - valid_keys, 0)
|
2024-07-12 02:30:17 +02:00
|
|
|
slack_user_map: dict[str, int] = {}
|
|
|
|
users: list[dict[str, Any]] = [{}]
|
|
|
|
channel_map: dict[str, tuple[str, int]] = {}
|
2021-02-12 08:20:45 +01:00
|
|
|
converted = convert_to_zulip_markdown(test["input"], users, channel_map, slack_user_map)
|
2018-01-15 13:29:33 +01:00
|
|
|
converted_text = converted[0]
|
2019-09-13 08:11:25 +02:00
|
|
|
with self.subTest(slack_message_conversion=name):
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(converted_text, test["conversion_output"])
|
2018-01-15 13:29:33 +01:00
|
|
|
|
|
|
|
def test_mentioned_data(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
slack_user_map = {"U08RGD1RD": 540, "U0CBK5KAT": 554, "U09TYF5SK": 571}
|
2018-01-15 13:29:33 +01:00
|
|
|
# For this test, only relevant keys are 'id', 'name', 'deleted'
|
|
|
|
# and 'real_name'
|
2021-02-12 08:19:30 +01:00
|
|
|
users = [
|
|
|
|
{
|
|
|
|
"id": "U0CBK5KAT",
|
|
|
|
"name": "aaron.anzalone",
|
|
|
|
"deleted": False,
|
|
|
|
"is_mirror_dummy": False,
|
|
|
|
"real_name": "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "U08RGD1RD",
|
|
|
|
"name": "john",
|
|
|
|
"deleted": False,
|
|
|
|
"is_mirror_dummy": False,
|
|
|
|
"real_name": "John Doe",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "U09TYF5Sk",
|
|
|
|
"name": "Jane",
|
|
|
|
"is_mirror_dummy": False,
|
|
|
|
"deleted": True, # Deleted users don't have 'real_name' key in Slack
|
|
|
|
},
|
|
|
|
]
|
2021-02-12 08:20:45 +01:00
|
|
|
channel_map = {"general": ("C5Z73A7RA", 137)}
|
|
|
|
message = "Hi <@U08RGD1RD|john>: How are you? <#C5Z73A7RA|general>"
|
2021-02-12 08:19:30 +01:00
|
|
|
text, mentioned_users, has_link = convert_to_zulip_markdown(
|
|
|
|
message, users, channel_map, slack_user_map
|
|
|
|
)
|
2018-01-15 13:29:33 +01:00
|
|
|
full_name = get_user_full_name(users[1])
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(full_name, "John Doe")
|
|
|
|
self.assertEqual(get_user_full_name(users[2]), "Jane")
|
2018-01-15 13:29:33 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(text, f"Hi @**{full_name}**: How are you? #**general**")
|
2018-01-15 13:29:33 +01:00
|
|
|
self.assertEqual(mentioned_users, [540])
|
|
|
|
|
|
|
|
# multiple mentioning
|
2021-02-12 08:20:45 +01:00
|
|
|
message = "Hi <@U08RGD1RD|john>: How are you?<@U0CBK5KAT> asked."
|
2021-02-12 08:19:30 +01:00
|
|
|
text, mentioned_users, has_link = convert_to_zulip_markdown(
|
|
|
|
message, users, channel_map, slack_user_map
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(text, "Hi @**John Doe**: How are you?@**aaron.anzalone** asked.")
|
2018-01-15 13:29:33 +01:00
|
|
|
self.assertEqual(mentioned_users, [540, 554])
|
|
|
|
|
|
|
|
# Check wrong mentioning
|
2021-02-12 08:20:45 +01:00
|
|
|
message = "Hi <@U08RGD1RD|jon>: How are you?"
|
2021-02-12 08:19:30 +01:00
|
|
|
text, mentioned_users, has_link = convert_to_zulip_markdown(
|
|
|
|
message, users, channel_map, slack_user_map
|
|
|
|
)
|
2018-01-15 13:29:33 +01:00
|
|
|
self.assertEqual(text, message)
|
|
|
|
self.assertEqual(mentioned_users, [])
|
2018-01-28 14:37:35 +01:00
|
|
|
|
|
|
|
def test_has_link(self) -> None:
|
2024-07-12 02:30:17 +02:00
|
|
|
slack_user_map: dict[str, int] = {}
|
2018-01-28 14:37:35 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
message = "<http://journals.plos.org/plosone/article>"
|
2018-04-07 00:27:48 +02:00
|
|
|
text, mentioned_users, has_link = convert_to_zulip_markdown(message, [], {}, slack_user_map)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(text, "http://journals.plos.org/plosone/article")
|
2018-01-28 14:37:35 +01:00
|
|
|
self.assertEqual(has_link, True)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
message = "<mailto:foo@foo.com>"
|
2018-04-07 00:27:48 +02:00
|
|
|
text, mentioned_users, has_link = convert_to_zulip_markdown(message, [], {}, slack_user_map)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(text, "mailto:foo@foo.com")
|
2018-01-28 14:37:35 +01:00
|
|
|
self.assertEqual(has_link, True)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
message = "random message"
|
2018-04-07 00:27:48 +02:00
|
|
|
text, mentioned_users, has_link = convert_to_zulip_markdown(message, [], {}, slack_user_map)
|
2018-01-28 14:37:35 +01:00
|
|
|
self.assertEqual(has_link, False)
|