mirror of https://github.com/zulip/zulip.git
slack importer: Pass avatar data through the import script.
This commit is contained in:
parent
30b9d35d5e
commit
7076a49e04
|
@ -50,13 +50,15 @@ def allocate_ids(model_class: Any, count: int) -> List[int]:
|
|||
def slack_workspace_to_realm(REALM_ID: int, user_list: List[ZerverFieldsT],
|
||||
realm_subdomain: str, fixtures_path: str,
|
||||
slack_data_dir: str) -> Tuple[ZerverFieldsT, AddedUsersT,
|
||||
AddedRecipientsT, AddedChannelsT]:
|
||||
AddedRecipientsT, AddedChannelsT,
|
||||
List[ZerverFieldsT]]:
|
||||
"""
|
||||
Returns:
|
||||
1. realm, Converted Realm data
|
||||
2. added_users, which is a dictionary to map from slack user id to zulip user id
|
||||
3. added_recipient, which is a dictionary to map from channel name to zulip recipient_id
|
||||
4. added_channels, which is a dictionary to map from channel name to zulip stream_id
|
||||
5. avatars, which is list to map avatars to zulip avatar records.json
|
||||
"""
|
||||
DOMAIN_NAME = settings.EXTERNAL_HOST
|
||||
NOW = float(timezone_now().timestamp())
|
||||
|
@ -80,11 +82,8 @@ def slack_workspace_to_realm(REALM_ID: int, user_list: List[ZerverFieldsT],
|
|||
zerver_realmfilter=[],
|
||||
zerver_realmemoji=[])
|
||||
|
||||
zerver_userprofile, added_users = users_to_zerver_userprofile(slack_data_dir,
|
||||
user_list,
|
||||
REALM_ID,
|
||||
int(NOW),
|
||||
DOMAIN_NAME)
|
||||
zerver_userprofile, avatars, added_users = users_to_zerver_userprofile(
|
||||
slack_data_dir, user_list, REALM_ID, int(NOW), DOMAIN_NAME)
|
||||
channels_to_zerver_stream_fields = channels_to_zerver_stream(slack_data_dir,
|
||||
REALM_ID,
|
||||
added_users,
|
||||
|
@ -100,7 +99,7 @@ def slack_workspace_to_realm(REALM_ID: int, user_list: List[ZerverFieldsT],
|
|||
added_channels = channels_to_zerver_stream_fields[2]
|
||||
added_recipient = channels_to_zerver_stream_fields[5]
|
||||
|
||||
return realm, added_users, added_recipient, added_channels
|
||||
return realm, added_users, added_recipient, added_channels, avatars
|
||||
|
||||
def build_zerver_realm(fixtures_path: str, REALM_ID: int, realm_subdomain: str,
|
||||
time: float) -> List[ZerverFieldsT]:
|
||||
|
@ -116,11 +115,13 @@ def build_zerver_realm(fixtures_path: str, REALM_ID: int, realm_subdomain: str,
|
|||
|
||||
def users_to_zerver_userprofile(slack_data_dir: str, users: List[ZerverFieldsT], realm_id: int,
|
||||
timestamp: Any, domain_name: str) -> Tuple[List[ZerverFieldsT],
|
||||
List[ZerverFieldsT],
|
||||
AddedUsersT]:
|
||||
"""
|
||||
Returns:
|
||||
1. zerver_userprofile, which is a list of user profile
|
||||
2. added_users, which is a dictionary to map from slack user id to zulip
|
||||
2. avatar_list, which is list to map avatars to zulip avatard records.json
|
||||
3. added_users, which is a dictionary to map from slack user id to zulip
|
||||
user id
|
||||
"""
|
||||
logging.info('######### IMPORTING USERS STARTED #########\n')
|
||||
|
@ -226,7 +227,7 @@ def users_to_zerver_userprofile(slack_data_dir: str, users: List[ZerverFieldsT],
|
|||
|
||||
logging.info(u"{} -> {}".format(user['name'], userprofile['email']))
|
||||
logging.info('######### IMPORTING USERS FINISHED #########\n')
|
||||
return zerver_userprofile, added_users
|
||||
return zerver_userprofile, avatar_list, added_users
|
||||
|
||||
def get_user_email(user: ZerverFieldsT, domain_name: str) -> str:
|
||||
if 'email' not in user['profile']:
|
||||
|
@ -652,11 +653,9 @@ def do_convert_data(slack_zip_file: str, realm_subdomain: str, output_dir: str,
|
|||
REALM_ID = allocate_ids(Realm, 1)[0]
|
||||
|
||||
user_list = get_user_data(token)
|
||||
realm, added_users, added_recipient, added_channels = slack_workspace_to_realm(REALM_ID,
|
||||
user_list,
|
||||
realm_subdomain,
|
||||
fixtures_path,
|
||||
slack_data_dir)
|
||||
realm, added_users, added_recipient, added_channels, avatar_list = slack_workspace_to_realm(
|
||||
REALM_ID, user_list, realm_subdomain, fixtures_path, slack_data_dir)
|
||||
|
||||
message_json = convert_slack_workspace_messages(slack_data_dir, user_list, REALM_ID,
|
||||
added_users, added_recipient, added_channels,
|
||||
realm)
|
||||
|
|
|
@ -162,11 +162,12 @@ class SlackImporter(ZulipTestCase):
|
|||
'U09TYF5Sk': 3}
|
||||
slack_data_dir = './random_path'
|
||||
timestamp = int(timezone_now().timestamp())
|
||||
zerver_userprofile, added_users = users_to_zerver_userprofile(slack_data_dir, user_data,
|
||||
1, timestamp, 'test_domain')
|
||||
zerver_userprofile, avatar_list, added_users = users_to_zerver_userprofile(
|
||||
slack_data_dir, user_data, 1, timestamp, 'test_domain')
|
||||
|
||||
# test that the primary owner should always be imported first
|
||||
self.assertDictEqual(added_users, test_added_users)
|
||||
self.assertEqual(len(avatar_list), 3)
|
||||
|
||||
self.assertEqual(zerver_userprofile[1]['id'], test_added_users['U0CBK5KAT'])
|
||||
self.assertEqual(len(zerver_userprofile), 3)
|
||||
|
@ -307,7 +308,7 @@ class SlackImporter(ZulipTestCase):
|
|||
|
||||
@mock.patch("zerver.lib.slack_data_to_zulip_data.build_zerver_realm", return_value=[{}])
|
||||
@mock.patch("zerver.lib.slack_data_to_zulip_data.users_to_zerver_userprofile",
|
||||
return_value=[[], {}])
|
||||
return_value=[[], [], {}])
|
||||
@mock.patch("zerver.lib.slack_data_to_zulip_data.channels_to_zerver_stream",
|
||||
return_value=[[], [], {}, [], [], {}])
|
||||
def test_slack_workspace_to_realm(self, mock_channels_to_zerver_stream: mock.Mock,
|
||||
|
@ -317,17 +318,15 @@ class SlackImporter(ZulipTestCase):
|
|||
realm_id = 1
|
||||
user_list = [] # type: List[Dict[str, Any]]
|
||||
with self.settings(EXTERNAL_HOST='testdomain'):
|
||||
realm, added_users, added_recipient, added_channels = slack_workspace_to_realm(realm_id,
|
||||
user_list,
|
||||
'test-realm',
|
||||
'./fixture',
|
||||
'./random_path')
|
||||
realm, added_users, added_recipient, added_channels, avatar_list = slack_workspace_to_realm(
|
||||
realm_id, user_list, 'test-realm', './fixtures', './random_path')
|
||||
test_zerver_realmdomain = [{'realm': realm_id, 'allow_subdomains': False,
|
||||
'domain': 'testdomain', 'id': realm_id}]
|
||||
# Functioning already tests in helper functions
|
||||
self.assertEqual(added_users, {})
|
||||
self.assertEqual(added_channels, {})
|
||||
self.assertEqual(added_recipient, {})
|
||||
self.assertEqual(avatar_list, [])
|
||||
|
||||
zerver_realmdomain = realm['zerver_realmdomain']
|
||||
self.assertListEqual(zerver_realmdomain, test_zerver_realmdomain)
|
||||
|
@ -493,8 +492,12 @@ class SlackImporter(ZulipTestCase):
|
|||
self.assertEqual(message_json['zerver_message'], zerver_message1 + zerver_message2)
|
||||
self.assertEqual(message_json['zerver_usermessage'], zerver_usermessage1 + zerver_usermessage2)
|
||||
|
||||
@mock.patch("zerver.lib.slack_data_to_zulip_data.build_avatar_url")
|
||||
@mock.patch("zerver.lib.slack_data_to_zulip_data.build_avatar", return_value = [])
|
||||
@mock.patch("zerver.lib.slack_data_to_zulip_data.get_user_data")
|
||||
def test_slack_import_to_existing_database(self, mock_get_user_data: mock.Mock) -> None:
|
||||
def test_slack_import_to_existing_database(self, mock_get_user_data: mock.Mock,
|
||||
mock_build_avatar_url: mock.Mock,
|
||||
mock_build_avatar: mock.Mock) -> None:
|
||||
test_slack_zip_file = os.path.join(settings.DEPLOY_ROOT, "zerver", "fixtures",
|
||||
"slack_fixtures", "test_slack_importer.zip")
|
||||
test_realm_subdomain = 'test-slack-import'
|
||||
|
|
Loading…
Reference in New Issue