2018-06-01 17:39:56 +02:00
|
|
|
import os
|
2019-05-21 12:21:32 +02:00
|
|
|
from typing import Any
|
2020-06-11 00:54:34 +02:00
|
|
|
from unittest import mock
|
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2020-06-11 00:54:34 +02:00
|
|
|
|
|
|
|
from zerver.data_import.gitter import do_convert_data, get_usermentions
|
|
|
|
from zerver.lib.import_realm import do_import_realm
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.models import Message, UserProfile, get_realm
|
|
|
|
|
2018-06-01 17:39:56 +02:00
|
|
|
|
|
|
|
class GitterImporter(ZulipTestCase):
|
2021-02-12 08:20:45 +01:00
|
|
|
@mock.patch("zerver.data_import.gitter.process_avatars", return_value=[])
|
2018-08-09 17:50:43 +02:00
|
|
|
def test_gitter_import_data_conversion(self, mock_process_avatars: mock.Mock) -> None:
|
2019-04-04 12:05:54 +02:00
|
|
|
output_dir = self.make_import_output_dir("gitter")
|
2021-02-12 08:20:45 +01:00
|
|
|
gitter_file = os.path.join(os.path.dirname(__file__), "fixtures/gitter_data.json")
|
|
|
|
with self.assertLogs(level="INFO"):
|
2020-07-26 00:44:31 +02:00
|
|
|
do_convert_data(gitter_file, output_dir)
|
2018-06-01 17:39:56 +02:00
|
|
|
|
|
|
|
def read_file(output_file: str) -> Any:
|
|
|
|
full_path = os.path.join(output_dir, output_file)
|
2020-08-07 01:09:47 +02:00
|
|
|
with open(full_path, "rb") as f:
|
|
|
|
return orjson.loads(f.read())
|
2018-06-01 17:39:56 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(os.path.exists(os.path.join(output_dir, "avatars")), True)
|
|
|
|
self.assertEqual(os.path.exists(os.path.join(output_dir, "emoji")), True)
|
|
|
|
self.assertEqual(os.path.exists(os.path.join(output_dir, "attachment.json")), True)
|
2018-06-01 17:39:56 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = read_file("realm.json")
|
2018-06-01 17:39:56 +02:00
|
|
|
|
|
|
|
# test realm
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
"Organization imported from Gitter!", realm["zerver_realm"][0]["description"]
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-06-01 17:39:56 +02:00
|
|
|
|
|
|
|
# test users
|
2021-02-12 08:20:45 +01:00
|
|
|
exported_user_ids = self.get_set(realm["zerver_userprofile"], "id")
|
|
|
|
exported_user_full_name = self.get_set(realm["zerver_userprofile"], "full_name")
|
|
|
|
self.assertIn("User Full Name", exported_user_full_name)
|
|
|
|
exported_user_email = self.get_set(realm["zerver_userprofile"], "email")
|
|
|
|
self.assertIn("username2@users.noreply.github.com", exported_user_email)
|
2018-06-01 17:39:56 +02:00
|
|
|
|
|
|
|
# test stream
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(realm["zerver_stream"], 1)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(realm["zerver_stream"][0]["name"], "from gitter")
|
|
|
|
self.assertEqual(realm["zerver_stream"][0]["deactivated"], False)
|
|
|
|
self.assertEqual(realm["zerver_stream"][0]["realm"], realm["zerver_realm"][0]["id"])
|
2018-06-01 17:39:56 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
realm["zerver_defaultstream"][0]["stream"], realm["zerver_stream"][0]["id"]
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-06-01 17:39:56 +02:00
|
|
|
|
|
|
|
# test recipient
|
2021-02-12 08:20:45 +01:00
|
|
|
exported_recipient_id = self.get_set(realm["zerver_recipient"], "id")
|
|
|
|
exported_recipient_type = self.get_set(realm["zerver_recipient"], "type")
|
2020-04-09 21:51:58 +02:00
|
|
|
self.assertEqual({1, 2}, exported_recipient_type)
|
2018-06-01 17:39:56 +02:00
|
|
|
|
|
|
|
# test subscription
|
2021-02-12 08:19:30 +01:00
|
|
|
exported_subscription_userprofile = self.get_set(
|
2021-02-12 08:20:45 +01:00
|
|
|
realm["zerver_subscription"], "user_profile"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-04-09 21:51:58 +02:00
|
|
|
self.assertEqual({0, 1}, exported_subscription_userprofile)
|
2021-02-12 08:20:45 +01:00
|
|
|
exported_subscription_recipient = self.get_set(realm["zerver_subscription"], "recipient")
|
2021-05-17 05:41:32 +02:00
|
|
|
self.assert_length(exported_subscription_recipient, 3)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertIn(realm["zerver_subscription"][1]["recipient"], exported_recipient_id)
|
2018-06-01 17:39:56 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
messages = read_file("messages-000001.json")
|
2018-06-01 17:39:56 +02:00
|
|
|
|
|
|
|
# test messages
|
2021-02-12 08:20:45 +01:00
|
|
|
exported_messages_id = self.get_set(messages["zerver_message"], "id")
|
|
|
|
self.assertIn(messages["zerver_message"][0]["sender"], exported_user_ids)
|
|
|
|
self.assertIn(messages["zerver_message"][1]["recipient"], exported_recipient_id)
|
|
|
|
self.assertIn(messages["zerver_message"][0]["content"], "test message")
|
2018-06-01 17:39:56 +02:00
|
|
|
|
|
|
|
# test usermessages
|
2021-02-12 08:19:30 +01:00
|
|
|
exported_usermessage_userprofile = self.get_set(
|
2021-02-12 08:20:45 +01:00
|
|
|
messages["zerver_usermessage"], "user_profile"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-06-01 17:39:56 +02:00
|
|
|
self.assertEqual(exported_user_ids, exported_usermessage_userprofile)
|
2021-02-12 08:20:45 +01:00
|
|
|
exported_usermessage_message = self.get_set(messages["zerver_usermessage"], "message")
|
2018-06-01 17:39:56 +02:00
|
|
|
self.assertEqual(exported_usermessage_message, exported_messages_id)
|
2018-07-04 22:59:40 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@mock.patch("zerver.data_import.gitter.process_avatars", return_value=[])
|
2018-08-09 17:50:43 +02:00
|
|
|
def test_gitter_import_to_existing_database(self, mock_process_avatars: mock.Mock) -> None:
|
2019-04-04 12:05:54 +02:00
|
|
|
output_dir = self.make_import_output_dir("gitter")
|
2021-02-12 08:20:45 +01:00
|
|
|
gitter_file = os.path.join(os.path.dirname(__file__), "fixtures/gitter_data.json")
|
2020-07-26 00:44:31 +02:00
|
|
|
with self.assertLogs(level="INFO"):
|
|
|
|
do_convert_data(gitter_file, output_dir)
|
2018-08-09 17:50:43 +02:00
|
|
|
|
2020-07-24 17:22:12 +02:00
|
|
|
with self.assertLogs(level="INFO"):
|
2021-02-12 08:20:45 +01:00
|
|
|
do_import_realm(output_dir, "test-gitter-import")
|
2020-07-24 17:22:12 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
realm = get_realm("test-gitter-import")
|
2018-08-09 17:50:43 +02:00
|
|
|
|
|
|
|
# test rendered_messages
|
|
|
|
realm_users = UserProfile.objects.filter(realm=realm)
|
|
|
|
messages = Message.objects.filter(sender__in=realm_users)
|
|
|
|
for message in messages:
|
|
|
|
self.assertIsNotNone(message.rendered_content, None)
|
|
|
|
|
2018-07-04 22:59:40 +02:00
|
|
|
def test_get_usermentions(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user_map = {"57124a4": 3, "57124b4": 5, "57124c4": 8}
|
2021-02-12 08:19:30 +01:00
|
|
|
user_short_name_to_full_name = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"user": "user name",
|
|
|
|
"user2": "user2",
|
|
|
|
"user3": "user name 3",
|
|
|
|
"user4": "user 4",
|
2021-02-12 08:19:30 +01:00
|
|
|
}
|
|
|
|
messages = [
|
2021-02-12 08:20:45 +01:00
|
|
|
{"text": "hi @user", "mentions": [{"screenName": "user", "userId": "57124a4"}]},
|
2021-02-12 08:19:30 +01:00
|
|
|
{
|
2021-02-12 08:20:45 +01:00
|
|
|
"text": "hi @user2 @user3",
|
|
|
|
"mentions": [
|
|
|
|
{"screenName": "user2", "userId": "57124b4"},
|
|
|
|
{"screenName": "user3", "userId": "57124c4"},
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
|
|
|
},
|
2021-02-12 08:20:45 +01:00
|
|
|
{"text": "hi @user4", "mentions": [{"screenName": "user4"}]},
|
|
|
|
{"text": "hi @user5", "mentions": [{"screenName": "user", "userId": "5712ds4"}]},
|
2021-02-12 08:19:30 +01:00
|
|
|
]
|
2018-07-04 22:59:40 +02:00
|
|
|
|
|
|
|
self.assertEqual(get_usermentions(messages[0], user_map, user_short_name_to_full_name), [3])
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(messages[0]["text"], "hi @**user name**")
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
|
|
|
get_usermentions(messages[1], user_map, user_short_name_to_full_name), [5, 8]
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(messages[1]["text"], "hi @**user2** @**user name 3**")
|
2018-07-04 22:59:40 +02:00
|
|
|
self.assertEqual(get_usermentions(messages[2], user_map, user_short_name_to_full_name), [])
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(messages[2]["text"], "hi @user4")
|
2018-07-04 22:59:40 +02:00
|
|
|
self.assertEqual(get_usermentions(messages[3], user_map, user_short_name_to_full_name), [])
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(messages[3]["text"], "hi @user5")
|