zulip/zerver/tests/test_slack_message_conversi...

109 lines
4.8 KiB
Python
Raw Normal View History

from zerver.data_import.slack_message_conversion import (
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
from typing import Any, Dict, List, Tuple
class SlackMessageConversion(ZulipTestCase):
def assertEqual(self, first: Any, second: Any, msg: str="") -> None:
if isinstance(first, str) and isinstance(second, str):
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 = {}
with open(os.path.join(os.path.dirname(__file__),
'fixtures/slack_message_conversion.json')) as f:
data = ujson.load(f)
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 = {'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)
python: Convert assignment type annotations to Python 3.6 style. This commit was split by tabbott; this piece covers the vast majority of files in Zulip, but excludes scripts/, tools/, and puppet/ to help ensure we at least show the right error messages for Xenial systems. We can likely further refine the remaining pieces with some testing. Generated by com2ann, with whitespace fixes and various manual fixes for runtime issues: - invoiced_through: Optional[LicenseLedger] = models.ForeignKey( + invoiced_through: Optional["LicenseLedger"] = models.ForeignKey( -_apns_client: Optional[APNsClient] = None +_apns_client: Optional["APNsClient"] = None - notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE) - signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE) + notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE) + signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE) - author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE) + author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE) - bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL) + bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL) - default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE) - default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE) + default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE) + default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE) -descriptors_by_handler_id: Dict[int, ClientDescriptor] = {} +descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {} -worker_classes: Dict[str, Type[QueueProcessingWorker]] = {} -queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {} +worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {} +queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {} -AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None +AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
slack_user_map: Dict[str, int] = {}
users: List[Dict[str, Any]] = [{}]
channel_map: Dict[str, Tuple[str, int]] = {}
converted = convert_to_zulip_markdown(test['input'], users, channel_map, slack_user_map)
converted_text = converted[0]
with self.subTest(slack_message_conversion=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,
"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
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)
full_name = get_user_full_name(users[1])
self.assertEqual(full_name, 'John Doe')
self.assertEqual(get_user_full_name(users[2]), 'Jane')
self.assertEqual(text, 'Hi @**%s**: How are you? #**general**' % (full_name,))
self.assertEqual(mentioned_users, [540])
# multiple mentioning
message = 'Hi <@U08RGD1RD|john>: How are you?<@U0CBK5KAT> asked.'
text, mentioned_users, has_link = convert_to_zulip_markdown(message, users, channel_map, slack_user_map)
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?'
text, mentioned_users, has_link = convert_to_zulip_markdown(message, users, channel_map, slack_user_map)
self.assertEqual(text, message)
self.assertEqual(mentioned_users, [])
def test_has_link(self) -> None:
python: Convert assignment type annotations to Python 3.6 style. This commit was split by tabbott; this piece covers the vast majority of files in Zulip, but excludes scripts/, tools/, and puppet/ to help ensure we at least show the right error messages for Xenial systems. We can likely further refine the remaining pieces with some testing. Generated by com2ann, with whitespace fixes and various manual fixes for runtime issues: - invoiced_through: Optional[LicenseLedger] = models.ForeignKey( + invoiced_through: Optional["LicenseLedger"] = models.ForeignKey( -_apns_client: Optional[APNsClient] = None +_apns_client: Optional["APNsClient"] = None - notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE) - signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE) + notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE) + signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE) - author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE) + author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE) - bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL) + bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL) - default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE) - default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE) + default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE) + default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE) -descriptors_by_handler_id: Dict[int, ClientDescriptor] = {} +descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {} -worker_classes: Dict[str, Type[QueueProcessingWorker]] = {} -queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {} +worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {} +queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {} -AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None +AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
slack_user_map: Dict[str, int] = {}
message = '<http://journals.plos.org/plosone/article>'
text, mentioned_users, has_link = convert_to_zulip_markdown(message, [], {}, slack_user_map)
self.assertEqual(text, 'http://journals.plos.org/plosone/article')
self.assertEqual(has_link, True)
message = '<mailto:foo@foo.com>'
text, mentioned_users, has_link = convert_to_zulip_markdown(message, [], {}, slack_user_map)
self.assertEqual(text, 'mailto:foo@foo.com')
self.assertEqual(has_link, True)
message = 'random message'
text, mentioned_users, has_link = convert_to_zulip_markdown(message, [], {}, slack_user_map)
self.assertEqual(has_link, False)