2018-01-15 13:29:33 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
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,
|
|
|
|
get_user_full_name
|
|
|
|
)
|
|
|
|
from zerver.lib.test_classes import (
|
|
|
|
ZulipTestCase,
|
|
|
|
)
|
|
|
|
from zerver.lib.test_runner import slow
|
|
|
|
from zerver.lib import mdiff
|
|
|
|
import ujson
|
|
|
|
|
|
|
|
import os
|
2019-02-02 23:53:44 +01:00
|
|
|
from typing import Any, Dict, List, Tuple
|
2018-01-15 13:29:33 +01:00
|
|
|
|
|
|
|
class SlackMessageConversion(ZulipTestCase):
|
2018-05-10 19:00:29 +02:00
|
|
|
def assertEqual(self, first: Any, second: Any, msg: str="") -> None:
|
|
|
|
if isinstance(first, str) and isinstance(second, str):
|
2018-01-15 13:29:33 +01:00
|
|
|
if first != second:
|
|
|
|
raise AssertionError("Actual and expected outputs do not match; showing diff.\n" +
|
|
|
|
mdiff.diff_strings(first, second) + msg)
|
|
|
|
else:
|
|
|
|
super().assertEqual(first, second)
|
|
|
|
|
|
|
|
def load_slack_message_conversion_tests(self) -> Dict[Any, Any]:
|
|
|
|
test_fixtures = {}
|
2019-07-14 21:37:08 +02:00
|
|
|
with open(os.path.join(os.path.dirname(__file__),
|
|
|
|
'fixtures/slack_message_conversion.json'), 'r') as f:
|
|
|
|
data = ujson.loads('\n'.join(f.readlines()))
|
2018-01-15 13:29:33 +01:00
|
|
|
for test in data['regular_tests']:
|
|
|
|
test_fixtures[test['name']] = test
|
|
|
|
|
|
|
|
return test_fixtures
|
|
|
|
|
|
|
|
@slow("Aggregate of runs of individual slack message conversion tests")
|
|
|
|
def test_message_conversion_fixtures(self) -> None:
|
|
|
|
format_tests = self.load_slack_message_conversion_tests()
|
|
|
|
valid_keys = set(['name', "input", "conversion_output"])
|
|
|
|
|
|
|
|
for name, test in format_tests.items():
|
|
|
|
# Check that there aren't any unexpected keys as those are often typos
|
|
|
|
self.assertEqual(len(set(test.keys()) - valid_keys), 0)
|
|
|
|
slack_user_map = {} # type: Dict[str, int]
|
|
|
|
users = [{}] # type: List[Dict[str, Any]]
|
2018-04-07 00:27:48 +02:00
|
|
|
channel_map = {} # type: Dict[str, Tuple[str, int]]
|
|
|
|
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]
|
|
|
|
print("Running Slack Message Conversion test: %s" % (name,))
|
|
|
|
self.assertEqual(converted_text, test['conversion_output'])
|
|
|
|
|
|
|
|
def test_mentioned_data(self) -> None:
|
|
|
|
slack_user_map = {'U08RGD1RD': 540,
|
|
|
|
'U0CBK5KAT': 554,
|
|
|
|
'U09TYF5SK': 571}
|
|
|
|
# For this test, only relevant keys are 'id', 'name', 'deleted'
|
|
|
|
# and 'real_name'
|
|
|
|
users = [{"id": "U0CBK5KAT",
|
|
|
|
"name": "aaron.anzalone",
|
|
|
|
"deleted": False,
|
|
|
|
"real_name": ""},
|
|
|
|
{"id": "U08RGD1RD",
|
|
|
|
"name": "john",
|
|
|
|
"deleted": False,
|
|
|
|
"real_name": "John Doe"},
|
|
|
|
{"id": "U09TYF5Sk",
|
|
|
|
"name": "Jane",
|
|
|
|
"deleted": True}] # Deleted users don't have 'real_name' key in Slack
|
2018-04-07 00:27:48 +02:00
|
|
|
channel_map = {'general': ('C5Z73A7RA', 137)}
|
|
|
|
message = 'Hi <@U08RGD1RD|john>: How are you? <#C5Z73A7RA|general>'
|
|
|
|
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])
|
|
|
|
self.assertEqual(full_name, 'John Doe')
|
|
|
|
self.assertEqual(get_user_full_name(users[2]), 'Jane')
|
|
|
|
|
2019-04-20 01:00:46 +02:00
|
|
|
self.assertEqual(text, 'Hi @**%s**: How are you? #**general**' % (full_name,))
|
2018-01-15 13:29:33 +01:00
|
|
|
self.assertEqual(mentioned_users, [540])
|
|
|
|
|
|
|
|
# multiple mentioning
|
|
|
|
message = 'Hi <@U08RGD1RD|john>: How are you?<@U0CBK5KAT> asked.'
|
2018-04-07 00:27:48 +02: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, 'Hi @**%s**: How are you?@**%s** asked.' %
|
|
|
|
('John Doe', 'aaron.anzalone'))
|
|
|
|
self.assertEqual(mentioned_users, [540, 554])
|
|
|
|
|
|
|
|
# Check wrong mentioning
|
|
|
|
message = 'Hi <@U08RGD1RD|jon>: How are you?'
|
2018-04-07 00:27:48 +02: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:
|
|
|
|
slack_user_map = {} # type: Dict[str, int]
|
|
|
|
|
|
|
|
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)
|
2018-01-28 14:37:35 +01:00
|
|
|
self.assertEqual(text, 'http://journals.plos.org/plosone/article')
|
|
|
|
self.assertEqual(has_link, True)
|
|
|
|
|
|
|
|
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)
|
2018-01-28 14:37:35 +01:00
|
|
|
self.assertEqual(text, 'mailto:foo@foo.com')
|
|
|
|
self.assertEqual(has_link, True)
|
|
|
|
|
|
|
|
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)
|