diff --git a/tools/test-backend b/tools/test-backend index d6a6ec9903..f85e9204dd 100755 --- a/tools/test-backend +++ b/tools/test-backend @@ -36,6 +36,7 @@ target_fully_covered = {path for target in [ 'zproject/backends.py', 'confirmation/*.py', 'zerver/webhooks/*/*.py', + 'zerver/data_import/import_util.py', # Once we have a nice negative tests system, we can add these: # 'zerver/webhooks/*/*.py', # 'zerver/webhooks/*/*/*.py', diff --git a/zerver/data_import/gitter.py b/zerver/data_import/gitter.py index 9684343059..c46abdae59 100644 --- a/zerver/data_import/gitter.py +++ b/zerver/data_import/gitter.py @@ -18,10 +18,10 @@ from zerver.lib.actions import STREAM_ASSIGNMENT_COLORS as stream_colors from zerver.lib.export import MESSAGE_BATCH_CHUNK_SIZE from zerver.lib.avatar_hash import user_avatar_path_from_ids from zerver.lib.parallel import run_parallel +from zerver.data_import.import_util import ZerverFieldsT, build_zerver_realm # stubs GitterDataT = List[Dict[str, Any]] -ZerverFieldsT = Dict[str, Any] realm_id = 0 @@ -36,7 +36,7 @@ def gitter_workspace_to_realm(domain_name: str, gitter_data: GitterDataT, 3. user_map, which is a dictionary to map from gitter user id to zulip user id """ NOW = float(timezone_now().timestamp()) - zerver_realm = build_zerver_realm(realm_subdomain, NOW) + zerver_realm = build_zerver_realm(realm_id, realm_subdomain, NOW, 'Gitter') # type: List[ZerverFieldsT] realm = dict(zerver_client=[{"name": "populate_db", "id": 1}, {"name": "website", "id": 2}, @@ -71,15 +71,6 @@ def gitter_workspace_to_realm(domain_name: str, gitter_data: GitterDataT, return realm, avatars, user_map -def build_zerver_realm(realm_subdomain: str, time: float) -> List[ZerverFieldsT]: - realm = Realm(id=realm_id, date_created=time, - name=realm_subdomain, string_id=realm_subdomain, - description="Organization imported from Gitter!") - auth_methods = [[flag[0], flag[1]] for flag in realm.authentication_methods] - realm_dict = model_to_dict(realm, exclude='authentication_methods') - realm_dict['authentication_methods'] = auth_methods - return[realm_dict] - def build_userprofile(timestamp: Any, domain_name: str, gitter_data: GitterDataT) -> Tuple[List[ZerverFieldsT], List[ZerverFieldsT], diff --git a/zerver/data_import/import_util.py b/zerver/data_import/import_util.py new file mode 100644 index 0000000000..0613643ee6 --- /dev/null +++ b/zerver/data_import/import_util.py @@ -0,0 +1,17 @@ +from typing import List, Dict, Any +from django.forms.models import model_to_dict + +from zerver.models import Realm + +# stubs +ZerverFieldsT = Dict[str, Any] + +def build_zerver_realm(realm_id: int, realm_subdomain: str, time: float, + other_product: str) -> List[ZerverFieldsT]: + realm = Realm(id=realm_id, date_created=time, + name=realm_subdomain, string_id=realm_subdomain, + description=("Organization imported from %s!" % (other_product))) + auth_methods = [[flag[0], flag[1]] for flag in realm.authentication_methods] + realm_dict = model_to_dict(realm, exclude='authentication_methods') + realm_dict['authentication_methods'] = auth_methods + return[realm_dict] diff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.py index bdb012b1ac..a96b866917 100755 --- a/zerver/data_import/slack.py +++ b/zerver/data_import/slack.py @@ -21,6 +21,7 @@ from zerver.forms import check_subdomain_available from zerver.models import Reaction, RealmEmoji, Realm, UserProfile from zerver.data_import.slack_message_conversion import convert_to_zulip_markdown, \ get_user_full_name +from zerver.data_import.import_util import ZerverFieldsT, build_zerver_realm from zerver.lib.parallel import run_parallel from zerver.lib.avatar_hash import user_avatar_path_from_ids from zerver.lib.actions import STREAM_ASSIGNMENT_COLORS as stream_colors @@ -29,7 +30,6 @@ from zerver.lib.export import MESSAGE_BATCH_CHUNK_SIZE from zerver.lib.emoji import NAME_TO_CODEPOINT_PATH # stubs -ZerverFieldsT = Dict[str, Any] AddedUsersT = Dict[str, int] AddedChannelsT = Dict[str, Tuple[str, int]] AddedRecipientsT = Dict[str, int] @@ -56,7 +56,7 @@ def slack_workspace_to_realm(domain_name: str, realm_id: int, user_list: List[Ze """ NOW = float(timezone_now().timestamp()) - zerver_realm = build_zerver_realm(realm_id, realm_subdomain, NOW) + zerver_realm = build_zerver_realm(realm_id, realm_subdomain, NOW, 'Slack') # type: List[ZerverFieldsT] realm = dict(zerver_client=[{"name": "populate_db", "id": 1}, {"name": "website", "id": 2}, @@ -101,16 +101,6 @@ def slack_workspace_to_realm(domain_name: str, realm_id: int, user_list: List[Ze return realm, added_users, added_recipient, added_channels, avatars, emoji_url_map -def build_zerver_realm(realm_id: int, realm_subdomain: str, - time: float) -> List[ZerverFieldsT]: - realm = Realm(id=realm_id, date_created=time, - name=realm_subdomain, string_id=realm_subdomain, - description="Organization imported from Slack!") - auth_methods = [[flag[0], flag[1]] for flag in realm.authentication_methods] - realm_dict = model_to_dict(realm, exclude='authentication_methods') - realm_dict['authentication_methods'] = auth_methods - return[realm_dict] - def build_realmemoji(custom_emoji_list: ZerverFieldsT, realm_id: int) -> Tuple[List[ZerverFieldsT], ZerverFieldsT]: diff --git a/zerver/tests/test_slack_importer.py b/zerver/tests/test_slack_importer.py index e800de5b52..7d5075faaf 100644 --- a/zerver/tests/test_slack_importer.py +++ b/zerver/tests/test_slack_importer.py @@ -5,7 +5,6 @@ from django.utils.timezone import now as timezone_now from zerver.data_import.slack import ( rm_tree, get_slack_api_data, - build_zerver_realm, get_user_email, build_avatar_url, build_avatar, @@ -24,6 +23,9 @@ from zerver.data_import.slack import ( do_convert_data, process_avatars, ) +from zerver.data_import.import_util import ( + build_zerver_realm, +) from zerver.lib.import_realm import ( do_import_realm, ) @@ -96,7 +98,7 @@ class SlackImporter(ZulipTestCase): realm_id = 2 realm_subdomain = "test-realm" time = float(timezone_now().timestamp()) - test_realm = build_zerver_realm(realm_id, realm_subdomain, time) + test_realm = build_zerver_realm(realm_id, realm_subdomain, time, 'Slack') # type: List[Dict[str, Any]] test_zerver_realm_dict = test_realm[0] self.assertEqual(test_zerver_realm_dict['id'], realm_id) @@ -321,14 +323,12 @@ class SlackImporter(ZulipTestCase): self.assertEqual(zerver_stream[2]['id'], test_added_channels[zerver_stream[2]['name']][1]) - @mock.patch("zerver.data_import.slack.build_zerver_realm", return_value=[{}]) @mock.patch("zerver.data_import.slack.users_to_zerver_userprofile", return_value=[[], [], {}, [], []]) @mock.patch("zerver.data_import.slack.channels_to_zerver_stream", return_value=[[], [], {}, [], [], {}]) def test_slack_workspace_to_realm(self, mock_channels_to_zerver_stream: mock.Mock, - mock_users_to_zerver_userprofile: mock.Mock, - mock_build_zerver_realm: mock.Mock) -> None: + mock_users_to_zerver_userprofile: mock.Mock) -> None: realm_id = 1 user_list = [] # type: List[Dict[str, Any]] @@ -347,7 +347,7 @@ class SlackImporter(ZulipTestCase): self.assertEqual(realm['zerver_userpresence'], []) self.assertEqual(realm['zerver_stream'], []) self.assertEqual(realm['zerver_userprofile'], []) - self.assertEqual(realm['zerver_realm'], [{}]) + self.assertEqual(realm['zerver_realm'][0]['description'], 'Organization imported from Slack!') def test_get_message_sending_user(self) -> None: message_with_file = {'subtype': 'file', 'type': 'message',